-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
210 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import 'package:flutter/foundation.dart'; | ||
|
||
/// | ||
/// | ||
/// | ||
@immutable | ||
class Range { | ||
final int _first; | ||
final int _last; | ||
|
||
/// | ||
/// | ||
/// | ||
const Range(int first, int last) | ||
: assert(last >= first, 'First must be less than or equal to last'), | ||
_first = first, | ||
_last = last; | ||
|
||
/// | ||
/// | ||
/// | ||
const Range.exclusive(int first, int last) : this(first, last - 1); | ||
|
||
/// | ||
/// | ||
/// | ||
int get first => _first; | ||
|
||
/// | ||
/// | ||
/// | ||
int get last => _last; | ||
|
||
/// | ||
/// | ||
/// | ||
int get length => _last - _first + 1; | ||
|
||
/// | ||
/// | ||
/// | ||
bool contains(int value) => value >= _first && value <= _last; | ||
|
||
/// | ||
/// | ||
/// | ||
List<int> toList() => List<int>.generate(length, (int i) => _first + i); | ||
|
||
/// | ||
/// | ||
/// | ||
@override | ||
String toString() => 'Range($_first, $_last)'; | ||
|
||
/// | ||
/// | ||
/// | ||
@override | ||
int get hashCode => '$_first$_last'.hashCode; | ||
|
||
/// | ||
/// | ||
/// | ||
@override | ||
bool operator ==(Object other) => | ||
other is Range && _first == other._first && _last == other._last; | ||
|
||
/// | ||
/// | ||
/// | ||
Range copyWith({int? first, int? last}) => | ||
Range(first ?? _first, last ?? _last); | ||
|
||
/// | ||
/// | ||
/// | ||
bool intersects(Range other) => | ||
_first <= other._last && _last >= other._first; | ||
} |
Oops, something went wrong.