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
|
package dstream
import "fmt"
type selectCols struct {
xform
keepVars []string
keepPos []int
}
// SelectCols retains only the given variables in a Dstream.
func SelectCols(data Dstream, keepvars ...string) Dstream {
sl := &selectCols{
xform: xform{
source: data,
},
keepVars: keepvars,
}
vn := make(map[string]int)
for k, na := range data.Names() {
vn[na] = k
}
for _, na := range keepvars {
pos, ok := vn[na]
if !ok {
panic(fmt.Sprintf("Variable '%s' not found in dstream.", na))
}
sl.keepPos = append(sl.keepPos, pos)
}
sl.names = keepvars
sl.bdata = make([]interface{}, len(sl.keepVars))
return sl
}
func (sl *selectCols) Next() bool {
if !sl.source.Next() {
return false
}
for j, k := range sl.keepPos {
sl.bdata[j] = sl.source.GetPos(k)
}
return true
}
|