-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: 迁移 concurrent.Slice 至 listings.SyncSlice
- Loading branch information
1 parent
e3475c6
commit e28a5a2
Showing
4 changed files
with
62 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package listings | ||
|
||
import ( | ||
"github.com/kercylan98/minotaur/utils/collection" | ||
"sync" | ||
) | ||
|
||
// NewSyncSlice 创建一个 SyncSlice | ||
func NewSyncSlice[V any](length, cap int) *SyncSlice[V] { | ||
s := &SyncSlice[V]{} | ||
if length > 0 || cap > 0 { | ||
s.data = make([]V, length, cap) | ||
} | ||
return s | ||
} | ||
|
||
// SyncSlice 是基于 sync.RWMutex 实现的线程安全的 slice | ||
type SyncSlice[V any] struct { | ||
rw sync.RWMutex | ||
data []V | ||
} | ||
|
||
func (slf *SyncSlice[V]) Get(index int) V { | ||
slf.rw.RLock() | ||
defer slf.rw.RUnlock() | ||
return slf.data[index] | ||
} | ||
|
||
func (slf *SyncSlice[V]) GetWithRange(start, end int) []V { | ||
return slf.data[start:end] | ||
} | ||
|
||
func (slf *SyncSlice[V]) Set(index int, value V) { | ||
slf.rw.Lock() | ||
slf.data[index] = value | ||
slf.rw.Unlock() | ||
} | ||
|
||
func (slf *SyncSlice[V]) Append(values ...V) { | ||
slf.rw.Lock() | ||
slf.data = append(slf.data, values...) | ||
slf.rw.Unlock() | ||
} | ||
|
||
func (slf *SyncSlice[V]) Release() { | ||
slf.rw.Lock() | ||
slf.data = nil | ||
slf.rw.Unlock() | ||
} | ||
|
||
func (slf *SyncSlice[V]) Clear() { | ||
slf.rw.Lock() | ||
slf.data = slf.data[:0] | ||
slf.rw.Unlock() | ||
} | ||
|
||
func (slf *SyncSlice[V]) GetData() []V { | ||
slf.rw.Lock() | ||
defer slf.rw.Unlock() | ||
return collection.CloneSlice(slf.data) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.