number-allocator
Advanced tools
Comparing version 1.0.4 to 1.0.5
@@ -0,1 +1,5 @@ | ||
## 1.0.5 | ||
- Added debug logs. | ||
- Improved free() with vacant value behavior. | ||
## 1.0.4 | ||
@@ -2,0 +6,0 @@ - Fixed module export point again. `module.exports.NumberAllocator = NumberAllocator` |
@@ -8,3 +8,4 @@ // Copyright Takatoshi Kondo 2021 | ||
const SortedSet = require('collections/sorted-set') | ||
const debugTrace = require('debug')('number-allocator:trace') | ||
const debugError = require('debug')('number-allocator:error') | ||
/** | ||
@@ -47,2 +48,3 @@ * Interval constructor | ||
this.ss = new SortedSet() | ||
debugTrace('Create') | ||
this.clear() | ||
@@ -68,3 +70,6 @@ } | ||
NumberAllocator.prototype.alloc = function () { | ||
if (this.ss.length === 0) return null | ||
if (this.ss.length === 0) { | ||
debugTrace('alloc():empty') | ||
return null | ||
} | ||
const it = this.ss.min() | ||
@@ -80,2 +85,3 @@ const num = it.low | ||
} | ||
debugTrace('alloc():' + num) | ||
return num | ||
@@ -98,2 +104,3 @@ } | ||
this.ss.delete(it.value) | ||
debugTrace('use():' + num) | ||
return true | ||
@@ -111,2 +118,3 @@ } | ||
++it.value.low | ||
debugTrace('use():' + num) | ||
return true | ||
@@ -121,2 +129,3 @@ } | ||
--it.value.high | ||
debugTrace('use():' + num) | ||
return true | ||
@@ -135,4 +144,7 @@ } | ||
this.ss.push(new Interval(low, num - 1)) | ||
debugTrace('use():' + num) | ||
return true | ||
} | ||
debugTrace('use():failed') | ||
return false | ||
@@ -148,5 +160,13 @@ } | ||
NumberAllocator.prototype.free = function (num) { | ||
if (num < this.min || num > this.max) { | ||
debugError('free():' + num + ' is out of range') | ||
return | ||
} | ||
const key = new Interval(num, num) | ||
const it = this.ss.findLeastGreaterThanOrEqual(key) | ||
if (it) { | ||
if (it.value.low <= num && num <= it.value.high) { | ||
debugError('free():' + num + ' has already been vacant') | ||
return | ||
} | ||
if (it === this.ss.findLeast()) { | ||
@@ -209,2 +229,3 @@ // v.... | ||
} | ||
debugTrace('free():' + num) | ||
} | ||
@@ -218,2 +239,3 @@ | ||
NumberAllocator.prototype.clear = function () { | ||
debugTrace('clear()') | ||
this.ss.clear() | ||
@@ -220,0 +242,0 @@ this.ss.push(new Interval(this.min, this.max)) |
{ | ||
"name": "number-allocator", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"description": "A library for the unique number allocator", | ||
@@ -33,3 +33,4 @@ "main": "index.js", | ||
"dependencies": { | ||
"collections": "^5.1.12" | ||
"collections": "^5.1.12", | ||
"debug": "^4.3.1" | ||
}, | ||
@@ -36,0 +37,0 @@ "devDependencies": { |
# Unique number allocator for JavaScript. | ||
Version 1.0.4 [![number-allocator CI](https://github.com/redboltz/number-allocator/workflows/number-allocator%20CI/badge.svg)](https://github.com/redboltz/number-allocator/actions) [![codecov](https://codecov.io/gh/redboltz/number-allocator/branch/master/graph/badge.svg)](https://codecov.io/gh/redboltz/number-allocator) | ||
Version 1.0.5 [![number-allocator CI](https://github.com/redboltz/number-allocator/workflows/number-allocator%20CI/badge.svg)](https://github.com/redboltz/number-allocator/actions) [![codecov](https://codecov.io/gh/redboltz/number-allocator/branch/master/graph/badge.svg)](https://codecov.io/gh/redboltz/number-allocator) | ||
@@ -5,0 +5,0 @@ ## How to use |
@@ -37,2 +37,3 @@ // Copyright Takatoshi Kondo 2021 | ||
assert.equal(a.use(1), false) | ||
assert.equal(a.use(-1), false) | ||
a.free(0) | ||
@@ -282,2 +283,10 @@ assert.equal(a.intervalCount(), 1) | ||
}) | ||
it('should do nothing non allocated free', function (done) { | ||
const a = NumberAllocator(0, 1) | ||
// if DEBUG="nuber-allocator:error" then output error log | ||
a.free(0) | ||
a.free(1) | ||
a.free(5) | ||
done() | ||
}) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
28274
617
2
+ Addeddebug@^4.3.1
+ Addeddebug@4.3.7(transitive)
+ Addedms@2.1.3(transitive)