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.

29 lines
432 B

1 year ago
  1. package genutils
  2. type Set[T comparable] struct {
  3. m map[T]bool
  4. a []T
  5. }
  6. func (set *Set[T]) Add(values ...T) {
  7. if set.m == nil {
  8. set.m = make(map[T]bool, len(values)*4)
  9. }
  10. for _, value := range values {
  11. if set.m[value] {
  12. continue
  13. }
  14. set.m[value] = true
  15. set.a = append(set.a, value)
  16. }
  17. }
  18. func (set *Set[T]) Len() int {
  19. return len(set.a)
  20. }
  21. func (set *Set[T]) Values() []T {
  22. return set.a[:len(set.a):len(set.a)]
  23. }