-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset.go
56 lines (47 loc) · 1.02 KB
/
set.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package go_common
// Set /**
type Set struct {
list map[interface{}]bool
}
func NewSet() *Set {
return &Set{
list: map[interface{}]bool{},
}
}
func (s *Set) IsEmpty() bool {
return len(s.list) <= 0
}
// Size return the number of the items in the set
func (s *Set) Size() int {
return len(s.list)
}
// Clear Removes all items from the set
func (s *Set) Clear() {
s.list = map[interface{}]bool{}
}
// Add : add the specific item to the set
// Return true if the set contains the elements to be removed
func (s *Set) Add(item interface{}) bool {
if _, ok := s.list[item]; !ok {
s.list[item] = true
return true
} else {
return false
}
}
// Remove : remove the specific item from the set if contains it.
// Returns true if the set contains the elements to be removed
func (s *Set) Remove(item interface{}) bool {
if _, ok := s.list[item]; ok {
delete(s.list, item)
return true
} else {
return false
}
}
func (s *Set) Contains(item interface{}) bool {
if _, ok := s.list[item]; ok {
return true
}
return false
}