[OT] The horizon of a stream
bearophile
bearophileHUGS at lycos.com
Thu Oct 23 12:55:04 PDT 2008
First try, D1, Phobos:
import std.stdio, std.stream;
void main() {
int[string] seen;
int horizon;
int nline;
foreach (string line; new BufferedFile("data.txt")) {
auto pos_ptr = line in seen;
if (pos_ptr) {
if ((nline - *pos_ptr) > horizon)
horizon = nline - *pos_ptr;
} else
seen[line.dup] = nline;
nline++;
}
writefln(horizon);
}
Idem, with my libs:
import d.all, d.string;
void main() {
int[string] seen;
int horizon;
foreach (nline, line; xfile("data.txt")) {
auto pos_ptr = line in seen;
if (pos_ptr) {
if ((nline - *pos_ptr) > horizon)
horizon = nline - *pos_ptr;
} else
seen[line.dup] = nline;
}
putr(horizon);
putr(seen);
}
I'll show an "improved" version soon...
Bye,
bearophile
More information about the Digitalmars-d
mailing list