Line data Source code
1 : part of super_ranges; 2 : 3 : class StrideRange extends BaseRange { 4 : 5 : // Properties 6 : final int start, end; 7 : final bool closed; 8 : final int stride; 9 : 10 : // Constructors 11 2 : const StrideRange(this.start, this.end, {this.stride, this.closed = true}) : super(start: start, end: end, closed: closed); 12 6 : const StrideRange.named({this.start, this.end, this.stride, this.closed}) : super(start: start, end: end, closed: closed); 13 : 14 3 : @override 15 6 : Iterator<int> get iterator => super.iteratorWith(stride: this.stride); 16 : 17 : // Methods 18 : 19 : // TODO: improve length to be O(1) 20 : /*@override 21 : int get length { 22 : if (this.stride == 1) { 23 : return this.upperBound - this.lowerBound; 24 : } 25 : // 26 : return this.expensiveLength; 27 : }*/ 28 : 29 1 : @override 30 : String toString() { 31 4 : final stepsString = stride != 1 ? ", stride: $stride" : ""; 32 2 : final openRangeSymbol = closed ? "." : (ascending ? "<" :">"); 33 3 : return "($start..$openRangeSymbol$end$stepsString)"; 34 : } 35 : 36 : // TODO: `contains` should be different (?) 37 : 38 : /// O(n) sum of the members 39 1 : @override 40 : int get sum { 41 3 : if (this.start == this.end) { 42 2 : return this.closed ? this.start : 0; 43 : } 44 3 : return this.reduce((acc, newValue) => acc + newValue); 45 : } 46 : }