-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset.go
More file actions
49 lines (38 loc) · 973 Bytes
/
Copy pathset.go
File metadata and controls
49 lines (38 loc) · 973 Bytes
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
package ungo
import "fmt"
type Set[T comparable] SmallMap[T, struct{}]
func NewSet[T comparable](elements ...T) Set[T] {
s := Set[T](*NewSmallMap[T, struct{}](0xFFF))
for _, elem := range elements {
s.Add(elem)
}
return s
}
func (s *Set[T]) String() string {
return fmt.Sprintf("Set(%v)", s.ToSlice())
}
func (s *Set[T]) Add(element T) {
(*SmallMap[T, struct{}])(s).Set(element, struct{}{})
}
func (s *Set[T]) Remove(element T) {
(*SmallMap[T, struct{}])(s).Delete(element)
}
func (s Set[T]) Contains(element T) bool {
return (*SmallMap[T, struct{}])(&s).Contains(element)
}
func (s Set[T]) Size() int {
return (*SmallMap[T, struct{}])(&s).Size()
}
func (s *Set[T]) Clear() {
(*SmallMap[T, struct{}])(s).Clear()
}
func (s *Set[T]) ToSlice() []T {
return (*SmallMap[T, struct{}])(s).Keys()
}
func SetFromSlice[T comparable](slice []T) Set[T] {
s := Set[T](*NewSmallMap[T, struct{}](0xFFF))
for _, elem := range slice {
s.Add(elem)
}
return s
}