Skip to content

Instantly share code, notes, and snippets.

@sausheong
Created May 16, 2022 06:46
Show Gist options
  • Save sausheong/c1225c91501f238b9686b3e1c8553c08 to your computer and use it in GitHub Desktop.
Save sausheong/c1225c91501f238b9686b3e1c8553c08 to your computer and use it in GitHub Desktop.
generics
type TSet[K comparable, V int] struct {
items map[K]V
}
func NewTSet[K comparable, V int]() TSet[K, V] {
return TSet[K, V]{items: make(map[K]V)}
}
func (s *TSet[K, V]) Add(item K) {
s.items[item] = 1
}
func (s *TSet[K, V]) Remove(item K) {
delete(s.items, item)
}
func (s TSet[K, V]) Has(item K) (ok bool) {
_, ok = s.items[item]
return
}
func (s *TSet[K, V]) List() (list []K) {
for k := range s.items {
list = append(list, k)
}
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment