Skip to content

Commit

Permalink
add rwlist
Browse files Browse the repository at this point in the history
  • Loading branch information
hunter-bera committed Jul 3, 2024
1 parent 3c7d8b7 commit bd68ebb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
42 changes: 42 additions & 0 deletions tools/rwstore/rwlist.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package rwstore

import "sync"

type RWList[V any] struct {
a []V
sync.RWMutex
}

func NewRWList[V any]() *RWList[V] {
return &RWList[V]{
a: make([]V, 0),
}
}

func (rw *RWList[V]) Get(index int) (V, bool) {
rw.RLock()
defer rw.RUnlock()
if index < 0 || index >= len(rw.a) {
var zero V
return zero, false
}
return rw.a[index], true
}

func (rw *RWList[V]) Replace(newList []V) {
rw.Lock()
rw.a = newList
rw.Unlock()
}

func (rw *RWList[V]) Append(value V) {
rw.Lock()
rw.a = append(rw.a, value)
rw.Unlock()
}

func (rw *RWList[V]) Copy() []V {
rw.RLock()
defer rw.RUnlock()
return append([]V(nil), rw.a...)
}
2 changes: 1 addition & 1 deletion tools/rwmap/rwmap.go → tools/rwstore/rwmap.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package rwmap
package rwstore

import (
"sync"
Expand Down

0 comments on commit bd68ebb

Please sign in to comment.