Skip to content

Commit 4881011

Browse files
committed
Added check function and fixed up boolean status from null changed values
1 parent 5a58d93 commit 4881011

12 files changed

+784
-34
lines changed

dist/Counter-browser.js

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,27 @@ function setupBitMapConstructors(blockSize) {
11421142
callback(null, null);
11431143
}
11441144
}
1145+
1146+
/**
1147+
* Checks if the counter has been set
1148+
* @param {number} counter
1149+
* @param {function} callback
1150+
*/
1151+
1152+
}, {
1153+
key: 'check',
1154+
value: function check(counter, callback) {
1155+
var index = counter - this.begin;
1156+
if (index >= 0 && index < blockSize) {
1157+
if (isSet(this.bitMap, index)) {
1158+
callback(true);
1159+
} else {
1160+
callback(false);
1161+
}
1162+
} else {
1163+
callback(null);
1164+
}
1165+
}
11451166
}]);
11461167

11471168
return Leaf;
@@ -1279,6 +1300,25 @@ function setupBitMapConstructors(blockSize) {
12791300
callback(null, null);
12801301
}
12811302
}
1303+
1304+
/**
1305+
* Checks if the counter has been set
1306+
* @param {number} counter
1307+
* @param {function} callback
1308+
*/
1309+
1310+
}, {
1311+
key: 'check',
1312+
value: function check(counter, callback) {
1313+
var index = Math.floor((counter - this.begin) / Math.pow(blockSize, this.depth));
1314+
if (this.bitMapTrees[index]) {
1315+
this.bitMapTrees[index].check(counter, function (set) {
1316+
callback(set);
1317+
});
1318+
} else {
1319+
callback(null);
1320+
}
1321+
}
12821322
}]);
12831323

12841324
return Node;
@@ -1325,7 +1365,7 @@ var Counter = function () {
13251365
* Allocates a counter sequentially
13261366
* If a counter is specified, it will allocate it explicitly
13271367
* But it will skip over intermediate children, and subsequent allocations is still sequential
1328-
* @param {number} [counter]
1368+
* @param {?number} counter
13291369
* @returns {number|boolean}
13301370
* @throws {RangeError} - Will throw if the explicitly allocated counter is out of bounds
13311371
*/
@@ -1344,6 +1384,7 @@ var Counter = function () {
13441384
}
13451385
this._bitMapTree.allocate(index, function (index_, bitMap, changed_) {
13461386
index = index_;
1387+
// this can be null if the index checked is outside of the tree
13471388
changed = changed_;
13481389
});
13491390
if (index !== null) {
@@ -1368,9 +1409,29 @@ var Counter = function () {
13681409
value: function deallocate(counter) {
13691410
var changed = void 0;
13701411
this._bitMapTree.deallocate(counter - this._begin, function (bitMap, changed_) {
1412+
// this can be null if the index checked is outside of the tree
13711413
changed = changed_;
13721414
});
1373-
return changed;
1415+
// an index outside of the tree is assumed to be unallocated
1416+
return !!changed;
1417+
}
1418+
1419+
/**
1420+
* Checks if a number has been allocated or not
1421+
* @param {number} counter
1422+
* @returns {boolean}
1423+
*/
1424+
1425+
}, {
1426+
key: 'check',
1427+
value: function check(counter) {
1428+
var set = void 0;
1429+
this._bitMapTree.check(counter - this._begin, function (set_) {
1430+
// this can be null if the index checked is outside of the tree
1431+
set = set_;
1432+
});
1433+
// an index outside of the tree is assumed to be unallocated
1434+
return !!set;
13741435
}
13751436
}]);
13761437

dist/Counter.cjs.js

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,24 @@ function setupBitMapConstructors(blockSize) {
173173
}
174174
}
175175

176+
/**
177+
* Checks if the counter has been set
178+
* @param {number} counter
179+
* @param {function} callback
180+
*/
181+
check(counter, callback) {
182+
const index = counter - this.begin;
183+
if (index >= 0 && index < blockSize) {
184+
if (isSet(this.bitMap, index)) {
185+
callback(true);
186+
} else {
187+
callback(false);
188+
}
189+
} else {
190+
callback(null);
191+
}
192+
}
193+
176194
}
177195

178196
/**
@@ -283,6 +301,22 @@ function setupBitMapConstructors(blockSize) {
283301
}
284302
}
285303

304+
/**
305+
* Checks if the counter has been set
306+
* @param {number} counter
307+
* @param {function} callback
308+
*/
309+
check(counter, callback) {
310+
const index = Math.floor((counter - this.begin) / Math.pow(blockSize, this.depth));
311+
if (this.bitMapTrees[index]) {
312+
this.bitMapTrees[index].check(counter, set => {
313+
callback(set);
314+
});
315+
} else {
316+
callback(null);
317+
}
318+
}
319+
286320
}
287321

288322
return {
@@ -321,7 +355,7 @@ class Counter {
321355
* Allocates a counter sequentially
322356
* If a counter is specified, it will allocate it explicitly
323357
* But it will skip over intermediate children, and subsequent allocations is still sequential
324-
* @param {number} [counter]
358+
* @param {?number} counter
325359
* @returns {number|boolean}
326360
* @throws {RangeError} - Will throw if the explicitly allocated counter is out of bounds
327361
*/
@@ -336,6 +370,7 @@ class Counter {
336370
}
337371
this._bitMapTree.allocate(index, (index_, bitMap, changed_) => {
338372
index = index_;
373+
// this can be null if the index checked is outside of the tree
339374
changed = changed_;
340375
});
341376
if (index !== null) {
@@ -357,9 +392,26 @@ class Counter {
357392
deallocate(counter) {
358393
let changed;
359394
this._bitMapTree.deallocate(counter - this._begin, (bitMap, changed_) => {
395+
// this can be null if the index checked is outside of the tree
360396
changed = changed_;
361397
});
362-
return changed;
398+
// an index outside of the tree is assumed to be unallocated
399+
return !!changed;
400+
}
401+
402+
/**
403+
* Checks if a number has been allocated or not
404+
* @param {number} counter
405+
* @returns {boolean}
406+
*/
407+
check(counter) {
408+
let set;
409+
this._bitMapTree.check(counter - this._begin, set_ => {
410+
// this can be null if the index checked is outside of the tree
411+
set = set_;
412+
});
413+
// an index outside of the tree is assumed to be unallocated
414+
return !!set;
363415
}
364416

365417
}

dist/Counter.es.js

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,24 @@ function setupBitMapConstructors(blockSize) {
169169
}
170170
}
171171

172+
/**
173+
* Checks if the counter has been set
174+
* @param {number} counter
175+
* @param {function} callback
176+
*/
177+
check(counter, callback) {
178+
const index = counter - this.begin;
179+
if (index >= 0 && index < blockSize) {
180+
if (isSet(this.bitMap, index)) {
181+
callback(true);
182+
} else {
183+
callback(false);
184+
}
185+
} else {
186+
callback(null);
187+
}
188+
}
189+
172190
}
173191

174192
/**
@@ -279,6 +297,22 @@ function setupBitMapConstructors(blockSize) {
279297
}
280298
}
281299

300+
/**
301+
* Checks if the counter has been set
302+
* @param {number} counter
303+
* @param {function} callback
304+
*/
305+
check(counter, callback) {
306+
const index = Math.floor((counter - this.begin) / Math.pow(blockSize, this.depth));
307+
if (this.bitMapTrees[index]) {
308+
this.bitMapTrees[index].check(counter, set => {
309+
callback(set);
310+
});
311+
} else {
312+
callback(null);
313+
}
314+
}
315+
282316
}
283317

284318
return {
@@ -317,7 +351,7 @@ class Counter {
317351
* Allocates a counter sequentially
318352
* If a counter is specified, it will allocate it explicitly
319353
* But it will skip over intermediate children, and subsequent allocations is still sequential
320-
* @param {number} [counter]
354+
* @param {?number} counter
321355
* @returns {number|boolean}
322356
* @throws {RangeError} - Will throw if the explicitly allocated counter is out of bounds
323357
*/
@@ -332,6 +366,7 @@ class Counter {
332366
}
333367
this._bitMapTree.allocate(index, (index_, bitMap, changed_) => {
334368
index = index_;
369+
// this can be null if the index checked is outside of the tree
335370
changed = changed_;
336371
});
337372
if (index !== null) {
@@ -353,9 +388,26 @@ class Counter {
353388
deallocate(counter) {
354389
let changed;
355390
this._bitMapTree.deallocate(counter - this._begin, (bitMap, changed_) => {
391+
// this can be null if the index checked is outside of the tree
356392
changed = changed_;
357393
});
358-
return changed;
394+
// an index outside of the tree is assumed to be unallocated
395+
return !!changed;
396+
}
397+
398+
/**
399+
* Checks if a number has been allocated or not
400+
* @param {number} counter
401+
* @returns {boolean}
402+
*/
403+
check(counter) {
404+
let set;
405+
this._bitMapTree.check(counter - this._begin, set_ => {
406+
// this can be null if the index checked is outside of the tree
407+
set = set_;
408+
});
409+
// an index outside of the tree is assumed to be unallocated
410+
return !!set;
359411
}
360412

361413
}

0 commit comments

Comments
 (0)