Spaces:
Sleeping
Sleeping
File size: 747 Bytes
b593f0b | 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 | // @flow strict
import assert from 'assert';
class DictionaryCoder {
_stringToNumber: {[_: string]: number };
_numberToString: Array<string>;
constructor(strings: Array<string>) {
this._stringToNumber = {};
this._numberToString = [];
for (let i = 0; i < strings.length; i++) {
const string = strings[i];
this._stringToNumber[string] = i;
this._numberToString[i] = string;
}
}
encode(string: string) {
assert(string in this._stringToNumber);
return this._stringToNumber[string];
}
decode(n: number) {
assert(n < this._numberToString.length);
return this._numberToString[n];
}
}
export default DictionaryCoder;
|