2022-01-19 20:45:50 +03:00
|
|
|
// Package aghalg contains common generic algorithms and data structures.
|
2021-12-28 17:00:52 +03:00
|
|
|
//
|
2022-08-03 14:36:18 +03:00
|
|
|
// TODO(a.garipov): Move parts of this into golibs.
|
2022-01-19 20:45:50 +03:00
|
|
|
package aghalg
|
2021-12-28 17:00:52 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2022-08-03 14:36:18 +03:00
|
|
|
"golang.org/x/exp/constraints"
|
|
|
|
"golang.org/x/exp/slices"
|
|
|
|
)
|
2021-12-28 17:00:52 +03:00
|
|
|
|
2022-01-19 20:45:50 +03:00
|
|
|
// UniqChecker allows validating uniqueness of comparable items.
|
2022-08-03 14:36:18 +03:00
|
|
|
//
|
|
|
|
// TODO(a.garipov): The Ordered constraint is only really necessary in Validate.
|
|
|
|
// Consider ways of making this constraint comparable instead.
|
|
|
|
type UniqChecker[T constraints.Ordered] map[T]int64
|
2021-12-28 17:00:52 +03:00
|
|
|
|
|
|
|
// Add adds a value to the validator. v must not be nil.
|
2022-08-03 14:36:18 +03:00
|
|
|
func (uc UniqChecker[T]) Add(elems ...T) {
|
2021-12-28 17:00:52 +03:00
|
|
|
for _, e := range elems {
|
2022-01-19 20:45:50 +03:00
|
|
|
uc[e]++
|
2021-12-28 17:00:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-20 17:19:09 +03:00
|
|
|
// Merge returns a checker containing data from both uc and other.
|
2022-08-03 14:36:18 +03:00
|
|
|
func (uc UniqChecker[T]) Merge(other UniqChecker[T]) (merged UniqChecker[T]) {
|
|
|
|
merged = make(UniqChecker[T], len(uc)+len(other))
|
2022-01-19 20:45:50 +03:00
|
|
|
for elem, num := range uc {
|
2021-12-28 17:00:52 +03:00
|
|
|
merged[elem] += num
|
|
|
|
}
|
|
|
|
|
|
|
|
for elem, num := range other {
|
|
|
|
merged[elem] += num
|
|
|
|
}
|
|
|
|
|
|
|
|
return merged
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate returns an error enumerating all elements that aren't unique.
|
2022-08-03 14:36:18 +03:00
|
|
|
func (uc UniqChecker[T]) Validate() (err error) {
|
|
|
|
var dup []T
|
2022-01-19 20:45:50 +03:00
|
|
|
for elem, num := range uc {
|
2021-12-28 17:00:52 +03:00
|
|
|
if num > 1 {
|
|
|
|
dup = append(dup, elem)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(dup) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-03 14:36:18 +03:00
|
|
|
slices.Sort(dup)
|
2021-12-28 17:00:52 +03:00
|
|
|
|
|
|
|
return fmt.Errorf("duplicated values: %v", dup)
|
|
|
|
}
|