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