Debian Codegraph @

dstream/concatvertical.go

raw symbols SCIP
1 package dstream
2
3 type concatVertical struct {
4
5 // The streams to be concatenated
6 streams []Dstream
7
8 // The index of the current stream within streams
9 pos int
10
11 // The number of observations in the concatenated stream
12 nobs int
13
14 // True if nobs is known yet (nobs is not known until reading
15 // the entire concatenated stream).
16 nobsKnown bool
17
18 // Map from variable names to column positions
19 namepos map[string]int
20 }
21
22 // ConcatVertical concatenates a collection of Dstreams vertically
23 // (appending additional observations). The column names and data
24 // types of all the Dstreams being combined must be identical.
25 func ConcatVertical(streams ...Dstream) Dstream {
26
27 c := &concatVertical{
28 streams: streams,
29 }
30
31 // Construct the name to position mapping
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 // Advance within current stream
73 if c.streams[c.pos].Next() {
74 return true
75 }
76
77 // Try to advance to next stream
78 c.nobs += c.streams[c.pos].NumObs()
79 c.pos++
80 if c.pos >= len(c.streams) {
81 // Done with all streams
82 c.nobsKnown = true
83 return false
84 }
85
86 // Advance to next stream
87 c.streams[c.pos].Next()
88
89 // Check that the names are the same
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 }