Debian Codegraph @

dstream/xform.go

raw symbols SCIP
1 package dstream
2
3 import "fmt"
4
5 // xform can be embedded into any transformer object that converts a
6 // Dstream into another Dstream. It implements the basic behavior
7 // that is common to all transformers. While xform implements the
8 // Dstream interface in a trivial way, Types that embed xform can
9 // provide their own implementation of any of these methods.
10 //
11 // In most cases, it will be sufficient to (i) set names properly, and
12 // (ii) implement Next to set bdata correctly (bdata holds the data
13 // slices by column). In some cases it may be desirable to implement
14 // other Dstream methods. Note that due to the way that Go embedding
15 // works, if a type implements its own, say, Names function, all the
16 // code below will continue to call the xform Names implementation,
17 // not the customized Names implementation. For this reason, it is
18 // usually better to modify the bdata and names members rather than to
19 // provide new implementations of Get, etc. If Get and other Dstream
20 // methods need to be implemented by a type, it may be necessary to
21 // implement all Dstream interface components.
22 type xform struct {
23
24 // The data to be transformed.
25 source Dstream
26
27 // The transformed data, held as references or copies (if
28 // needed) of the source data.
29 bdata []interface{}
30
31 // List of variable names, used if names for the xform are
32 // different from the names of its source, otherwise use
33 // source.Names().
34 names []string
35
36 // Map from variable name to column number
37 namepos map[string]int
38 }
39
40 func (x *xform) Close() {
41 }
42
43 // setNamePos constructs a map from variable names to their column
44 // positions.
45 func (x *xform) setNamePos() {
46 x.namepos = make(map[string]int)
47 for k, n := range x.Names() {
48 x.namepos[n] = k
49 }
50 }
51
52 func (x *xform) Reset() {
53 x.source.Reset()
54 if x.bdata != nil {
55 truncate(x.bdata)
56 }
57 }
58
59 func (x *xform) Next() bool {
60 return x.source.Next()
61 }
62
63 func (x *xform) NumVar() int {
64 return len(x.Names())
65 }
66
67 func (x *xform) NumObs() int {
68 return x.source.NumObs()
69 }
70
71 func (x *xform) Names() []string {
72 // If there is a names, then the xform has modified the names
73 // so we need to return the modified names, otherwise pass
74 // through to the source.
75 if x.names != nil {
76 return x.names
77 }
78 return x.source.Names()
79 }
80
81 func (x *xform) GetPos(j int) interface{} {
82 // If there is a bdata, then the xform has modified the source
83 // data so we need to return the modified data, otherwise pass
84 // through to the source.
85 if x.bdata != nil {
86 return x.bdata[j]
87 }
88 return x.source.GetPos(j)
89 }
90
91 func (x *xform) Get(na string) interface{} {
92
93 if x.namepos == nil {
94 x.setNamePos()
95 }
96
97 pos, ok := x.namepos[na]
98 if !ok {
99 msg := fmt.Sprintf("Variable '%s' not found", na)
100 panic(msg)
101 }
102 return x.GetPos(pos)
103 }