mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-24 14:05:45 +03:00
d4c3a43bcb
Merge in DNS/adguard-home from add-dnssvc to master
Squashed commit of the following:
commit 55f4f114bab65a03c0d65383e89020a7356cff32
Merge: 95dc28d9 6e63757f
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date: Mon Aug 15 20:53:07 2022 +0300
Merge branch 'master' into add-dnssvc
commit 95dc28d9d77d06e8ac98c1e6772557bffbf1705b
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date: Mon Aug 15 20:52:50 2022 +0300
all: imp tests, docs
commit 0d9d02950d84afd160b4b1c118da856cee6f12e5
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date: Thu Aug 11 19:27:59 2022 +0300
all: imp docs
commit 8990e038a81da4430468da12fcebedf79fe14df6
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date: Thu Aug 11 19:05:29 2022 +0300
all: imp tests more
commit 92730d93a2a1ac77888c2655508e43efaf0e9fde
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date: Thu Aug 11 18:37:48 2022 +0300
all: imp tests more
commit 8cd45ba30da7ac310e9dc666fb2af438e577b02d
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date: Thu Aug 11 18:11:15 2022 +0300
all: add v1 dnssvc stub; refactor tests
70 lines
1.6 KiB
Go
70 lines
1.6 KiB
Go
// Package aghalg contains common generic algorithms and data structures.
|
|
//
|
|
// TODO(a.garipov): Move parts of this into golibs.
|
|
package aghalg
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"golang.org/x/exp/constraints"
|
|
"golang.org/x/exp/slices"
|
|
)
|
|
|
|
// Coalesce returns the first non-zero value. It is named after the function
|
|
// COALESCE in SQL. If values or all its elements are empty, it returns a zero
|
|
// value.
|
|
func Coalesce[T comparable](values ...T) (res T) {
|
|
var zero T
|
|
for _, v := range values {
|
|
if v != zero {
|
|
return v
|
|
}
|
|
}
|
|
|
|
return zero
|
|
}
|
|
|
|
// 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)
|
|
}
|