-
Notifications
You must be signed in to change notification settings - Fork 0
/
NumberPool.js
395 lines (346 loc) · 14.3 KB
/
NumberPool.js
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*jslint indent: 4, node: true, nomen: true */
/*global */
//
// createNumberPool
//
// Create a NumberPool object that manages a pool of numbers in a range through the methods allocate and release.
// The NumberPool object represents the pool of numbers ranging from one to ten million (1 to 10,000,000), unless
// you pass a custom size for testing.
//
exports.createNumberPool = function (size) {
"use strict";
// We trust that the user will not pass a value for size, since we don't document
// one outside of this source file. If that changes we should validate that the
// size passed in is a number and it's greater than 1.
var assert = require('assert'),
_max = size || 10000000,
_pool = [{begin: 1, end: _max}],
//
// _validRangeTest
//
// Throws an expection if the given range is not valid.
//
_validRangeTest = function (range) {
if (typeof range !== 'object') { throw new Error("range is not an object"); }
if (typeof range.begin !== 'number') { throw new Error("range.begin is not a number"); }
if (typeof range.end !== 'number') { throw new Error("range.end is not a number"); }
if (range.begin < 1 || range.begin > range.end) { throw new Error("range is inverted or not valid"); }
if (range.begin !== Math.floor(range.begin)) { throw new Error("range.begin is not an integer"); }
if (range.end !== Math.floor(range.end)) { throw new Error("range.end is not an integer"); }
},
//
// _promoteToRange
//
// Helper that keeps other functions simple.
//
_promoteToRange = function (value) {
if (typeof value === 'number') {
value = {
begin: value,
end: value
};
}
_validRangeTest(value);
return value;
},
_promoteToRangeTests = function () {
console.log("_promoteToRangeTests");
assert.doesNotThrow(function () {
_promoteToRange(1);
});
assert.throws(function () {
_promoteToRange(false);
}, Error);
assert.throws(function () {
_promoteToRange(/reg/);
}, Error);
assert.throws(function () {
_promoteToRange(23.45);
}, Error);
assert.throws(function () {
_promoteToRange({});
}, Error);
assert.throws(function () {
_promoteToRange({begin: true});
}, Error);
assert.throws(function () {
_promoteToRange({begin: 0, end: false});
}, Error);
assert.throws(function () {
_promoteToRange({begin: 0.5, end: 10});
}, Error);
assert.throws(function () {
_promoteToRange({begin: 1, end: 10.5});
}, Error);
assert.throws(function () {
_promoteToRange({begin: 0, end: 10});
}, Error);
assert.throws(function () {
_promoteToRange({begin: 50, end: 10});
}, Error);
assert.doesNotThrow(function () {
_promoteToRange({begin: 5, end: 10});
});
assert.doesNotThrow(function () {
_promoteToRange({begin: 1, end: 10});
});
assert.doesNotThrow(function () {
_promoteToRange({begin: 10, end: 10});
});
},
//
// _findBracket
//
// Returns a bracket, where left is the index to the left of where
// value belongs in the pool, and right is the index to the right
// of where value belongs in the pool. If the value is in the pool,
// the left and right will be equal. If the value belongs at the
// beginning of the pool, left will be -1 and right will be 0. If
// the value belongs at the end of the pool, then left will be the
// last index in the pool and right will be -1.
//
// Loosely borrowed from the binary search algorithm found here.
// http://www.anujgakhar.com/2014/03/01/binary-search-in-javascript/
//
_findBracket = function (value) {
var first = 0,
last = _pool.length - 1,
result = {
left: first,
right: last
},
mid,
candidate;
if (last === -1) { throw new Error("Don't call _findBracket with an empty pool"); }
while (result.left <= result.right) {
// Shifting is faster than dividing by two.
// http://jsperf.com/code-review-1480
mid = (result.left + result.right) >> 1;
candidate = _promoteToRange(_pool[mid]);
if (candidate.end < value) {
result.left = mid + 1;
} else if (candidate.begin > value) {
result.right = mid - 1;
} else {
result.left = mid;
result.right = mid;
return result;
}
}
if (result.left <= last && value < _promoteToRange(_pool[result.left]).begin) {
result.right = result.left;
result.left = result.right - 1;
return result;
}
if (result.right >= first && value > _promoteToRange(_pool[result.right]).end) {
result.left = result.right;
result.right = result.left + 1;
if (result.right > last) {
result.right = -1;
}
return result;
}
throw new Error("Failed to find a bracket\n" +
"value = " + value + "\n" +
"_pool = " + _pool + "\n" +
"result.left = " + result.left + "\n" +
"result.right = " + result.right + "\n");
},
_findBracketTests = function () {
var oldPool = _pool;
_pool = [];
console.log("_findBracketTests");
assert.throws(function () {
_findBracket(1);
}, Error);
_pool = [10];
assert.doesNotThrow(function () {
assert.deepEqual(_findBracket(1), {left: -1, right: 0});
assert.deepEqual(_findBracket(20), {left: 0, right: -1});
assert.deepEqual(_findBracket(10), {left: 0, right: 0});
});
_pool = [10, 20];
assert.doesNotThrow(function () {
assert.deepEqual(_findBracket(5), {left: -1, right: 0});
assert.deepEqual(_findBracket(10), {left: 0, right: 0});
assert.deepEqual(_findBracket(15), {left: 0, right: 1});
assert.deepEqual(_findBracket(20), {left: 1, right: 1});
assert.deepEqual(_findBracket(25), {left: 1, right: -1});
});
_pool = [10, 20, 30];
assert.doesNotThrow(function () {
assert.deepEqual(_findBracket(5), {left: -1, right: 0});
assert.deepEqual(_findBracket(10), {left: 0, right: 0});
assert.deepEqual(_findBracket(15), {left: 0, right: 1});
assert.deepEqual(_findBracket(20), {left: 1, right: 1});
assert.deepEqual(_findBracket(25), {left: 1, right: 2});
assert.deepEqual(_findBracket(30), {left: 2, right: 2});
assert.deepEqual(_findBracket(35), {left: 2, right: -1});
});
_pool = [
{begin: 5, end: 10},
{begin: 11, end: 20},
{begin: 21, end: 30},
{begin: 31, end: 40},
{begin: 41, end: 45}
];
assert.doesNotThrow(function () {
assert.deepEqual(_findBracket(1), {left: -1, right: 0});
assert.deepEqual(_findBracket(5), {left: 0, right: 0});
assert.deepEqual(_findBracket(10), {left: 0, right: 0});
assert.deepEqual(_findBracket(11), {left: 1, right: 1});
assert.deepEqual(_findBracket(20), {left: 1, right: 1});
assert.deepEqual(_findBracket(21), {left: 2, right: 2});
assert.deepEqual(_findBracket(30), {left: 2, right: 2});
assert.deepEqual(_findBracket(31), {left: 3, right: 3});
assert.deepEqual(_findBracket(40), {left: 3, right: 3});
assert.deepEqual(_findBracket(41), {left: 4, right: 4});
assert.deepEqual(_findBracket(45), {left: 4, right: 4});
assert.deepEqual(_findBracket(50), {left: 4, right: -1});
});
_pool = [
{begin: 5, end: 9},
{begin: 11, end: 19},
{begin: 21, end: 29},
{begin: 31, end: 39},
{begin: 41, end: 45}
];
assert.doesNotThrow(function () {
assert.deepEqual(_findBracket(1), {left: -1, right: 0});
assert.deepEqual(_findBracket(5), {left: 0, right: 0});
assert.deepEqual(_findBracket(7), {left: 0, right: 0});
assert.deepEqual(_findBracket(9), {left: 0, right: 0});
assert.deepEqual(_findBracket(10), {left: 0, right: 1});
assert.deepEqual(_findBracket(11), {left: 1, right: 1});
assert.deepEqual(_findBracket(19), {left: 1, right: 1});
assert.deepEqual(_findBracket(20), {left: 1, right: 2});
assert.deepEqual(_findBracket(21), {left: 2, right: 2});
assert.deepEqual(_findBracket(29), {left: 2, right: 2});
assert.deepEqual(_findBracket(30), {left: 2, right: 3});
assert.deepEqual(_findBracket(31), {left: 3, right: 3});
assert.deepEqual(_findBracket(39), {left: 3, right: 3});
assert.deepEqual(_findBracket(40), {left: 3, right: 4});
assert.deepEqual(_findBracket(41), {left: 4, right: 4});
assert.deepEqual(_findBracket(45), {left: 4, right: 4});
assert.deepEqual(_findBracket(50), {left: 4, right: -1});
});
_pool = oldPool;
},
//
// _mergeEnd
//
// Attempts to merge the end of the range at the
// given index with the number provided.
// Returns true if successful, otherwise false.
//
_mergeEnd = function (number, index) {
var range = _promoteToRange(_pool[index]);
if (range.end + 1 === number) {
range.end = number;
_pool[index] = range;
return true;
}
return false;
},
_mergeEndTests = function () {
var oldPool = _pool;
_pool = [10];
assert.doesNotThrow(function () {
assert.stricitEqual(_mergeEnd(1, 0))
assert.deepEqual(_findBracket(1), {left: -1, right: 0});
assert.deepEqual(_findBracket(20), {left: 0, right: -1});
assert.deepEqual(_findBracket(10), {left: 0, right: 0});
});
_pool = oldPool;
},
//
// _mergeBegin
//
// Attempts to merge the beginning of the range at
// the given index with the number provided.
// Returns true if successful, otherwise false.
//
_mergeBegin = function (number, index) {
var range = _promoteToRange(_pool[index]);
if (number + 1 === range.begin) {
range.begin = number;
_pool[index] = range;
return true;
}
return false;
};
return {
//
// allocate
//
// Returns a number from the pool of available numbers.
// If no more numbers are available, returns 0.
//
// Design:
//
// We always feed from the back to minimize array copies.
//
allocate: function () {
var lastIndex = _pool.length - 1,
result = 0;
if (_pool.length === 0) {
// If the pool is empty then
// all numbers have been allocated.
return result;
}
if (typeof _pool[lastIndex] === 'number') {
result = _pool.pop();
} else {
result = _pool[lastIndex].end;
_pool[lastIndex].end -= 1;
// Demote ranges of one number to single numbers to save space.
if (_pool[lastIndex].end === _pool[lastIndex].begin) {
_pool[lastIndex] = _pool[lastIndex].begin;
}
}
return result;
},
//
// release
//
// Adds an available number value back to the pool.
// If the value is successfully added back to the pool,
// returns true. If the value is already in the pool,
// returns false.
//
release: function (number) {
var bracket, rangeLeft, rangeRight;
if (number < 0 || number > _max) {
return false;
}
bracket = _findBracket(number);
if (bracket.left === bracket.right) {
return false;
}
if (bracket.left === -1) {
if (!_mergeBegin(number, bracket.right)) {
_pool.splice(0, 0, number);
}
return true;
}
if (bracket.right === -1) {
if (!_mergeEnd(number, bracket.left)) {
_pool.push(number);
}
return true;
}
if (_mergeEnd(number, bracket.left) && _mergeBegin(number, bracket.right)) {
_pool[bracket.left].end = _pool[bracket.right].end;
_pool.splice(bracket.right, 1);
}
return true;
},
internalTests: function () {
_promoteToRangeTests();
_findBracketTests();
},
dumpPool: function () {
console.log(_pool);
}
};
};