CircularList<T> constructor
CircularList<T> (
- int capacity, {
- void onElementRemoved(
- T element
- void onElementAdded(
- T element
A fixed-size list implementation that automatically removes oldest elements when capacity is reached.
Provides O(1) operations for most common tasks and efficient memory usage with fixed capacity. Implements full List interface and adds specialized operations for numeric types.
Key features:
- Fixed memory footprint
- Automatic oldest element removal
- O(1) operations for add/remove/access
- Built-in numeric operations
- Change tracking via callbacks
Example:
final list = CircularList<int>(3)
..add(1)
..add(2)
..add(3);
print(list); // [1, 2, 3]
list.add(4);
print(list); // [2, 3, 4]
For numeric types (num, int, double), additional operations are available:
final prices = CircularList<double>(5)
..addAll([10.5, 11.0, 9.8, 10.2, 10.8]);
print(prices.average); // 10.46
print(prices.movingAverage(3)); // last 3 values average
Change tracking:
final list = CircularList<int>(3,
onElementAdded: (e) => print('Added: $e'),
onElementRemoved: (e) => print('Removed: $e'),
);
Implementation
CircularList(
this.capacity, {
this.onElementRemoved,
this.onElementAdded,
}) : assert(capacity > 1, 'Capacity must be greater than 1');