github.com/orangebees/go-oneutils@v0.0.10/Set/set.go (about) 1 package Set 2 3 import "sync" 4 5 type Set map[string]struct{} 6 7 func New() Set { 8 return make(map[string]struct{}, 64) 9 } 10 func (s Set) SAdd(strs ...string) { 11 for i := 0; i < len(strs); i++ { 12 s[strs[i]] = struct{}{} 13 } 14 } 15 func (s Set) SMembers() []string { 16 strs := make([]string, len(s)) 17 strs = strs[:0] 18 for s2 := range s { 19 strs = append(strs, s2) 20 } 21 return strs 22 } 23 func (s Set) SIsMember(str string) bool { 24 _, err := s[str] 25 return err 26 } 27 func (s Set) SRem(str string) error { 28 _, err := s[str] 29 if err { 30 31 } 32 delete(s, str) 33 return nil 34 } 35 36 // SUnion 并集 37 func (s Set) SUnion(sets ...Set) Set { 38 newset := AcquireSet() 39 for i := 0; i < len(sets); i++ { 40 for key, _ := range sets[i] { 41 newset.SAdd(key) 42 } 43 } 44 return newset 45 } 46 func (s Set) SCard() int { 47 return len(s) 48 } 49 50 func (s Set) Reset() { 51 for s2, _ := range s { 52 delete(s, s2) 53 } 54 } 55 56 var pool = sync.Pool{ 57 New: func() any { 58 return Set{} 59 }, 60 } 61 62 func AcquireSet() Set { 63 return pool.Get().(Set) 64 } 65 66 func ReleaseSet(s Set) { 67 s.Reset() 68 pool.Put(s) 69 }