1616// under the License.
1717
1818import { ChunkedData } from '../data' ;
19- import { View , Vector } from '../vector' ;
19+ import { View , Vector , NestedVector } from '../vector' ;
2020import { DataType , TypedArray , IterableArrayLike } from '../type' ;
2121
2222export 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 > [ ] ;
2526 constructor ( data : ChunkedData < T > ) {
26- this . chunks = data . childVectors ;
27- this . offsets = data . childOffsets ;
27+ this . childVectors = data . childVectors ;
28+ this . childOffsets = data . childOffsets ;
2829 }
2930 public clone ( data : ChunkedData < T > ) : this {
3031 return new ChunkedView ( data ) as this;
3132 }
3233 public * [ Symbol . iterator ] ( ) : IterableIterator < T [ 'TValue' ] | null > {
33- for ( const vector of this . chunks ) {
34+ for ( const vector of this . childVectors ) {
3435 yield * vector ;
3536 }
3637 }
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+ }
3743 public isValid ( index : number ) : boolean {
3844 // 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 ;
4046 let lhs = 0 , mid = 0 , rhs = offsets . length - 1 ;
4147 while ( index < offsets [ rhs ] && index >= ( pos = offsets [ lhs ] ) ) {
4248 if ( lhs + 1 === rhs ) {
43- return this . chunks [ lhs ] . isValid ( index - pos ) ;
49+ return this . childVectors [ lhs ] . isValid ( index - pos ) ;
4450 }
4551 mid = lhs + ( ( rhs - lhs ) / 2 ) | 0 ;
4652 index >= offsets [ mid ] ? ( lhs = mid ) : ( rhs = mid ) ;
@@ -49,11 +55,11 @@ export class ChunkedView<T extends DataType> implements View<T> {
4955 }
5056 public get ( index : number ) : T [ 'TValue' ] | null {
5157 // 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 ;
5359 let lhs = 0 , mid = 0 , rhs = offsets . length - 1 ;
5460 while ( index < offsets [ rhs ] && index >= ( pos = offsets [ lhs ] ) ) {
5561 if ( lhs + 1 === rhs ) {
56- return this . chunks [ lhs ] . get ( index - pos ) ;
62+ return this . childVectors [ lhs ] . get ( index - pos ) ;
5763 }
5864 mid = lhs + ( ( rhs - lhs ) / 2 ) | 0 ;
5965 index >= offsets [ mid ] ? ( lhs = mid ) : ( rhs = mid ) ;
@@ -62,18 +68,18 @@ export class ChunkedView<T extends DataType> implements View<T> {
6268 }
6369 public set ( index : number , value : T [ 'TValue' ] | null ) : void {
6470 // 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 ;
6672 let lhs = 0 , mid = 0 , rhs = offsets . length - 1 ;
6773 while ( index < offsets [ rhs ] && index >= ( pos = offsets [ lhs ] ) ) {
6874 if ( lhs + 1 === rhs ) {
69- return this . chunks [ lhs ] . set ( index - pos , value ) ;
75+ return this . childVectors [ lhs ] . set ( index - pos , value ) ;
7076 }
7177 mid = lhs + ( ( rhs - lhs ) / 2 ) | 0 ;
7278 index >= offsets [ mid ] ? ( lhs = mid ) : ( rhs = mid ) ;
7379 }
7480 }
7581 public toArray ( ) : IterableArrayLike < T [ 'TValue' ] | null > {
76- const chunks = this . chunks ;
82+ const chunks = this . childVectors ;
7783 const numChunks = chunks . length ;
7884 if ( numChunks === 1 ) {
7985 return chunks [ 0 ] . toArray ( ) ;
0 commit comments