1
0
mirror of https://github.com/charlienet/go-mixed.git synced 2025-07-17 16:12:42 +08:00
This commit is contained in:
2022-07-26 14:20:09 +08:00
parent 792458a185
commit 52fabedd66
4 changed files with 64 additions and 14 deletions

View File

@ -6,37 +6,62 @@ import (
"fmt" "fmt"
"strings" "strings"
"github.com/charlienet/go-mixed/locker"
"golang.org/x/exp/constraints" "golang.org/x/exp/constraints"
) )
var _ Set[string] = &hash_set[string]{} var _ Set[string] = &hash_set[string]{}
type hash_set[T constraints.Ordered] map[T]struct{} type hash_set[T constraints.Ordered] struct {
m map[T]struct{}
lock locker.RWLocker
}
func NewHashSet[T constraints.Ordered](values ...T) *hash_set[T] { func NewHashSet[T constraints.Ordered](values ...T) *hash_set[T] {
set := make(hash_set[T], len(values)) set := hash_set[T]{
m: make(map[T]struct{}, len(values)),
lock: locker.EmptyLocker,
}
set.Add(values...) set.Add(values...)
return &set return &set
} }
func (s *hash_set[T]) WithSync() *hash_set[T] {
s.lock = locker.NewRWLocker()
return s
}
func (s hash_set[T]) Add(values ...T) { func (s hash_set[T]) Add(values ...T) {
s.lock.Lock()
defer s.lock.Unlock()
for _, v := range values { for _, v := range values {
s[v] = struct{}{} s.m[v] = struct{}{}
} }
} }
func (s hash_set[T]) Remove(v T) { func (s hash_set[T]) Remove(v T) {
delete(s, v) s.lock.Lock()
defer s.lock.Unlock()
delete(s.m, v)
} }
func (s hash_set[T]) Contains(value T) bool { func (s hash_set[T]) Contains(value T) bool {
_, ok := s[value] s.lock.RLock()
defer s.lock.RUnlock()
_, 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()
defer s.lock.RUnlock()
for _, v := range values { for _, v := range values {
if _, ok := s[v]; ok { if _, ok := s.m[v]; ok {
return true return true
} }
} }
@ -45,8 +70,11 @@ func (s hash_set[T]) ContainsAny(values ...T) bool {
} }
func (s hash_set[T]) ContainsAll(values ...T) bool { func (s hash_set[T]) ContainsAll(values ...T) bool {
s.lock.RLock()
defer s.lock.RUnlock()
for _, v := range values { for _, v := range values {
if _, ok := s[v]; !ok { if _, ok := s.m[v]; !ok {
return false return false
} }
} }
@ -64,7 +92,7 @@ func (s hash_set[T]) Desc() Set[T] {
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 { for k := range s.m {
orderd.Add(k) orderd.Add(k)
} }
@ -78,7 +106,7 @@ func (s *hash_set[T]) Clone() *hash_set[T] {
} }
func (s hash_set[T]) Iterate(fn func(value T)) { func (s hash_set[T]) Iterate(fn func(value T)) {
for v := range s { for v := range s.m {
fn(v) fn(v)
} }
} }
@ -93,17 +121,17 @@ func (s hash_set[T]) ToSlice() []T {
} }
func (s hash_set[T]) IsEmpty() bool { func (s hash_set[T]) IsEmpty() bool {
return len(s) == 0 return len(s.m) == 0
} }
func (s hash_set[T]) Size() int { func (s hash_set[T]) Size() int {
return len(s) 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 { for ele := range s.m {
b, err := json.Marshal(ele) b, err := json.Marshal(ele)
if err != nil { if err != nil {
return nil, err return nil, err
@ -135,8 +163,8 @@ func (s hash_set[T]) UnmarshalJSON(b []byte) error {
} }
func (s hash_set[T]) String() string { func (s hash_set[T]) String() string {
l := make([]string, 0, len(s)) l := make([]string, 0, len(s.m))
for k := range s { for k := range s.m {
l = append(l, fmt.Sprint(k)) l = append(l, fmt.Sprint(k))
} }

View File

@ -3,6 +3,7 @@ package tests
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"net"
"testing" "testing"
) )
@ -37,3 +38,16 @@ func BenchmarkStringSplice(b *testing.B) {
} }
}) })
} }
func TestIPSegment(t *testing.T) {
i, n, err := net.ParseCIDR("0.0.0.0/0")
if err != nil {
t.Fatal(err)
}
t.Log(i, n)
address := net.ParseIP("192.168.0.2")
t.Log(n.Contains(address))
}

View File

@ -0,0 +1,7 @@
package validatortranslation
import (
ut "github.com/go-playground/universal-translator"
)
var Trans ut.Translator

View File

@ -0,0 +1 @@
package workerpool