[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:20:24 PST 2013


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


timon.gehr at gmx.ch changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |timon.gehr at gmx.ch


--- Comment #1 from timon.gehr at gmx.ch 2013-03-09 15:20:21 PST ---
A solution that does not require caching front is the following:

struct Filter(alias a,R){
    private R source;
    // ...
    private bool processed = false;
    void process(){
        if(!processed)
            while(!source.empty && !a(source.front))
                source.popFront();
        processed = true;
    }
    @property auto ref front(){ process(); return source.front; }
    @property bool empty(){ process(); return source.empty; }
    void popFront(){ process(); source.popFront(); }
}

It also fixes the problem of filter not being properly lazy (i.e. currently the
function call might loop forever, even if no element is actually requested
later.)

Of course, this might be slower than the current solution in case the compiler
is unable to properly track the 'processed' field value.

-- 
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