mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-26 23:28:08 +03:00
3c0d2a9253
Updates #4890.
Squashed commit of the following:
commit 20c8f3348125672403c3968b8e08b15eba69347d
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date: Tue Sep 6 16:55:11 2022 +0300
dnsforward: imp names
commit 2c21644623c321df46a5c386ec00ca532b7603b6
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date: Tue Sep 6 16:36:46 2022 +0300
dnsforward: imp validations; refactor more
commit 221e8c5ebbd0b64e5c554cddb683d116212e5901
Merge: e5f5b76e 58512c3a
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date: Tue Sep 6 14:57:31 2022 +0300
Merge branch 'master' into 4890-panic-internal-proxy
commit e5f5b76e3e2b43656af9939a52a9e46e5d9b5a40
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date: Tue Sep 6 14:51:48 2022 +0300
dnsforward: fix panic; refactor
89 lines
2.1 KiB
Go
89 lines
2.1 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 function
|
|
// COALESCE in SQL. If values or all its elements are empty, it returns a zero
|
|
// value.
|
|
//
|
|
// T is comparable, because Go currently doesn't have a comparableWithZeroValue
|
|
// constraint.
|
|
//
|
|
// TODO(a.garipov): Think of ways to merge with [CoalesceSlice].
|
|
func Coalesce[T comparable](values ...T) (res T) {
|
|
var zero T
|
|
for _, v := range values {
|
|
if v != zero {
|
|
return v
|
|
}
|
|
}
|
|
|
|
return zero
|
|
}
|
|
|
|
// 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.
|
|
//
|
|
// TODO(a.garipov): Think of ways to merge with [Coalesce].
|
|
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)
|
|
}
|