diff --git a/dataext/ringBuffer.go b/dataext/ringBuffer.go index bb1b311..10b3cbb 100644 --- a/dataext/ringBuffer.go +++ b/dataext/ringBuffer.go @@ -26,6 +26,20 @@ func (rb *RingBuffer[T]) Push(item T) { rb.head = (rb.head + 1) % rb.capacity } +func (rb *RingBuffer[T]) PushPop(item T) *T { + if rb.size < rb.capacity { + rb.size++ + rb.items[rb.head] = item + rb.head = (rb.head + 1) % rb.capacity + return nil + } else { + prev := rb.items[rb.head] + rb.items[rb.head] = item + rb.head = (rb.head + 1) % rb.capacity + return &prev + } +} + func (rb *RingBuffer[T]) Peek() (T, bool) { if rb.size == 0 { return *new(T), false @@ -96,3 +110,35 @@ func (rb *RingBuffer[T]) Iter2() iter.Seq2[int, T] { } } } + +func (rb *RingBuffer[T]) Remove(fnEqual func(v T) bool) int { + // Mike [2024-11-13]: I *really* tried to write an in-place algorithm to remove elements + // But after carful consideration, I left that as an exercise for future readers + // It is, suprisingly, non-trivial, especially because the head-ptr must be weirdly updated + // And out At() method does not work correctly with {head<>0 && size