On 07/06/2014 16:39, monarch_dodra wrote:
Then, you are using the code:
.partition!(forest_invalid).sort.uniq.array;

The "advantage" of "partition" over "filter" is that "partition" is a
greedy inplace algorithm. But then, you go on to use "uniq", which is
lazy, and requires "array". I implemented a (very) simplistic in-place
"removeDuplicates":

More generally, perhaps we could have a function to apply a lazy range to an existing random-access range:

import std.range;

/** Copy up to r.length elements of src to r. */
auto refill(R, Input)(ref R r, Input src)
if (isRandomAccessRange!R && isInputRange!Input)
{
    foreach (i, ref e; r)
    {
        if (src.empty)
        {
            r.length = i;
            return r;
        }
        e = src.front;
        src.popFront;
    }
    // ignore leftover elements in src
    return r;
}

import std.algorithm : uniq;

/// eager wrapper for uniq
auto dedup(T)(ref T v){return v.refill(v.uniq);}
//alias dedup = (ref v) => v.refill(v.uniq);

void main()
{
    auto a = [5,5,5,5,4,3,3,3,1];
    a.dedup;
    assert(a == [5, 4, 3, 1]);
}

Reply via email to