-
Notifications
You must be signed in to change notification settings - Fork 11
/
linkedhashsetint.go
82 lines (73 loc) · 1.99 KB
/
linkedhashsetint.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
79
80
81
82
package set
// LinkedHashSetINT linked hash set implementation using linkedHashMap as its
// underlying data structure.
//
// - Does not allow storing duplicated values
// - Does not allow storing nil values
// - Maintains insertion order over iteration
type LinkedHashSetINT struct {
linkedHashMap *linkedHashMap
}
// Add adds elements to the linked hash set
func (l *LinkedHashSetINT) Add(elements ...int) {
for _, element := range elements {
l.linkedHashMap.Put(element, nil)
}
}
// Remove removes elements from the linked hash set
func (l *LinkedHashSetINT) Remove(elements ...int) {
for _, element := range elements {
l.linkedHashMap.Remove(element)
}
}
// Iter iterates over each element of the linked hash set
func (l *LinkedHashSetINT) Iter() <-chan int {
ch := make(chan int, l.Length())
go func() {
for element := range l.linkedHashMap.Iter() {
ch <- element.key.(int)
}
close(ch)
}()
return ch
}
// Length returns the length of the linked hash set
func (l *LinkedHashSetINT) Length() int {
return l.linkedHashMap.Length()
}
// AsSlice returns a slice of all values of the linked hash set
func (l *LinkedHashSetINT) AsSlice() []int {
values := make([]int, 0, l.Length())
for value := range l.Iter() {
values = append(values, value)
}
return values
}
// AsInterface returns a slice of all values of the linked hash set
// as interface{}
func (l *LinkedHashSetINT) AsInterface() []interface{} {
values := make([]interface{}, 0, l.Length())
for value := range l.Iter() {
values = append(values, value)
}
return values
}
// InArray returns whether the given item is in array or not
func (l *LinkedHashSetINT) InArray(search int) bool {
for item := range l.Iter() {
if item == search {
return true
}
}
return false
}
// NewLinkedHashSetINT returns a new LinkedHashSetINT with the provided items
func NewLinkedHashSetINT(ints ...int) *LinkedHashSetINT {
lhm := &LinkedHashSetINT{
linkedHashMap: newLinkedHashMap(),
}
if len(ints) > 0 {
lhm.Add(ints...)
}
return lhm
}