| 1 |
package dstream |
| 2 |
|
| 3 |
import "fmt" |
| 4 |
|
| 5 |
type concatHorizontal struct { |
| 6 |
|
| 7 |
|
| 8 |
streams []Dstream |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
names []string |
| 13 |
|
| 14 |
|
| 15 |
pos int |
| 16 |
|
| 17 |
|
| 18 |
nobs int |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
nobsKnown bool |
| 23 |
|
| 24 |
|
| 25 |
namestream map[string]int |
| 26 |
|
| 27 |
|
| 28 |
namepos map[string]int |
| 29 |
|
| 30 |
|
| 31 |
posstream map[int]int |
| 32 |
|
| 33 |
|
| 34 |
pospos map[int]int |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
func ConcatHorizontal(streams ...Dstream) Dstream { |
| 41 |
|
| 42 |
c := &concatHorizontal{ |
| 43 |
streams: streams, |
| 44 |
} |
| 45 |
|
| 46 |
haveName := make(map[string]bool) |
| 47 |
|
| 48 |
|
| 49 |
c.namepos = make(map[string]int) |
| 50 |
c.namestream = make(map[string]int) |
| 51 |
c.posstream = make(map[int]int) |
| 52 |
c.pospos = make(map[int]int) |
| 53 |
pos := 0 |
| 54 |
for j, stream := range streams { |
| 55 |
for k, na := range stream.Names() { |
| 56 |
|
| 57 |
if haveName[na] { |
| 58 |
msg := fmt.Sprintf("Name '%s' is not unique\n", na) |
| 59 |
panic(msg) |
| 60 |
} |
| 61 |
haveName[na] = true |
| 62 |
|
| 63 |
c.namestream[na] = j |
| 64 |
c.namepos[na] = k |
| 65 |
c.posstream[pos] = j |
| 66 |
c.pospos[pos] = k |
| 67 |
c.names = append(c.names, na) |
| 68 |
pos++ |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
return c |
| 73 |
} |
| 74 |
|
| 75 |
func (c *concatHorizontal) Close() { |
| 76 |
|
| 77 |
for _, s := range c.streams { |
| 78 |
s.Close() |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
func (c *concatHorizontal) GetPos(pos int) interface{} { |
| 83 |
return c.streams[c.posstream[pos]].GetPos(c.pospos[pos]) |
| 84 |
} |
| 85 |
|
| 86 |
func (c *concatHorizontal) NumObs() int { |
| 87 |
if c.nobsKnown { |
| 88 |
return c.nobs |
| 89 |
} |
| 90 |
return -1 |
| 91 |
} |
| 92 |
|
| 93 |
func (c *concatHorizontal) NumVar() int { |
| 94 |
return len(c.Names()) |
| 95 |
} |
| 96 |
|
| 97 |
func (c *concatHorizontal) Names() []string { |
| 98 |
return c.names |
| 99 |
} |
| 100 |
|
| 101 |
func (c *concatHorizontal) Get(name string) interface{} { |
| 102 |
return c.streams[c.namestream[name]].GetPos(c.namepos[name]) |
| 103 |
} |
| 104 |
|
| 105 |
func (c *concatHorizontal) Next() bool { |
| 106 |
|
| 107 |
|
| 108 |
var n1, n0 int |
| 109 |
for _, s := range c.streams { |
| 110 |
if s.Next() { |
| 111 |
n1++ |
| 112 |
} else { |
| 113 |
n0++ |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
if n0 == 0 { |
| 118 |
|
| 119 |
return true |
| 120 |
} |
| 121 |
|
| 122 |
if n0 > 0 && n1 > 0 { |
| 123 |
panic("Streams have different lengths\n") |
| 124 |
|
| 125 |
} |
| 126 |
|
| 127 |
return false |
| 128 |
} |
| 129 |
|
| 130 |
func (c *concatHorizontal) Reset() { |
| 131 |
|
| 132 |
c.pos = 0 |
| 133 |
c.nobs = 0 |
| 134 |
c.nobsKnown = false |
| 135 |
|
| 136 |
for _, s := range c.streams { |
| 137 |
s.Reset() |
| 138 |
} |
| 139 |
} |