15
15
// specific language governing permissions and limitations
16
16
// under the License.
17
17
18
- import { Vector } from './vector' ;
19
- import { StructVector , StructRow } from './struct' ;
20
- import { read , readAsync } from '../reader/arrow' ;
18
+ import { Vector } from './vector/vector' ;
19
+ import { read , readAsync } from './reader/arrow' ;
21
20
import { Predicate } from './predicate' ;
22
21
23
22
export type NextFunc = ( idx : number , cols : Vector [ ] ) => void ;
24
23
25
- export class DataFrameRow extends StructRow < any > {
26
- constructor ( batch : Vector [ ] , idx : number ) {
27
- super ( new StructVector ( { columns : batch } ) , idx ) ;
24
+ export class TableRow {
25
+ constructor ( readonly batch : Vector [ ] , readonly idx : number ) { }
26
+ toArray ( ) {
27
+ return this . batch . map ( ( vec ) => vec . get ( this . idx ) ) ;
28
28
}
29
29
toString ( ) {
30
30
return this . toArray ( ) . map ( ( x ) => JSON . stringify ( x ) ) . join ( ', ' ) ;
31
31
}
32
+ * [ Symbol . iterator ] ( ) {
33
+ for ( const vec of this . batch ) {
34
+ yield vec . get ( this . idx ) ;
35
+ }
36
+ }
32
37
}
33
38
34
39
export interface DataFrame {
@@ -46,7 +51,7 @@ function columnsFromBatches(batches: Vector[][]) {
46
51
) ;
47
52
}
48
53
49
- export class Table extends StructVector < any > implements DataFrame {
54
+ export class Table implements DataFrame {
50
55
static from ( sources ?: Iterable < Uint8Array | Buffer | string > | object | string ) {
51
56
let batches : Vector < any > [ ] [ ] = [ [ ] ] ;
52
57
if ( sources ) {
@@ -73,20 +78,20 @@ export class Table extends StructVector<any> implements DataFrame {
73
78
readonly lengths : Uint32Array ;
74
79
readonly length : number ;
75
80
constructor ( argv : { batches : Vector < any > [ ] [ ] } ) {
76
- super ( { columns : columnsFromBatches ( argv . batches ) } ) ;
77
81
this . batches = argv . batches ;
82
+ this . columns = columnsFromBatches ( this . batches ) ;
78
83
this . lengths = new Uint32Array ( this . batches . map ( ( batch ) => batch [ 0 ] . length ) ) ;
79
84
80
85
this . length = this . lengths . reduce ( ( acc , length ) => acc + length ) ;
81
86
}
82
- get ( idx : number ) : DataFrameRow {
87
+ get ( idx : number ) : TableRow {
83
88
let batch = 0 ;
84
89
while ( idx > this . lengths [ batch ] && batch < this . lengths . length )
85
90
idx -= this . lengths [ batch ++ ] ;
86
91
87
92
if ( batch === this . lengths . length ) throw new Error ( "Overflow" )
88
93
89
- else return new DataFrameRow ( this . batches [ batch ] , idx ) ;
94
+ else return new TableRow ( this . batches [ batch ] , idx ) ;
90
95
}
91
96
filter ( predicate : Predicate ) : DataFrame {
92
97
return new FilteredDataFrame ( this , predicate ) ;
@@ -116,7 +121,7 @@ export class Table extends StructVector<any> implements DataFrame {
116
121
117
122
// yield all indices
118
123
for ( let idx = - 1 ; ++ idx < length ; ) {
119
- yield new DataFrameRow ( columns , idx ) ;
124
+ yield new TableRow ( columns , idx ) ;
120
125
}
121
126
}
122
127
}
0 commit comments