Line data Source code
1 : part of super_ranges; 2 : 3 : class Range extends BaseRange { 4 : 5 : // Properties 6 : final int start, end; 7 : final bool closed; 8 : 9 : // Constructors 10 4 : const Range(this.start, this.end, {this.closed = false}) : super(start: start, end: end, closed: closed); 11 4 : const Range.named({this.start, this.end, this.closed}) : super(start: start, end: end, closed: closed); 12 4 : const Range.closed(this.start, this.end) : this.closed = true, super(start: start, end: end, closed: true); 13 : 14 3 : static StrideRange bySteps(int start, int end, {int stride, bool closed = true}) { 15 : final finalStride = stride ?? 1; 16 3 : if (finalStride > 0) { 17 3 : return StrideRange.named(start: start, end: end, stride: finalStride, closed: closed); 18 : } 19 1 : return StrideRange.named(start: end, end: start, stride: finalStride, closed: closed); 20 : } 21 : 22 : // Methods 23 : 24 : /// O(1) sum of the members 25 1 : @override 26 : int get sum { 27 : 28 1 : if (!ascending) { 29 6 : return Range(this.lowerBound+1, this.upperBound+1).sum; 30 : } 31 : 32 4 : final uB = closed ? this._upperBoundWithoutClose : (this._upperBoundWithoutClose - 1); 33 1 : final lB = this._lowerBoundWithoutClose; 34 5 : return ((uB - (lB-1)) * (uB + lB)) ~/ 2; 35 : } 36 : 37 : /// O(1) count of the members. Use `expensiveLength` for default O(n) implementation. 38 1 : @override 39 3 : int get length => this.upperBound - this.lowerBound; 40 : 41 : /// O(1) method to know if a values is contained within the range. 42 : /// If a list object is passed it will iterate that list, so it can be O(n) 43 2 : @override 44 : bool contains(Object value) { 45 2 : if (value is num) { 46 2 : if (ascending) { 47 14 : return start <= value && (closed ? value <= end : value < end); 48 : } else { 49 7 : return start >= value && (closed ? value >= end : value > end); 50 : } 51 : } 52 1 : else if (value is List<num>) { 53 2 : for (final aValue in value) { 54 1 : if (!this.contains(aValue)) { 55 : return false; 56 : } 57 : } 58 : return true; 59 : } 60 : return false; 61 : } 62 : }