Debian Codegraph @

dstream/gen/segment.tmpl

raw
1 package dstream
2
3 import (
4 "fmt"
5 "time"
6 )
7
8 func (sd *segmentedData) GetPos(j int) interface{} {
9 var x interface{}
10 var stash bool
11 if ilen(sd.stash[j]) > 0 {
12 x = sd.stash[j]
13 stash = true
14 } else {
15 x = sd.bdata[j]
16 }
17 switch x := x.(type) {
18 {{- range . }}
19 case []{{ .Type }}:
20 pos := sd.pos
21 if stash {
22 pos = len(x)
23 }
24 return x[0:pos]
25 {{- end }}
26 default:
27 msg := fmt.Sprintf("Segment: unknown type %T\n", x)
28 panic(msg)
29 }
30 }
31
32
33 // fixstash appends the matching initial segment of bdata to the
34 // end of the stash
35 func (sd *segmentedData) fixstash() bool {
36 pos, fd := sd.findSegmentStash()
37 if pos == 0 {
38 return true
39 }
40 for j := 0; j<sd.source.NumVar(); j++ {
41 x := sd.bdata[j]
42 switch x := x.(type) {
43 {{- range . }}
44 case []{{ .Type }}:
45 z := sd.stash[j].([]{{ .Type }})
46 sd.stash[j] = append(z, x[0:pos]...)
47 sd.bdata[j] = x[pos:len(x)]
48 {{- end }}
49 default:
50 msg := fmt.Sprintf("Segment: unknown type %T\n", x)
51 panic(msg)
52 }
53 }
54 return fd
55 }
56
57
58 // setstash copies bdata into stash, replacing whatever was there.
59 func (sd *segmentedData) setstash() {
60 sd.stash = make([]interface{}, sd.source.NumVar())
61 for j := 0; j<sd.source.NumVar(); j++ {
62 x := sd.bdata[j]
63 switch x := x.(type) {
64 {{- range . }}
65 case []{{ .Type }}:
66 var z []{{ .Type }}
67 if sd.stash[j] != nil {
68 z = sd.stash[j].([]{{ .Type }})
69 }
70 z = resize{{ .Utype }}(z, len(x))
71 copy(z, x)
72 sd.stash[j] = z
73 {{- end }}
74 default:
75 msg := fmt.Sprintf("Segment: unknown type %T\n", x)
76 panic(msg)
77 }
78 }
79 }
80
81 // leftsliceb reslices every element of bdata from position
82 // pos to the end of the slice.
83 func (sd *segmentedData) leftsliceb(pos int) {
84 for j := 0; j < sd.source.NumVar(); j++ {
85 x := sd.bdata[j]
86 switch x := x.(type) {
87 {{- range . }}
88 case []{{ .Type }}:
89 sd.bdata[j] = x[pos:len(x)]
90 {{- end }}
91 default:
92 msg := fmt.Sprintf("Segment: unknown type %T\n", x)
93 panic(msg)
94 }
95 }
96 }
97
98
99 // findSegment finds the next segment boundary after start in the
100 // current backing slice. If there is no boundary, returns -1.
101 func (sd *segmentedData) findSegment(start int) int {
102 pos := -1
103 for _, j := range sd.vpos {
104 x := sd.bdata[j]
105 switch x := x.(type) {
106 {{- range . }}
107 case []{{ .Type }}:
108 for i := start+1; i < len(x); i++ {
109 if pos != -1 && i >= pos {
110 break
111 }
112 if x[i] != x[i-1] {
113 pos = i
114 break
115 }
116 }
117 {{- end }}
118 case nil:
119 return -1
120 default:
121 msg := fmt.Sprintf("Segment: unknown type %T\n", x)
122 panic(msg)
123 }
124 }
125
126 return pos
127 }
128
129
130 // findSegmentStash finds the first segment boundary in bdata, viewing bstash
131 // as a continuation of stash.
132 func (sd *segmentedData) findSegmentStash() (int, bool) {
133 pos := -1
134 var m int
135 for _, j := range sd.vpos {
136 x := sd.bdata[j]
137 switch x := x.(type) {
138 {{- range . }}
139 case []{{ .Type }}:
140 m = len(x)
141 y := sd.stash[j].([]{{ .Type }})
142 v := y[len(y) - 1]
143 for i := 0; i < len(x); i++ {
144 if pos != -1 && i >= pos {
145 break
146 }
147 if x[i] != v {
148 pos = i
149 break
150 }
151 }
152 {{- end }}
153 default:
154 msg := fmt.Sprintf("Segment: unknown type %T", x)
155 panic(msg)
156 }
157 }
158
159 if pos != -1 {
160 return pos, true
161 } else {
162 return m, false
163 }
164 }