| 1 |
package dstream |
| 2 |
|
| 3 |
import "fmt" |
| 4 |
|
| 5 |
type addcol struct { |
| 6 |
xform |
| 7 |
newname string |
| 8 |
pos int |
| 9 |
newdat []float64 |
| 10 |
first bool |
| 11 |
} |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
func AddCol(da Dstream, newdat []float64, newname string) Dstream { |
| 18 |
|
| 19 |
r := &addcol{ |
| 20 |
xform: xform{ |
| 21 |
source: da, |
| 22 |
}, |
| 23 |
newname: newname, |
| 24 |
newdat: newdat, |
| 25 |
} |
| 26 |
|
| 27 |
for _, na := range r.source.Names() { |
| 28 |
if newname == na { |
| 29 |
msg := fmt.Sprintf("AddCol: a variable named '%s' already exists.", na) |
| 30 |
panic(msg) |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
r.names = append(r.names, r.source.Names()...) |
| 35 |
r.names = append(r.names, r.newname) |
| 36 |
r.first = true |
| 37 |
r.bdata = make([]interface{}, len(r.names)) |
| 38 |
|
| 39 |
return r |
| 40 |
} |
| 41 |
|
| 42 |
func (ac *addcol) Reset() { |
| 43 |
ac.pos = 0 |
| 44 |
ac.first = true |
| 45 |
ac.source.Reset() |
| 46 |
} |
| 47 |
|
| 48 |
func (ac *addcol) Next() bool { |
| 49 |
|
| 50 |
if !ac.first { |
| 51 |
ac.pos += ilen(ac.bdata[0]) |
| 52 |
} |
| 53 |
ac.first = false |
| 54 |
f := ac.source.Next() |
| 55 |
if !f { |
| 56 |
return false |
| 57 |
} |
| 58 |
for k := 0; k < ac.source.NumVar(); k++ { |
| 59 |
ac.bdata[k] = ac.source.GetPos(k) |
| 60 |
} |
| 61 |
csize := ilen(ac.bdata[0]) |
| 62 |
|
| 63 |
ac.bdata[len(ac.bdata)-1] = ac.newdat[ac.pos : ac.pos+csize] |
| 64 |
|
| 65 |
return true |
| 66 |
} |