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

Changeset 1567

Show
Ignore:
Timestamp:
05/29/10 14:18:18 (15 years ago)
Author:
rsinfu
Message:

Removed backByBack code from r1566. It was too slow for large input ranges.
Fixed typo.

Files:

Legend:

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

    r1566 r1567  
    11901190---- 
    11911191 */ 
    11921192 
    11931193struct Take(R) if (isInputRange!(R)) 
    11941194{ 
    11951195private: 
    11961196    R _input; 
    11971197    size_t _maxAvailable; 
    11981198    enum bool byRef = is(typeof(&_input.front) == ElementType!(R)*); 
    11991199 
    1200     enum bool backByIndex = // Take.back = input[min(n,$) - 1] 
    1201         isRandomAccessRange!(R) && (hasLength!(R) || isInfinite!(R)); 
    1202  
    1203     enum bool backByBack  = // Take.back = input.back 
    1204         !backByIndex && isBidirectionalRange!(R) && hasLength!(R); 
    1205  
    12061200public: 
    12071201    alias R Source; 
    12081202 
    12091203    static if (byRef) 
    12101204        alias ref .ElementType!(R) ElementType; 
    12111205    else 
    12121206        alias .ElementType!(R) ElementType; 
    12131207 
    1214     this(R input, size_t maxAvailable) 
    1215     { 
    1216         static if (backByBack) 
    1217         { 
    1218             while (input.length > maxAvailable) 
    1219                 input.popBack; 
    1220         } 
    1221         _input = input; 
    1222         _maxAvailable = maxAvailable; 
    1223     } 
    1224  
    12251208    bool empty() 
    12261209    { 
    12271210        return _maxAvailable == 0 || _input.empty; 
    12281211    } 
    12291212 
    12301213    void popFront() 
    12311214    { 
    12321215        enforce(_maxAvailable > 0, 
    1233             "Attenpting to popFront() past the end of a " 
     1216            "Attempting to popFront() past the end of a " 
    12341217            ~ Take.stringof); 
    12351218        _input.popFront; 
    12361219        --_maxAvailable; 
    12371220    } 
    12381221 
    12391222    // @@@@@@@@@@@ UGLY @@@@@@@@@@@@@@@ 
    12401223    mixin( 
    12411224        (byRef ? "ref " : "")~ 
    12421225        q{ElementType front() 
    12431226        { 
     
    12551238        } 
    12561239    } 
    12571240    else static if (hasLength!(R)) 
    12581241    { 
    12591242        @property size_t length() 
    12601243        { 
    12611244            return min(_maxAvailable, _input.length); 
    12621245        } 
    12631246    } 
    12641247 
    1265     static if (backByIndex
     1248    static if (isRandomAccessRange!(R) && (hasLength!(R) || isInfinite!(R))
    12661249    { 
    12671250        void popBack() 
    12681251        { 
    12691252            enforce(_maxAvailable > 0, 
    1270                 "Attenpting to popBack() past the beginning of a " 
     1253                "Attempting to popBack() past the beginning of a " 
    12711254                ~ Take.stringof); 
    12721255            --_maxAvailable; 
    12731256        } 
    12741257 
    12751258        mixin( 
    12761259            (byRef ? "ref " : "")~ 
    12771260            q{/+auto ref+/ ElementType back() 
    12781261            { 
    12791262                return _input[this.length - 1]; 
    1280             }}); 
    1281     } 
    1282     else static if (backByBack) 
    1283     { 
    1284         invariant() 
    1285         { 
    1286             assert(_input.length <= _maxAvailable); 
    1287         } 
    1288  
    1289         void popBack() 
    1290         { 
    1291             enforce(_maxAvailable > 0, 
    1292                 "Attenpting to popBack() past the beginning of a " 
    1293                 ~ Take.stringof); 
    1294             --_maxAvailable; 
    1295             _input.popBack; 
    1296         } 
    1297  
    1298         mixin( 
    1299             (byRef ? "ref " : "")~ 
    1300             q{/+auto ref+/ ElementType back() 
    1301             { 
    1302                 return _input.back; 
    13031263            }}); 
    13041264    } 
    13051265 
    13061266    static if (isRandomAccessRange!(R)) 
    13071267    { 
    13081268        mixin( 
    13091269            (byRef ? "ref " : "")~ 
    13101270            q{/+auto ref+/ ElementType opIndex(size_t index) 
    13111271            { 
    13121272                enforce(index < this.length,