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