token.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /**
  2. * Copyright (c) 2017 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import { Column, ColumnHelpers } from '../../../../../mol-data/db';
  7. import { Tokens } from '../tokenizer';
  8. import { parseInt as fastParseInt, parseFloat as fastParseFloat } from '../number-parser';
  9. export function TokenColumnProvider(tokens: Tokens) {
  10. return function<T extends Column.Schema>(type: T) {
  11. return TokenColumn(tokens, type);
  12. };
  13. }
  14. export function TokenColumn<T extends Column.Schema>(tokens: Tokens, schema: T): Column<T['T']> {
  15. const { data, indices, count: rowCount } = tokens;
  16. const { valueType: type } = schema;
  17. const value: Column<T['T']>['value'] =
  18. type === 'str'
  19. ? row => data.substring(indices[2 * row], indices[2 * row + 1])
  20. : type === 'int'
  21. ? row => fastParseInt(data, indices[2 * row], indices[2 * row + 1]) || 0
  22. : row => fastParseFloat(data, indices[2 * row], indices[2 * row + 1]) || 0;
  23. return {
  24. schema: schema,
  25. __array: void 0,
  26. isDefined: true,
  27. rowCount,
  28. value,
  29. valueKind: row => Column.ValueKind.Present,
  30. toArray: params => ColumnHelpers.createAndFillArray(rowCount, value, params),
  31. areValuesEqual: areValuesEqualProvider(tokens)
  32. };
  33. }
  34. export function areValuesEqualProvider(tokens: Tokens) {
  35. const { data, indices } = tokens;
  36. return function (rowA: number, rowB: number) {
  37. const aS = indices[2 * rowA], bS = indices[2 * rowB];
  38. const len = indices[2 * rowA + 1] - aS;
  39. if (len !== indices[2 * rowB + 1] - bS) return false;
  40. for (let i = 0; i < len; i++) {
  41. if (data.charCodeAt(i + aS) !== data.charCodeAt(i + bS)) {
  42. return false;
  43. }
  44. }
  45. return true;
  46. };
  47. }