File: drop.go

package info (click to toggle)
golang-github-kshedden-dstream 0.0~git20190512.c4c4106-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 596 kB
  • sloc: makefile: 30
file content (61 lines) | stat: -rw-r--r-- 983 bytes parent folder | download
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
package dstream

import "fmt"

type drop struct {
	xform

	dropVars []string
	keepPos  []int
}

func (d *drop) init() {

	// Check that all variables to drop exist.
	hna := make(map[string]bool)
	for _, v := range d.source.Names() {
		hna[v] = true
	}
	dmp := make(map[string]bool)
	for _, na := range d.dropVars {
		if !hna[na] {
			msg := fmt.Sprintf("Drop: variable '%s' not found.\n", na)
			panic(msg)
		}
		dmp[na] = true
	}

	for k, na := range d.source.Names() {
		if !dmp[na] {
			d.keepPos = append(d.keepPos, k)
			d.names = append(d.names, na)
		}
	}

	d.bdata = make([]interface{}, len(d.keepPos))
}

// DropCols removes the given variables from a Dstream.
func DropCols(data Dstream, dropvars ...string) Dstream {
	d := &drop{
		xform: xform{
			source: data,
		},
		dropVars: dropvars,
	}
	d.init()
	return d
}

func (d *drop) Next() bool {

	if !d.source.Next() {
		return false
	}

	for j, k := range d.keepPos {
		d.bdata[j] = d.source.GetPos(k)
	}

	return true
}