encoding.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**
  2. * Copyright (c) 2017 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * From CIFTools.js
  5. * @author David Sehnal <david.sehnal@gmail.com>
  6. */
  7. export const VERSION = '0.3.0';
  8. export type Encoding =
  9. | Encoding.ByteArray
  10. | Encoding.FixedPoint
  11. | Encoding.RunLength
  12. | Encoding.Delta
  13. | Encoding.IntervalQuantization
  14. | Encoding.IntegerPacking
  15. | Encoding.StringArray;
  16. export interface EncodedFile {
  17. version: string,
  18. encoder: string,
  19. dataBlocks: EncodedDataBlock[]
  20. }
  21. export interface EncodedDataBlock {
  22. header: string,
  23. categories: EncodedCategory[],
  24. }
  25. export interface EncodedCategory {
  26. name: string,
  27. rowCount: number,
  28. columns: EncodedColumn[],
  29. }
  30. export interface EncodedColumn {
  31. name: string,
  32. data: EncodedData,
  33. /**
  34. * The mask represents the presence or absent of particular "CIF value".
  35. * If the mask is not set, every value is present.
  36. *
  37. * 0 = Value is present
  38. * 1 = . = value not specified
  39. * 2 = ? = value unknown
  40. */
  41. mask?: EncodedData
  42. }
  43. export interface EncodedData {
  44. encoding: Encoding[],
  45. data: Uint8Array
  46. }
  47. export namespace Encoding {
  48. export const enum IntDataType {
  49. Int8 = 1,
  50. Int16 = 2,
  51. Int32 = 3,
  52. Uint8 = 4,
  53. Uint16 = 5,
  54. Uint32 = 6,
  55. }
  56. export const enum FloatDataType {
  57. Float32 = 32,
  58. Float64 = 33
  59. }
  60. export type DataType = IntDataType | FloatDataType
  61. export type TypedIntArray = Helpers.TypedIntArray
  62. export type TypedFloatArray = Helpers.TypedFloatArray
  63. export function getDataType(data: TypedIntArray | TypedFloatArray): DataType {
  64. let srcType: DataType;
  65. if (data instanceof Int8Array) srcType = Encoding.IntDataType.Int8;
  66. else if (data instanceof Int16Array) srcType = Encoding.IntDataType.Int16;
  67. else if (data instanceof Int32Array) srcType = Encoding.IntDataType.Int32;
  68. else if (data instanceof Uint8Array) srcType = Encoding.IntDataType.Uint8;
  69. else if (data instanceof Uint16Array) srcType = Encoding.IntDataType.Uint16;
  70. else if (data instanceof Uint32Array) srcType = Encoding.IntDataType.Uint32;
  71. else if (data instanceof Float32Array) srcType = Encoding.FloatDataType.Float32;
  72. else if (data instanceof Float64Array) srcType = Encoding.FloatDataType.Float64;
  73. else srcType = Encoding.IntDataType.Int32; // throw new Error('Unsupported integer data type.');
  74. return srcType;
  75. }
  76. export function isSignedIntegerDataType(data: TypedIntArray) {
  77. if (data instanceof Int8Array || data instanceof Int16Array || data instanceof Int32Array) return true;
  78. for (let i = 0, _i = data.length; i < _i; i++) {
  79. if (i < 0) return false;
  80. }
  81. return true;
  82. }
  83. // type[] -> Uint8[]
  84. export interface ByteArray {
  85. kind: 'ByteArray',
  86. type: DataType
  87. }
  88. // (Float32 | Float64)[] -> Int32[]
  89. export interface FixedPoint {
  90. kind: 'FixedPoint',
  91. factor: number,
  92. srcType: FloatDataType
  93. }
  94. // (Float32|Float64)[] -> Int32
  95. export interface IntervalQuantization {
  96. kind: 'IntervalQuantization',
  97. min: number,
  98. max: number,
  99. numSteps: number,
  100. srcType: FloatDataType
  101. }
  102. // (Uint8 | Int8 | Int16 | Int32)[] -> Int32[]
  103. export interface RunLength {
  104. kind: 'RunLength',
  105. srcType: IntDataType,
  106. srcSize: number
  107. }
  108. // T=(Int8Array | Int16Array | Int32Array)[] -> T[]
  109. export interface Delta {
  110. kind: 'Delta',
  111. origin: number,
  112. srcType: IntDataType
  113. }
  114. // Int32[] -> (Int8 | Int16 | Uint8 | Uint16)[]
  115. export interface IntegerPacking {
  116. kind: 'IntegerPacking',
  117. byteCount: number,
  118. isUnsigned: boolean,
  119. srcSize: number
  120. }
  121. // string[] -> Uint8[]
  122. // stores 0 and indices of ends of strings:
  123. // stringData = '123456'
  124. // offsets = [0,2,5,6]
  125. // encodes ['12','345','6']
  126. export interface StringArray {
  127. kind: 'StringArray',
  128. dataEncoding: Encoding[],
  129. stringData: string,
  130. offsetEncoding: Encoding[],
  131. offsets: Uint8Array
  132. }
  133. }