| 1 |
package dstream |
| 2 |
|
| 3 |
type concatVertical struct { |
| 4 |
|
| 5 |
|
| 6 |
streams []Dstream |
| 7 |
|
| 8 |
|
| 9 |
pos int |
| 10 |
|
| 11 |
|
| 12 |
nobs int |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
nobsKnown bool |
| 17 |
|
| 18 |
|
| 19 |
namepos map[string]int |
| 20 |
} |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
func ConcatVertical(streams ...Dstream) Dstream { |
| 26 |
|
| 27 |
c := &concatVertical{ |
| 28 |
streams: streams, |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
c.namepos = make(map[string]int) |
| 33 |
for k, n := range streams[0].Names() { |
| 34 |
c.namepos[n] = k |
| 35 |
} |
| 36 |
|
| 37 |
return c |
| 38 |
} |
| 39 |
|
| 40 |
func (c *concatVertical) Close() { |
| 41 |
|
| 42 |
for _, s := range c.streams { |
| 43 |
s.Close() |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
func (c *concatVertical) GetPos(pos int) interface{} { |
| 48 |
return c.streams[c.pos].GetPos(pos) |
| 49 |
} |
| 50 |
|
| 51 |
func (c *concatVertical) NumObs() int { |
| 52 |
if c.nobsKnown { |
| 53 |
return c.nobs |
| 54 |
} |
| 55 |
return -1 |
| 56 |
} |
| 57 |
|
| 58 |
func (c *concatVertical) NumVar() int { |
| 59 |
return len(c.Names()) |
| 60 |
} |
| 61 |
|
| 62 |
func (c *concatVertical) Names() []string { |
| 63 |
return c.streams[0].Names() |
| 64 |
} |
| 65 |
|
| 66 |
func (c *concatVertical) Get(name string) interface{} { |
| 67 |
return c.GetPos(c.namepos[name]) |
| 68 |
} |
| 69 |
|
| 70 |
func (c *concatVertical) Next() bool { |
| 71 |
|
| 72 |
|
| 73 |
if c.streams[c.pos].Next() { |
| 74 |
return true |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
c.nobs += c.streams[c.pos].NumObs() |
| 79 |
c.pos++ |
| 80 |
if c.pos >= len(c.streams) { |
| 81 |
|
| 82 |
c.nobsKnown = true |
| 83 |
return false |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
c.streams[c.pos].Next() |
| 88 |
|
| 89 |
|
| 90 |
a := c.streams[0].Names() |
| 91 |
b := c.streams[c.pos].Names() |
| 92 |
msg := "Streams to be concatenated have different column names" |
| 93 |
if len(a) != len(b) { |
| 94 |
panic(msg) |
| 95 |
} |
| 96 |
for i := range a { |
| 97 |
if a[i] != b[i] { |
| 98 |
panic(msg) |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
return true |
| 103 |
} |
| 104 |
|
| 105 |
func (c *concatVertical) Reset() { |
| 106 |
|
| 107 |
c.pos = 0 |
| 108 |
c.nobs = 0 |
| 109 |
c.nobsKnown = false |
| 110 |
|
| 111 |
for _, s := range c.streams { |
| 112 |
s.Reset() |
| 113 |
} |
| 114 |
} |