-
Notifications
You must be signed in to change notification settings - Fork 1
/
set.go
65 lines (54 loc) · 1.46 KB
/
set.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
package container
// Set is an hash set data structure wrapped by go's map
type Set[K comparable] map[K]struct{}
// NewSet returns a new Set object
func NewSet[K comparable]() Set[K] {
return make(Set[K])
}
// Insert a new element into the Set
func (s Set[K]) Insert(k K) {
s[k] = struct{}{}
}
// Has the given key in the Set
func (s Set[K]) Has(k K) bool {
_, ok := s[k]
return ok
}
// Remove the given key from the Set.
// It has no-op if the key doesn't exists in Set
func (s Set[K]) Remove(k K) {
delete(s, k)
}
// Len returns the size of the Set
func (s Set[K]) Len() int {
return len(s)
}
/////////////////////////////
///////// Testing ///////////
/////////////////////////////
func checkHas[T comparable](s Set[T], entry T, expect bool) {
if got := s.Has(entry); got != expect {
panic(fmt.Sprintf("Wrong status of entry %v, expect %v, got %v", entry, expect, got))
}
}
type checkSetType struct {
first int
second string
}
func testSet() {
s := NewSet[string]()
checkHas[string](s, "apple", false)
s.Insert("apple")
checkHas[string](s, "apple", true)
s.Insert("banana")
checkHas[string](s, "apple", true)
checkHas[string](s, "banana", true)
s.Remove("banana")
checkHas[string](s, "apple", true)
checkHas[string](s, "banana", false)
s2 := NewSet[checkSetType]()
s2.Insert(checkSetType{1, "apple"})
checkHas[checkSetType](s2, checkSetType{1, "apple"}, true)
s2.Remove(checkSetType{1, "apple"})
checkHas[checkSetType](s2, checkSetType{1, "apple"}, false)
}