[Issue 9674] std.algorithm.filter problems with non-deterministic _input.front

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Mar 9 15:39:50 PST 2013


http://d.puremagic.com/issues/show_bug.cgi?id=9674



--- Comment #3 from bearophile_hugs at eml.cc 2013-03-09 15:39:49 PST ---
I am trying your code, but I see duplications in the output:


import std.stdio: writeln;
import std.algorithm: map, sort;
import std.array: array;
import std.range: iota, isInputRange;
import std.random: uniform;
import std.traits: ForeachType;


struct FilterResult3(alias pred, Range) {
    private Range _input;

    this(Range r) {
        _input = r;
    }

    private bool processed = false;

    void process() {
        if (!processed)
            while (!_input.empty && !pred(_input.front))
                _input.popFront();
        processed = true;
    }

    @property auto ref front() {
        process();
        return _input.front;
    }

    @property bool empty() {
        process();
        return _input.empty;
    }

    void popFront() {
        process();
        _input.popFront();
        processed = false;
    }
}

FilterResult3!(pred, Range) filter3(alias pred, Range)(Range rs) {
    return FilterResult3!(pred, Range)(rs);
}


auto distinct3(Range)(Range r)
if (isInputRange!Range) {
    bool[ForeachType!Range] mySet;

    return r.filter3!((k) {
        if (k in mySet)
            return false;
        mySet[k] = true;
        return true;
    });
}

void main() {
    100.iota.map!(_ => uniform(0, 10))
    .distinct3.array.sort.writeln; //
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list