Line data Source code
1 : //import 'dart:collection' show IterableBase; 2 : 3 : part of super_ranges; 4 : 5 : abstract class BaseRange extends IterableBase<int> { 6 : 7 : // Properties 8 : final int start, end; 9 : final bool closed; 10 : 11 : // Constructors 12 3 : const BaseRange({this.start, this.end, this.closed}); 13 : 14 : // Iterables / iterator 15 3 : @override 16 : Iterator<int> get iterator => 17 3 : ascending 18 6 : ? _range(1).iterator 19 6 : : _reverseRange(1).iterator; 20 : 21 3 : Iterator<int> iteratorWith({int stride}) => 22 3 : ascending 23 6 : ? _range(stride).iterator 24 4 : : _reverseRange(stride).iterator; 25 : 26 3 : Iterable<int> _range(int stride) sync* { 27 15 : for (int i = lowerBound; i < upperBound; i += stride.abs()) { 28 : yield i; 29 : } 30 : } 31 : 32 3 : Iterable<int> _reverseRange(int stride) sync* { 33 15 : for (int i = upperBound; i > lowerBound; i -= stride.abs()) { 34 : yield i; 35 : } 36 : } 37 : 38 : // Computed properties 39 12 : bool get ascending => start <= end; 40 : 41 3 : int get lowerBound { 42 3 : if (ascending) { 43 3 : return _lowerBoundWithoutClose; 44 : } 45 11 : return closed ? (_lowerBoundWithoutClose - 1) : _lowerBoundWithoutClose; 46 : } 47 : 48 3 : int get upperBound { 49 3 : if (ascending) { 50 11 : return closed ? (_upperBoundWithoutClose + 1) : _upperBoundWithoutClose; 51 : } 52 3 : return _upperBoundWithoutClose; 53 : } 54 : 55 : // Methods 56 : 57 : int get sum; 58 : 59 2 : @override 60 : bool operator==(Object other) { 61 2 : return identical(this, other) || (other is BaseRange && ( 62 12 : this.lowerBound == other.lowerBound && this.upperBound == other.upperBound 63 : )); 64 : } 65 : 66 1 : @override 67 3 : int get hashCode => lowerBound ^ upperBound; 68 : 69 1 : @override 70 : String toString() { 71 2 : final openRangeSymbol = closed ? "." : (ascending ? "<" :">"); 72 3 : return "($start..$openRangeSymbol$end)"; 73 : } 74 : 75 : // 76 2 : int get expensiveLength => super.length; 77 : // Private 78 12 : int get _lowerBoundWithoutClose => ascending ? start : end; 79 12 : int get _upperBoundWithoutClose => ascending ? end : start; 80 : }