blob: 79e1ae2fcd4c61bf859298e5f0cfa89a486d3e9d (
plain)
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
|
package collections
type Set[T comparable] struct {
items []T
index map[T]bool
}
func SetOf[T comparable](items ...T) Set[T] {
index := make(map[T]bool, len(items))
uniqueItems := make([]T, 0, len(items))
for _, item := range items {
if index[item] {
continue
}
index[item] = true
uniqueItems = append(uniqueItems, item)
}
return Set[T]{
items: uniqueItems,
index: index,
}
}
func (set Set[T]) Contains(item T) bool {
return set.index[item]
}
func (set Set[T]) All() []T {
copied := make([]T, len(set.items))
copy(copied, set.items)
return copied
}
func (set Set[T]) Len() int {
return len(set.items)
}
|