1
0
mirror of https://github.com/charlienet/go-mixed.git synced 2025-07-18 00:22:41 +08:00
This commit is contained in:
2024-05-28 04:25:17 +08:00
parent b4ac1cc449
commit 38f7cc75c9
2 changed files with 39 additions and 40 deletions

View File

@ -14,13 +14,12 @@ var _ Set[string] = &hash_set[string]{}
type hash_set[T constraints.Ordered] struct { type hash_set[T constraints.Ordered] struct {
m map[T]struct{} m map[T]struct{}
lock locker.RWLocker locker locker.RWLocker
} }
func NewHashSet[T constraints.Ordered](values ...T) *hash_set[T] { func NewHashSet[T constraints.Ordered](values ...T) *hash_set[T] {
set := hash_set[T]{ set := hash_set[T]{
m: make(map[T]struct{}, len(values)), m: make(map[T]struct{}, len(values)),
lock: locker.EmptyLocker,
} }
set.Add(values...) set.Add(values...)
@ -28,37 +27,41 @@ func NewHashSet[T constraints.Ordered](values ...T) *hash_set[T] {
} }
func (s *hash_set[T]) Sync() *hash_set[T] { func (s *hash_set[T]) Sync() *hash_set[T] {
s.lock = locker.NewRWLocker() s.locker.Synchronize()
return s return s
} }
func (s hash_set[T]) Add(values ...T) { func (s *hash_set[T]) Add(values ...T) Set[T] {
s.lock.Lock() s.locker.Lock()
defer s.lock.Unlock() defer s.locker.Unlock()
for _, v := range values { for _, v := range values {
s.m[v] = struct{}{} s.m[v] = struct{}{}
} }
return s
} }
func (s hash_set[T]) Remove(v T) { func (s *hash_set[T]) Remove(v T) Set[T] {
s.lock.Lock() s.locker.Lock()
defer s.lock.Unlock() defer s.locker.Unlock()
delete(s.m, v) delete(s.m, v)
return s
} }
func (s hash_set[T]) Contains(value T) bool { func (s *hash_set[T]) Contains(value T) bool {
s.lock.RLock() s.locker.RLock()
defer s.lock.RUnlock() defer s.locker.RUnlock()
_, ok := s.m[value] _, ok := s.m[value]
return ok return ok
} }
func (s hash_set[T]) ContainsAny(values ...T) bool { func (s *hash_set[T]) ContainsAny(values ...T) bool {
s.lock.RLock() s.locker.RLock()
defer s.lock.RUnlock() defer s.locker.RUnlock()
for _, v := range values { for _, v := range values {
if _, ok := s.m[v]; ok { if _, ok := s.m[v]; ok {
@ -69,9 +72,9 @@ func (s hash_set[T]) ContainsAny(values ...T) bool {
return false return false
} }
func (s hash_set[T]) ContainsAll(values ...T) bool { func (s *hash_set[T]) ContainsAll(values ...T) bool {
s.lock.RLock() s.locker.RLock()
defer s.lock.RUnlock() defer s.locker.RUnlock()
for _, v := range values { for _, v := range values {
if _, ok := s.m[v]; !ok { if _, ok := s.m[v]; !ok {
@ -82,15 +85,15 @@ func (s hash_set[T]) ContainsAll(values ...T) bool {
return true return true
} }
func (s hash_set[T]) Asc() Set[T] { func (s *hash_set[T]) Asc() Set[T] {
return s.copyToSorted().Asc() return s.copyToSorted().Asc()
} }
func (s hash_set[T]) Desc() Set[T] { func (s *hash_set[T]) Desc() Set[T] {
return s.copyToSorted().Desc() return s.copyToSorted().Desc()
} }
func (s hash_set[T]) copyToSorted() Set[T] { func (s *hash_set[T]) copyToSorted() Set[T] {
orderd := NewSortedSet[T]() orderd := NewSortedSet[T]()
for k := range s.m { for k := range s.m {
orderd.Add(k) orderd.Add(k)
@ -109,13 +112,13 @@ func (s *hash_set[T]) Clone() *hash_set[T] {
return set return set
} }
func (s hash_set[T]) Iterate(fn func(value T)) { func (s *hash_set[T]) Iterate(fn func(value T)) {
for v := range s.m { for v := range s.m {
fn(v) fn(v)
} }
} }
func (s hash_set[T]) ToSlice() []T { func (s *hash_set[T]) ToSlice() []T {
values := make([]T, 0, s.Size()) values := make([]T, 0, s.Size())
s.Iterate(func(value T) { s.Iterate(func(value T) {
values = append(values, value) values = append(values, value)
@ -124,15 +127,15 @@ func (s hash_set[T]) ToSlice() []T {
return values return values
} }
func (s hash_set[T]) IsEmpty() bool { func (s *hash_set[T]) IsEmpty() bool {
return len(s.m) == 0 return len(s.m) == 0
} }
func (s hash_set[T]) Size() int { func (s *hash_set[T]) Size() int {
return len(s.m) return len(s.m)
} }
func (s hash_set[T]) MarshalJSON() ([]byte, error) { func (s *hash_set[T]) MarshalJSON() ([]byte, error) {
items := make([]string, 0, s.Size()) items := make([]string, 0, s.Size())
for ele := range s.m { for ele := range s.m {
@ -147,7 +150,7 @@ func (s hash_set[T]) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("[%s]", strings.Join(items, ", "))), nil return []byte(fmt.Sprintf("[%s]", strings.Join(items, ", "))), nil
} }
func (s hash_set[T]) UnmarshalJSON(b []byte) error { func (s *hash_set[T]) UnmarshalJSON(b []byte) error {
var i []any var i []any
d := json.NewDecoder(bytes.NewReader(b)) d := json.NewDecoder(bytes.NewReader(b))
@ -166,7 +169,7 @@ func (s hash_set[T]) UnmarshalJSON(b []byte) error {
return nil return nil
} }
func (s hash_set[T]) String() string { func (s *hash_set[T]) String() string {
l := make([]string, 0, len(s.m)) l := make([]string, 0, len(s.m))
for k := range s.m { for k := range s.m {
l = append(l, fmt.Sprint(k)) l = append(l, fmt.Sprint(k))

View File

@ -1,15 +1,13 @@
package sets package sets
import ( import (
"sync"
"github.com/charlienet/go-mixed/locker" "github.com/charlienet/go-mixed/locker"
"golang.org/x/exp/constraints" "golang.org/x/exp/constraints"
) )
type Set[T comparable] interface { type Set[T comparable] interface {
Add(...T) Add(...T) Set[T]
Remove(v T) Remove(v T) Set[T]
Asc() Set[T] Asc() Set[T]
Desc() Set[T] Desc() Set[T]
Contains(T) bool Contains(T) bool
@ -19,18 +17,16 @@ type Set[T comparable] interface {
ToSlice() []T // 转换为切片 ToSlice() []T // 转换为切片
} }
var defaultOptions = option{locker: locker.NewEmptyLocker()}
type option struct { type option struct {
locker sync.Locker locker locker.Locker
} }
type setFunc func(option) type setFunc func(*option)
func WithSync() setFunc { func WithSync() setFunc {
return func(o option) { return func(o *option) {
o.locker = &sync.RWMutex{} o.locker.Synchronize()
} }
} }