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

Changeset 1690

Show
Ignore:
Timestamp:
06/23/10 15:50:01 (14 years ago)
Author:
andrei
Message:

Fixed bugzilla 3839

Files:

Legend:

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

    r1689 r1690  
    21692169            this.pastLast = pastLast - 1; 
    21702170            this.pastLast -= (this.pastLast - current) % step; 
    21712171        } 
    21722172        else 
    21732173        { 
    21742174            this.pastLast = pastLast + 1; 
    21752175            this.pastLast += (this.pastLast - current) % step; 
    21762176        } 
    21772177        this.pastLast += step; 
    21782178    } 
    2179     bool empty() const { return current == pastLast; } 
    2180     N front() { return current; } 
     2179    /// Ditto 
     2180    @property bool empty() const { return current == pastLast; } 
     2181    /// Ditto 
     2182    @property N front() { return current; } 
     2183    /// Ditto 
    21812184    alias front moveFront; 
     2185    /// Ditto 
    21822186    void popFront() 
    21832187    { 
    21842188        current += step; 
    21852189    } 
    2186     N back() { return pastLast - step; } 
     2190    /// Ditto 
     2191    @property N back() { return pastLast - step; } 
     2192    /// Ditto 
    21872193    alias back moveBack; 
     2194    /// Ditto 
    21882195    void popBack() 
    21892196    { 
    21902197        pastLast -= step; 
    21912198    } 
    2192     Iota save() { return this; } 
     2199    /// Ditto 
     2200    @property Iota save() { return this; } 
     2201    /// Ditto 
    21932202    N opIndex(size_t n) 
    21942203    { 
    21952204        return current + step * n; 
    21962205    } 
    2197     size_t length() 
     2206    /// Ditto 
     2207    @property Select!(max(N.sizeof, S.sizeof) > size_t.sizeof, ulong, size_t) 
     2208    length() const 
    21982209    { 
    21992210        return (pastLast - current) / step; 
    22002211    } 
    22012212} 
    22022213 
    22032214// Iota for floating-point numbers 
    22042215/// Ditto 
    22052216struct Iota(N, S) if (isFloatingPoint!N && isNumeric!S) { 
    22062217    private N start; 
    22072218    private S step; 
    22082219    private size_t index, count; 
    22092220    this(N start, N end, S step) 
    22102221    { 
    22112222        this.start = start; 
    22122223        this.step = step; 
    2213         enforce(step != 0 
    2214                 && (start <= end && step > 0 || start >= end && step < 0)); 
     2224        enforce(step != 0); 
    22152225        immutable fcount = (end - start) / step; 
    22162226        enforce(fcount >= 0, "iota: incorrect startup parameters"); 
    22172227        count = to!size_t(fcount); 
    22182228        auto pastEnd = start + count * step; 
    22192229        if (step > 0) 
    22202230        { 
    22212231            if (pastEnd < end) ++count; 
    2222             assert(start + count * step >= end, text(count)); 
     2232            assert(start + count * step >= end); 
    22232233        } 
    22242234        else 
    22252235        { 
    22262236            if (pastEnd > end) ++count; 
    22272237            assert(start + count * step <= end); 
    22282238        } 
    22292239    } 
    2230     bool empty() const { return index == count; } 
    2231     N front() { return start + step * index; } 
     2240    /// Range primitives 
     2241    @property bool empty() const { return index == count; } 
     2242    /// Ditto 
     2243    @property N front() { return start + step * index; } 
     2244    /// Ditto 
    22322245    alias front moveFront; 
     2246    /// Ditto 
    22332247    void popFront() 
    22342248    { 
    22352249        enforce(!empty); 
    22362250        ++index; 
    22372251    } 
    2238     N back() 
     2252    /// Ditto 
     2253    @property N back() 
    22392254    { 
    22402255        enforce(!empty); 
    22412256        return start + step * (count - 1); 
    22422257    } 
     2258    /// Ditto 
    22432259    alias back moveBack; 
     2260    /// Ditto 
    22442261    void popBack() 
    22452262    { 
    22462263        enforce(!empty); 
    22472264        --count; 
    22482265    } 
    2249     Iota save() { return this; } 
     2266    /// Ditto 
     2267    @property Iota save() { return this; } 
     2268    /// Ditto 
    22502269    N opIndex(size_t n) 
    22512270    { 
    22522271        enforce(n < count); 
    22532272        return start + step * n; 
    22542273    } 
    2255     size_t length() 
     2274    /// Ditto 
     2275    @property size_t length() const 
    22562276    { 
    22572277        return count; 
    22582278    } 
    22592279} 
    22602280 
    22612281unittest 
    22622282{ 
    22632283    auto r = iota(0, 10, 1); 
    22642284    assert(equal(r, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9][])); 
    22652285    auto rr = iota(10); 
     
    22822302    //foreach (e; rf) writeln(e); 
    22832303    assert(approxEqual(rf, [0.0, 0.1, 0.2, 0.3, 0.4, 0.5][])); 
    22842304 
    22852305    // going down 
    22862306    rf = iota(0.0, -0.5, -0.1); 
    22872307    //foreach (e; rf) writeln(e); 
    22882308    assert(approxEqual(rf, [0.0, -0.1, -0.2, -0.3, -0.4][])); 
    22892309    rf = iota(0.0, nextDown(-0.5), -0.1); 
    22902310    //foreach (e; rf) writeln(e); 
    22912311    assert(approxEqual(rf, [0.0, -0.1, -0.2, -0.3, -0.4, -0.5][])); 
     2312 
     2313    // iota of longs 
     2314    auto rl = iota(5_000_000L); 
     2315    assert(rl.length == 5_000_000L); 
    22922316} 
    22932317 
    22942318unittest 
    22952319{ 
    22962320    auto idx = new size_t[100]; 
    22972321    copy(iota(0, idx.length), idx); 
    22982322} 
    22992323 
    23002324/** 
    23012325Options for the $(D FrontTransversal) and $(D Transversal) ranges