-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.js
More file actions
193 lines (159 loc) · 5.7 KB
/
Copy pathdata.js
File metadata and controls
193 lines (159 loc) · 5.7 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
const _dbVersion = 3;
function indexField(data, field) {
let count = 0;
const valueToId = new Map();
for (const item of data) {
const value = item[field];
let id = valueToId.get(value);
if (id == undefined) {
id = count++;
valueToId.set(value, id);
}
item[field] = id;
}
return [...valueToId.keys()];
}
function packModel(json) {
const sols = json.filter(sol => sol.scoring === "bytes");
for (const sol of sols) {
sol.submitted = Date.parse(sol.submitted) / 60000 | 0; // minutes since the Unix epoch
}
sols.sort((a, b) => a.submitted - b.submitted);
const langsNames = indexField(sols, "lang");
const holesNames = indexField(sols, "hole");
const golfersNames = indexField(sols, "login");
const langsCount = langsNames.length;
const bests = Array(langsCount * holesNames.length).fill(Infinity);
for (const sol of sols) {
const key = sol.hole * langsCount + sol.lang;
if (bests[key] > sol.bytes)
bests[key] = sol.bytes;
}
const writer = new Writer();
writer.writeLong(_dbVersion, 0);
const golfersCache = [];
let prevTime = 0;
const infos = [];
const langsWriter = DynamicHuffmanWriter.fromList(project(sols, "lang"));
const holesWriter = DynamicHuffmanWriter.fromList(project(sols, "hole"));
const golfersWriter = new StringHuffmanWriter(writer, golfersNames);
writer.writeLong(sols.length, 16);
for (const sol of sols) {
// info
const g = sol.login;
let info = infos[g];
let newGolfer = info == undefined;
if (newGolfer) {
infos.push(info = [-1, -1]);
}
// golfer
const id = newGolfer || golfersCache.length - 1 - golfersCache.lastIndexOf(g);
writer.write(id != 0, 1);
if (id != 0) {
writer.writeLong(newGolfer ? 0 : id, 3);
if (newGolfer)
golfersWriter.write(writer, golfersNames[g]);
else
golfersCache.splice(golfersCache.length - 1 - id, 1);
golfersCache.push(g);
}
// lang
const newLang = info[0] != sol.lang;
if (!newGolfer)
writer.write(newLang, 1);
if (newLang) {
langsWriter.write(writer, info[0] = sol.lang);
}
// hole
const newHole = info[1] != sol.hole;
if (!newGolfer && newLang)
writer.write(newHole, 1);
if (newHole) {
holesWriter.write(writer, info[1] = sol.hole);
}
// size
writer.writeLong(sol.bytes - bests[sol.hole * langsCount + sol.lang], 5);
// timeDiff
const curTime = sol.submitted;
writer.writeLong(curTime - prevTime, 3);
prevTime = curTime;
}
for (const best of bests) {
if (best != Infinity)
writer.writeLong(best - 1, 7);
}
const stringWriter = new StringHuffmanWriter(writer, langsNames, holesNames);
for (const lang of langsNames) {
stringWriter.write(writer, lang);
}
for (const hole of holesNames) {
stringWriter.write(writer, hole);
}
return writer.toString();
}
function unpackModel(s) {
const reader = new Reader(s);
const dbVersion = reader.readLong(0);
if (dbVersion != _dbVersion) {
console.log("Found version " + dbVersion + ", expected version " + _dbVersion);
return null;
}
const holesReader = new DynamicHuffmanReader();
const langsReader = new DynamicHuffmanReader();
const golfersReader = new StringHuffmanReader(reader);
const golfers = [];
const golfersCache = [];
let submitted = 0;
const infos = [];
const solutions = [];
for (let solsCount = reader.readLong(16); solsCount > 0; solsCount--) {
// golfer
const otherGolfer = reader.read(1);
let golferId;
if (!otherGolfer) {
golferId = golfersCache[golfersCache.length - 1];
} else {
const id = reader.readLong(3);
if (id == 0) {
golferId = golfers.length;
golfers.push(golfersReader.read(reader));
infos.push([-1, -1]);
} else {
golferId = golfersCache.splice(golfersCache.length - 1 - id, 1)[0];
}
golfersCache.push(golferId);
}
// info
const info = infos[golferId];
const newGolfer = info[0] == -1;
// lang
const newLang = newGolfer || reader.read(1);
const lang = newLang ? info[0] = langsReader.read(reader) : info[0];
// hole
const newHole = newGolfer || !newLang || reader.read(1);
const hole = newHole ? info[1] = holesReader.read(reader) : info[1];
// size
const size = reader.readLong(5);
// timeDiff
submitted += reader.readLong(3);
if (solutions.length == hole)
solutions.push((solutions[0] || []).map(_ => []));
if (solutions[0].length == lang)
solutions.forEach(solutions_h => solutions_h.push([]));
solutions[hole][lang].push([golferId, size, submitted]);
}
for (const solutions_h of solutions) {
for (const solutions_hl of solutions_h) {
if (solutions_hl.length == 0)
continue;
const best = reader.readLong(7) + 1;
for (const solution of solutions_hl) {
solution[1] += best;
}
}
}
const stringReader = new StringHuffmanReader(reader);
const langs = solutions[0].map(_ => stringReader.read(reader));
const holes = solutions.map(_ => stringReader.read(reader));
return new Model(solutions, holes, langs, golfers, submitted);
}