|
|
@ -3,26 +3,37 @@ package main
|
|
|
|
// Unique array data structure
|
|
|
|
// Unique array data structure
|
|
|
|
type uniq_arr[T comparable] struct {
|
|
|
|
type uniq_arr[T comparable] struct {
|
|
|
|
backingMap map[T]struct{} // Map is used to check if element exists - Empty structs occupy zero memory
|
|
|
|
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] {
|
|
|
|
func new_uniq_arr[T comparable]() uniq_arr[T] {
|
|
|
|
return uniq_arr[T]{
|
|
|
|
return uniq_arr[T]{
|
|
|
|
make(map[T]struct{}),
|
|
|
|
make(map[T]struct{}),
|
|
|
|
make([]T, 0),
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (s *uniq_arr[T]) add(vals ...T) {
|
|
|
|
func (s *uniq_arr[T]) add(vals ...T) {
|
|
|
|
for _, item := range vals {
|
|
|
|
for _, item := range vals {
|
|
|
|
if !s.contains(item) {
|
|
|
|
if !s.contains(item) {
|
|
|
|
s.backingMap[item] = struct{}{}
|
|
|
|
s.backingMap[item] = struct{}{}
|
|
|
|
s.vals = append(s.vals, item)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *uniq_arr[T]) contains(val T) bool {
|
|
|
|
func (s uniq_arr[T]) contains(val T) bool {
|
|
|
|
_, ok := s.backingMap[val]
|
|
|
|
_, ok := s.backingMap[val]
|
|
|
|
return ok
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (s *uniq_arr[T]) delete(val T) {
|
|
|
|
|
|
|
|
delete(s.backingMap, val)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (s uniq_arr[T]) values() []T {
|
|
|
|
|
|
|
|
toRet := make([]T, len(s.backingMap))
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
|
|
|
|
for k := range s.backingMap {
|
|
|
|
|
|
|
|
toRet[i] = k
|
|
|
|
|
|
|
|
i++
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return toRet
|
|
|
|
|
|
|
|
}
|
|
|
|