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.

29 lines
584 B
Go

package main
// Unique array data structure
type uniq_arr[T comparable] struct {
backingMap map[T]struct{} // Map is used to check if element exists - Empty structs occupy zero memory
vals []T
}
func new_uniq_arr[T comparable]() uniq_arr[T] {
return uniq_arr[T]{
make(map[T]struct{}),
make([]T, 0),
}
}
func (s *uniq_arr[T]) add(vals ...T) {
for _, item := range vals {
if !s.contains(item) {
s.backingMap[item] = struct{}{}
s.vals = append(s.vals, item)
}
}
return
}
func (s *uniq_arr[T]) contains(val T) bool {
_, ok := s.backingMap[val]
return ok
}