16
16
// under the License.
17
17
18
18
import { ChunkedData } from '../data' ;
19
- import { View , Vector } from '../vector' ;
19
+ import { View , Vector , NestedVector } from '../vector' ;
20
20
import { DataType , TypedArray , IterableArrayLike } from '../type' ;
21
21
22
22
export class ChunkedView < T extends DataType > implements View < T > {
23
- public chunks : Vector < T > [ ] ;
24
- public offsets : Uint32Array ;
23
+ public childVectors : Vector < T > [ ] ;
24
+ public childOffsets : Uint32Array ;
25
+ protected _childColumns : Vector < any > [ ] ;
25
26
constructor ( data : ChunkedData < T > ) {
26
- this . chunks = data . childVectors ;
27
- this . offsets = data . childOffsets ;
27
+ this . childVectors = data . childVectors ;
28
+ this . childOffsets = data . childOffsets ;
28
29
}
29
30
public clone ( data : ChunkedData < T > ) : this {
30
31
return new ChunkedView ( data ) as this;
31
32
}
32
33
public * [ Symbol . iterator ] ( ) : IterableIterator < T [ 'TValue' ] | null > {
33
- for ( const vector of this . chunks ) {
34
+ for ( const vector of this . childVectors ) {
34
35
yield * vector ;
35
36
}
36
37
}
38
+ public getChildAt < R extends DataType = DataType > ( index : number ) {
39
+ return ( this . _childColumns || ( this . _childColumns = [ ] ) ) [ index ] || (
40
+ this . _childColumns [ index ] = Vector . concat < R > (
41
+ ...( < any > this . childVectors as NestedVector < any > [ ] ) . map ( ( v ) => v . getChildAt ( index ) ) ) ) ;
42
+ }
37
43
public isValid ( index : number ) : boolean {
38
44
// binary search to find the child vector and value index offset (inlined for speed)
39
- let offsets = this . offsets , pos = 0 ;
45
+ let offsets = this . childOffsets , pos = 0 ;
40
46
let lhs = 0 , mid = 0 , rhs = offsets . length - 1 ;
41
47
while ( index < offsets [ rhs ] && index >= ( pos = offsets [ lhs ] ) ) {
42
48
if ( lhs + 1 === rhs ) {
43
- return this . chunks [ lhs ] . isValid ( index - pos ) ;
49
+ return this . childVectors [ lhs ] . isValid ( index - pos ) ;
44
50
}
45
51
mid = lhs + ( ( rhs - lhs ) / 2 ) | 0 ;
46
52
index >= offsets [ mid ] ? ( lhs = mid ) : ( rhs = mid ) ;
@@ -49,11 +55,11 @@ export class ChunkedView<T extends DataType> implements View<T> {
49
55
}
50
56
public get ( index : number ) : T [ 'TValue' ] | null {
51
57
// binary search to find the child vector and value index offset (inlined for speed)
52
- let offsets = this . offsets , pos = 0 ;
58
+ let offsets = this . childOffsets , pos = 0 ;
53
59
let lhs = 0 , mid = 0 , rhs = offsets . length - 1 ;
54
60
while ( index < offsets [ rhs ] && index >= ( pos = offsets [ lhs ] ) ) {
55
61
if ( lhs + 1 === rhs ) {
56
- return this . chunks [ lhs ] . get ( index - pos ) ;
62
+ return this . childVectors [ lhs ] . get ( index - pos ) ;
57
63
}
58
64
mid = lhs + ( ( rhs - lhs ) / 2 ) | 0 ;
59
65
index >= offsets [ mid ] ? ( lhs = mid ) : ( rhs = mid ) ;
@@ -62,18 +68,18 @@ export class ChunkedView<T extends DataType> implements View<T> {
62
68
}
63
69
public set ( index : number , value : T [ 'TValue' ] | null ) : void {
64
70
// binary search to find the child vector and value index offset (inlined for speed)
65
- let offsets = this . offsets , pos = 0 ;
71
+ let offsets = this . childOffsets , pos = 0 ;
66
72
let lhs = 0 , mid = 0 , rhs = offsets . length - 1 ;
67
73
while ( index < offsets [ rhs ] && index >= ( pos = offsets [ lhs ] ) ) {
68
74
if ( lhs + 1 === rhs ) {
69
- return this . chunks [ lhs ] . set ( index - pos , value ) ;
75
+ return this . childVectors [ lhs ] . set ( index - pos , value ) ;
70
76
}
71
77
mid = lhs + ( ( rhs - lhs ) / 2 ) | 0 ;
72
78
index >= offsets [ mid ] ? ( lhs = mid ) : ( rhs = mid ) ;
73
79
}
74
80
}
75
81
public toArray ( ) : IterableArrayLike < T [ 'TValue' ] | null > {
76
- const chunks = this . chunks ;
82
+ const chunks = this . childVectors ;
77
83
const numChunks = chunks . length ;
78
84
if ( numChunks === 1 ) {
79
85
return chunks [ 0 ] . toArray ( ) ;
0 commit comments