forked from go/golangs_learn
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
139 lines
2.0 KiB
139 lines
2.0 KiB
4 years ago
|
package final_common
|
||
|
|
||
|
import (
|
||
|
"sort"
|
||
|
"sync"
|
||
|
)
|
||
|
|
||
|
type Set struct {
|
||
|
sync.RWMutex
|
||
|
m map[int]bool
|
||
|
}
|
||
|
|
||
|
// 新建集合对象
|
||
|
func NewSet(items ...int) *Set {
|
||
|
s := &Set{
|
||
|
m: make(map[int]bool, len(items)),
|
||
|
}
|
||
|
s.AddSet(items...)
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
// 添加元素
|
||
|
func (s *Set) AddSet(items ...int) {
|
||
|
s.Lock()
|
||
|
defer s.Unlock()
|
||
|
for _, v := range items {
|
||
|
s.m[v] = true
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// 删除元素
|
||
|
func (s *Set) RemoveSet(items ...int) {
|
||
|
s.Lock()
|
||
|
defer s.Unlock()
|
||
|
for _, v := range items {
|
||
|
delete(s.m, v)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// 判断元素是否存在
|
||
|
func (s *Set) HasSet(items ...int) bool {
|
||
|
s.RLock()
|
||
|
defer s.RUnlock()
|
||
|
for _, v := range items {
|
||
|
if _, ok := s.m[v]; !ok {
|
||
|
return false
|
||
|
}
|
||
|
}
|
||
|
return true
|
||
|
}
|
||
|
|
||
|
// 元素个数
|
||
|
func (s *Set) CountSet() int {
|
||
|
return len(s.m)
|
||
|
}
|
||
|
|
||
|
// 清空集合
|
||
|
func (s *Set) ClearSet() {
|
||
|
s.Lock()
|
||
|
defer s.Unlock()
|
||
|
s.m = map[int]bool{}
|
||
|
}
|
||
|
|
||
|
// 空集合判断
|
||
|
func (s *Set) EmptySet() bool {
|
||
|
return len(s.m) == 0
|
||
|
}
|
||
|
|
||
|
// 无序列表
|
||
|
func (s *Set) ListSet() []int {
|
||
|
s.RLock()
|
||
|
defer s.RUnlock()
|
||
|
list := make([]int, 0, len(s.m))
|
||
|
for item := range s.m {
|
||
|
list = append(list, item)
|
||
|
}
|
||
|
return list
|
||
|
}
|
||
|
|
||
|
// 排序列表
|
||
|
func (s *Set) SortListSet() []int {
|
||
|
s.RLock()
|
||
|
defer s.RUnlock()
|
||
|
list := make([]int, 0, len(s.m))
|
||
|
for item := range s.m {
|
||
|
list = append(list, item)
|
||
|
}
|
||
|
sort.Ints(list)
|
||
|
return list
|
||
|
}
|
||
|
|
||
|
// 并集
|
||
|
func (s *Set) UnionSet(sets ...*Set) *Set {
|
||
|
r := NewSet(s.ListSet()...)
|
||
|
for _, set := range sets {
|
||
|
for e := range set.m {
|
||
|
r.m[e] = true
|
||
|
}
|
||
|
}
|
||
|
return r
|
||
|
}
|
||
|
|
||
|
// 差集
|
||
|
func (s *Set) MinusSet(sets ...*Set) *Set {
|
||
|
r := NewSet(s.ListSet()...)
|
||
|
for _, set := range sets {
|
||
|
for e := range set.m {
|
||
|
if _, ok := s.m[e]; ok {
|
||
|
delete(r.m, e)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return r
|
||
|
}
|
||
|
|
||
|
// 交集
|
||
|
func (s *Set) IntersectSet(sets ...*Set) *Set {
|
||
|
r := NewSet(s.ListSet()...)
|
||
|
for _, set := range sets {
|
||
|
for e := range s.m {
|
||
|
if _, ok := set.m[e]; !ok {
|
||
|
delete(r.m, e)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return r
|
||
|
}
|
||
|
|
||
|
// 补集
|
||
|
func (s *Set) ComplementSet(full *Set) *Set {
|
||
|
r := NewSet()
|
||
|
for e := range full.m {
|
||
|
if _, ok := s.m[e]; !ok {
|
||
|
r.AddSet(e)
|
||
|
}
|
||
|
}
|
||
|
return r
|
||
|
}
|