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