Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Changeset 1703

Show
Ignore:
Timestamp:
07/01/10 02:39:50 (14 years ago)
Author:
dsimcha
Message:

@property and infiniteness propagation in std.algorithm.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/phobos/std/algorithm.d

    r1702 r1703  
    570570        } 
    571571 
    572572    } 
    573573 
    574574    ref Filter opSlice() 
    575575    { 
    576576        return this; 
    577577    } 
    578578 
    579579    static if(isInfinite!Range) { 
    580         enum bool empty = false; 
     580        enum bool empty = false;  // Propagate infiniteness. 
    581581    } else { 
    582582        bool empty() { return _input.empty; } 
    583583    } 
    584584 
    585585    void popFront() 
    586586    { 
    587587        do 
    588588        { 
    589589            _input.popFront; 
    590590        } while (!_input.empty && !pred(_input.front)); 
     
    867867 
    868868public: 
    869869    this(Range input, Separator separator) 
    870870    { 
    871871        _input = input; 
    872872        _separator = separator; 
    873873        // computeFront(); 
    874874        // computeBack(); 
    875875    } 
    876876 
    877     @property bool empty() 
    878     { 
    879         return _frontLength == _atEnd; 
     877    static if(isInfinite!Range) 
     878    { 
     879        enum bool empty = false; 
     880    } else 
     881    { 
     882        @property bool empty() 
     883        { 
     884            return _frontLength == _atEnd; 
     885        } 
    880886    } 
    881887 
    882888    @property Range front() 
    883889    { 
    884890        assert(!empty); 
    885891        if (_frontLength == _unComputed) 
    886892        { 
    887893            _frontLength = _input.indexOf(_separator); 
    888894            if (_frontLength == -1) _frontLength = _input.length; 
    889895        } 
     
    989995        } 
    990996    } 
    991997 
    992998public: 
    993999    this(Range input, Separator separator) 
    9941000    { 
    9951001        _input = input; 
    9961002        _separator = separator; 
    9971003    } 
    9981004 
    999     Range front() 
     1005    @property Range front() 
    10001006    { 
    10011007        assert(!empty); 
    10021008        ensureFrontLength; 
    10031009        return _input[0 .. _frontLength]; 
    10041010    } 
    10051011 
    1006     bool empty() 
    1007     { 
    1008         return _frontLength == size_t.max && _input.empty; 
     1012    static if(isInfinite!Range) 
     1013    { 
     1014        enum bool empty = false;  // Propagate infiniteness 
     1015    } 
     1016    else 
     1017    { 
     1018        @property bool empty() 
     1019        { 
     1020            return _frontLength == size_t.max && _input.empty; 
     1021        } 
    10091022    } 
    10101023 
    10111024    void popFront() 
    10121025    { 
    10131026        assert(!empty); 
    10141027        ensureFrontLength; 
    10151028        if (_frontLength == _input.length) 
    10161029        { 
    10171030            // done, there's no separator in sight 
    10181031            _input = _input[_frontLength .. _frontLength]; 
     
    10341047        // Normal case, pop one item and the separator, get ready for 
    10351048        // reading the next item 
    10361049        _input = _input[_frontLength + separatorLength .. _input.length]; 
    10371050        // mark _frontLength as uninitialized 
    10381051        _frontLength = _frontLength.max; 
    10391052    } 
    10401053 
    10411054// Bidirectional functionality as suggested by Brad Roberts. 
    10421055    static if (isBidirectionalRange!Range) 
    10431056    { 
    1044         Range back() 
     1057        @property Range back() 
    10451058        { 
    10461059            ensureBackLength; 
    10471060            return _input[_input.length - _backLength .. _input.length]; 
    10481061        } 
    10491062 
    10501063        void popBack() 
    10511064        { 
    10521065            ensureBackLength; 
    10531066            if (_backLength == _input.length) 
    10541067            { 
     
    11361149        else 
    11371150        { 
    11381151            // Chase first terminator 
    11391152            while (_end < _input.length && !_isTerminator(_input[_end])) 
    11401153            { 
    11411154                ++_end; 
    11421155            } 
    11431156        } 
    11441157    } 
    11451158 
    1146     @property bool empty() 
    1147     { 
    1148         return _end == _end.max; 
     1159    static if(isInfinite!Range) 
     1160    { 
     1161        enum bool empty = false;  // Propagate infiniteness. 
     1162    } 
     1163    else 
     1164    { 
     1165        @property bool empty() 
     1166        { 
     1167            return _end == _end.max; 
     1168        } 
    11491169    } 
    11501170 
    11511171    @property Range front() 
    11521172    { 
    11531173        assert(!empty); 
    11541174        return _input[0 .. _end]; 
    11551175    } 
    11561176 
    11571177    void popFront() 
    11581178    { 
     
    12501270    void popFront() 
    12511271    { 
    12521272        auto last = _input.front; 
    12531273        do 
    12541274        { 
    12551275            _input.popFront; 
    12561276        } 
    12571277        while (!_input.empty && binaryFun!(pred)(last, _input.front)); 
    12581278    } 
    12591279 
    1260     static if(isBidirectionalRange!R) { 
     1280    @property ElementType!(R) front() { return _input.front; } 
     1281 
     1282    static if(isBidirectionalRange!R) 
     1283    { 
    12611284        void popBack() 
    12621285        { 
    12631286            auto last = _input.back; 
    12641287            do 
    12651288            { 
    12661289                _input.popBack; 
    12671290            } 
    12681291            while (!_input.empty && binaryFun!(pred)(last, _input.back)); 
    12691292        } 
    12701293 
    1271         ElementType!(R) back() { return _input.back; } 
    1272     } 
    1273  
    1274     bool empty() { return _input.empty; } 
    1275     ElementType!(R) front() { return _input.front; } 
     1294        @property ElementType!(R) back() { return _input.back; } 
     1295    } 
     1296 
     1297    static if(isInfinite!R) 
     1298    { 
     1299        enum bool empty = false;  // Propagate infiniteness. 
     1300    } 
     1301    else 
     1302    { 
     1303        @property bool empty() { return _input.empty; } 
     1304    } 
     1305 
    12761306 
    12771307    static if(isForwardRange!R) { 
    12781308        @property typeof(this) save() { 
    12791309            return typeof(this)(_input.save); 
    12801310        } 
    12811311    } 
    12821312} 
    12831313 
    12841314/// Ditto 
    12851315Uniq!(pred, Range) uniq(alias pred = "a == b", Range)(Range r) 
     
    13481378            _current = tuple(_input.front, 1u); 
    13491379            _input.popFront; 
    13501380            while (!_input.empty && comp(_current.field[0], _input.front)) 
    13511381            { 
    13521382                ++_current.field[1]; 
    13531383                _input.popFront; 
    13541384            } 
    13551385        } 
    13561386    } 
    13571387 
    1358     @property bool empty() 
    1359     { 
    1360         return _current.field[1] == 0; 
     1388    static if(isInfinite!R) 
     1389    { 
     1390        enum bool empty = false;  // Propagate infiniteness. 
     1391    } 
     1392    else 
     1393    { 
     1394        @property bool empty() 
     1395        { 
     1396            return _current.field[1] == 0; 
     1397        } 
    13611398    } 
    13621399 
    13631400    @property ref Tuple!(ElementType!R, uint) front() 
    13641401    { 
    13651402        assert(!empty); 
    13661403        return _current; 
    13671404    } 
    13681405 
    13691406    static if(isForwardRange!R) { 
    13701407        @property typeof(this) save() { 
     
    20312068            _done = _input.empty || openRight && predSatisfied(); 
    20322069        } 
    20332070    else 
    20342071        this(Range input, OpenRight openRight = OpenRight.yes) 
    20352072        { 
    20362073            _input = input; 
    20372074            _openRight = openRight; 
    20382075            _done = _input.empty || openRight && predSatisfied(); 
    20392076        } 
    20402077 
    2041     bool empty() 
     2078    @property bool empty() 
    20422079    { 
    20432080        return _done; 
    20442081    } 
    20452082 
    2046     ElementType!Range front() 
     2083    @property ElementType!Range front() 
    20472084    { 
    20482085        assert(!empty); 
    20492086        return _input.front; 
    20502087    } 
    20512088 
    20522089    bool predSatisfied() 
    20532090    { 
    20542091        static if (is(Sentinel == void)) 
    20552092            return unaryFun!pred(_input.front); 
    20562093        else 
     
    54675504 
    54685505public: 
    54695506    alias CommonType!(staticMap!(.ElementType, Rs)) ElementType; 
    54705507 
    54715508    this(Rs rs) 
    54725509    { 
    54735510        this._r = rs; 
    54745511        adjustPosition(); 
    54755512    } 
    54765513 
    5477     bool empty() 
     5514    @property bool empty() 
    54785515    { 
    54795516        return _crt == _crt.max; 
    54805517    } 
    54815518 
    54825519    void popFront() 
    54835520    { 
    54845521        // Assumes _crt is correct 
    54855522        assert(!empty); 
    54865523        foreach (i, U; Rs) 
    54875524        { 
    54885525            if (i < _crt) continue; 
    54895526            // found _crt 
    54905527            assert(!_r[i].empty); 
    54915528            _r[i].popFront; 
    54925529            adjustPosition(); 
    54935530            return; 
    54945531        } 
    54955532        assert(false); 
    54965533    } 
    54975534 
    5498     ElementType front() 
     5535    @property ElementType front() 
    54995536    { 
    55005537        assert(!empty); 
    55015538        // Assume _crt is correct 
    55025539        foreach (i, U; Rs) 
    55035540        { 
    55045541            if (i < _crt) continue; 
    55055542            assert(!_r[i].empty); 
    55065543            return _r[i].front; 
    55075544        } 
    55085545        assert(false); 
     
    55865623    } 
    55875624 
    55885625public: 
    55895626    this(Rs input) 
    55905627    { 
    55915628        this._input = input; 
    55925629        // position to the first element 
    55935630        adjustPosition; 
    55945631    } 
    55955632 
    5596     bool empty() 
     5633    @property bool empty() 
    55975634    { 
    55985635        foreach (i, U; Rs) 
    55995636        { 
    56005637            if (_input[i].empty) return true; 
    56015638        } 
    56025639        return false; 
    56035640    } 
    56045641 
    56055642    void popFront() 
    56065643    { 
    56075644        assert(!empty); 
    56085645        assert(!comp(_input[0].front, _input[1].front) 
    56095646                && !comp(_input[1].front, _input[0].front)); 
    56105647        _input[0].popFront; 
    56115648        _input[1].popFront; 
    56125649        adjustPosition; 
    56135650    } 
    56145651 
    5615     ElementType front() 
     5652    @property ElementType front() 
    56165653    { 
    56175654        assert(!empty); 
    56185655        return _input[0].front; 
    56195656    } 
    56205657} 
    56215658 
    56225659/// Ditto 
    56235660SetIntersection!(less, Rs) setIntersection(alias less = "a < b", Rs...) 
    56245661(Rs ranges) 
    56255662if (allSatisfy!(isInputRange, Rs)) 
     
    56905727        // position to the first element 
    56915728        adjustPosition; 
    56925729    } 
    56935730 
    56945731    void popFront() 
    56955732    { 
    56965733        r1.popFront; 
    56975734        adjustPosition; 
    56985735    } 
    56995736 
    5700     ElementType!(R1) front() 
     5737    @property ElementType!(R1) front() 
    57015738    { 
    57025739        assert(!empty); 
    57035740        return r1.front; 
    57045741    } 
    57055742 
    57065743    bool empty() { return r1.empty; } 
    57075744} 
    57085745 
    57095746/// Ditto 
    57105747SetDifference!(less, R1, R2) setDifference(alias less = "a < b", R1, R2) 
     
    57815818            } 
    57825819            else 
    57835820            { 
    57845821                assert(comp(r2.front, r1.front)); 
    57855822                r2.popFront; 
    57865823            } 
    57875824        } 
    57885825        adjustPosition; 
    57895826    } 
    57905827 
    5791     ElementType!(R1) front() 
     5828    @property ElementType!(R1) front() 
    57925829    { 
    57935830        assert(!empty); 
    57945831        if (r2.empty || !r1.empty && comp(r1.front, r2.front)) 
    57955832        { 
    57965833            return r1.front; 
    57975834        } 
    57985835        assert(r1.empty || comp(r2.front, r1.front)); 
    57995836        return r2.front; 
    58005837    } 
    58015838 
    58025839    ref auto opSlice() { return this; } 
    58035840 
    5804     bool empty() { return r1.empty && r2.empty; } 
     5841    @property bool empty() { return r1.empty && r2.empty; } 
    58055842} 
    58065843 
    58075844/// Ditto 
    58085845SetSymmetricDifference!(less, R1, R2) 
    58095846setSymmetricDifference(alias less = "a < b", R1, R2) 
    58105847(R1 r1, R2 r2) 
    58115848{ 
    58125849    return typeof(return)(r1, r2); 
    58135850} 
    58145851 
     
    59315968 
    59325969    this(RangeOfRanges ror) 
    59335970    { 
    59345971        // Preemptively get rid of all empty ranges in the input 
    59355972        // No need for stability either 
    59365973        _ror = remove!("a.empty", SwapStrategy.unstable)(ror); 
    59375974        //Build the heap across the range 
    59385975        _heap.acquire(_ror); 
    59395976    } 
    59405977 
    5941     bool empty() { return _ror.empty; } 
    5942  
    5943     ref ElementType front() 
     5978    @property bool empty() { return _ror.empty; } 
     5979 
     5980    @property ref ElementType front() 
    59445981    { 
    59455982        return _heap.front.front; 
    59465983    } 
    59475984 
    59485985    void popFront() 
    59495986    { 
    59505987        _heap.removeFront(); 
    59515988        // let's look at the guy just popped 
    59525989        _ror.back.popFront; 
    59535990        if (_ror.back.empty)