@@ -1038,6 +1038,16 @@ function setupBitMapConstructors(blockSize) {
1038
1038
return first ;
1039
1039
} ;
1040
1040
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
+
1041
1051
/**
1042
1052
* Class representing a lazy recursive bitmap tree
1043
1053
* Only the leaf bitmaps correspond to counters
@@ -1046,48 +1056,21 @@ function setupBitMapConstructors(blockSize) {
1046
1056
* If an interior bit is not set, that means there's at least 1 free bit in the child bitmap
1047
1057
*/
1048
1058
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 =
1081
1060
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 ) ;
1088
1069
1089
- return BitMapTree ;
1090
- } ( ) ;
1070
+ this . begin = begin ;
1071
+ this . depth = depth ;
1072
+ this . bitMap = createBitMap ( ) ;
1073
+ } ;
1091
1074
1092
1075
1093
1076
@@ -1127,10 +1110,14 @@ function setupBitMapConstructors(blockSize) {
1127
1110
index = counter - this . begin ;
1128
1111
}
1129
1112
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
+ }
1132
1119
} else {
1133
- callback ( null , null ) ;
1120
+ callback ( null , null , null ) ;
1134
1121
}
1135
1122
}
1136
1123
@@ -1145,10 +1132,14 @@ function setupBitMapConstructors(blockSize) {
1145
1132
value : function deallocate ( counter , callback ) {
1146
1133
var index = counter - this . begin ;
1147
1134
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
+ }
1150
1141
} else {
1151
- callback ( null ) ;
1142
+ callback ( null , null ) ;
1152
1143
}
1153
1144
}
1154
1145
} ] ) ;
@@ -1230,13 +1221,13 @@ function setupBitMapConstructors(blockSize) {
1230
1221
index = Math . floor ( ( counter - this . begin ) / Math . pow ( blockSize , this . depth ) ) ;
1231
1222
}
1232
1223
if ( index === null || index < 0 || index >= blockSize ) {
1233
- callback ( null , null ) ;
1224
+ callback ( null , null , null ) ;
1234
1225
} else if ( this . bitMapTrees [ index ] ) {
1235
- this . bitMapTrees [ index ] . allocate ( counter , function ( counter , bitMap ) {
1226
+ this . bitMapTrees [ index ] . allocate ( counter , function ( counter , bitMap , changed ) {
1236
1227
if ( bitMap && allSet ( bitMap ) ) {
1237
1228
setBit ( _this3 . bitMap , index ) ;
1238
1229
}
1239
- callback ( counter , _this3 . bitMap ) ;
1230
+ callback ( counter , _this3 . bitMap , changed ) ;
1240
1231
} ) ;
1241
1232
} else {
1242
1233
var newBegin = this . begin + index * Math . pow ( blockSize , this . depth ) ;
@@ -1248,11 +1239,11 @@ function setupBitMapConstructors(blockSize) {
1248
1239
child = new Node ( newBegin , newDepth ) ;
1249
1240
}
1250
1241
this . setChild ( index , child ) ;
1251
- child . allocate ( counter , function ( counter , bitMap ) {
1242
+ child . allocate ( counter , function ( counter , bitMap , changed ) {
1252
1243
if ( bitMap && allSet ( bitMap ) ) {
1253
1244
setBit ( _this3 . bitMap , index ) ;
1254
1245
}
1255
- callback ( counter , _this3 . bitMap ) ;
1246
+ callback ( counter , _this3 . bitMap , changed ) ;
1256
1247
} ) ;
1257
1248
}
1258
1249
}
@@ -1275,17 +1266,17 @@ function setupBitMapConstructors(blockSize) {
1275
1266
var index = Math . floor ( ( counter - this . begin ) / Math . pow ( blockSize , this . depth ) ) ;
1276
1267
if ( this . bitMapTrees [ index ] ) {
1277
1268
var allSetPrior = allSet ( this . bitMapTrees [ index ] . bitMap ) ;
1278
- this . bitMapTrees [ index ] . deallocate ( counter , function ( bitMap ) {
1269
+ this . bitMapTrees [ index ] . deallocate ( counter , function ( bitMap , changed ) {
1279
1270
if ( bitMap && allSetPrior ) {
1280
1271
unsetBit ( _this4 . bitMap , index ) ;
1281
1272
}
1282
1273
if ( _this4 . bitMapTrees . length - 1 === index && allUnset ( bitMap ) ) {
1283
1274
_this4 . popChild ( ) ;
1284
1275
}
1285
- callback ( _this4 . bitMap ) ;
1276
+ callback ( _this4 . bitMap , changed ) ;
1286
1277
} ) ;
1287
1278
} else {
1288
- callback ( null ) ;
1279
+ callback ( null , null ) ;
1289
1280
}
1290
1281
}
1291
1282
} ] ) ;
@@ -1335,7 +1326,7 @@ var Counter = function () {
1335
1326
* If a counter is specified, it will allocate it explicitly
1336
1327
* But it will skip over intermediate children, and subsequent allocations is still sequential
1337
1328
* @param {number } [counter]
1338
- * @returns {number }
1329
+ * @returns {number|boolean }
1339
1330
* @throws {RangeError } - Will throw if the explicitly allocated counter is out of bounds
1340
1331
*/
1341
1332
@@ -1344,16 +1335,19 @@ var Counter = function () {
1344
1335
key : 'allocate' ,
1345
1336
value : function allocate ( counter ) {
1346
1337
var index = null ;
1338
+ var changed = void 0 ;
1347
1339
if ( counter !== undefined ) {
1348
1340
if ( counter < this . _begin ) {
1349
1341
throw new RangeError ( 'Counter needs to be greater or equal to the counter beginning offset' ) ;
1350
1342
}
1351
1343
index = counter - this . _begin ;
1352
1344
}
1353
- this . _bitMapTree . allocate ( index , function ( index_ , bitMap ) {
1345
+ this . _bitMapTree . allocate ( index , function ( index_ , bitMap , changed_ ) {
1354
1346
index = index_ ;
1347
+ changed = changed_ ;
1355
1348
} ) ;
1356
1349
if ( index !== null ) {
1350
+ if ( counter != null ) return changed ;
1357
1351
return index + this . _begin ;
1358
1352
} else {
1359
1353
var newRoot = new this . _bitMapConst . Node ( this . _bitMapTree . begin , this . _bitMapTree . depth + 1 ) ;
@@ -1366,12 +1360,17 @@ var Counter = function () {
1366
1360
/**
1367
1361
* Deallocates a number, it makes it available for reuse
1368
1362
* @param {number } counter
1363
+ * @returns {boolean }
1369
1364
*/
1370
1365
1371
1366
} , {
1372
1367
key : 'deallocate' ,
1373
1368
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 ;
1375
1374
}
1376
1375
} ] ) ;
1377
1376
0 commit comments