circular_list.dart 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. class CircularList<T> {
  2. List<T?> _array;
  3. int _length = 0;
  4. int _startIndex = 0;
  5. Function(int num)? onTrimmed;
  6. CircularList(int maxLength) : _array = List<T?>.filled(maxLength, null);
  7. // Gets the cyclic index for the specified regular index. The cyclic index can then be used on the
  8. // backing array to get the element associated with the regular index.
  9. int _getCyclicIndex(int index) {
  10. return (_startIndex + index) % _array.length;
  11. }
  12. int get maxLength {
  13. return _array.length;
  14. }
  15. set maxLength(int value) {
  16. if (value <= 0)
  17. throw ArgumentError.value(
  18. value, 'value', 'maxLength can\'t be negative!');
  19. if (value == _array.length) return;
  20. // Reconstruct array, starting at index 0. Only transfer values from the
  21. // indexes 0 to length.
  22. final newArray = List<T?>.generate(
  23. value,
  24. (index) => index < _array.length ? _array[_getCyclicIndex(index)] : null,
  25. );
  26. _startIndex = 0;
  27. _array = newArray;
  28. }
  29. int get length {
  30. return _length;
  31. }
  32. set length(int value) {
  33. if (value > _length) {
  34. for (int i = length; i < value; i++) {
  35. _array[i] = null;
  36. }
  37. }
  38. _length = value;
  39. }
  40. void forEach(
  41. void Function(T? item, int index) callback, [
  42. bool includeBuffer = false,
  43. ]) {
  44. final len = includeBuffer ? _array.length : _length;
  45. for (int i = 0; i < len; i++) {
  46. callback(_array[_getCyclicIndex(i)], i);
  47. }
  48. }
  49. T operator [](int index) {
  50. if (index > length - 1) {
  51. throw RangeError.range(index, 0, length - 1);
  52. }
  53. return _array[_getCyclicIndex(index)]!;
  54. }
  55. operator []=(int index, T value) {
  56. if (index > length - 1) {
  57. throw RangeError.range(index, 0, length - 1);
  58. }
  59. _array[_getCyclicIndex(index)] = value;
  60. }
  61. void clear() {
  62. _startIndex = 0;
  63. _length = 0;
  64. }
  65. void push(T value) {
  66. _array[_getCyclicIndex(_length)] = value;
  67. if (_length == _array.length) {
  68. _startIndex++;
  69. if (_startIndex == _array.length) {
  70. _startIndex = 0;
  71. }
  72. onTrimmed?.call(1);
  73. } else {
  74. _length++;
  75. }
  76. }
  77. /// Removes and returns the last value on the list
  78. T pop() {
  79. return _array[_getCyclicIndex(_length-- - 1)]!;
  80. }
  81. /// Deletes item at [index].
  82. void remove(int index, [int count = 1]) {
  83. if (count > 0) {
  84. for (var i = index; i < _length - count; i++) {
  85. _array[_getCyclicIndex(i)] = _array[_getCyclicIndex(i + count)];
  86. }
  87. length -= count;
  88. }
  89. }
  90. /// Inserts [item] at [index].
  91. void insert(int index, T item) {
  92. for (var i = _length - 1; i >= index; i--) {
  93. _array[_getCyclicIndex(i + 1)] = _array[_getCyclicIndex(i)];
  94. }
  95. _array[_getCyclicIndex(index)] = item;
  96. if (_length + 1 > _array.length) {
  97. _startIndex += 1;
  98. onTrimmed?.call(1);
  99. } else {
  100. _length++;
  101. }
  102. }
  103. /// Inserts [items] at [index] in order.
  104. void insertAll(int index, List<T> items) {
  105. for (var i = _length - 1; i >= index; i--) {
  106. _array[_getCyclicIndex(i + 1)] = _array[_getCyclicIndex(i)];
  107. }
  108. for (var i = 0; i < items.length; i++) {
  109. _array[_getCyclicIndex(index + i)] = items[i];
  110. }
  111. if (_length + items.length > _array.length) {
  112. final countToTrim = _length + items.length - _array.length;
  113. _startIndex += countToTrim;
  114. length = _array.length;
  115. onTrimmed?.call(countToTrim);
  116. } else {
  117. _length += items.length;
  118. }
  119. }
  120. void trimStart(int count) {
  121. if (count > _length) count = _length;
  122. // TODO: perhaps bug in original code, this does not clamp the value of startIndex
  123. _startIndex += count;
  124. _length -= count;
  125. onTrimmed?.call(count);
  126. }
  127. void shiftElements(int start, int count, int offset) {
  128. if (count < 0) return;
  129. if (start < 0 || start >= _length)
  130. throw Exception('Start argument is out of range');
  131. if (start + offset < 0)
  132. throw Exception('Can not shift elements in list beyond index 0');
  133. if (offset > 0) {
  134. for (var i = count - 1; i >= 0; i--) {
  135. this[start + i + offset] = this[start + i];
  136. }
  137. var expandListBy = (start + count + offset) - _length;
  138. if (expandListBy > 0) {
  139. _length += expandListBy;
  140. while (_length > _array.length) {
  141. length--;
  142. _startIndex++;
  143. onTrimmed?.call(1);
  144. }
  145. }
  146. } else {
  147. for (var i = 0; i < count; i++) {
  148. this[start + i + offset] = this[start + i];
  149. }
  150. }
  151. }
  152. bool get isFull => length == maxLength;
  153. List<T> toList() {
  154. return List<T>.generate(length, (index) => this[index]!);
  155. }
  156. }