@@ -21,10 +21,6 @@ import { View, Vector, createVector } from '../vector';
21
21
import { DataType , NestedType , DenseUnion , SparseUnion , Struct , Map_ } from '../type' ;
22
22
23
23
export abstract class NestedView < T extends NestedType > implements View < T > {
24
- // @ts -ignore
25
- public typeIds : Int8Array ;
26
- // @ts -ignore
27
- public valueOffsets : any ;
28
24
public readonly numChildren : number ;
29
25
public readonly childData : Data < any > [ ] ;
30
26
protected children : { [ k : number ] : Vector < any > } = Object . create ( null ) ;
@@ -56,9 +52,15 @@ export abstract class NestedView<T extends NestedType> implements View<T> {
56
52
}
57
53
58
54
export class UnionView < T extends ( DenseUnion | SparseUnion ) = SparseUnion > extends NestedView < T > {
55
+ public length : number ;
56
+ // @ts -ignore
57
+ public typeIds : Int8Array ;
58
+ // @ts -ignore
59
+ public valueOffsets ?: Int32Array ;
59
60
constructor ( data : Data < T > ) {
60
61
super ( data ) ;
61
- this . typeIds = this . typeIds ;
62
+ this . length = data . length ;
63
+ this . typeIds = data . typeIds ;
62
64
}
63
65
public getNested ( view : NestedView < T > , index : number ) : T [ 'TValue' ] {
64
66
return this . getUnionValue ( view , index , this . typeIds , this . valueOffsets ) ;
@@ -68,8 +70,8 @@ export class UnionView<T extends (DenseUnion | SparseUnion) = SparseUnion> exten
68
70
return child ? child . get ( index ) : null ;
69
71
}
70
72
public * [ Symbol . iterator ] ( ) : IterableIterator < T [ 'TValue' ] > {
73
+ const length = this . length ;
71
74
const get = this . getUnionValue ;
72
- const length = this . data . length ;
73
75
const { typeIds, valueOffsets } = this ;
74
76
for ( let index = - 1 ; ++ index < length ; ) {
75
77
yield get ( this , index , typeIds , valueOffsets ) ;
@@ -78,6 +80,8 @@ export class UnionView<T extends (DenseUnion | SparseUnion) = SparseUnion> exten
78
80
}
79
81
80
82
export class DenseUnionView extends UnionView < DenseUnion > {
83
+ // @ts -ignore
84
+ public valueOffsets : Int32Array ;
81
85
constructor ( data : Data < DenseUnion > ) {
82
86
super ( data ) ;
83
87
this . valueOffsets = data . valueOffsets ;
@@ -91,41 +95,38 @@ export class DenseUnionView extends UnionView<DenseUnion> {
91
95
}
92
96
}
93
97
94
- export abstract class TabularView < T extends NestedType > extends NestedView < T > {
95
- constructor ( data : Data < T > ) {
96
- super ( data ) ;
97
- this . typeIds = new Int8Array ( data . childData . map ( ( x ) => x . typeId ) ) ;
98
- this . valueOffsets = data . type . children . reduce ( ( xs , x ) =>
99
- ( xs [ x . name ] = x . typeId ) && xs || xs , Object . create ( null ) ) ;
98
+ export class StructView extends NestedView < Struct > {
99
+ public getNested ( view : StructView , index : number ) {
100
+ return new RowView ( view as any , index ) ;
100
101
}
101
102
}
102
103
103
- export class MapView extends TabularView < Map_ > {
104
+ export class MapView extends NestedView < Map_ > {
105
+ // @ts -ignore
106
+ public typeIds : { [ k : string ] : number } ;
107
+ constructor ( data : Data < Map_ > ) {
108
+ super ( data ) ;
109
+ this . typeIds = data . type . children . reduce ( ( xs , x , i ) =>
110
+ ( xs [ x . name ] = i ) && xs || xs , Object . create ( null ) ) ;
111
+ }
104
112
public getNested ( view : MapView , index : number ) {
105
- return new MapRow ( view as any , index ) ;
113
+ return new MapRowView ( view as any , index ) ;
106
114
}
107
115
}
108
116
109
- export class MapRow extends DenseUnionView {
110
- constructor ( data : Data < DenseUnion > , protected rowIndex : number ) {
117
+ export class RowView extends UnionView < SparseUnion > {
118
+ constructor ( data : Data < SparseUnion > & NestedView < any > , protected rowIndex : number ) {
111
119
super ( data ) ;
120
+ this . length = data . numChildren ;
112
121
}
113
- protected getUnionValue ( view : NestedView < DenseUnion > , index : number , typeIds : Int8Array , valueOffsets : any ) : any | null {
114
- const child = view . getChildAt ( typeIds [ valueOffsets [ index ] ] ) ;
122
+ protected getUnionValue ( view : NestedView < SparseUnion > , index : number , _typeIds : any , _valueOffsets ? : any ) : any | null {
123
+ const child = view . getChildAt ( index ) ;
115
124
return child ? child . get ( this . rowIndex ) : null ;
116
125
}
117
126
}
118
- export class StructView extends TabularView < Struct > {
119
- public getNested ( view : StructView , index : number ) {
120
- return new StructRow ( view as any , index ) ;
121
- }
122
- }
123
127
124
- export class StructRow extends UnionView < SparseUnion > {
125
- constructor ( data : Data < SparseUnion > , protected rowIndex : number ) {
126
- super ( data ) ;
127
- }
128
- protected getUnionValue ( view : NestedView < SparseUnion > , index : number , typeIds : Int8Array , _valueOffsets ?: any ) : any | null {
128
+ export class MapRowView extends RowView {
129
+ protected getUnionValue ( view : NestedView < SparseUnion > , index : number , typeIds : any , _valueOffsets : any ) : any | null {
129
130
const child = view . getChildAt ( typeIds [ index ] ) ;
130
131
return child ? child . get ( this . rowIndex ) : null ;
131
132
}
0 commit comments