Skip to content

Commit

Permalink
add a concurrent-safe map
Browse files Browse the repository at this point in the history
  • Loading branch information
hunter-bera committed Jul 3, 2024
1 parent b290c91 commit 3c7d8b7
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions tools/rwmap/rwmap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package rwmap

import (
"sync"
)

type RWMap[K comparable, V any] struct {
m map[K]V
sync.RWMutex
}

func NewRWMap[K comparable, V any]() *RWMap[K, V] {
return &RWMap[K, V]{
m: make(map[K]V),
}
}

func (rw *RWMap[K, V]) Get(key K) (V, bool) {
rw.RLock()
value, ok := rw.m[key]
rw.RUnlock()
return value, ok
}

func (rw *RWMap[K, V]) Set(key K, value V) {
rw.Lock()
rw.m[key] = value
rw.Unlock()
}

func (rw *RWMap[K, V]) Delete(key K) {
rw.Lock()
delete(rw.m, key)
rw.Unlock()
}

func (rw *RWMap[K, V]) Exists(key K) bool {
rw.RLock()
_, exists := rw.m[key]
rw.RUnlock()
return exists
}

func (rw *RWMap[K, V]) Len() int {
rw.RLock()
length := len(rw.m)
rw.RUnlock()
return length
}

func (rw *RWMap[K, V]) Iterate(iter func(K, V) bool) {
rw.RLock()
defer rw.RUnlock()
for k, v := range rw.m {
if !iter(k, v) {
break
}
}
}

0 comments on commit 3c7d8b7

Please sign in to comment.