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
|
||||
|
||||
}
|
||||
|
447
dataext/ringBuffer_test.go
Normal file
447
dataext/ringBuffer_test.go
Normal file
@@ -0,0 +1,447 @@
|
||||
package dataext
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestRingBufferPushAddsItem(t *testing.T) {
|
||||
rb := NewRingBuffer[int](3)
|
||||
rb.Push(1)
|
||||
if rb.Size() != 1 {
|
||||
t.Errorf("Expected size 1, got %d", rb.Size())
|
||||
}
|
||||
if item, _ := rb.Peek(); item != 1 {
|
||||
t.Errorf("Expected item 1, got %d", item)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRingBufferPushPopReturnsOldestItem(t *testing.T) {
|
||||
rb := NewRingBuffer[int](3)
|
||||
rb.Push(1)
|
||||
rb.Push(2)
|
||||
rb.Push(3)
|
||||
if item := rb.PushPop(4); item == nil || *item != 1 {
|
||||
t.Errorf("Expected item 1, got %v", item)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRingBufferPeekReturnsLastPushedItem(t *testing.T) {
|
||||
rb := NewRingBuffer[int](3)
|
||||
rb.Push(1)
|
||||
rb.Push(2)
|
||||
if item, _ := rb.Peek(); item != 2 {
|
||||
t.Errorf("Expected item 2, got %d", item)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRingBufferOverflow1(t *testing.T) {
|
||||
rb := NewRingBuffer[int](5)
|
||||
rb.Push(1) // overriden
|
||||
rb.Push(2) // overriden
|
||||
rb.Push(3)
|
||||
rb.Push(9)
|
||||
rb.Push(4)
|
||||
rb.Push(5)
|
||||
rb.Push(7)
|
||||
if rb.Size() != 5 {
|
||||
t.Errorf("Expected size 4, got %d", rb.Size())
|
||||
}
|
||||
expected := []int{3, 9, 4, 5, 7}
|
||||
items := rb.Items()
|
||||
for i, item := range items {
|
||||
if item != expected[i] {
|
||||
t.Errorf("Expected item %d, got %d", expected[i], item)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRingBufferItemsReturnsAllItems(t *testing.T) {
|
||||
rb := NewRingBuffer[int](3)
|
||||
rb.Push(1)
|
||||
rb.Push(2)
|
||||
rb.Push(3)
|
||||
items := rb.Items()
|
||||
expected := []int{1, 2, 3}
|
||||
for i, item := range items {
|
||||
if item != expected[i] {
|
||||
t.Errorf("Expected item %d, got %d", expected[i], item)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRingBufferClearEmptiesBuffer(t *testing.T) {
|
||||
rb := NewRingBuffer[int](3)
|
||||
rb.Push(1)
|
||||
rb.Clear()
|
||||
if rb.Size() != 0 {
|
||||
t.Errorf("Expected size 0, got %d", rb.Size())
|
||||
}
|
||||
}
|
||||
|
||||
func TestRingBufferIsFullReturnsTrueWhenFull(t *testing.T) {
|
||||
rb := NewRingBuffer[int](3)
|
||||
rb.Push(1)
|
||||
rb.Push(2)
|
||||
rb.Push(3)
|
||||
if !rb.IsFull() {
|
||||
t.Errorf("Expected buffer to be full")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRingBufferAtReturnsCorrectItem(t *testing.T) {
|
||||
rb := NewRingBuffer[int](3)
|
||||
rb.Push(1)
|
||||
rb.Push(2)
|
||||
rb.Push(3)
|
||||
if item := rb.At(1); item != 2 {
|
||||
t.Errorf("Expected item 2, got %d", item)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRingBufferGetReturnsCorrectItem(t *testing.T) {
|
||||
rb := NewRingBuffer[int](3)
|
||||
rb.Push(1)
|
||||
rb.Push(2)
|
||||
rb.Push(3)
|
||||
if item, ok := rb.Get(1); !ok || item != 2 {
|
||||
t.Errorf("Expected item 2, got %d", item)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRingBufferRemoveDeletesMatchingItems(t *testing.T) {
|
||||
rb := NewRingBuffer[int](5)
|
||||
rb.Push(1)
|
||||
rb.Push(2)
|
||||
rb.Push(3)
|
||||
rb.Push(2)
|
||||
rb.Push(4)
|
||||
removed := rb.Remove(func(v int) bool { return v == 2 })
|
||||
if removed != 2 {
|
||||
t.Errorf("Expected 2 items removed, got %d", removed)
|
||||
}
|
||||
if rb.Size() != 3 {
|
||||
t.Errorf("Expected size 3, got %d", rb.Size())
|
||||
}
|
||||
expected := []int{1, 3, 4}
|
||||
items := rb.Items()
|
||||
for i, item := range items {
|
||||
if item != expected[i] {
|
||||
t.Errorf("Expected item %d, got %d", expected[i], item)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRingBufferRemoveDeletesMatchingItems2(t *testing.T) {
|
||||
rb := NewRingBuffer[int](5)
|
||||
rb.Push(1)
|
||||
rb.Push(2)
|
||||
rb.Push(3)
|
||||
rb.Push(2)
|
||||
rb.Push(4)
|
||||
removed := rb.Remove(func(v int) bool { return v == 3 })
|
||||
if removed != 1 {
|
||||
t.Errorf("Expected 2 items removed, got %d", removed)
|
||||
}
|
||||
if rb.Size() != 4 {
|
||||
t.Errorf("Expected size 3, got %d", rb.Size())
|
||||
}
|
||||
expected := []int{1, 2, 2, 4}
|
||||
items := rb.Items()
|
||||
for i, item := range items {
|
||||
if item != expected[i] {
|
||||
t.Errorf("Expected item %d, got %d", expected[i], item)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRingBufferRemoveDeletesMatchingItems3(t *testing.T) {
|
||||
rb := NewRingBuffer[int](5)
|
||||
rb.Push(1)
|
||||
rb.Push(2)
|
||||
rb.Push(3)
|
||||
rb.Push(9)
|
||||
rb.Push(4)
|
||||
removed := rb.Remove(func(v int) bool { return v == 3 })
|
||||
if removed != 1 {
|
||||
t.Errorf("Expected 2 items removed, got %d", removed)
|
||||
}
|
||||
if rb.Size() != 4 {
|
||||
t.Errorf("Expected size 3, got %d", rb.Size())
|
||||
}
|
||||
expected := []int{1, 2, 9, 4}
|
||||
items := rb.Items()
|
||||
for i, item := range items {
|
||||
if item != expected[i] {
|
||||
t.Errorf("Expected item %d, got %d", expected[i], item)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRingBufferRemoveDeletesMatchingItems4(t *testing.T) {
|
||||
rb := NewRingBuffer[int](5)
|
||||
rb.Push(1) // overriden
|
||||
rb.Push(2) // overriden
|
||||
rb.Push(3)
|
||||
rb.Push(9)
|
||||
rb.Push(4)
|
||||
rb.Push(5)
|
||||
rb.Push(7)
|
||||
removed := rb.Remove(func(v int) bool { return v == 7 })
|
||||
if removed != 1 {
|
||||
t.Errorf("Expected 1 items removed, got %d", removed)
|
||||
}
|
||||
if rb.Size() != 4 {
|
||||
t.Errorf("Expected size 4, got %d", rb.Size())
|
||||
}
|
||||
expected := []int{3, 9, 4, 5}
|
||||
items := rb.Items()
|
||||
for i, item := range items {
|
||||
if item != expected[i] {
|
||||
t.Errorf("Expected item %d, got %d", expected[i], item)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRingBufferRemoveDeletesMatchingItems5(t *testing.T) {
|
||||
rb := NewRingBuffer[int](5)
|
||||
rb.Push(1) // overriden
|
||||
rb.Push(2) // overriden
|
||||
rb.Push(3)
|
||||
rb.Push(9)
|
||||
rb.Push(4)
|
||||
rb.Push(5)
|
||||
rb.Push(7)
|
||||
removed := rb.Remove(func(v int) bool { return v == 3 })
|
||||
if removed != 1 {
|
||||
t.Errorf("Expected 1 items removed, got %d", removed)
|
||||
}
|
||||
if rb.Size() != 4 {
|
||||
t.Errorf("Expected size 4, got %d", rb.Size())
|
||||
}
|
||||
expected := []int{9, 4, 5, 7}
|
||||
items := rb.Items()
|
||||
for i, item := range items {
|
||||
if item != expected[i] {
|
||||
t.Errorf("Expected item %d, got %d", expected[i], item)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRingBufferRemoveDeletesMatchingItems6(t *testing.T) {
|
||||
rb := NewRingBuffer[int](5)
|
||||
rb.Push(1) // overriden
|
||||
rb.Push(2) // overriden
|
||||
rb.Push(3)
|
||||
rb.Push(9)
|
||||
rb.Push(4)
|
||||
rb.Push(5)
|
||||
rb.Push(7)
|
||||
removed := rb.Remove(func(v int) bool { return v == 1 })
|
||||
if removed != 0 {
|
||||
t.Errorf("Expected 0 items removed, got %d", removed)
|
||||
}
|
||||
if rb.Size() != 5 {
|
||||
t.Errorf("Expected size 5, got %d", rb.Size())
|
||||
}
|
||||
expected := []int{3, 9, 4, 5, 7}
|
||||
items := rb.Items()
|
||||
for i, item := range items {
|
||||
if item != expected[i] {
|
||||
t.Errorf("Expected item %d, got %d", expected[i], item)
|
||||
}
|
||||
}
|
||||
if !rb.IsFull() {
|
||||
t.Errorf("Expected buffer to not be full")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRingBufferRemoveDeletesMatchingItems7(t *testing.T) {
|
||||
rb := NewRingBuffer[int](5)
|
||||
rb.Push(1) // overriden
|
||||
rb.Push(2) // overriden
|
||||
rb.Push(3)
|
||||
rb.Push(9)
|
||||
rb.Push(4)
|
||||
rb.Push(5)
|
||||
rb.Push(7)
|
||||
removed := rb.Remove(func(v int) bool { return v == 9 })
|
||||
if removed != 1 {
|
||||
t.Errorf("Expected 1 items removed, got %d", removed)
|
||||
}
|
||||
if rb.Size() != 4 {
|
||||
t.Errorf("Expected size 4, got %d", rb.Size())
|
||||
}
|
||||
expected := []int{3, 4, 5, 7}
|
||||
items := rb.Items()
|
||||
for i, item := range items {
|
||||
if item != expected[i] {
|
||||
t.Errorf("Expected item %d, got %d", expected[i], item)
|
||||
}
|
||||
}
|
||||
if rb.IsFull() {
|
||||
t.Errorf("Expected buffer to not be full")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRingBufferAddItemsToFullRingBuffer(t *testing.T) {
|
||||
rb := NewRingBuffer[int](3)
|
||||
rb.Push(1)
|
||||
rb.Push(2)
|
||||
rb.Push(3)
|
||||
rb.Push(4)
|
||||
if rb.Size() != 3 {
|
||||
t.Errorf("Expected size 3, got %d", rb.Size())
|
||||
}
|
||||
expected := []int{2, 3, 4}
|
||||
items := rb.Items()
|
||||
for i, item := range items {
|
||||
if item != expected[i] {
|
||||
t.Errorf("Expected item %d, got %d", expected[i], item)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRingBufferAddItemsToNonFullRingBuffer(t *testing.T) {
|
||||
rb := NewRingBuffer[int](3)
|
||||
rb.Push(1)
|
||||
rb.Push(2)
|
||||
if rb.Size() != 2 {
|
||||
t.Errorf("Expected size 2, got %d", rb.Size())
|
||||
}
|
||||
expected := []int{1, 2}
|
||||
items := rb.Items()
|
||||
for i, item := range items {
|
||||
if item != expected[i] {
|
||||
t.Errorf("Expected item %d, got %d", expected[i], item)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRingBufferRemoveItemsFromNonFullRingBuffer(t *testing.T) {
|
||||
rb := NewRingBuffer[int](3)
|
||||
rb.Push(1)
|
||||
rb.Push(2)
|
||||
removed := rb.Remove(func(v int) bool { return v == 1 })
|
||||
if removed != 1 {
|
||||
t.Errorf("Expected 1 item removed, got %d", removed)
|
||||
}
|
||||
if rb.Size() != 1 {
|
||||
t.Errorf("Expected size 1, got %d", rb.Size())
|
||||
}
|
||||
expected := []int{2}
|
||||
items := rb.Items()
|
||||
for i, item := range items {
|
||||
if item != expected[i] {
|
||||
t.Errorf("Expected item %d, got %d", expected[i], item)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRingBufferRemoveItemsFromFullRingBuffer(t *testing.T) {
|
||||
rb := NewRingBuffer[int](3)
|
||||
rb.Push(1)
|
||||
rb.Push(2)
|
||||
rb.Push(3)
|
||||
removed := rb.Remove(func(v int) bool { return v == 2 })
|
||||
if removed != 1 {
|
||||
t.Errorf("Expected 1 item removed, got %d", removed)
|
||||
}
|
||||
if rb.Size() != 2 {
|
||||
t.Errorf("Expected size 2, got %d", rb.Size())
|
||||
}
|
||||
expected := []int{1, 3}
|
||||
items := rb.Items()
|
||||
for i, item := range items {
|
||||
if item != expected[i] {
|
||||
t.Errorf("Expected item %d, got %d", expected[i], item)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRingBufferRemoveMultipleItemsFromRingBuffer(t *testing.T) {
|
||||
rb := NewRingBuffer[int](5)
|
||||
rb.Push(1)
|
||||
rb.Push(2)
|
||||
rb.Push(3)
|
||||
rb.Push(2)
|
||||
rb.Push(4)
|
||||
removed := rb.Remove(func(v int) bool { return v == 2 })
|
||||
if removed != 2 {
|
||||
t.Errorf("Expected 2 items removed, got %d", removed)
|
||||
}
|
||||
if rb.Size() != 3 {
|
||||
t.Errorf("Expected size 3, got %d", rb.Size())
|
||||
}
|
||||
expected := []int{1, 3, 4}
|
||||
items := rb.Items()
|
||||
for i, item := range items {
|
||||
if item != expected[i] {
|
||||
t.Errorf("Expected item %d, got %d", expected[i], item)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRingBufferRemoveAllItemsFromRingBuffer(t *testing.T) {
|
||||
rb := NewRingBuffer[int](3)
|
||||
rb.Push(1)
|
||||
rb.Push(2)
|
||||
rb.Push(3)
|
||||
removed := rb.Remove(func(v int) bool { return true })
|
||||
if removed != 3 {
|
||||
t.Errorf("Expected 3 items removed, got %d", removed)
|
||||
}
|
||||
if rb.Size() != 0 {
|
||||
t.Errorf("Expected size 0, got %d", rb.Size())
|
||||
}
|
||||
}
|
||||
|
||||
func TestRingBufferRemoveNoItemsFromRingBuffer(t *testing.T) {
|
||||
rb := NewRingBuffer[int](3)
|
||||
rb.Push(1)
|
||||
rb.Push(2)
|
||||
rb.Push(3)
|
||||
removed := rb.Remove(func(v int) bool { return false })
|
||||
if removed != 0 {
|
||||
t.Errorf("Expected 0 items removed, got %d", removed)
|
||||
}
|
||||
if rb.Size() != 3 {
|
||||
t.Errorf("Expected size 3, got %d", rb.Size())
|
||||
}
|
||||
}
|
||||
|
||||
func TestRingBufferIteratesOverAllItems(t *testing.T) {
|
||||
rb := NewRingBuffer[int](3)
|
||||
rb.Push(1)
|
||||
rb.Push(2)
|
||||
rb.Push(3)
|
||||
expected := []int{1, 2, 3}
|
||||
i := 0
|
||||
for item := range rb.Iter() {
|
||||
if item != expected[i] {
|
||||
t.Errorf("Expected item %d, got %d", expected[i], item)
|
||||
}
|
||||
i++
|
||||
}
|
||||
if i != len(expected) {
|
||||
t.Errorf("Expected to iterate over %d items, but iterated over %d", len(expected), i)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRingBufferIter2IteratesOverAllItemsWithIndices(t *testing.T) {
|
||||
rb := NewRingBuffer[int](3)
|
||||
rb.Push(1)
|
||||
rb.Push(2)
|
||||
rb.Push(3)
|
||||
expected := []int{1, 2, 3}
|
||||
i := 0
|
||||
for index, item := range rb.Iter2() {
|
||||
if index != i {
|
||||
t.Errorf("Expected index %d, got %d", i, index)
|
||||
}
|
||||
if item != expected[i] {
|
||||
t.Errorf("Expected item %d, got %d", expected[i], item)
|
||||
}
|
||||
i++
|
||||
}
|
||||
if i != len(expected) {
|
||||
t.Errorf("Expected to iterate over %d items, but iterated over %d", len(expected), i)
|
||||
}
|
||||
}
|
@@ -7,6 +7,10 @@ type SyncMap[TKey comparable, TData any] struct {
|
||||
lock sync.Mutex
|
||||
}
|
||||
|
||||
func NewSyncMap[TKey comparable, TData any]() *SyncMap[TKey, TData] {
|
||||
return &SyncMap[TKey, TData]{data: make(map[TKey]TData), lock: sync.Mutex{}}
|
||||
}
|
||||
|
||||
func (s *SyncMap[TKey, TData]) Set(key TKey, data TData) {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
|
143
dataext/syncRingSet.go
Normal file
143
dataext/syncRingSet.go
Normal file
@@ -0,0 +1,143 @@
|
||||
package dataext
|
||||
|
||||
import "sync"
|
||||
|
||||
type SyncRingSet[TData comparable] struct {
|
||||
data map[TData]bool
|
||||
lock sync.Mutex
|
||||
ring *RingBuffer[TData]
|
||||
}
|
||||
|
||||
func NewSyncRingSet[TData comparable](capacity int) *SyncRingSet[TData] {
|
||||
return &SyncRingSet[TData]{
|
||||
data: make(map[TData]bool, capacity+1),
|
||||
lock: sync.Mutex{},
|
||||
ring: NewRingBuffer[TData](capacity),
|
||||
}
|
||||
}
|
||||
|
||||
// Add adds `value` to the set
|
||||
// returns true if the value was actually inserted (value did not exist beforehand)
|
||||
// returns false if the value already existed
|
||||
func (s *SyncRingSet[TData]) Add(value TData) bool {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
|
||||
if s.data == nil {
|
||||
s.data = make(map[TData]bool)
|
||||
}
|
||||
|
||||
_, existsInPreState := s.data[value]
|
||||
if existsInPreState {
|
||||
return false
|
||||
}
|
||||
|
||||
prev := s.ring.PushPop(value)
|
||||
|
||||
s.data[value] = true
|
||||
if prev != nil {
|
||||
delete(s.data, *prev)
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (s *SyncRingSet[TData]) AddAll(values []TData) {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
|
||||
if s.data == nil {
|
||||
s.data = make(map[TData]bool)
|
||||
}
|
||||
|
||||
for _, value := range values {
|
||||
_, existsInPreState := s.data[value]
|
||||
if existsInPreState {
|
||||
continue
|
||||
}
|
||||
|
||||
prev := s.ring.PushPop(value)
|
||||
|
||||
s.data[value] = true
|
||||
if prev != nil {
|
||||
delete(s.data, *prev)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SyncRingSet[TData]) Remove(value TData) bool {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
|
||||
if s.data == nil {
|
||||
s.data = make(map[TData]bool)
|
||||
}
|
||||
|
||||
_, existsInPreState := s.data[value]
|
||||
if !existsInPreState {
|
||||
return false
|
||||
}
|
||||
|
||||
delete(s.data, value)
|
||||
s.ring.Remove(func(v TData) bool { return value == v })
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (s *SyncRingSet[TData]) RemoveAll(values []TData) {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
|
||||
if s.data == nil {
|
||||
s.data = make(map[TData]bool)
|
||||
}
|
||||
|
||||
for _, value := range values {
|
||||
delete(s.data, value)
|
||||
s.ring.Remove(func(v TData) bool { return value == v })
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SyncRingSet[TData]) Contains(value TData) bool {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
|
||||
if s.data == nil {
|
||||
s.data = make(map[TData]bool)
|
||||
}
|
||||
|
||||
_, ok := s.data[value]
|
||||
|
||||
return ok
|
||||
}
|
||||
|
||||
func (s *SyncRingSet[TData]) Get() []TData {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
|
||||
if s.data == nil {
|
||||
s.data = make(map[TData]bool)
|
||||
}
|
||||
|
||||
r := make([]TData, 0, len(s.data))
|
||||
|
||||
for k := range s.data {
|
||||
r = append(r, k)
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
// AddIfNotContains
|
||||
// returns true if the value was actually added (value did not exist beforehand)
|
||||
// returns false if the value already existed
|
||||
func (s *SyncRingSet[TData]) AddIfNotContains(key TData) bool {
|
||||
return s.Add(key)
|
||||
}
|
||||
|
||||
// RemoveIfContains
|
||||
// returns true if the value was actually removed (value did exist beforehand)
|
||||
// returns false if the value did not exist in the set
|
||||
func (s *SyncRingSet[TData]) RemoveIfContains(key TData) bool {
|
||||
return s.Remove(key)
|
||||
}
|
@@ -7,8 +7,12 @@ type SyncSet[TData comparable] struct {
|
||||
lock sync.Mutex
|
||||
}
|
||||
|
||||
func NewSyncSet[TData comparable]() *SyncSet[TData] {
|
||||
return &SyncSet[TData]{data: make(map[TData]bool), lock: sync.Mutex{}}
|
||||
}
|
||||
|
||||
// Add adds `value` to the set
|
||||
// returns true if the value was actually inserted
|
||||
// returns true if the value was actually inserted (value did not exist beforehand)
|
||||
// returns false if the value already existed
|
||||
func (s *SyncSet[TData]) Add(value TData) bool {
|
||||
s.lock.Lock()
|
||||
@@ -19,9 +23,12 @@ func (s *SyncSet[TData]) Add(value TData) bool {
|
||||
}
|
||||
|
||||
_, existsInPreState := s.data[value]
|
||||
s.data[value] = true
|
||||
if existsInPreState {
|
||||
return false
|
||||
}
|
||||
|
||||
return !existsInPreState
|
||||
s.data[value] = true
|
||||
return true
|
||||
}
|
||||
|
||||
func (s *SyncSet[TData]) AddAll(values []TData) {
|
||||
@@ -37,6 +44,36 @@ func (s *SyncSet[TData]) AddAll(values []TData) {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SyncSet[TData]) Remove(value TData) bool {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
|
||||
if s.data == nil {
|
||||
s.data = make(map[TData]bool)
|
||||
}
|
||||
|
||||
_, existsInPreState := s.data[value]
|
||||
if !existsInPreState {
|
||||
return false
|
||||
}
|
||||
|
||||
delete(s.data, value)
|
||||
return true
|
||||
}
|
||||
|
||||
func (s *SyncSet[TData]) RemoveAll(values []TData) {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
|
||||
if s.data == nil {
|
||||
s.data = make(map[TData]bool)
|
||||
}
|
||||
|
||||
for _, value := range values {
|
||||
delete(s.data, value)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SyncSet[TData]) Contains(value TData) bool {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
@@ -66,3 +103,17 @@ func (s *SyncSet[TData]) Get() []TData {
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
// AddIfNotContains
|
||||
// returns true if the value was actually added (value did not exist beforehand)
|
||||
// returns false if the value already existed
|
||||
func (s *SyncSet[TData]) AddIfNotContains(key TData) bool {
|
||||
return s.Add(key)
|
||||
}
|
||||
|
||||
// RemoveIfContains
|
||||
// returns true if the value was actually removed (value did exist beforehand)
|
||||
// returns false if the value did not exist in the set
|
||||
func (s *SyncSet[TData]) RemoveIfContains(key TData) bool {
|
||||
return s.Remove(key)
|
||||
}
|
||||
|
Reference in New Issue
Block a user