-
Notifications
You must be signed in to change notification settings - Fork 0
/
iter.go
80 lines (61 loc) · 1.46 KB
/
iter.go
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package kredis
import (
"github.com/redis/go-redis/v9"
)
type iterator[T KredisTyped] interface {
values() []interface{}
valuesWithScoring(scoreable, bool) []redis.Z
unique() []interface{}
uniqueMap() map[T]struct{}
}
type iter[T KredisTyped] struct {
elements []T // rename "elements" to "items" (to be less list-y)
}
func newIter[T KredisTyped](elements []T) iterator[T] {
return &iter[T]{elements}
}
func (i *iter[T]) values() []interface{} {
values := make([]interface{}, len(i.elements))
for idx, e := range i.elements {
values[idx] = typeToInterface(e)
}
return values
}
type scoreable interface {
prependScore(int) float64
appendScore(int) float64
}
func (i *iter[T]) valuesWithScoring(scorer scoreable, prepended bool) []redis.Z {
values := make([]redis.Z, len(i.elements))
for idx, e := range i.elements {
if prepended {
values[idx].Score = scorer.prependScore(idx)
} else {
values[idx].Score = scorer.appendScore(idx)
}
values[idx].Member = typeToInterface(e)
}
return values
}
func (i *iter[T]) unique() []interface{} {
m := make(map[T]struct{})
values := make([]interface{}, 0)
for _, e := range i.elements {
if _, exists := m[e]; exists {
continue
}
m[e] = struct{}{}
values = append(values, typeToInterface(e))
}
return values
}
func (i *iter[T]) uniqueMap() (m map[T]struct{}) {
if len(i.elements) == 0 {
return
}
m = make(map[T]struct{})
for _, e := range i.elements {
m[e] = struct{}{}
}
return
}