-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalues.go
More file actions
60 lines (49 loc) · 1.52 KB
/
Copy pathvalues.go
File metadata and controls
60 lines (49 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// SPDX-FileCopyrightText: 2026 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package hashy
import (
"iter"
"slices"
)
// Values is a slice of sortable, dedupable values.
type Values[V comparable] []V
// Add appends more values. After adding several values,
// use Normalize to dedupe and sort this collection again.
func (vs *Values[V]) Add(more ...V) {
*vs = slices.Grow(*vs, len(more))
*vs = append(*vs, more...)
}
// Append is like Add, but is appends to this Deduper and returns
// the appended Deduper rather than adding in-place.
func (vs Values[V]) Append(more ...V) Values[V] {
vs = slices.Grow(vs, len(more))
vs = append(vs, more...)
return vs
}
// Dedupe removes any duplicates from this set. After this method is
// called, the order of elements may have shifted.
func (vs *Values[V]) Dedupe() {
if len(*vs) < 2 {
return
}
var deduper Deduper[V]
deduper.Add((*vs)...)
if deduper.Len() < vs.Len() {
// zero out the elements we no longer need, but retain the
// slice storage for future use.
*vs = slices.Delete(*vs, deduper.Len(), vs.Len())
*vs = deduper.AppendTo((*vs)[:])
}
}
// SortFunc in-place sorts this set using the supplied comparison function.
func (vs Values[V]) SortFunc(cmp func(V, V) int) {
slices.SortFunc(vs, cmp)
}
// Len returns the count of elements in this set.
func (vs Values[V]) Len() int {
return len(vs)
}
// All returns a sequence of this set's values in the order they appear.
func (vs Values[V]) All() iter.Seq[V] {
return slices.Values(vs)
}