Debian Codegraph @

dstream/replace.go

raw symbols SCIP
1 package dstream
2
3 import "fmt"
4
5 type replaceColumn struct {
6
7 // The data stream in which we are replacing a column
8 source Dstream
9
10 // Name of the column that we are replacing
11 name string
12
13 // Column position of the column that we are replacing
14 colpos int
15
16 // First row index of the current chunk
17 rowpos int
18
19 // Size of the current chunk.
20 csize int
21
22 // The data being used to replace the column
23 coldata interface{}
24
25 // Map from column names to position
26 vpos map[string]int
27 }
28
29 // ReplaceColumn returns a new Dstream in which the column with the given
30 // name is replaced with the given data. The col value must be an
31 // array type of a valid primitive type (e.g. int, float64, string),
32 // and must have length equal to the number of rows of data.
33 func ReplaceColumn(data Dstream, name string, coldata interface{}) Dstream {
34
35 vpos := make(map[string]int)
36 for q, na := range data.Names() {
37 vpos[na] = q
38 }
39
40 colpos := -1
41 for j, na := range data.Names() {
42 if na == name {
43 colpos = j
44 break
45 }
46 }
47 if colpos == -1 {
48 msg := fmt.Sprintf("Replace: column '%s' not found.\n", name)
49 panic(msg)
50 }
51
52 r := &replaceColumn{
53 source: data,
54 name: name,
55 colpos: colpos,
56 coldata: coldata,
57 vpos: vpos,
58 }
59
60 return r
61 }
62
63 func (r *replaceColumn) Next() bool {
64
65 if !r.source.Next() {
66 return false
67 }
68
69 r.rowpos += r.csize
70 r.csize = ilen(r.source.GetPos(0))
71
72 return true
73 }
74
75 func (r *replaceColumn) Names() []string {
76 return r.source.Names()
77 }
78
79 func (r *replaceColumn) NumVar() int {
80 return r.source.NumVar()
81 }
82
83 func (r *replaceColumn) NumObs() int {
84 return r.source.NumObs()
85 }
86
87 func (r *replaceColumn) Reset() {
88 r.rowpos = 0
89 r.csize = 0
90 r.source.Reset()
91 }
92
93 func (r *replaceColumn) Close() {
94 r.source.Close()
95 }
96
97 func (r *replaceColumn) Get(name string) interface{} {
98
99 j, ok := r.vpos[name]
100 if !ok {
101 msg := fmt.Sprintf("Replace: variable '%s' not found.\n", name)
102 panic(msg)
103 }
104
105 return r.GetPos(j)
106 }