Skip to content

Commit b6a555e

Browse files
committed
Added boolean return status to allocate and deallocate to indicate change
1 parent d2f151b commit b6a555e

13 files changed

+286
-485
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ let first = c.allocate();
1616
let second = c.allocate();
1717
let third = c.allocate();
1818
let fourth = c.allocate();
19-
c.deallocate(second);
19+
c.deallocate(second); // returns boolean indicating whether second was previously allocated
2020
c.deallocate(third);
2121
console.log(c.allocate() === second);
2222
console.log(c.allocate() === third);
2323
console.log(c.allocate() === (fourth + 1));
2424
// you can also explicitly set a specific number
2525
// and all subsequent allocations are still sequential
26-
c.allocate(100);
26+
c.allocate(100); // returns boolean indicating whether 100 was previously unallocated
2727
```
2828

2929
Documentation

dist/Counter-browser.js

Lines changed: 56 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,6 +1038,16 @@ function setupBitMapConstructors(blockSize) {
10381038
return first;
10391039
};
10401040

1041+
/**
1042+
* Checks if a bit is set.
1043+
* @param {BitSet} bitMap
1044+
* @param {number} i
1045+
* @returns {boolean}
1046+
*/
1047+
var isSet = function isSet(bitMap, i) {
1048+
return !bitMap.get(i);
1049+
};
1050+
10411051
/**
10421052
* Class representing a lazy recursive bitmap tree
10431053
* Only the leaf bitmaps correspond to counters
@@ -1046,48 +1056,21 @@ function setupBitMapConstructors(blockSize) {
10461056
* If an interior bit is not set, that means there's at least 1 free bit in the child bitmap
10471057
*/
10481058

1049-
var BitMapTree = function () {
1050-
1051-
/**
1052-
* Creates a BitMapTree, this is an abstract class
1053-
* It is not meant to by directly instantiated
1054-
* @param {number} begin
1055-
* @param {number} depth
1056-
*/
1057-
function BitMapTree(begin, depth) {
1058-
_classCallCheck(this, BitMapTree);
1059-
1060-
this.begin = begin;
1061-
this.depth = depth;
1062-
this.bitMap = createBitMap();
1063-
}
1064-
1065-
/**
1066-
* Sets a bit to allocated
1067-
* @param {number} index
1068-
*/
1069-
1070-
1071-
_createClass(BitMapTree, [{
1072-
key: 'set',
1073-
value: function set(index) {
1074-
setBit(this.bitMap, index);
1075-
}
1076-
1077-
/**
1078-
* Unsets a bit so that is free
1079-
* @param {number} index
1080-
*/
1059+
var BitMapTree =
10811060

1082-
}, {
1083-
key: 'unset',
1084-
value: function unset(index) {
1085-
unsetBit(this.bitMap, index);
1086-
}
1087-
}]);
1061+
/**
1062+
* Creates a BitMapTree, this is an abstract class
1063+
* It is not meant to by directly instantiated
1064+
* @param {number} begin
1065+
* @param {number} depth
1066+
*/
1067+
function BitMapTree(begin, depth) {
1068+
_classCallCheck(this, BitMapTree);
10881069

1089-
return BitMapTree;
1090-
}();
1070+
this.begin = begin;
1071+
this.depth = depth;
1072+
this.bitMap = createBitMap();
1073+
};
10911074

10921075

10931076

@@ -1127,10 +1110,14 @@ function setupBitMapConstructors(blockSize) {
11271110
index = counter - this.begin;
11281111
}
11291112
if (index !== null && index >= 0 && index < blockSize) {
1130-
setBit(this.bitMap, index);
1131-
callback(this.begin + index, this.bitMap);
1113+
if (!isSet(this.bitMap, index)) {
1114+
setBit(this.bitMap, index);
1115+
callback(this.begin + index, this.bitMap, true);
1116+
} else {
1117+
callback(this.begin + index, this.bitMap, false);
1118+
}
11321119
} else {
1133-
callback(null, null);
1120+
callback(null, null, null);
11341121
}
11351122
}
11361123

@@ -1145,10 +1132,14 @@ function setupBitMapConstructors(blockSize) {
11451132
value: function deallocate(counter, callback) {
11461133
var index = counter - this.begin;
11471134
if (index >= 0 && index < blockSize) {
1148-
unsetBit(this.bitMap, index);
1149-
callback(this.bitMap);
1135+
if (isSet(this.bitMap, index)) {
1136+
unsetBit(this.bitMap, index);
1137+
callback(this.bitMap, true);
1138+
} else {
1139+
callback(this.bitMap, false);
1140+
}
11501141
} else {
1151-
callback(null);
1142+
callback(null, null);
11521143
}
11531144
}
11541145
}]);
@@ -1230,13 +1221,13 @@ function setupBitMapConstructors(blockSize) {
12301221
index = Math.floor((counter - this.begin) / Math.pow(blockSize, this.depth));
12311222
}
12321223
if (index === null || index < 0 || index >= blockSize) {
1233-
callback(null, null);
1224+
callback(null, null, null);
12341225
} else if (this.bitMapTrees[index]) {
1235-
this.bitMapTrees[index].allocate(counter, function (counter, bitMap) {
1226+
this.bitMapTrees[index].allocate(counter, function (counter, bitMap, changed) {
12361227
if (bitMap && allSet(bitMap)) {
12371228
setBit(_this3.bitMap, index);
12381229
}
1239-
callback(counter, _this3.bitMap);
1230+
callback(counter, _this3.bitMap, changed);
12401231
});
12411232
} else {
12421233
var newBegin = this.begin + index * Math.pow(blockSize, this.depth);
@@ -1248,11 +1239,11 @@ function setupBitMapConstructors(blockSize) {
12481239
child = new Node(newBegin, newDepth);
12491240
}
12501241
this.setChild(index, child);
1251-
child.allocate(counter, function (counter, bitMap) {
1242+
child.allocate(counter, function (counter, bitMap, changed) {
12521243
if (bitMap && allSet(bitMap)) {
12531244
setBit(_this3.bitMap, index);
12541245
}
1255-
callback(counter, _this3.bitMap);
1246+
callback(counter, _this3.bitMap, changed);
12561247
});
12571248
}
12581249
}
@@ -1275,17 +1266,17 @@ function setupBitMapConstructors(blockSize) {
12751266
var index = Math.floor((counter - this.begin) / Math.pow(blockSize, this.depth));
12761267
if (this.bitMapTrees[index]) {
12771268
var allSetPrior = allSet(this.bitMapTrees[index].bitMap);
1278-
this.bitMapTrees[index].deallocate(counter, function (bitMap) {
1269+
this.bitMapTrees[index].deallocate(counter, function (bitMap, changed) {
12791270
if (bitMap && allSetPrior) {
12801271
unsetBit(_this4.bitMap, index);
12811272
}
12821273
if (_this4.bitMapTrees.length - 1 === index && allUnset(bitMap)) {
12831274
_this4.popChild();
12841275
}
1285-
callback(_this4.bitMap);
1276+
callback(_this4.bitMap, changed);
12861277
});
12871278
} else {
1288-
callback(null);
1279+
callback(null, null);
12891280
}
12901281
}
12911282
}]);
@@ -1335,7 +1326,7 @@ var Counter = function () {
13351326
* If a counter is specified, it will allocate it explicitly
13361327
* But it will skip over intermediate children, and subsequent allocations is still sequential
13371328
* @param {number} [counter]
1338-
* @returns {number}
1329+
* @returns {number|boolean}
13391330
* @throws {RangeError} - Will throw if the explicitly allocated counter is out of bounds
13401331
*/
13411332

@@ -1344,16 +1335,19 @@ var Counter = function () {
13441335
key: 'allocate',
13451336
value: function allocate(counter) {
13461337
var index = null;
1338+
var changed = void 0;
13471339
if (counter !== undefined) {
13481340
if (counter < this._begin) {
13491341
throw new RangeError('Counter needs to be greater or equal to the counter beginning offset');
13501342
}
13511343
index = counter - this._begin;
13521344
}
1353-
this._bitMapTree.allocate(index, function (index_, bitMap) {
1345+
this._bitMapTree.allocate(index, function (index_, bitMap, changed_) {
13541346
index = index_;
1347+
changed = changed_;
13551348
});
13561349
if (index !== null) {
1350+
if (counter != null) return changed;
13571351
return index + this._begin;
13581352
} else {
13591353
var newRoot = new this._bitMapConst.Node(this._bitMapTree.begin, this._bitMapTree.depth + 1);
@@ -1366,12 +1360,17 @@ var Counter = function () {
13661360
/**
13671361
* Deallocates a number, it makes it available for reuse
13681362
* @param {number} counter
1363+
* @returns {boolean}
13691364
*/
13701365

13711366
}, {
13721367
key: 'deallocate',
13731368
value: function deallocate(counter) {
1374-
this._bitMapTree.deallocate(counter - this._begin, function () {});
1369+
var changed = void 0;
1370+
this._bitMapTree.deallocate(counter - this._begin, function (bitMap, changed_) {
1371+
changed = changed_;
1372+
});
1373+
return changed;
13751374
}
13761375
}]);
13771376

0 commit comments

Comments
 (0)