encoding.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 throw new Error('Unsupported integer data type.');
  74. return srcType;
  75. }
  76. export function isSignedIntegerDataType(data: TypedIntArray) {
  77. return data instanceof Int8Array || data instanceof Int16Array || data instanceof Int32Array;
  78. }
  79. // type[] -> Uint8[]
  80. export interface ByteArray {
  81. kind: 'ByteArray',
  82. type: DataType
  83. }
  84. // (Float32 | Float64)[] -> Int32[]
  85. export interface FixedPoint {
  86. kind: 'FixedPoint',
  87. factor: number,
  88. srcType: FloatDataType
  89. }
  90. // (Float32|Float64)[] -> Int32
  91. export interface IntervalQuantization {
  92. kind: 'IntervalQuantization',
  93. min: number,
  94. max: number,
  95. numSteps: number,
  96. srcType: FloatDataType
  97. }
  98. // (Uint8 | Int8 | Int16 | Int32)[] -> Int32[]
  99. export interface RunLength {
  100. kind: 'RunLength',
  101. srcType: IntDataType,
  102. srcSize: number
  103. }
  104. // T=(Int8Array | Int16Array | Int32Array)[] -> T[]
  105. export interface Delta {
  106. kind: 'Delta',
  107. origin: number,
  108. srcType: IntDataType
  109. }
  110. // Int32[] -> (Int8 | Int16 | Uint8 | Uint16)[]
  111. export interface IntegerPacking {
  112. kind: 'IntegerPacking',
  113. byteCount: number,
  114. isUnsigned: boolean,
  115. srcSize: number
  116. }
  117. // string[] -> Uint8[]
  118. // stores 0 and indices of ends of strings:
  119. // stringData = '123456'
  120. // offsets = [0,2,5,6]
  121. // encodes ['12','345','6']
  122. export interface StringArray {
  123. kind: 'StringArray',
  124. dataEncoding: Encoding[],
  125. stringData: string,
  126. offsetEncoding: Encoding[],
  127. offsets: Uint8Array
  128. }
  129. }