forked from AdguardTeam/AdGuardHome
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
165 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Package aghalgo contains common generic algorithms and data structures. | ||
// | ||
// TODO(a.garipov): Update to use type parameters in Go 1.18. | ||
package aghalgo | ||
|
||
import ( | ||
"fmt" | ||
"sort" | ||
) | ||
|
||
// any is a convenient alias for interface{} | ||
// | ||
// TODO(a.garipov): Remove in Go 1.18. | ||
type any = interface{} | ||
|
||
// UniquenessValidator allows validating uniqueness of comparable items. | ||
type UniquenessValidator map[any]int64 | ||
|
||
// Add adds a value to the validator. v must not be nil. | ||
func (v UniquenessValidator) Add(elems ...any) { | ||
for _, e := range elems { | ||
v[e]++ | ||
} | ||
} | ||
|
||
// Merge returns a validator containing data from both v and other. | ||
func (v UniquenessValidator) Merge(other UniquenessValidator) (merged UniquenessValidator) { | ||
merged = make(UniquenessValidator, len(v)+len(other)) | ||
for elem, num := range v { | ||
merged[elem] += num | ||
} | ||
|
||
for elem, num := range other { | ||
merged[elem] += num | ||
} | ||
|
||
return merged | ||
} | ||
|
||
// Validate returns an error enumerating all elements that aren't unique. | ||
// isBefore is an optional sorting function to make the error message | ||
// deterministic. | ||
func (v UniquenessValidator) Validate(isBefore func(a, b any) (less bool)) (err error) { | ||
var dup []any | ||
for elem, num := range v { | ||
if num > 1 { | ||
dup = append(dup, elem) | ||
} | ||
} | ||
|
||
if len(dup) == 0 { | ||
return nil | ||
} | ||
|
||
if isBefore != nil { | ||
sort.Slice(dup, func(i, j int) (less bool) { | ||
return isBefore(dup[i], dup[j]) | ||
}) | ||
} | ||
|
||
return fmt.Errorf("duplicated values: %v", dup) | ||
} | ||
|
||
// IntIsBefore is a helper sort function for UniquenessValidator.Validate. | ||
// a and b must be of type int. | ||
func IntIsBefore(a, b any) (less bool) { | ||
return a.(int) < b.(int) | ||
} | ||
|
||
// StringIsBefore is a helper sort function for UniquenessValidator.Validate. | ||
// a and b must be of type string. | ||
func StringIsBefore(a, b any) (less bool) { | ||
return a.(string) < b.(string) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.