|
@@ -97,25 +97,44 @@ class CircularList<T> {
|
|
|
return _array[_getCyclicIndex(_length-- - 1)]!;
|
|
return _array[_getCyclicIndex(_length-- - 1)]!;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /// Deletes and/or inserts items at a particular index (in that order).
|
|
|
|
|
- void splice(int start, int deleteCount, List<T> items) {
|
|
|
|
|
- // delete items
|
|
|
|
|
- if (deleteCount > 0) {
|
|
|
|
|
- for (int i = start; i < _length - deleteCount; i++)
|
|
|
|
|
- _array[_getCyclicIndex(i)] = _array[_getCyclicIndex(i + deleteCount)];
|
|
|
|
|
- length -= deleteCount;
|
|
|
|
|
|
|
+ /// Deletes item at [index].
|
|
|
|
|
+ void remove(int index, [int count = 1]) {
|
|
|
|
|
+ if (count > 0) {
|
|
|
|
|
+ for (var i = index; i < _length - count; i++) {
|
|
|
|
|
+ _array[_getCyclicIndex(i)] = _array[_getCyclicIndex(i + count)];
|
|
|
|
|
+ }
|
|
|
|
|
+ length -= count;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /// Inserts [item] at [index].
|
|
|
|
|
+ void insert(int index, T item) {
|
|
|
|
|
+ for (var i = _length - 1; i >= index; i--) {
|
|
|
|
|
+ _array[_getCyclicIndex(i + 1)] = _array[_getCyclicIndex(i)];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ _array[_getCyclicIndex(index)] = item;
|
|
|
|
|
+
|
|
|
|
|
+ if (_length + 1 > _array.length) {
|
|
|
|
|
+ _startIndex += 1;
|
|
|
|
|
+ onTrimmed?.call(1);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ _length++;
|
|
|
}
|
|
}
|
|
|
- if (items.length != 0) {
|
|
|
|
|
- // add items
|
|
|
|
|
- for (int i = _length - 1; i >= start; i--)
|
|
|
|
|
- _array[_getCyclicIndex(i + items.length)] = _array[_getCyclicIndex(i)];
|
|
|
|
|
- for (int i = 0; i < items.length; i++)
|
|
|
|
|
- _array[_getCyclicIndex(start + i)] = items[i];
|
|
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /// Inserts [items] at [index] in order.
|
|
|
|
|
+ void insertAll(int index, List<T> items) {
|
|
|
|
|
+ for (var i = _length - 1; i >= index; i--) {
|
|
|
|
|
+ _array[_getCyclicIndex(i + 1)] = _array[_getCyclicIndex(i)];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ for (var i = 0; i < items.length; i++) {
|
|
|
|
|
+ _array[_getCyclicIndex(index + i)] = items[i];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // Adjust length as needed
|
|
|
|
|
if (_length + items.length > _array.length) {
|
|
if (_length + items.length > _array.length) {
|
|
|
- int countToTrim = _length + items.length - _array.length;
|
|
|
|
|
|
|
+ final countToTrim = _length + items.length - _array.length;
|
|
|
_startIndex += countToTrim;
|
|
_startIndex += countToTrim;
|
|
|
length = _array.length;
|
|
length = _array.length;
|
|
|
onTrimmed?.call(countToTrim);
|
|
onTrimmed?.call(countToTrim);
|