46 lines
655 B
Go
46 lines
655 B
Go
package set
|
|
|
|
type Set[T comparable] map[T]bool
|
|
|
|
func (this *Set[T]) Add(items ...T) {
|
|
for _, item := range items {
|
|
(*this)[item] = true
|
|
}
|
|
}
|
|
|
|
func (this Set[T]) Has(item T) bool {
|
|
return this[item] == true
|
|
}
|
|
|
|
func (this *Set[T]) Remove(items ...T) {
|
|
for _, item := range items {
|
|
delete(*this, item)
|
|
}
|
|
}
|
|
|
|
func (this *Set[T]) Union(o Set[T]) {
|
|
for item := range o {
|
|
this.Add(item)
|
|
}
|
|
}
|
|
|
|
func (this Set[T]) ToList() []T {
|
|
list := []T{}
|
|
|
|
for item := range this {
|
|
list = append(list, item)
|
|
}
|
|
|
|
return list
|
|
}
|
|
|
|
func New[T comparable](items ...T) Set[T] {
|
|
result := Set[T]{}
|
|
|
|
for _, item := range items {
|
|
result.Add(item)
|
|
}
|
|
|
|
return result
|
|
}
|