/
rleaf.go
339 lines (305 loc) · 7.99 KB
/
rleaf.go
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// Copyright ©2020 The go-hep Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package rtree
import (
"fmt"
"reflect"
"go-hep.org/x/hep/groot/rbytes"
"go-hep.org/x/hep/groot/rdict"
)
// rleafCtx is the interface that wraps the rcount method.
type rleafCtx interface {
// rcountFunc returns the function that gives the leaf-count
// of the provided leaf.
rcountFunc(leaf string) func() int
rcountLeaf(leaf string) leafCount
}
// rleaf is the leaf reading interface.
type rleaf interface {
Leaf() Leaf
Offset() int64
readFromBuffer(*rbytes.RBuffer) error
}
// rleafDefaultSliceCap is the default capacity for all
// rleaves that hold slices of data.
const rleafDefaultSliceCap = 8
func rleafFrom(leaf Leaf, rvar ReadVar, rctx rleafCtx) rleaf {
switch leaf := leaf.(type) {
case *LeafO:
return newRLeafBool(leaf, rvar, rctx)
case *LeafB:
switch rv := reflect.ValueOf(rvar.Value); rv.Interface().(type) {
case *int8, *[]int8:
return newRLeafI8(leaf, rvar, rctx)
case *uint8, *[]uint8:
return newRLeafU8(leaf, rvar, rctx)
default:
rv := rv.Elem()
switch rv.Kind() {
case reflect.Array:
rt, _ := flattenArrayType(rv.Type())
switch rt.Kind() {
case reflect.Int8:
return newRLeafI8(leaf, rvar, rctx)
case reflect.Uint8:
return newRLeafU8(leaf, rvar, rctx)
}
case reflect.Slice:
rt, _ := flattenArrayType(rv.Type().Elem())
switch rt.Kind() {
case reflect.Int8:
return newRLeafI8(leaf, rvar, rctx)
case reflect.Uint8:
return newRLeafU8(leaf, rvar, rctx)
}
}
}
panic(fmt.Errorf("rvar mismatch for %T", leaf))
case *LeafS:
switch rv := reflect.ValueOf(rvar.Value); rv.Interface().(type) {
case *int16, *[]int16:
return newRLeafI16(leaf, rvar, rctx)
case *uint16, *[]uint16:
return newRLeafU16(leaf, rvar, rctx)
default:
rv := rv.Elem()
switch rv.Kind() {
case reflect.Array:
rt, _ := flattenArrayType(rv.Type())
switch rt.Kind() {
case reflect.Int16:
return newRLeafI16(leaf, rvar, rctx)
case reflect.Uint16:
return newRLeafU16(leaf, rvar, rctx)
}
case reflect.Slice:
rt, _ := flattenArrayType(rv.Type().Elem())
switch rt.Kind() {
case reflect.Int16:
return newRLeafI16(leaf, rvar, rctx)
case reflect.Uint16:
return newRLeafU16(leaf, rvar, rctx)
}
}
}
panic(fmt.Errorf("rvar mismatch for %T", leaf))
case *LeafI:
switch rv := reflect.ValueOf(rvar.Value); rv.Interface().(type) {
case *int32, *[]int32:
return newRLeafI32(leaf, rvar, rctx)
case *uint32, *[]uint32:
return newRLeafU32(leaf, rvar, rctx)
default:
rv := rv.Elem()
switch rv.Kind() {
case reflect.Array:
rt, _ := flattenArrayType(rv.Type())
switch rt.Kind() {
case reflect.Int32:
return newRLeafI32(leaf, rvar, rctx)
case reflect.Uint32:
return newRLeafU32(leaf, rvar, rctx)
}
case reflect.Slice:
rt, _ := flattenArrayType(rv.Type().Elem())
switch rt.Kind() {
case reflect.Int32:
return newRLeafI32(leaf, rvar, rctx)
case reflect.Uint32:
return newRLeafU32(leaf, rvar, rctx)
}
}
}
panic(fmt.Errorf("rvar mismatch for %T", leaf))
case *LeafL:
switch rv := reflect.ValueOf(rvar.Value); rv.Interface().(type) {
case *int64, *[]int64:
return newRLeafI64(leaf, rvar, rctx)
case *uint64, *[]uint64:
return newRLeafU64(leaf, rvar, rctx)
default:
rv := rv.Elem()
switch rv.Kind() {
case reflect.Array:
rt, _ := flattenArrayType(rv.Type())
switch rt.Kind() {
case reflect.Int64:
return newRLeafI64(leaf, rvar, rctx)
case reflect.Uint64:
return newRLeafU64(leaf, rvar, rctx)
}
case reflect.Slice:
rt, _ := flattenArrayType(rv.Type().Elem())
switch rt.Kind() {
case reflect.Int64:
return newRLeafI64(leaf, rvar, rctx)
case reflect.Uint64:
return newRLeafU64(leaf, rvar, rctx)
}
}
panic(fmt.Errorf("rvar mismatch for %T", leaf))
}
case *LeafF:
return newRLeafF32(leaf, rvar, rctx)
case *LeafD:
return newRLeafF64(leaf, rvar, rctx)
case *LeafF16:
return newRLeafF16(leaf, rvar, rctx)
case *LeafD32:
return newRLeafD32(leaf, rvar, rctx)
case *LeafC:
return newRLeafStr(leaf, rvar, rctx)
case *tleafElement:
return newRLeafElem(leaf, rvar, rctx)
case *tleafObject:
return newRLeafObject(leaf, rvar, rctx)
default:
panic(fmt.Errorf("not implemented %T", leaf))
}
}
type rleafObject struct {
base *tleafObject
v rbytes.Unmarshaler
}
var (
_ rleaf = (*rleafObject)(nil)
)
func newRLeafObject(leaf *tleafObject, rvar ReadVar, rctx rleafCtx) rleaf {
switch {
case leaf.count != nil:
panic("not implemented")
case leaf.len > 1:
panic("not implemented")
default:
return &rleafObject{
base: leaf,
v: reflect.ValueOf(rvar.Value).Interface().(rbytes.Unmarshaler),
}
}
}
func (leaf *rleafObject) Leaf() Leaf { return leaf.base }
func (leaf *rleafObject) Offset() int64 {
return int64(leaf.base.Offset())
}
func (leaf *rleafObject) readFromBuffer(r *rbytes.RBuffer) error {
if leaf.base.virtual {
var (
n = int(r.ReadU8())
class = r.ReadCString(n + 1)
)
if class != leaf.base.Title() {
// FIXME(sbinet): we should be able to handle (C++) polymorphism.
// but in Go, this should translate to interfaces.
panic(fmt.Errorf(
"rtree: rleaf object with incompatible class names: got=%q, want=%q",
class, leaf.base.Title(),
))
}
}
return leaf.v.UnmarshalROOT(r)
}
func newRLeafElem(leaf *tleafElement, rvar ReadVar, rctx rleafCtx) rleaf {
const kind = rbytes.ObjectWise // FIXME(sbinet): infer from stream?
var (
b = leaf.branch.(*tbranchElement)
si = b.streamer
err error
rstreamer rbytes.RStreamer
)
switch {
case b.id < 0:
rstreamer, err = si.NewRStreamer(kind)
default:
rstreamer, err = rdict.RStreamerOf(si, int(b.id), kind)
}
if err != nil {
panic(fmt.Errorf(
"rtree: could not find read-streamer for leaf=%q (type=%s): %+v",
leaf.Name(), leaf.TypeName(), err,
))
}
err = rstreamer.(rbytes.Binder).Bind(rvar.Value)
if err != nil {
panic(fmt.Errorf("rtree: could not bind read-streamer for leaf=%q (type=%s) to ptr=%T: %w",
leaf.Name(), leaf.TypeName(), rvar.Value, err,
))
}
if leaf.count != nil {
r, ok := rstreamer.(rbytes.Counter)
if !ok {
panic(fmt.Errorf(
"rtree: could not set read-streamer counter for leaf=%q (type=%s): %+v",
leaf.Name(), leaf.TypeName(), err,
))
}
lc := rctx.rcountLeaf(leaf.count.Name())
err = r.Count(lc.ivalue)
if err != nil {
panic(fmt.Errorf(
"rtree: could not set read-streamer counter for leaf=%q (type=%s): %+v",
leaf.Name(), leaf.TypeName(), err,
))
}
}
return &rleafElem{
base: leaf,
v: rvar.Value,
streamer: rstreamer,
}
}
type rleafElem struct {
base *tleafElement
v interface{}
n func() int
streamer rbytes.RStreamer
}
func (leaf *rleafElem) Leaf() Leaf { return leaf.base }
func (leaf *rleafElem) Offset() int64 {
return int64(leaf.base.Offset())
}
func (leaf *rleafElem) readFromBuffer(r *rbytes.RBuffer) error {
return leaf.streamer.RStreamROOT(r)
}
func (leaf *rleafElem) bindCount() {
switch v := reflect.ValueOf(leaf.v).Interface().(type) {
case *int8:
leaf.n = func() int { return int(*v) }
case *int16:
leaf.n = func() int { return int(*v) }
case *int32:
leaf.n = func() int { return int(*v) }
case *int64:
leaf.n = func() int { return int(*v) }
case *uint8:
leaf.n = func() int { return int(*v) }
case *uint16:
leaf.n = func() int { return int(*v) }
case *uint32:
leaf.n = func() int { return int(*v) }
case *uint64:
leaf.n = func() int { return int(*v) }
default:
panic(fmt.Errorf("invalid leaf-elem type: %T", v))
}
}
func (leaf *rleafElem) ivalue() int {
return leaf.n()
}
var (
_ rleaf = (*rleafElem)(nil)
)
type rleafCount struct {
Leaf
n func() int
leaf rleaf
}
func (l *rleafCount) ivalue() int {
return l.n()
}
func (l *rleafCount) imax() int {
panic("not implemented")
}
var (
_ leafCount = (*rleafCount)(nil)
)