-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
53 lines (40 loc) · 1.31 KB
/
utils.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
package hll
import "fmt"
// Rollup merges sketches into a single (new) Sketch that is slightly more efficient than
// successively merging each into a common base, one at a time.
func Rollup(sketches []Sketch) (Sketch, error) {
if len(sketches) <= 0 || sketches == nil {
return nil, fmt.Errorf("rollup requires a list of sketches")
}
// Validate version and len(registers).
firstVersion := sketches[0].getVersion()
firstLen := len(sketches[0].getRegisters())
for i := 1; i < len(sketches); i++ {
if sketches[i].getVersion() != firstVersion {
return nil, fmt.Errorf("rollup requires a list of sketches with the same version")
}
if len(sketches[i].getRegisters()) != firstLen {
return nil, fmt.Errorf("rollup requires a list of sketches with the same precision (len of registers)")
}
}
base := createSketch()
if base.version != firstVersion {
base.version = firstVersion
}
// Pull the registers for sketches.
registers := make([][]uint8, len(sketches))
for i, sk := range sketches {
registers[i] = sk.getRegisters()
}
// For each register, take the highest entry for this i across each other sketch.
for i := 0; i < len(base.registers); i++ {
max := uint8(0)
for _, reg := range registers {
if max < reg[i] {
max = reg[i]
}
}
base.registers[i] = max
}
return base, nil
}