Text processing/Max licenses in use: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
(Add source for Rust) |
||
Line 42: | Line 42: | ||
-- to record the time of max OUT licenses |
-- to record the time of max OUT licenses |
||
infile |
infile : File_Type; -- file handle |
||
str |
str : Unbounded_String; -- input string buffer of unknown length |
||
outcnt, maxoutcnt : integer := 0; |
outcnt, maxoutcnt : integer := 0; |
||
infilename : string := "license.log"; |
infilename : string := "license.log"; |
||
Line 74: | Line 74: | ||
if outcnt > maxoutcnt then |
if outcnt > maxoutcnt then |
||
maxoutcnt := outcnt; |
maxoutcnt := outcnt; |
||
logtime.logtext := slice(str,15,33); -- date_time field |
|||
licenselog.clear; -- reset list for new time(s) |
licenselog.clear; -- reset list for new time(s) |
||
licenselog.append (logtime); -- put current time into list |
licenselog.append (logtime); -- put current time into list |
||
Line 247: | Line 247: | ||
On a 2.53MHz machine, these timings were obtained using GNU Awk 4.0.2: |
On a 2.53MHz machine, these timings were obtained using GNU Awk 4.0.2: |
||
user |
user 0m0.015s |
||
sys |
sys 0m0.008s |
||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
Line 354: | Line 354: | ||
if ( l_out == maxout ) { |
if ( l_out == maxout ) { |
||
if ( maxcount < MAX_MAXOUT ) { |
if ( maxcount < MAX_MAXOUT ) { |
||
strncpy(maxtime[maxcount], time, TIME_LEN); |
|||
maxcount++; |
|||
} else { |
} else { |
||
fprintf(stderr, "increase MAX_MAXOUT (now it is %u)\n", MAX_MAXOUT); |
|||
exit(1); |
|||
} |
} |
||
} |
} |
||
Line 383: | Line 383: | ||
int main() |
int main() |
||
{ |
{ |
||
struct stat s; |
|||
int fd = open("mlijobs.txt", O_RDONLY); |
|||
int cnt, max_cnt, occur; |
|||
char *buf, *ptr; |
|||
if (fd == -1) err(1, "open"); |
|||
fstat(fd, &s); |
|||
ptr = buf = mmap(0, s.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); |
|||
cnt = max_cnt = 0; |
|||
while(ptr - buf < s.st_size - 33) { |
|||
if (!strncmp(ptr, "License OUT", 11) && ++cnt >= max_cnt) { |
|||
if (cnt > max_cnt) { |
|||
max_cnt = cnt; |
|||
occur = 0; |
|||
} |
|||
} |
|||
/* can't sprintf time stamp: might overlap */ |
|||
memmove(buf + 26 * occur, ptr + 14, 19); |
|||
sprintf(buf + 26 * occur + 19, "%6d\n", cnt); |
|||
occur++; |
|||
} else if (!strncmp(ptr, "License IN ", 11)) cnt --; |
|||
while (ptr < buf + s.st_size && *ptr++ != '\n'); |
|||
} |
|||
⚫ | |||
printf(buf); |
|||
munmap(buf, s.st_size); |
|||
return close(fd); |
|||
}</lang>output<lang>2008/10/03_08:39:34 99 |
}</lang>output<lang>2008/10/03_08:39:34 99 |
||
2008/10/03_08:40:40 99</lang> |
2008/10/03_08:40:40 99</lang> |
||
Line 727: | Line 727: | ||
<lang Eiffel> |
<lang Eiffel> |
||
class |
class |
||
APPLICATION |
|||
create |
create |
||
make |
|||
feature |
feature |
||
make |
|||
-- Max Licences used. |
|||
local |
|||
count: INTEGER |
|||
max_count: INTEGER |
|||
date: STRING |
|||
do |
|||
do |
|||
read_list |
|||
create date.make_empty |
|||
across |
|||
data as d |
|||
loop |
|||
if d.item.has_substring ("OUT") then |
|||
count := count + 1 |
|||
if count > max_count then |
|||
max_count := count |
|||
date := d.item |
|||
end |
|||
end |
|||
elseif d.item.has_substring ("IN") then |
|||
count := count - 1 |
|||
end |
|||
end |
|||
end |
|||
end |
|||
io.put_string ("Max Licences OUT: " + max_count.out) |
|||
io.new_line |
|||
io.put_string ("Date: " + date.substring (15, 33)) |
|||
end |
|||
original_list: STRING = "mlijobs.txt" |
|||
feature {NONE} |
feature {NONE} |
||
read_list |
|||
-- Data read into 'data. |
|||
local |
|||
l_file: PLAIN_TEXT_FILE |
|||
do |
|||
do |
|||
create l_file.make_open_read_write (original_list) |
|||
l_file.read_stream (l_file.count) |
|||
data := l_file.last_string.split ('%N') |
|||
l_file.close |
|||
end |
|||
data: LIST [STRING] |
|||
end |
end |
||
Line 793: | Line 793: | ||
out_dates_from_file( Name ) -> |
out_dates_from_file( Name ) -> |
||
{ok, Binary} = file:read_file( Name ), |
|||
Lines = binary:split( Binary, <<"\n">>, [global] ), |
|||
{_N, _Date, Dict} = lists:foldl( fun out_dates/2, {0, "", dict:new()}, Lines ), |
|||
[{X, dict:fetch(X, Dict)} || X <- dict:fetch_keys( Dict )]. |
|||
task() -> |
task() -> |
||
Line 806: | Line 806: | ||
out_dates( <<>>, Acc ) -> Acc; |
out_dates( <<>>, Acc ) -> Acc; |
||
out_dates( Line, {N, Date, Dict} ) -> |
out_dates( Line, {N, Date, Dict} ) -> |
||
[_License, Direction, <<"@">>, New_date | _T] = [X || X <- binary:split(Line, <<" ">>, [global]), X =/= <<>>], |
|||
New_n = out_dates_n( N, Direction ), |
|||
New_dict = out_dates_dict( N, New_n, Date, Dict ), |
|||
{New_n, New_date, New_dict}. |
|||
out_dates_dict( N, New_n, Date, Dict ) when N > New_n -> dict:append( N, Date, Dict ); |
out_dates_dict( N, New_n, Date, Dict ) when N > New_n -> dict:append( N, Date, Dict ); |
||
Line 1,057: | Line 1,057: | ||
import ( |
import ( |
||
"bufio" |
|||
"bytes" |
|||
"fmt" |
|||
"log" |
|||
"os" |
|||
) |
) |
||
const ( |
const ( |
||
filename = "mlijobs.txt" |
|||
inoutField = 1 |
|||
timeField = 3 |
|||
numFields = 7 |
|||
) |
) |
||
func main() { |
func main() { |
||
file, err := os.Open(filename) |
|||
if err != nil { |
|||
log.Fatal(err) |
|||
} |
|||
} |
|||
defer file.Close() |
|||
var ml, out int |
|||
var mlTimes []string |
|||
in := []byte("IN") |
|||
s := bufio.NewScanner(file) |
|||
for s.Scan() { |
|||
f := bytes.Fields(s.Bytes()) |
|||
if len(f) != numFields { |
|||
log.Fatal("unexpected format,", len(f), "fields.") |
|||
} |
|||
} |
|||
if bytes.Equal(f[inoutField], in) { |
|||
out-- |
|||
if out < 0 { |
|||
log.Fatalf("negative license use at %s", f[timeField]) |
|||
} |
|||
} |
|||
continue |
|||
} |
|||
} |
|||
out++ |
|||
if out < ml { |
|||
continue |
|||
} |
|||
} |
|||
if out > ml { |
|||
ml = out |
|||
mlTimes = mlTimes[:0] |
|||
} |
|||
} |
|||
mlTimes = append(mlTimes, string(f[timeField])) |
|||
} |
|||
} |
|||
if err = s.Err(); err != nil { |
|||
log.Fatal(err) |
|||
} |
|||
} |
|||
fmt.Println("max licenses:", ml) |
|||
fmt.Println("at:") |
|||
for _, t := range mlTimes { |
|||
fmt.Println(" ", t) |
|||
} |
|||
} |
|||
}</lang> |
}</lang> |
||
{{out}} |
{{out}} |
||
Line 1,341: | Line 1,341: | ||
2008/10/03_08:40:40 |
2008/10/03_08:40:40 |
||
real |
real 0m0.163s |
||
user |
user 0m0.154s |
||
sys |
sys 0m0.005s |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
Line 1,468: | Line 1,468: | ||
<pre>Maximum licenses in use: 99 |
<pre>Maximum licenses in use: 99 |
||
Occurrences: |
Occurrences: |
||
2008/10/03_08:39:34 |
|||
2008/10/03_08:40:40</pre> |
|||
=={{header|M2000 Interpreter}}== |
=={{header|M2000 Interpreter}}== |
||
Line 1,543: | Line 1,543: | ||
local maxOut = -1 |
local maxOut = -1 |
||
local maxTimes = #() |
local maxTimes = #() |
||
while not EOF logFile do |
while not EOF logFile do |
||
( |
( |
||
Line 1,649: | Line 1,649: | ||
for Job in {ReadLines Filename} do |
for Job in {ReadLines Filename} do |
||
case {List.take Job 11} of "License OUT" then |
case {List.take Job 11} of "License OUT" then |
||
InUse := @InUse + 1 |
|||
if @InUse > @MaxInUse then |
|||
MaxInUse := @InUse |
|||
MaxTimes := nil |
|||
end |
|||
if @InUse == @MaxInUse then |
|||
JobTime = {Nth {String.tokens Job & } 4} |
|||
in |
|||
MaxTimes := JobTime|@MaxTimes |
|||
end |
|||
[] "License IN " then |
[] "License IN " then |
||
InUse := @InUse - 1 |
|||
end |
end |
||
end |
end |
||
Line 1,700: | Line 1,700: | ||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
<lang parigp>license()={ |
<lang parigp>license()={ |
||
my(v=externstr("type mlijobs.txt"),u,cur,rec,t); |
|||
for(i=1,#v, |
|||
u=Vec(v[i]); |
|||
if(#u>9 && u[9] == "O", |
|||
if(cur++>rec, |
|||
rec=cur; |
|||
t=[v[i]] |
|||
, |
|||
, |
|||
if(cur == rec,t=concat(t,[v[i]])) |
|||
) |
|||
) |
|||
, |
|||
, |
|||
cur-- |
|||
) |
|||
) |
|||
); |
|||
print(apply(s->concat(vecextract(Vec(s),"15..33")), t)); |
|||
rec |
|||
};</lang> |
};</lang> |
||
<pre>["2008/10/03_08:39:34", "2008/10/03_08:40:40"] |
<pre>["2008/10/03_08:39:34", "2008/10/03_08:40:40"] |
||
Line 1,805: | Line 1,805: | ||
$buffer = fgets($handle); |
$buffer = fgets($handle); |
||
$op = trim(substr($buffer,8,3)); |
$op = trim(substr($buffer,8,3)); |
||
switch ($op){ |
|||
case 'IN': |
|||
$count--; |
|||
break; |
|||
case 'OUT': |
|||
$count++; |
|||
preg_match('/([\d|\/|_|:]+)/',$buffer,$time); |
|||
if($count>$maxcount){ |
|||
$maxcount = $count; |
|||
$times = Array($time[0]); |
|||
}elseif($count == $maxcount){ |
|||
$times[] = $time[0]; |
|||
} |
|||
} |
|||
break; |
|||
} |
|||
} |
|||
} |
} |
||
fclose ($handle); |
fclose ($handle); |
||
Line 1,825: | Line 1,825: | ||
echo $maxcount . '<br>'; |
echo $maxcount . '<br>'; |
||
for($i=0;$i<count($times);$i++){ |
for($i=0;$i<count($times);$i++){ |
||
echo $times[$i] . '<br>'; |
|||
}</lang> |
}</lang> |
||
<pre> |
<pre> |
||
Line 2,327: | Line 2,327: | ||
wend |
wend |
||
print maxCount;" ";theDate$</lang> |
print maxCount;" ";theDate$</lang> |
||
=={{header|Rust}}== |
|||
<lang Rust>type Timestamp = String; |
|||
fn compute_usage<R, S, E>(lines: R) -> Result<(u32, Vec<Timestamp>), E> |
|||
where |
|||
S: AsRef<str>, |
|||
R: Iterator<Item = Result<S, E>>, |
|||
{ |
|||
let mut timestamps = Vec::new(); |
|||
let mut current = 0; |
|||
let mut maximum = 0; |
|||
for line in lines { |
|||
let line = line?; |
|||
let line = line.as_ref(); |
|||
if line.starts_with("License IN") { |
|||
current -= 1; |
|||
} else if line.starts_with("License OUT") { |
|||
current += 1; |
|||
if maximum <= current { |
|||
let date = line.split_whitespace().nth(3).unwrap().to_string(); |
|||
if maximum < current { |
|||
maximum = current; |
|||
timestamps.clear(); |
|||
} |
|||
timestamps.push(date); |
|||
} |
|||
} |
|||
} |
|||
Ok((maximum, timestamps)) |
|||
⚫ | |||
fn main() -> std::io::Result<()> { |
|||
use std::io::{BufRead, BufReader}; |
|||
let file = std::fs::OpenOptions::new().read(true).open("mlijobs.txt")?; |
|||
let (max, timestamps) = compute_usage(BufReader::new(file).lines())?; |
|||
println!("Maximum licenses out: {}", max); |
|||
println!("At time(s): {:?}", timestamps); |
|||
Ok(()) |
|||
}</lang> |
|||
=={{header|Scala}}== |
=={{header|Scala}}== |