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
|
package dstream
// FilterFunc is a filtering function for use with Filter. The first
// argument holds a map from variable names to data slices. The second
// argument is a boolean slice that is initialized to all 'true'. The
// FilterFunc should set elements of the boolean slice to false wherever
// the corresponding dstream record should be excluded.
type FilterFunc func(map[string]interface{}, []bool)
type filterCol struct {
xform
filter FilterFunc
keep []bool
keeppos []int
nobs int
nobsKnown bool
}
// Filter selects rows from the dstream. A filtering function determines
// which rows are selected.
func Filter(data Dstream, f FilterFunc) Dstream {
fc := &filterCol{
xform: xform{
source: data,
},
filter: f,
}
fc.init()
return fc
}
func (fc *filterCol) init() {
fc.names = fc.source.Names()
// We need to know this early so call explicitly
fc.setNamePos()
fc.bdata = make([]interface{}, len(fc.names))
fc.nobsKnown = false
fc.nobs = 0
}
func (fc *filterCol) Reset() {
fc.source.Reset()
fc.nobs = 0
fc.nobsKnown = false
fc.init()
}
func (fc *filterCol) NumObs() int {
if fc.nobsKnown {
return fc.nobs
}
return -1
}
|