Debian Codegraph @

dstream/regroup.go

raw symbols SCIP
1 package dstream
2
3 import (
4 "fmt"
5 "sort"
6 )
7
8 type argsort struct {
9 s []uint64
10 inds []int
11 }
12
13 func (a argsort) Len() int {
14 return len(a.s)
15 }
16
17 func (a argsort) Less(i, j int) bool {
18 return a.s[i] < a.s[j]
19 }
20
21 func (a argsort) Swap(i, j int) {
22 a.s[i], a.s[j] = a.s[j], a.s[i]
23 a.inds[i], a.inds[j] = a.inds[j], a.inds[i]
24 }
25
26 // Regroup creates a new Dstream from the provided Dstream having
27 // identical rows, but with the chunks defined by the values of a
28 // provided id variable. The resulting Dstream will have a chunk for
29 // each distinct level of the id variable, containing all the rows of
30 // the input Dstream with the given id value. The id variable must
31 // have uint64 type.
32 func Regroup(ds Dstream, groupvar string, sortchunks bool) Dstream {
33
34 // Find the variable's position
35 idpos := -1
36 for j, n := range ds.Names() {
37 if n == groupvar {
38 idpos = j
39 break
40 }
41 }
42 if idpos == -1 {
43 msg := fmt.Sprintf("Regroup: variable '%s' not found", groupvar)
44 panic(msg)
45 }
46
47 r := doRegroup(ds, idpos)
48
49 if sortchunks {
50 var x []uint64
51 for _, v := range r.data[idpos] {
52 y := v.([]uint64)
53 x = append(x, y[0])
54 }
55 ii := make([]int, len(x))
56 for i := range ii {
57 ii[i] = i
58 }
59 a := argsort{x, ii}
60 sort.Sort(a)
61
62 nchunk := len(r.data[0])
63 newar := make([][]interface{}, len(r.data))
64 for j := 0; j < ds.NumVar(); j++ {
65 newar[j] = make([]interface{}, nchunk)
66 for k, i := range ii {
67 newar[j][k] = r.data[j][i]
68 }
69 }
70 r.data = newar
71 }
72
73 return r
74 }