Repository navigation
Expand file tree
/
Copy pathset.go
More file actions
118 lines (103 loc) · 2.83 KB
/
Copy pathset.go
File metadata and controls
118 lines (103 loc) · 2.83 KB
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
113
114
115
116
117
118
package funk
// Set is a mutable collection of distinct comparable values. Its zero value is
// ready to use. A Set must not be copied after first use because copies share
// the same backing map. Set is not safe for concurrent mutation. Method
// receivers and Set operands must be non-nil.
type Set[T comparable] struct {
items map[T]struct{}
}
// Values returns the values in s in unspecified order.
func (s *Set[T]) Values() []T {
values := make([]T, 0, len(s.items))
for value := range s.items {
values = append(values, value)
}
return values
}
// Contains reports whether value is in s.
func (s *Set[T]) Contains(value T) bool {
_, exists := s.items[value]
return exists
}
// Add inserts value into s.
func (s *Set[T]) Add(value T) {
if s.items == nil {
s.items = make(map[T]struct{})
}
s.items[value] = struct{}{}
}
// Remove deletes value from s.
func (s *Set[T]) Remove(value T) {
delete(s.items, value)
}
// Clear removes all values from s.
func (s *Set[T]) Clear() {
clear(s.items)
}
// Len returns the number of values in s.
func (s *Set[T]) Len() int {
return len(s.items)
}
// AddAll inserts every value in items into s.
func (s *Set[T]) AddAll(items ...T) {
if s.items == nil {
s.items = make(map[T]struct{}, len(items))
}
for _, value := range items {
s.items[value] = struct{}{}
}
}
// Filter returns a new set containing values accepted by predicate.
func (s *Set[T]) Filter(predicate func(T) bool) *Set[T] {
result := newSet[T](len(s.items))
for value := range s.items {
if predicate(value) {
result.items[value] = struct{}{}
}
}
return result
}
// Union returns a new set containing values from s and other.
func (s *Set[T]) Union(other *Set[T]) *Set[T] {
result := newSet[T](len(s.items) + len(other.items))
for value := range s.items {
result.items[value] = struct{}{}
}
for value := range other.items {
result.items[value] = struct{}{}
}
return result
}
// Intersect returns a new set containing values present in both s and other.
func (s *Set[T]) Intersect(other *Set[T]) *Set[T] {
smaller, larger := s, other
if len(smaller.items) > len(larger.items) {
smaller, larger = larger, smaller
}
result := newSet[T](len(smaller.items))
for value := range smaller.items {
if larger.Contains(value) {
result.items[value] = struct{}{}
}
}
return result
}
// Difference returns a new set containing values in s but not other.
func (s *Set[T]) Difference(other *Set[T]) *Set[T] {
result := newSet[T](len(s.items))
for value := range s.items {
if !other.Contains(value) {
result.items[value] = struct{}{}
}
}
return result
}
// NewSet returns a set initialized with initial.
func NewSet[T comparable](initial ...T) *Set[T] {
set := newSet[T](len(initial))
set.AddAll(initial...)
return set
}
func newSet[T comparable](capacity int) *Set[T] {
return &Set[T]{items: make(map[T]struct{}, capacity)}
}