Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | 2x 2x 2x 7x 7x 7x 7x 8x 8x 8x 8x 28x 66x 28x 28x 38x 61x 19x 15x 15x 8x 8x 20x 20x 8x 7x 7x 32x 7x 15x 42x 8x 25x 8x 8x 8x 8x 8x 8x 8x 8x 8x 17x 17x 17x 17x 8x 8x 8x 8x 31x 31x 31x 8x 8x 33x 33x 33x 7x 25x 9x 7x 7x 4x 4x 9x 4x 3x 3x 3x 12x 3x 16x | import {JsonPackExtension} from '../JsonPackExtension';
const isSafeInteger = Number.isSafeInteger;
export class CompressionTable {
public static create(value: unknown): CompressionTable {
const table = new CompressionTable();
table.walk(value);
table.finalize();
return table;
}
protected integers = new Set<number>();
protected nonIntegers = new Set<unknown>();
protected table: unknown[] = [];
protected map: Map<unknown, number> = new Map();
public addInteger(int: number): void {
this.integers.add(int);
}
public addLiteral(value: number | string | unknown): void {
if (isSafeInteger(value)) {
this.addInteger(value as number);
return;
}
this.nonIntegers.add(value);
}
public walk(value: unknown): void {
switch (typeof value) {
case 'object': {
if (!value) return this.addLiteral(null);
const construct = value.constructor;
switch (construct) {
case Object: {
const obj = value as Record<string, unknown>;
for (const key in obj) {
this.addLiteral(key);
this.walk(obj[key]);
}
break;
}
case Array: {
const arr = value as unknown[];
const len = arr.length;
for (let i = 0; i < len; i++) this.walk(arr[i]);
break;
}
case Map: {
const map = value as Map<unknown, unknown>;
map.forEach((value, key) => {
this.walk(key);
this.walk(value);
});
break;
}
case Set: {
const set = value as Set<unknown>;
set.forEach((value) => {
this.walk(value);
});
break;
}
case JsonPackExtension: {
const ext = value as JsonPackExtension;
this.addInteger(ext.tag);
this.walk(ext.val);
}
}
return;
}
default:
return this.addLiteral(value);
}
}
public finalize(): void {
const integers = Array.from(this.integers);
integers.sort((a, b) => a - b);
const len = integers.length;
const table = this.table;
const map = this.map;
if (len > 0) {
const first = integers[0];
table.push(first);
map.set(first, 0);
let last = first;
for (let i = 1; i < len; i++) {
const int = integers[i];
table.push(int - last);
map.set(int, i);
last = int;
}
}
const nonIntegers = Array.from(this.nonIntegers);
nonIntegers.sort();
const lenNonIntegers = nonIntegers.length;
for (let i = 0; i < lenNonIntegers; i++) {
const value = nonIntegers[i];
table.push(value);
map.set(value, len + i);
}
this.integers.clear();
this.nonIntegers.clear();
}
public getIndex(value: unknown): number {
const index = this.map.get(value);
Iif (index === undefined) throw new Error(`Value [${value}] not found in compression table.`);
return index;
}
public getTable(): unknown[] {
return this.table;
}
public compress(value: unknown): unknown {
switch (typeof value) {
case 'object': {
if (!value) return this.getIndex(null);
const construct = value.constructor;
switch (construct) {
case Object: {
const obj = value as Record<string, unknown>;
const newObj: Record<string, unknown> = {};
for (const key in obj) newObj[this.getIndex(key)] = this.compress(obj[key]);
return newObj;
}
case Array: {
const arr = value as unknown[];
const newArr: unknown[] = [];
const len = arr.length;
for (let i = 0; i < len; i++) newArr.push(this.compress(arr[i]));
return newArr;
}
case Map: {
const map = value as Map<unknown, unknown>;
const newMap = new Map<unknown, unknown>();
map.forEach((value, key) => {
newMap.set(this.compress(key), this.compress(value));
});
return newMap;
}
case Set: {
const set = value as Set<unknown>;
const newSet = new Set<unknown>();
set.forEach((value) => {
newSet.add(this.compress(value));
});
break;
}
case JsonPackExtension: {
const ext = value as JsonPackExtension;
const newExt = new JsonPackExtension(this.getIndex(ext.tag), this.compress(ext.val));
return newExt;
}
}
throw new Error('UNEXPECTED_OBJECT');
}
default: {
return this.getIndex(value);
}
}
}
}
|