-
Notifications
You must be signed in to change notification settings - Fork 0
/
set.go
71 lines (60 loc) · 1.03 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package ds
//Data structure to represent a mathematical Set
type Set struct {
data map[interface{}]bool
}
//Public Fucntions
func MakeEmptySet() *Set {
var r Set
r.data = make(map[interface{}]bool)
return &r
}
func (s Set) Empty() bool {
return len(s.data) == 0
}
func (s Set) Size() int {
return len(s.data)
}
func (s *Set) AddElem(val interface{}) {
_, present := s.data[val]
if !present {
s.data[val] = true
}
}
func (s *Set) DelElem(val interface{}) {
if s.Exist(val) {
delete(s.data, val)
}
}
func (s Set) Exist(val interface{}) bool {
_, present := s.data[val]
return present
}
func (s Set) Union(o *Set) *Set {
r := MakeEmptySet()
for v, _ := range s.data {
r.AddElem(v)
}
for v, _ := range o.data {
r.AddElem(v)
}
return r
}
func (s Set) Intersect(o *Set) *Set {
r := MakeEmptySet()
for v, _ := range s.data {
if o.Exist(v) {
r.AddElem(v)
}
}
return r
}
func (s Set) Difference(o *Set) *Set {
r := MakeEmptySet()
for v, _ := range s.data {
if !o.Exist(v) {
r.AddElem(v)
}
}
return r
}