Debian Codegraph @

dstream/select_cols.go

raw symbols SCIP
1 package dstream
2
3 import "fmt"
4
5 type selectCols struct {
6 xform
7
8 keepVars []string
9 keepPos []int
10 }
11
12 // SelectCols retains only the given variables in a Dstream.
13 func SelectCols(data Dstream, keepvars ...string) Dstream {
14 sl := &selectCols{
15 xform: xform{
16 source: data,
17 },
18 keepVars: keepvars,
19 }
20
21 vn := make(map[string]int)
22 for k, na := range data.Names() {
23 vn[na] = k
24 }
25
26 for _, na := range keepvars {
27 pos, ok := vn[na]
28 if !ok {
29 panic(fmt.Sprintf("Variable '%s' not found in dstream.", na))
30 }
31 sl.keepPos = append(sl.keepPos, pos)
32 }
33
34 sl.names = keepvars
35 sl.bdata = make([]interface{}, len(sl.keepVars))
36
37 return sl
38 }
39
40 func (sl *selectCols) Next() bool {
41
42 if !sl.source.Next() {
43 return false
44 }
45
46 for j, k := range sl.keepPos {
47 sl.bdata[j] = sl.source.GetPos(k)
48 }
49
50 return true
51 }