-
Notifications
You must be signed in to change notification settings - Fork 0
/
randset.go
112 lines (85 loc) · 2.28 KB
/
randset.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package randset
import (
"sync"
)
// RandomizedSet represents a data structure that stores any comparable type uniquely in a randomized manner.
type RandomizedSet[T comparable] struct {
m map[T]struct{}
mu sync.Mutex
}
// New creates and returns a new instance of RandomizedSet for elements of type T.
func New[T comparable]() *RandomizedSet[T] {
return &RandomizedSet[T]{
m: make(map[T]struct{}),
}
}
// NewWithInitialSize creates and returns a new instance of RandomizedSet with an initial capacity.
func NewWithInitialSize[T comparable](size int) *RandomizedSet[T] {
return &RandomizedSet[T]{
m: make(map[T]struct{}, size),
}
}
// Add adds a new element to the set.
func (s *RandomizedSet[T]) Add(key T) {
s.mu.Lock()
defer s.mu.Unlock()
s.m[key] = struct{}{}
}
// Clear removes all elements from the set, resetting its state to empty.
func (s *RandomizedSet[T]) Clear() {
s.mu.Lock()
defer s.mu.Unlock()
s.m = make(map[T]struct{}, len(s.m))
}
// Contains checks whether a given element exists in the set.
func (s *RandomizedSet[T]) Contains(key T) bool {
s.mu.Lock()
defer s.mu.Unlock()
_, exists := s.m[key]
return exists
}
// Content returns a slice containing all the elements in the set. The order of elements
// in the slice is not specified and can be random.
func (s *RandomizedSet[T]) Content() []T {
s.mu.Lock()
defer s.mu.Unlock()
keys := make([]T, 0, len(s.m))
for k := range s.m {
keys = append(keys, k)
}
return keys
}
// LoadAndDelete removes and returns a random element from the set. It returns the element
// and true if the set was not empty. If the set is empty, it returns the zero value for T and false.
func (s *RandomizedSet[T]) LoadAndDelete() (T, bool) {
var (
key T
found bool
)
s.mu.Lock()
defer s.mu.Unlock()
for key = range s.m {
found = true
delete(s.m, key)
break
}
return key, found
}
// IsEmpty checks if the set is empty.
func (s *RandomizedSet[T]) IsEmpty() bool {
s.mu.Lock()
defer s.mu.Unlock()
return len(s.m) == 0
}
// Remove removes an element from the set.
func (s *RandomizedSet[T]) Remove(key T) {
s.mu.Lock()
defer s.mu.Unlock()
delete(s.m, key)
}
// Size returns the number of elements currently stored in the set.
func (s *RandomizedSet[T]) Size() int {
s.mu.Lock()
defer s.mu.Unlock()
return len(s.m)
}