Files
Go-tool/util/set/Set.go
T
2020-06-15 18:04:01 +08:00

45 lines
747 B
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
* @Author : huangzj
* @Time : 2020/6/15 16:16
* @Description
*/
package set
import "sync"
type Set struct {
setMap sync.Map //用来存储元素的map
}
func (set *Set) Add(i interface{}) {
set.setMap.Store(i, nil)
}
func (set *Set) Remove(i interface{}) {
set.setMap.Delete(i)
}
func (set *Set) Contains(i interface{}) bool {
_, has := set.setMap.Load(i)
return has
}
func (set *Set) IsEmpty() bool {
length := 0
set.setMap.Range(func(key, value interface{}) bool {
length = length + 1
return true
})
return length == 0
}
func (set *Set) GetAllSet() []interface{} {
list := make([]interface{}, 0)
set.setMap.Range(func(key, value interface{}) bool {
list = append(list, key)
return true
})
return list
}