v0.0.543
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 2m7s
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 2m7s
This commit is contained in:
@@ -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<capacity}
|
||||
|
||||
dc := 0
|
||||
b := make([]T, rb.capacity)
|
||||
bsize := 0
|
||||
|
||||
for i := 0; i < rb.size; i++ {
|
||||
comp := rb.At(i)
|
||||
if fnEqual(comp) {
|
||||
dc++
|
||||
} else {
|
||||
b[bsize] = comp
|
||||
bsize++
|
||||
}
|
||||
}
|
||||
|
||||
if dc == 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
rb.items = b
|
||||
rb.size = bsize
|
||||
rb.head = bsize % rb.capacity
|
||||
|
||||
return dc
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user