Skip to content

Commit e8979ba

Browse files
author
Brian Hulette
committed
Refactor DataFrame to extend Vector<StructRow>
1 parent 6a41d68 commit e8979ba

File tree

1 file changed

+38
-12
lines changed

1 file changed

+38
-12
lines changed

js/src/dataframe/dataframe.ts

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
11
import { Vector } from "../vector/vector";
2-
import { StructVector } from "../vector/struct";
2+
import { StructVector, StructRow } from "../vector/struct";
33
import { VirtualVector } from "../vector/virtual";
44

55
import { Predicate } from "./predicate"
66

77
export type NextFunc = (idx: number, cols: Vector[]) => void;
88

9-
export class DataFrame {
9+
export class DataFrameRow extends StructRow<any> {
10+
constructor (batches: Vector[], idx: number) {
11+
super(new StructVector({columns: batches}), idx);
12+
}
13+
}
14+
15+
export interface DataFrameOps {
16+
readonly batches: Vector[][];
17+
readonly lengths: Uint32Array;
18+
filter(predicate: Predicate): DataFrameOps;
19+
scan(next: NextFunc): void;
20+
count(): number;
21+
}
22+
23+
export class DataFrame extends Vector<DataFrameRow> implements DataFrameOps {
1024
readonly lengths: Uint32Array;
11-
public columns: Vector<any>[];
12-
constructor(readonly batches: Vector<any>[][]) {
25+
constructor(readonly batches: Vector[][]) {
26+
super();
1327
// for each batch
1428
this.lengths = new Uint32Array(batches.map((batch)=>{
1529
// verify that every vector has the same length, and return that
@@ -23,7 +37,17 @@ export class DataFrame {
2337
}));
2438
}
2539

26-
public filter(predicate: Predicate): DataFrame {
40+
get(idx: number): DataFrameRow|null {
41+
let batch = 0;
42+
while (idx > this.lengths[batch] && batch < this.lengths.length)
43+
idx -= this.lengths[batch++];
44+
45+
if (batch === this.lengths.length) return null;
46+
47+
else return new DataFrameRow(this.batches[batch], idx);
48+
}
49+
50+
filter(predicate: Predicate): DataFrameOps {
2751
return new FilteredDataFrame(this, predicate);
2852
}
2953

@@ -50,11 +74,11 @@ export class DataFrame {
5074
const length = this.lengths[batch];
5175

5276
// load batches
53-
this.columns = this.batches[batch];
77+
const columns = this.batches[batch];
5478

5579
// yield all indices
5680
for (let idx = -1; ++idx < length;) {
57-
yield idx;
81+
yield new DataFrameRow(columns, idx);
5882
}
5983
}
6084
}
@@ -85,10 +109,12 @@ export class DataFrame {
85109
}
86110
}
87111

88-
class FilteredDataFrame extends DataFrame {
89-
public columns: Vector<any>[];
90-
constructor (readonly parent: DataFrame, private predicate: Predicate) {
91-
super(parent.batches);
112+
class FilteredDataFrame implements DataFrameOps {
113+
readonly lengths: Uint32Array;
114+
readonly batches: Vector[][];
115+
constructor (readonly parent: DataFrameOps, private predicate: Predicate) {
116+
this.batches = parent.batches;
117+
this.lengths = parent.lengths;
92118
}
93119

94120
scan(next: NextFunc) {
@@ -133,7 +159,7 @@ class FilteredDataFrame extends DataFrame {
133159
return sum;
134160
}
135161

136-
filter(predicate: Predicate): DataFrame {
162+
filter(predicate: Predicate): DataFrameOps {
137163
return new FilteredDataFrame(
138164
this.parent,
139165
this.predicate.and(predicate)

0 commit comments

Comments
 (0)