mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-12-15 03:02:07 +03:00
ee619b2dbd
Squashed commit of the following: commit a1bd3c249be043108c84a902d2e88bf80946d444 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Thu Apr 4 14:59:37 2024 +0300 all: upd more commit 9e55bbb02c2af2064aa2a2ca7b49fd28b544a02c Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Thu Apr 4 14:12:45 2024 +0300 all: upd go code
69 lines
1.5 KiB
Go
69 lines
1.5 KiB
Go
// Package aghalg contains common generic algorithms and data structures.
|
|
//
|
|
// TODO(a.garipov): Move parts of this into golibs.
|
|
package aghalg
|
|
|
|
import (
|
|
"fmt"
|
|
"slices"
|
|
|
|
"golang.org/x/exp/constraints"
|
|
)
|
|
|
|
// CoalesceSlice returns the first non-zero value. It is named after function
|
|
// COALESCE in SQL. If values or all its elements are empty, it returns nil.
|
|
func CoalesceSlice[E any, S []E](values ...S) (res S) {
|
|
for _, v := range values {
|
|
if v != nil {
|
|
return v
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// UniqChecker allows validating uniqueness of comparable items.
|
|
//
|
|
// 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
|
|
|
|
// Add adds a value to the validator. v must not be nil.
|
|
func (uc UniqChecker[T]) Add(elems ...T) {
|
|
for _, e := range elems {
|
|
uc[e]++
|
|
}
|
|
}
|
|
|
|
// Merge returns a checker containing data from both uc and other.
|
|
func (uc UniqChecker[T]) Merge(other UniqChecker[T]) (merged UniqChecker[T]) {
|
|
merged = make(UniqChecker[T], len(uc)+len(other))
|
|
for elem, num := range uc {
|
|
merged[elem] += num
|
|
}
|
|
|
|
for elem, num := range other {
|
|
merged[elem] += num
|
|
}
|
|
|
|
return merged
|
|
}
|
|
|
|
// Validate returns an error enumerating all elements that aren't unique.
|
|
func (uc UniqChecker[T]) Validate() (err error) {
|
|
var dup []T
|
|
for elem, num := range uc {
|
|
if num > 1 {
|
|
dup = append(dup, elem)
|
|
}
|
|
}
|
|
|
|
if len(dup) == 0 {
|
|
return nil
|
|
}
|
|
|
|
slices.Sort(dup)
|
|
|
|
return fmt.Errorf("duplicated values: %v", dup)
|
|
}
|