Created
March 2, 2022 07:15
-
-
Save suncle1993/41f44d5f975f6e794643558945fe6307 to your computer and use it in GitHub Desktop.
golang int64类型的集合
This file contains hidden or 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
package main | |
import ( | |
"fmt" | |
"sync" | |
) | |
// Int64Set int64类型的集合 | |
type Int64Set struct { | |
sync.RWMutex | |
m map[int64]bool | |
} | |
// NewInt64Set 新建集合对象 | |
func NewInt64Set(items ...int64) *Int64Set { | |
s := &Int64Set{ | |
m: make(map[int64]bool, len(items)), | |
} | |
s.Add(items...) | |
return s | |
} | |
// Add 添加元素 | |
func (s *Int64Set) Add(items ...int64) { | |
s.Lock() | |
defer s.Unlock() | |
for _, v := range items { | |
s.m[v] = true | |
} | |
} | |
// Remove 删除元素 | |
func (s *Int64Set) Remove(items ...int64) { | |
s.Lock() | |
defer s.Unlock() | |
for _, v := range items { | |
delete(s.m, v) | |
} | |
} | |
// Has 判断元素是否存在 | |
func (s *Int64Set) Has(items ...int64) bool { | |
s.RLock() | |
defer s.RUnlock() | |
for _, v := range items { | |
if _, ok := s.m[v]; !ok { | |
return false | |
} | |
} | |
return true | |
} | |
// Count 元素个数 | |
func (s *Int64Set) Count() int { | |
return len(s.m) | |
} | |
// Clear 清空集合 | |
func (s *Int64Set) Clear() { | |
s.Lock() | |
defer s.Unlock() | |
s.m = map[int64]bool{} | |
} | |
// Empty 空集合判断 | |
func (s *Int64Set) Empty() bool { | |
return len(s.m) == 0 | |
} | |
// List 无序列表 | |
func (s *Int64Set) List() []int64 { | |
s.RLock() | |
defer s.RUnlock() | |
list := make([]int64, len(s.m)) | |
for item := range s.m { | |
list = append(list, item) | |
} | |
return list | |
} | |
// Union 并集 | |
func (s *Int64Set) Union(sets ...*Int64Set) *Int64Set { | |
r := NewInt64Set(s.List()...) | |
for _, set := range sets { | |
for e := range set.m { | |
r.m[e] = true | |
} | |
} | |
return r | |
} | |
// Minus 差集 | |
func (s *Int64Set) Minus(sets ...*Int64Set) *Int64Set { | |
r := NewInt64Set(s.List()...) | |
fmt.Println(sets) | |
for _, set := range sets { | |
for e := range set.m { | |
if _, ok := s.m[e]; ok { | |
delete(r.m, e) | |
} | |
} | |
} | |
return r | |
} | |
// Intersect 交集 | |
func (s *Int64Set) Intersect(sets ...*Int64Set) *Int64Set { | |
r := NewInt64Set(s.List()...) | |
for _, set := range sets { | |
for e := range s.m { | |
if _, ok := set.m[e]; !ok { | |
delete(r.m, e) | |
} | |
} | |
} | |
return r | |
} | |
// Complement 补集 | |
func (s *Int64Set) Complement(full *Int64Set) *Int64Set { | |
r := NewInt64Set() | |
for e := range full.m { | |
if _, ok := s.m[e]; !ok { | |
r.Add(e) | |
} | |
} | |
return r | |
} | |
func main() { | |
fmt.Println("Hello, playground") | |
new := NewInt64Set(101683046, 101683036, 106740926, 789, 987, 321, 12) | |
old := NewInt64Set() | |
added := new.Minus(old).List() | |
fmt.Println(added) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment