Skip to content
Snippets Groups Projects
Commit 2d6c4538 authored by David Sehnal's avatar David Sehnal
Browse files

renamed Column.Schema.valueKind to valueType

parent 6b57151d
No related branches found
No related tags found
No related merge requests found
......@@ -20,7 +20,7 @@ function ofSchema(schema: Table.Schema) {
const fields: Encoder.FieldDefinition[] = [];
for (const k of Object.keys(schema)) {
const t = schema[k];
const type: any = t.valueKind === 'str' ? Encoder.FieldType.Str : t.valueKind === 'int' ? Encoder.FieldType.Int : Encoder.FieldType.Float;
const type: any = t.valueType === 'str' ? Encoder.FieldType.Str : t.valueType === 'int' ? Encoder.FieldType.Int : Encoder.FieldType.Float;
fields.push({ name: k, type, value: columnValue(k), valueKind: columnValueKind(k) })
}
return fields;
......
......@@ -25,20 +25,21 @@ namespace Column {
export type Schema<T = any> = Schema.Str | Schema.Int | Schema.Float | Schema.Coordinate | Schema.Aliased<T> | Schema.Tensor
export namespace Schema {
export type Str = { '@type': 'str', T: string, valueKind: 'str' }
export type Int = { '@type': 'int', T: number, valueKind: 'int' }
export type Float = { '@type': 'float', T: number, valueKind: 'float' }
export type Coordinate = { '@type': 'coord', T: number, valueKind: 'float' }
type Base<T extends string> = { valueType: T }
export type Str = { '@type': 'str', T: string } & Base<'str'>
export type Int = { '@type': 'int', T: number } & Base<'int'>
export type Float = { '@type': 'float', T: number } & Base<'float'>
export type Coordinate = { '@type': 'coord', T: number } & Base<'float'>
export type Tensor = { '@type': 'tensor', T: Tensors, space: Tensors.Space, valueKind: 'tensor' };
export type Aliased<T> = { '@type': 'aliased', T: T, valueKind: 'str' | 'int' }
export type Tensor = { '@type': 'tensor', T: Tensors, space: Tensors.Space } & Base<'tensor'>
export type Aliased<T> = { '@type': 'aliased', T: T } & Base<'str' | 'int'>
export const str: Str = { '@type': 'str', T: '', valueKind: 'str' };
export const int: Int = { '@type': 'int', T: 0, valueKind: 'int' };
export const coord: Coordinate = { '@type': 'coord', T: 0, valueKind: 'float' };
export const float: Float = { '@type': 'float', T: 0, valueKind: 'float' };
export const str: Str = { '@type': 'str', T: '', valueType: 'str' };
export const int: Int = { '@type': 'int', T: 0, valueType: 'int' };
export const coord: Coordinate = { '@type': 'coord', T: 0, valueType: 'float' };
export const float: Float = { '@type': 'float', T: 0, valueType: 'float' };
export function tensor(space: Tensors.Space): Tensor { return { '@type': 'tensor', T: space.create(), space, valueKind: 'tensor' }; }
export function tensor(space: Tensors.Space): Tensor { return { '@type': 'tensor', T: space.create(), space, valueType: 'tensor' }; }
export function vector(dim: number): Tensor { return tensor(Tensors.Vector(dim)); }
export function matrix(rows: number, cols: number): Tensor { return tensor(Tensors.ColumnMajorMatrix(rows, cols)); }
......@@ -182,7 +183,7 @@ function lambdaColumn<T extends Column.Schema>({ value, valueKind, rowCount, sch
function arrayColumn<T extends Column.Schema>({ array, schema, valueKind }: Column.ArraySpec<T>): Column<T['T']> {
const rowCount = array.length;
const value: Column<T['T']>['value'] = schema.valueKind === 'str'
const value: Column<T['T']>['value'] = schema.valueType === 'str'
? row => { const v = array[row]; return typeof v === 'string' ? v : '' + v; }
: row => array[row];
......@@ -194,7 +195,7 @@ function arrayColumn<T extends Column.Schema>({ array, schema, valueKind }: Colu
rowCount,
value,
valueKind: valueKind ? valueKind : row => Column.ValueKind.Present,
toArray: schema.valueKind === 'str'
toArray: schema.valueType === 'str'
? params => {
const { start, end } = ColumnHelpers.getArrayBounds(rowCount, params);
const ret = new (params && typeof params.array !== 'undefined' ? params.array : (array as any).constructor)(end - start) as any;
......@@ -297,7 +298,7 @@ function mapToArrayImpl<T, S>(c: Column<T>, f: (v: T) => S, ctor: Column.ArrayCt
}
function areColumnsEqual(a: Column<any>, b: Column<any>) {
if (a.rowCount !== b.rowCount || a.isDefined !== b.isDefined || a.schema.valueKind !== b.schema.valueKind) return false;
if (a.rowCount !== b.rowCount || a.isDefined !== b.isDefined || a.schema.valueType !== b.schema.valueType) return false;
if (!!a['@array'] && !!b['@array']) return areArraysEqual(a, b);
return areValuesEqual(a, b);
}
......
......@@ -19,7 +19,7 @@ export function toTable<Schema extends Table.Schema, R extends Table<Schema> = T
type ColumnCtor = (field: Data.Field, category: Data.Category, key: string) => Column<any>
function getColumnCtor(t: Column.Schema): ColumnCtor {
switch (t.valueKind) {
switch (t.valueType) {
case 'str': return (f, c, k) => createColumn(t, f, f.str, f.toStringArray);
case 'int': return (f, c, k) => createColumn(t, f, f.int, f.toIntArray);
case 'float': return (f, c, k) => createColumn(t, f, f.float, f.toFloatArray);
......@@ -74,13 +74,13 @@ class CategoryTable implements Table<any> { // tslint:disable-line:class-name
Object.defineProperty(this, k, {
get: function() {
if (cache[k]) return cache[k];
const cType = schema[k];
if (cType.valueKind === 'tensor') {
cache[k] = createTensorColumn(cType, category, k);
const fType = schema[k];
if (fType.valueType === 'tensor') {
cache[k] = createTensorColumn(fType, category, k);
} else {
const ctor = getColumnCtor(cType);
const ctor = getColumnCtor(fType);
const field = category.getField(k);
cache[k] = !!field ? ctor(field, category, k) : Column.Undefined(category.rowCount, cType);
cache[k] = !!field ? ctor(field, category, k) : Column.Undefined(category.rowCount, fType);
}
return cache[k];
},
......
......@@ -16,15 +16,15 @@ export default function FixedColumnProvider(lines: Tokens) {
export function FixedColumn<T extends Column.Schema>(lines: Tokens, offset: number, width: number, schema: T): Column<T['T']> {
const { data, indices, count: rowCount } = lines;
const { valueKind: kind } = schema;
const { valueType: type } = schema;
const value: Column<T['T']>['value'] = kind === 'str' ? row => {
const value: Column<T['T']>['value'] = type === 'str' ? row => {
let s = indices[2 * row] + offset, le = indices[2 * row + 1];
if (s >= le) return '';
let e = s + width;
if (e > le) e = le;
return trimStr(data, s, e);
} : kind === 'int' ? row => {
} : type === 'int' ? row => {
const s = indices[2 * row] + offset;
if (s > indices[2 * row + 1]) return 0;
return parseIntSkipLeadingWhitespace(data, s, s + width);
......
......@@ -16,12 +16,12 @@ export default function TokenColumnProvider(tokens: Tokens) {
export function TokenColumn<T extends Column.Schema>(tokens: Tokens, schema: T): Column<T['T']> {
const { data, indices, count: rowCount } = tokens;
const { valueKind: kind } = schema;
const { valueType: type } = schema;
const value: Column<T['T']>['value'] =
kind === 'str'
type === 'str'
? row => data.substring(indices[2 * row], indices[2 * row + 1])
: kind === 'int'
: type === 'int'
? row => fastParseInt(data, indices[2 * row], indices[2 * row + 1]) || 0
: row => fastParseFloat(data, indices[2 * row], indices[2 * row + 1]) || 0;
......
......@@ -49,7 +49,7 @@ function ofSchema(schema: Table.Schema) {
for (const k of Object.keys(schema)) {
const t = schema[k];
// TODO: matrix/vector/support
const type: any = t.valueKind === 'str' ? Encoder.FieldType.Str : t.valueKind === 'int' ? Encoder.FieldType.Int : Encoder.FieldType.Float;
const type: any = t.valueType === 'str' ? Encoder.FieldType.Str : t.valueType === 'int' ? Encoder.FieldType.Int : Encoder.FieldType.Float;
fields.push({ name: k, type, value: columnValue(k), valueKind: columnValueKind(k) })
}
return fields;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment