circular_list.dart 4.3 KB

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