-
Notifications
You must be signed in to change notification settings - Fork 3
/
set.go
78 lines (67 loc) · 1.48 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
72
73
74
75
76
77
78
package gohotdraw
import (
"container/vector"
)
type Set struct {
*vector.Vector
}
func NewSet() *Set {
return &Set{new(vector.Vector)}
}
//Convenience method for Push()
func (this *Set) Push(element interface{}) {
this.Add(element)
}
//Adds an element only if it is not already in the set
func (this *Set) Add(element interface{}) {
if !this.Contains(element) {
this.Vector.Push(element)
}
}
//Checks if element is conatined in set
func (this *Set) Contains(element interface{}) bool {
for i := 0; i < this.Vector.Len(); i++ {
currentElement := this.Vector.At(i)
if currentElement == element {
return true
}
}
return false
}
//Removes an element from the set
func (this *Set) Remove(element interface{}) {
for i := 0; i < this.Vector.Len(); i++ {
currentElement := this.Vector.At(i)
if currentElement == element {
this.Vector.Delete(i)
return
}
}
}
func (this *Set) Replace(toBeReplaced, replacement interface{}) {
for i := 0; i < this.Vector.Len(); i++ {
currentElement := this.Vector.At(i)
if currentElement == toBeReplaced {
this.Vector.Set(i, replacement)
}
}
}
//Returns a new Set with the elements of the original Set
func (this *Set) Clone() *Set {
set := NewSet()
for element := range this.Iter() {
set.Push(element)
}
return set
}
func (this *Set) Iter() <-chan interface{} {
c := make(chan interface{})
go this.iterate(c)
return c
}
func (this *Set) iterate(c chan<- interface{}) {
for _,v := range *this.Vector {
c <- v
}
close(c)
}