|
@@ -2,14 +2,17 @@ package syncutils
|
|
|
|
|
|
import "sync"
|
|
|
|
|
|
+
|
|
|
type RangeFunc[T any] func(index int, e T) bool
|
|
|
|
|
|
+
|
|
|
type SyncSlice[T any] struct {
|
|
|
writeLocker sync.Locker
|
|
|
readLocker sync.Locker
|
|
|
s []T
|
|
|
}
|
|
|
|
|
|
+
|
|
|
func NewSyncSlice[T any](s []T, isRWMutex bool) *SyncSlice[T] {
|
|
|
var writeLocker sync.Locker
|
|
|
var readLocker sync.Locker
|
|
@@ -31,22 +34,27 @@ func NewSyncSlice[T any](s []T, isRWMutex bool) *SyncSlice[T] {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
func (syncSlice *SyncSlice[T]) Lock() {
|
|
|
syncSlice.writeLocker.Lock()
|
|
|
}
|
|
|
|
|
|
+
|
|
|
func (syncSlice *SyncSlice[T]) Unlock() {
|
|
|
syncSlice.writeLocker.Unlock()
|
|
|
}
|
|
|
|
|
|
+
|
|
|
func (syncSlice *SyncSlice[T]) RLock() {
|
|
|
syncSlice.readLocker.Lock()
|
|
|
}
|
|
|
|
|
|
+
|
|
|
func (syncSlice *SyncSlice[T]) RUnlock() {
|
|
|
syncSlice.readLocker.Unlock()
|
|
|
}
|
|
|
|
|
|
+
|
|
|
func (syncSlice *SyncSlice[T]) AppendNoLock(elements ...T) {
|
|
|
if elements == nil || len(elements) == 0 {
|
|
|
return
|
|
@@ -55,6 +63,7 @@ func (syncSlice *SyncSlice[T]) AppendNoLock(elements ...T) {
|
|
|
syncSlice.appendNoLock(elements...)
|
|
|
}
|
|
|
|
|
|
+
|
|
|
func (syncSlice *SyncSlice[T]) DeleteAtNoLock(index int) T {
|
|
|
if index < 0 {
|
|
|
var zero T
|
|
@@ -64,6 +73,7 @@ func (syncSlice *SyncSlice[T]) DeleteAtNoLock(index int) T {
|
|
|
return syncSlice.deleteAtNoLock(index)
|
|
|
}
|
|
|
|
|
|
+
|
|
|
func (syncSlice *SyncSlice[T]) RangeNoLock(rangeFunc RangeFunc[T]) {
|
|
|
if rangeFunc == nil {
|
|
|
return
|
|
@@ -72,14 +82,17 @@ func (syncSlice *SyncSlice[T]) RangeNoLock(rangeFunc RangeFunc[T]) {
|
|
|
syncSlice.rangeNoLock(rangeFunc)
|
|
|
}
|
|
|
|
|
|
+
|
|
|
func (syncSlice *SyncSlice[T]) LenNoLock() int {
|
|
|
return syncSlice.lenNoLock()
|
|
|
}
|
|
|
|
|
|
+
|
|
|
func (syncSlice *SyncSlice[T]) CapNoLock() int {
|
|
|
return syncSlice.capNoLock()
|
|
|
}
|
|
|
|
|
|
+
|
|
|
func (syncSlice *SyncSlice[T]) Append(elements ...T) {
|
|
|
if elements == nil || len(elements) == 0 {
|
|
|
return
|
|
@@ -91,6 +104,7 @@ func (syncSlice *SyncSlice[T]) Append(elements ...T) {
|
|
|
syncSlice.appendNoLock(elements...)
|
|
|
}
|
|
|
|
|
|
+
|
|
|
func (syncSlice *SyncSlice[T]) DeleteAt(index int) T {
|
|
|
if index < 0 {
|
|
|
var zero T
|
|
@@ -103,6 +117,7 @@ func (syncSlice *SyncSlice[T]) DeleteAt(index int) T {
|
|
|
return syncSlice.deleteAtNoLock(index)
|
|
|
}
|
|
|
|
|
|
+
|
|
|
func (syncSlice *SyncSlice[T]) Range(rangeFunc RangeFunc[T]) {
|
|
|
if rangeFunc == nil {
|
|
|
return
|
|
@@ -114,6 +129,7 @@ func (syncSlice *SyncSlice[T]) Range(rangeFunc RangeFunc[T]) {
|
|
|
syncSlice.rangeNoLock(rangeFunc)
|
|
|
}
|
|
|
|
|
|
+
|
|
|
func (syncSlice *SyncSlice[T]) Len() int {
|
|
|
syncSlice.readLocker.Lock()
|
|
|
defer syncSlice.readLocker.Unlock()
|
|
@@ -121,6 +137,7 @@ func (syncSlice *SyncSlice[T]) Len() int {
|
|
|
return syncSlice.lenNoLock()
|
|
|
}
|
|
|
|
|
|
+
|
|
|
func (syncSlice *SyncSlice[T]) Cap() int {
|
|
|
syncSlice.readLocker.Lock()
|
|
|
defer syncSlice.readLocker.Unlock()
|