Created
May 16, 2022 06:46
-
-
Save sausheong/c1225c91501f238b9686b3e1c8553c08 to your computer and use it in GitHub Desktop.
generics
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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