You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
803 B
52 lines
803 B
package genutils
|
|
|
|
type Set[T comparable] struct {
|
|
m map[T]bool
|
|
a []T
|
|
}
|
|
|
|
func (set *Set[T]) Add(values ...T) {
|
|
if set.m == nil {
|
|
set.m = make(map[T]bool, len(values)*4)
|
|
}
|
|
|
|
for _, value := range values {
|
|
if set.m[value] {
|
|
continue
|
|
}
|
|
|
|
set.m[value] = true
|
|
set.a = append(set.a, value)
|
|
}
|
|
}
|
|
|
|
func (set *Set[T]) Del(values ...T) {
|
|
if set.m == nil {
|
|
return
|
|
}
|
|
|
|
for _, value := range values {
|
|
if set.m[value] {
|
|
delete(set.m, value)
|
|
for i, value2 := range set.a {
|
|
if value2 == value {
|
|
// swap delete
|
|
set.a[i] = set.a[len(set.a)-1]
|
|
set.a = set.a[:len(set.a)-1]
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func (set *Set[T]) Has(v T) bool {
|
|
return set.m[v]
|
|
}
|
|
|
|
func (set *Set[T]) Len() int {
|
|
return len(set.a)
|
|
}
|
|
|
|
func (set *Set[T]) Values() []T {
|
|
return set.a[:len(set.a):len(set.a)]
|
|
}
|