-
Notifications
You must be signed in to change notification settings - Fork 11
/
linkedhashsetint64.go
82 lines (73 loc) · 2.03 KB
/
linkedhashsetint64.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
// LinkedHashSetINT64 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 LinkedHashSetINT64 struct {
linkedHashMap *linkedHashMap
}
// Add adds elements to the linked hash set
func (l *LinkedHashSetINT64) Add(elements ...int64) {
for _, element := range elements {
l.linkedHashMap.Put(element, nil)
}
}
// Remove removes elements from the linked hash set
func (l *LinkedHashSetINT64) Remove(elements ...int64) {
for _, element := range elements {
l.linkedHashMap.Remove(element)
}
}
// Iter iterates over each element of the linked hash set
func (l *LinkedHashSetINT64) Iter() <-chan int64 {
ch := make(chan int64, l.Length())
go func() {
for element := range l.linkedHashMap.Iter() {
ch <- element.key.(int64)
}
close(ch)
}()
return ch
}
// Length returns the length of the linked hash set
func (l *LinkedHashSetINT64) Length() int {
return l.linkedHashMap.Length()
}
// AsSlice returns a slice of all values of the linked hash set
func (l *LinkedHashSetINT64) AsSlice() []int64 {
values := make([]int64, 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 *LinkedHashSetINT64) 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 *LinkedHashSetINT64) InArray(search int64) bool {
for item := range l.Iter() {
if item == search {
return true
}
}
return false
}
// NewLinkedHashSetINT64 returns a new LinkedHashSetINT64 with the provided items
func NewLinkedHashSetINT64(ints ...int64) *LinkedHashSetINT64 {
lhm := &LinkedHashSetINT64{
linkedHashMap: newLinkedHashMap(),
}
if len(ints) > 0 {
lhm.Add(ints...)
}
return lhm
}