Commit 14c8db8
authored
feat: add SlidingWindowAggregator, a ring buffer with windowed aggregation (#7591)
A fixed-capacity circular buffer that additionally keeps an aggregate of
every element it currently holds, so a sliding window sum, minimum or
maximum is available in constant time.
Rather than the usual "add the new value, subtract the evicted one"
trick, which only works for invertible operators, it uses the two-stack
sliding window aggregation algorithm: the window is split into a front
section holding suffix aggregates and a back section summarised by a
single running aggregate. Every element takes part in at most one
rotation between the two, which makes insertion and removal O(1)
amortised with no allocation after construction. Any associative
operator works, commutative or not, and no identity element is needed.
Comes with factories for sum, minimum and maximum, indexed access, a
fail-fast iterator, and tests that check the aggregate against a brute
force reference under randomly interleaved insertions and removals.
Signed-off-by: alxkm <19151554+alxkm@users.noreply.github.com>
Co-authored-by: alxkm <19151554+alxkm@users.noreply.github.com>1 parent 0a6c756 commit 14c8db8
2 files changed
Lines changed: 896 additions & 0 deletions
File tree
- src
- main/java/com/thealgorithms/datastructures/buffers
- test/java/com/thealgorithms/datastructures/buffers
0 commit comments