how to get top N distinct elements from range?
bearophile
bearophileHUGS at lycos.com
Fri Mar 8 11:36:01 PST 2013
> auto firstDistinct(Range)(Range r, in size_t n)
> if (isInputRange!Range) {
> bool[ForeachType!Range] mySet;
>
> return r.filter!((k) {
> if (k in mySet)
> return false;
> mySet[k] = true;
> return true;
> }).take(n);
> }
I think the standard library of Ada2012 has bounded containers,
so you can use them to stack-allocate a hash set that can contain
up to n items (plus some more free cells to keep its load factor
low enough).
In Rust there are fully safe stack-allocated closures. Combining
the two things it's possible to create a function like that
firstDistinct() with zero heap allocations, that is probably fast.
Bye,
bearophile
More information about the Digitalmars-d-learn
mailing list