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) { ...@@ -20,7 +20,7 @@ function ofSchema(schema: Table.Schema) {
const fields: Encoder.FieldDefinition[] = []; const fields: Encoder.FieldDefinition[] = [];
for (const k of Object.keys(schema)) { for (const k of Object.keys(schema)) {
const t = schema[k]; 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) }) fields.push({ name: k, type, value: columnValue(k), valueKind: columnValueKind(k) })
} }
return fields; return fields;
......
...@@ -25,20 +25,21 @@ namespace Column { ...@@ -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 type Schema<T = any> = Schema.Str | Schema.Int | Schema.Float | Schema.Coordinate | Schema.Aliased<T> | Schema.Tensor
export namespace Schema { export namespace Schema {
export type Str = { '@type': 'str', T: string, valueKind: 'str' } type Base<T extends string> = { valueType: T }
export type Int = { '@type': 'int', T: number, valueKind: 'int' } export type Str = { '@type': 'str', T: string } & Base<'str'>
export type Float = { '@type': 'float', T: number, valueKind: 'float' } export type Int = { '@type': 'int', T: number } & Base<'int'>
export type Coordinate = { '@type': 'coord', T: number, valueKind: 'float' } 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 Tensor = { '@type': 'tensor', T: Tensors, space: Tensors.Space } & Base<'tensor'>
export type Aliased<T> = { '@type': 'aliased', T: T, valueKind: 'str' | 'int' } export type Aliased<T> = { '@type': 'aliased', T: T } & Base<'str' | 'int'>
export const str: Str = { '@type': 'str', T: '', valueKind: 'str' }; export const str: Str = { '@type': 'str', T: '', valueType: 'str' };
export const int: Int = { '@type': 'int', T: 0, valueKind: 'int' }; export const int: Int = { '@type': 'int', T: 0, valueType: 'int' };
export const coord: Coordinate = { '@type': 'coord', T: 0, valueKind: 'float' }; export const coord: Coordinate = { '@type': 'coord', T: 0, valueType: 'float' };
export const float: Float = { '@type': 'float', T: 0, valueKind: '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 vector(dim: number): Tensor { return tensor(Tensors.Vector(dim)); }
export function matrix(rows: number, cols: number): Tensor { return tensor(Tensors.ColumnMajorMatrix(rows, cols)); } 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 ...@@ -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']> { function arrayColumn<T extends Column.Schema>({ array, schema, valueKind }: Column.ArraySpec<T>): Column<T['T']> {
const rowCount = array.length; 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 => { const v = array[row]; return typeof v === 'string' ? v : '' + v; }
: row => array[row]; : row => array[row];
...@@ -194,7 +195,7 @@ function arrayColumn<T extends Column.Schema>({ array, schema, valueKind }: Colu ...@@ -194,7 +195,7 @@ function arrayColumn<T extends Column.Schema>({ array, schema, valueKind }: Colu
rowCount, rowCount,
value, value,
valueKind: valueKind ? valueKind : row => Column.ValueKind.Present, valueKind: valueKind ? valueKind : row => Column.ValueKind.Present,
toArray: schema.valueKind === 'str' toArray: schema.valueType === 'str'
? params => { ? params => {
const { start, end } = ColumnHelpers.getArrayBounds(rowCount, 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; 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 ...@@ -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>) { 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); if (!!a['@array'] && !!b['@array']) return areArraysEqual(a, b);
return areValuesEqual(a, b); return areValuesEqual(a, b);
} }
......
...@@ -19,7 +19,7 @@ export function toTable<Schema extends Table.Schema, R extends Table<Schema> = T ...@@ -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> type ColumnCtor = (field: Data.Field, category: Data.Category, key: string) => Column<any>
function getColumnCtor(t: Column.Schema): ColumnCtor { 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 '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 '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); 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 ...@@ -74,13 +74,13 @@ class CategoryTable implements Table<any> { // tslint:disable-line:class-name
Object.defineProperty(this, k, { Object.defineProperty(this, k, {
get: function() { get: function() {
if (cache[k]) return cache[k]; if (cache[k]) return cache[k];
const cType = schema[k]; const fType = schema[k];
if (cType.valueKind === 'tensor') { if (fType.valueType === 'tensor') {
cache[k] = createTensorColumn(cType, category, k); cache[k] = createTensorColumn(fType, category, k);
} else { } else {
const ctor = getColumnCtor(cType); const ctor = getColumnCtor(fType);
const field = category.getField(k); 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]; return cache[k];
}, },
......
...@@ -16,15 +16,15 @@ export default function FixedColumnProvider(lines: Tokens) { ...@@ -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']> { 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 { 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]; let s = indices[2 * row] + offset, le = indices[2 * row + 1];
if (s >= le) return ''; if (s >= le) return '';
let e = s + width; let e = s + width;
if (e > le) e = le; if (e > le) e = le;
return trimStr(data, s, e); return trimStr(data, s, e);
} : kind === 'int' ? row => { } : type === 'int' ? row => {
const s = indices[2 * row] + offset; const s = indices[2 * row] + offset;
if (s > indices[2 * row + 1]) return 0; if (s > indices[2 * row + 1]) return 0;
return parseIntSkipLeadingWhitespace(data, s, s + width); return parseIntSkipLeadingWhitespace(data, s, s + width);
......
...@@ -16,12 +16,12 @@ export default function TokenColumnProvider(tokens: Tokens) { ...@@ -16,12 +16,12 @@ export default function TokenColumnProvider(tokens: Tokens) {
export function TokenColumn<T extends Column.Schema>(tokens: Tokens, schema: T): Column<T['T']> { export function TokenColumn<T extends Column.Schema>(tokens: Tokens, schema: T): Column<T['T']> {
const { data, indices, count: rowCount } = tokens; const { data, indices, count: rowCount } = tokens;
const { valueKind: kind } = schema; const { valueType: type } = schema;
const value: Column<T['T']>['value'] = const value: Column<T['T']>['value'] =
kind === 'str' type === 'str'
? row => data.substring(indices[2 * row], indices[2 * row + 1]) ? 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 => fastParseInt(data, indices[2 * row], indices[2 * row + 1]) || 0
: row => fastParseFloat(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) { ...@@ -49,7 +49,7 @@ function ofSchema(schema: Table.Schema) {
for (const k of Object.keys(schema)) { for (const k of Object.keys(schema)) {
const t = schema[k]; const t = schema[k];
// TODO: matrix/vector/support // 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) }) fields.push({ name: k, type, value: columnValue(k), valueKind: columnValueKind(k) })
} }
return fields; return fields;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment