mirror of
https://github.com/charlienet/go-mixed.git
synced 2025-07-17 08:02:40 +08:00
set
This commit is contained in:
@ -13,14 +13,13 @@ import (
|
||||
var _ Set[string] = &hash_set[string]{}
|
||||
|
||||
type hash_set[T constraints.Ordered] struct {
|
||||
m map[T]struct{}
|
||||
lock locker.RWLocker
|
||||
m map[T]struct{}
|
||||
locker locker.RWLocker
|
||||
}
|
||||
|
||||
func NewHashSet[T constraints.Ordered](values ...T) *hash_set[T] {
|
||||
set := hash_set[T]{
|
||||
m: make(map[T]struct{}, len(values)),
|
||||
lock: locker.EmptyLocker,
|
||||
m: make(map[T]struct{}, len(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] {
|
||||
s.lock = locker.NewRWLocker()
|
||||
s.locker.Synchronize()
|
||||
return s
|
||||
}
|
||||
|
||||
func (s hash_set[T]) Add(values ...T) {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
func (s *hash_set[T]) Add(values ...T) Set[T] {
|
||||
s.locker.Lock()
|
||||
defer s.locker.Unlock()
|
||||
|
||||
for _, v := range values {
|
||||
s.m[v] = struct{}{}
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
func (s hash_set[T]) Remove(v T) {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
func (s *hash_set[T]) Remove(v T) Set[T] {
|
||||
s.locker.Lock()
|
||||
defer s.locker.Unlock()
|
||||
|
||||
delete(s.m, v)
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
func (s hash_set[T]) Contains(value T) bool {
|
||||
s.lock.RLock()
|
||||
defer s.lock.RUnlock()
|
||||
func (s *hash_set[T]) Contains(value T) bool {
|
||||
s.locker.RLock()
|
||||
defer s.locker.RUnlock()
|
||||
|
||||
_, ok := s.m[value]
|
||||
return ok
|
||||
}
|
||||
|
||||
func (s hash_set[T]) ContainsAny(values ...T) bool {
|
||||
s.lock.RLock()
|
||||
defer s.lock.RUnlock()
|
||||
func (s *hash_set[T]) ContainsAny(values ...T) bool {
|
||||
s.locker.RLock()
|
||||
defer s.locker.RUnlock()
|
||||
|
||||
for _, v := range values {
|
||||
if _, ok := s.m[v]; ok {
|
||||
@ -69,9 +72,9 @@ func (s hash_set[T]) ContainsAny(values ...T) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (s hash_set[T]) ContainsAll(values ...T) bool {
|
||||
s.lock.RLock()
|
||||
defer s.lock.RUnlock()
|
||||
func (s *hash_set[T]) ContainsAll(values ...T) bool {
|
||||
s.locker.RLock()
|
||||
defer s.locker.RUnlock()
|
||||
|
||||
for _, v := range values {
|
||||
if _, ok := s.m[v]; !ok {
|
||||
@ -82,15 +85,15 @@ func (s hash_set[T]) ContainsAll(values ...T) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (s hash_set[T]) Asc() Set[T] {
|
||||
func (s *hash_set[T]) Asc() Set[T] {
|
||||
return s.copyToSorted().Asc()
|
||||
}
|
||||
|
||||
func (s hash_set[T]) Desc() Set[T] {
|
||||
func (s *hash_set[T]) Desc() Set[T] {
|
||||
return s.copyToSorted().Desc()
|
||||
}
|
||||
|
||||
func (s hash_set[T]) copyToSorted() Set[T] {
|
||||
func (s *hash_set[T]) copyToSorted() Set[T] {
|
||||
orderd := NewSortedSet[T]()
|
||||
for k := range s.m {
|
||||
orderd.Add(k)
|
||||
@ -109,13 +112,13 @@ func (s *hash_set[T]) Clone() *hash_set[T] {
|
||||
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 {
|
||||
fn(v)
|
||||
}
|
||||
}
|
||||
|
||||
func (s hash_set[T]) ToSlice() []T {
|
||||
func (s *hash_set[T]) ToSlice() []T {
|
||||
values := make([]T, 0, s.Size())
|
||||
s.Iterate(func(value T) {
|
||||
values = append(values, value)
|
||||
@ -124,15 +127,15 @@ func (s hash_set[T]) ToSlice() []T {
|
||||
return values
|
||||
}
|
||||
|
||||
func (s hash_set[T]) IsEmpty() bool {
|
||||
func (s *hash_set[T]) IsEmpty() bool {
|
||||
return len(s.m) == 0
|
||||
}
|
||||
|
||||
func (s hash_set[T]) Size() int {
|
||||
func (s *hash_set[T]) Size() int {
|
||||
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())
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func (s hash_set[T]) UnmarshalJSON(b []byte) error {
|
||||
func (s *hash_set[T]) UnmarshalJSON(b []byte) error {
|
||||
var i []any
|
||||
|
||||
d := json.NewDecoder(bytes.NewReader(b))
|
||||
@ -166,7 +169,7 @@ func (s hash_set[T]) UnmarshalJSON(b []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s hash_set[T]) String() string {
|
||||
func (s *hash_set[T]) String() string {
|
||||
l := make([]string, 0, len(s.m))
|
||||
for k := range s.m {
|
||||
l = append(l, fmt.Sprint(k))
|
||||
|
16
sets/set.go
16
sets/set.go
@ -1,15 +1,13 @@
|
||||
package sets
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/charlienet/go-mixed/locker"
|
||||
"golang.org/x/exp/constraints"
|
||||
)
|
||||
|
||||
type Set[T comparable] interface {
|
||||
Add(...T)
|
||||
Remove(v T)
|
||||
Add(...T) Set[T]
|
||||
Remove(v T) Set[T]
|
||||
Asc() Set[T]
|
||||
Desc() Set[T]
|
||||
Contains(T) bool
|
||||
@ -19,18 +17,16 @@ type Set[T comparable] interface {
|
||||
ToSlice() []T // 转换为切片
|
||||
}
|
||||
|
||||
var defaultOptions = option{locker: locker.NewEmptyLocker()}
|
||||
|
||||
type option struct {
|
||||
locker sync.Locker
|
||||
locker locker.Locker
|
||||
}
|
||||
|
||||
type setFunc func(option)
|
||||
type setFunc func(*option)
|
||||
|
||||
func WithSync() setFunc {
|
||||
|
||||
return func(o option) {
|
||||
o.locker = &sync.RWMutex{}
|
||||
return func(o *option) {
|
||||
o.locker.Synchronize()
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user