token.ts 2.1 KB

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