并发安全的切片

This commit is contained in:
kercylan98 2023-05-26 10:07:16 +08:00
parent 50d7689376
commit 84c8cefea9
2 changed files with 80 additions and 0 deletions

View File

@ -0,0 +1,59 @@
package synchronization
import (
"github.com/kercylan98/minotaur/utils/slice"
"sync"
)
func NewSlice[T any](options ...SliceOption[T]) *Slice[T] {
s := &Slice[T]{}
for _, option := range options {
option(s)
}
return s
}
type Slice[T any] struct {
rw sync.RWMutex
data []T
}
func (slf *Slice[T]) Get(index int) T {
slf.rw.RLock()
defer slf.rw.RUnlock()
return slf.data[index]
}
func (slf *Slice[T]) GetWithRange(start, end int) []T {
return slf.data[start:end]
}
func (slf *Slice[T]) Set(index int, value T) {
slf.rw.Lock()
slf.data[index] = value
slf.rw.Unlock()
}
func (slf *Slice[T]) Append(values ...T) {
slf.rw.Lock()
slf.data = append(slf.data, values...)
slf.rw.Unlock()
}
func (slf *Slice[T]) Release() {
slf.rw.Lock()
slf.data = nil
slf.rw.Unlock()
}
func (slf *Slice[T]) Clear() {
slf.rw.Lock()
slf.data = slf.data[:0]
slf.rw.Unlock()
}
func (slf *Slice[T]) GetData() []T {
slf.rw.Lock()
defer slf.rw.Unlock()
return slice.Copy(slf.data)
}

View File

@ -0,0 +1,21 @@
package synchronization
type SliceOption[T any] func(slice *Slice[T])
func WithSliceLen[T any](len int) SliceOption[T] {
return func(slice *Slice[T]) {
slice.data = make([]T, len)
}
}
func WithSliceCap[T any](cap int) SliceOption[T] {
return func(slice *Slice[T]) {
slice.data = make([]T, 0, cap)
}
}
func WithSliceLenCap[T any](len, cap int) SliceOption[T] {
return func(slice *Slice[T]) {
slice.data = make([]T, len, cap)
}
}