From d9517fe73c42958488638fe8ead69e3297e1095c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Wed, 13 Nov 2024 15:03:51 +0100 Subject: [PATCH] v0.0.543 --- dataext/ringBuffer.go | 46 ++++ dataext/ringBuffer_test.go | 447 +++++++++++++++++++++++++++++++++++++ dataext/syncMap.go | 4 + dataext/syncRingSet.go | 143 ++++++++++++ dataext/syncSet.go | 57 ++++- go.mod | 18 +- go.sum | 18 ++ goextVersion.go | 4 +- 8 files changed, 723 insertions(+), 14 deletions(-) create mode 100644 dataext/ringBuffer_test.go create mode 100644 dataext/syncRingSet.go 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