You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
27 lines
390 B
Go
27 lines
390 B
Go
2 years ago
|
package util
|
||
|
|
||
|
type Set[T comparable] map[T]struct{}
|
||
|
|
||
|
func NewSet[T comparable](elements ...T) Set[T] {
|
||
|
set := Set[T]{}
|
||
|
|
||
|
for _, e := range elements {
|
||
|
set[e] = struct{}{}
|
||
|
}
|
||
|
|
||
|
return set
|
||
|
}
|
||
|
|
||
|
func (set Set[T]) Add(value T) {
|
||
|
set[value] = struct{}{}
|
||
|
}
|
||
|
|
||
|
func (set Set[T]) Remove(value T) {
|
||
|
delete(set, value)
|
||
|
}
|
||
|
|
||
|
func (set Set[T]) Has(value T) bool {
|
||
|
_, found := set[value]
|
||
|
return found
|
||
|
}
|