Tracking de l'application VApp (IHM du jeu)

This commit is contained in:
2025-05-11 18:04:12 +02:00
commit 89e9db9b62
17763 changed files with 3718499 additions and 0 deletions

View File

@ -0,0 +1,33 @@
name: number-allocator CI
on:
pull_request:
types: [opened, synchronize]
push:
branches:
- main
tags:
- '*'
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14.x, 15.x, 16.x, 18.x, 19.x]
fail-fast: false
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm run typescript-test
- run: npm test && npm run codecov
env:
CI: true
DEBUG: "number-allocator*"

47
VApp/node_modules/number-allocator/CHANGELOG.md generated vendored Normal file
View File

@ -0,0 +1,47 @@
## 1.0.14
- Fixed README.md version.
## 1.0.13
- **Important** Fixed invalid free operator.
- Updated js-sdsl.
## 1.0.12
- Updated js-sdsl. updateKeyByIterator() is used.
## 1.0.11
- Updated js-sdsl. Bidirectional iterator is used.
## 1.0.10
- Fixed TypeScript number type
## 1.0.9
- Migrated from collections.js to js-sdsl to remove intrinsic library extention
## 1.0.8
- Updated collections.js to solve https://github.com/montagejs/collections/issues/241
## 1.0.7
- Fixed codecov badge on README.md.
## 1.0.6
- Fixed document.
- Added keywords.
## 1.0.5
- Added debug logs.
- Improved free() with vacant value behavior.
## 1.0.4
- Fixed module export point again. `module.exports.NumberAllocator = NumberAllocator`
## 1.0.3
- Fixed module export point.
## 1.0.2
- Fixed npm version.
## 1.0.1
- Fixed .gitignore.
## 1.0.0
- Initial version.

21
VApp/node_modules/number-allocator/LICENSE generated vendored Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2021 Takatoshi Kondo
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

203
VApp/node_modules/number-allocator/README.md generated vendored Normal file
View File

@ -0,0 +1,203 @@
# Unique number allocator for JavaScript.
Version 1.0.14 [![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/main/graph/badge.svg)](https://codecov.io/gh/redboltz/number-allocator)
## How to use
```js
const NumberAllocator = require('number-allocator').NumberAllocator
// Construct a NumerAllocator that has [0-10] numbers.
// All numbers are vacant.
const a = new NumberAllocator(0, 10)
// Allocate the least vacant number.
const num0 = a.alloc()
console.log(num0) // 0
// Allocate the least vacant number.
const num1 = a.alloc()
console.log(num1) // 1
// Use any vacant number.
const ret1 = a.use(5) // 5 is marked as used(occupied) in the NumberAllocator.
console.log(ret1) // true
// If use the used number, then return false.
const ret2 = a.use(1) // 1 has already been used, then return false
console.log(ret2) // false
// Free the used number.
a.free(1)
// Now 1 becomes vacant again.
const ret3 = a.use(1)
console.log(ret3) // true
// Get the lowest vacant number without marking used.
const num2 = a.firstVacant()
console.log(num2) // 2
// Clear all used mark. Now [0-10] are allocatable again.
a.clear()
```
## Reference
### NumberAllocator(min, max)
Constructor
- min: Number
- The maximum number of allocatable. The number must be integer.
- max: Number
- The minimum number of allocatable. The number must be integer.
The all numbers are set to vacant status.
Time Complexity O(1)
### firstVacant()
Get the first vacant number. The status of the number is not updated.
Time Complexity O(1)
- return: Number
- The first vacant number. If all numbers are occupied, return null.
When alloc() is called then the same value will be allocated.
### alloc()
Allocate the first vacant number. The number become occupied status.
Time Complexity O(1)
- return: Number
- The first vacant number. If all numbers are occupied, return null.
### use(num)
Use the number. The number become occupied status.
If the number has already been occupied, then return false.
Time Complexity O(logN) : N is the number of intervals (not numbers)
- num: Number
- The number to request use.
- return: Boolean
- If `num` was not occupied, then return true, otherwise return false.
### free(num)
Deallocate the number. The number become vacant status.
Time Complexity O(logN) : N is the number of intervals (not numbers)
- num: Number
- The number to deallocate. The number must be occupied status.
In other words, the number must be allocated by alloc() or occupied be use().
### clear()
Clear all occupied numbers.
The all numbers are set to vacant status.
Time Complexity O(1)
### intervalCount()
Get the number of intervals. Interval is internal structure of this library.
This function is for debugging.
Time Complexity O(1)
- return: Number
- The number of intervals.
### dump()
Dump the internal structor of the library.
This function is for debugging.
Time Complexity O(N) : N is the number of intervals (not numbers)
## Internal structure
NumberAllocator has a sorted-set of Interval.
Interval has `low` and `high` property.
I use `[low-high]` notation to describe Interval.
When NumberAllocator is constructed, it has only one Interval(min, max).
Let's say `new NumberAllocator(1, 9)` then the internal structure become as follows:
```
[1-------9]
```
When `alloc()` is called, the first Interval.low is returned.
And then the interval is shrinked.
```
alloc()
return 1
[2------9]
```
When `use(5)` is called, the interval is separated to the two intervals.
```
use(5)
[2-4] [6--9]
```
When `alloc()` is called again, the first Interval.low is returned.
And then the interval is shrinked.
```
alloc()
return 2
[3-4] [6--9]
```
When `free(1)` is called. the interval is inseted.
```
free(1)
[1] [3-4] [6--9]
```
When `free(2)` is called. the interval is inseted.
And check the left and right intervals. If they are continuours, then concatinate to them.
```
free(1)
[1][2][3-4] [6--9]
[1-------4] [6--9]
```
When `clear()` is called, then reset the interval as follows:
```
[1-------9]
```
When `intervalCount()` is called, then the number of intervals is retuned.
In the following case, return 3.
```
intervalCount()
return 3
[1] [3-4] [6--9]
```
Interval management (insertion/concatination/shrinking) is using efficient way.
Insert/Delete operation to sorted-set is minimized.
Some of operations requires O(logN) time complexity. N is number of intervals.
If the maximum count of allocatable values is M, N is at most floor((M + 1) / 2),
In this example, M is 9 so N is at most 5 as follows:
```
[1] [3] [5] [7] [9]
```

7
VApp/node_modules/number-allocator/index.js generated vendored Normal file
View File

@ -0,0 +1,7 @@
// Copyright Takatoshi Kondo 2021
//
// Distributed under the MIT License
const NumberAllocator = require('./lib/number-allocator.js')
module.exports.NumberAllocator = NumberAllocator

63
VApp/node_modules/number-allocator/karma.conf.js generated vendored Normal file
View File

@ -0,0 +1,63 @@
// Karma configuration
// Generated on Sat Jan 30 2021 21:44:35 GMT+0900 (GMT+09:00)
module.exports = function (config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['mocha', 'browserify'],
// list of files / patterns to load in the browser
files: [
'test/*.js'
],
// list of files / patterns to exclude
exclude: [
],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
'test/*.js': ['browserify']
},
browserify: {
debug: true,
transform: []
},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],
// web server port
port: 9876,
// enable / disable colors in the output (reporters and logs)
colors: true,
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['ChromeHeadless'],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: true,
// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity
})
}

View File

@ -0,0 +1,249 @@
// Copyright Takatoshi Kondo 2021
//
// Distributed under the MIT License
'use strict'
const SortedSet = require('js-sdsl').OrderedSet
const debugTrace = require('debug')('number-allocator:trace')
const debugError = require('debug')('number-allocator:error')
/**
* Interval constructor
* @constructor
* @param {Number} low - The lowest value of the interval
* @param {Number} high - The highest value of the interval
*/
function Interval (low, high) {
this.low = low
this.high = high
}
Interval.prototype.equals = function (other) {
return this.low === other.low && this.high === other.high
}
Interval.prototype.compare = function (other) {
if (this.low < other.low && this.high < other.low) return -1
if (other.low < this.low && other.high < this.low) return 1
return 0
}
/**
* NumberAllocator constructor.
* The all numbers are set to vacant status.
* Time Complexity O(1)
* @constructor
* @param {Number} min - The maximum number of allocatable. The number must be integer.
* @param {Number} maxh - The minimum number of allocatable. The number must be integer.
*/
function NumberAllocator (min, max) {
if (!(this instanceof NumberAllocator)) {
return new NumberAllocator(min, max)
}
this.min = min
this.max = max
this.ss = new SortedSet(
[],
(lhs, rhs) => {
return lhs.compare(rhs)
}
)
debugTrace('Create')
this.clear()
}
/**
* Get the first vacant number. The status of the number is not updated.
* Time Complexity O(1)
* @return {Number} - The first vacant number. If all numbers are occupied, return null.
* When alloc() is called then the same value will be allocated.
*/
NumberAllocator.prototype.firstVacant = function () {
if (this.ss.size() === 0) return null
return this.ss.front().low
}
/**
* Allocate the first vacant number. The number become occupied status.
* Time Complexity O(1)
* @return {Number} - The first vacant number. If all numbers are occupied, return null.
*/
NumberAllocator.prototype.alloc = function () {
if (this.ss.size() === 0) {
debugTrace('alloc():empty')
return null
}
const it = this.ss.begin()
const low = it.pointer.low
const high = it.pointer.high
const num = low
if (num + 1 <= high) {
// x|----|
this.ss.updateKeyByIterator(it, new Interval(low + 1, high))
} else {
this.ss.eraseElementByPos(0)
}
debugTrace('alloc():' + num)
return num
}
/**
* Use the number. The number become occupied status.
* If the number has already been occupied, then return false.
* Time Complexity O(logN) : N is the number of intervals (not numbers)
* @param {Number} num - The number to request use.
* @return {Boolean} - If `num` was not occupied, then return true, otherwise return false.
*/
NumberAllocator.prototype.use = function (num) {
const key = new Interval(num, num)
const it = this.ss.lowerBound(key)
if (!it.equals(this.ss.end())) {
const low = it.pointer.low
const high = it.pointer.high
if (it.pointer.equals(key)) {
// |x|
this.ss.eraseElementByIterator(it)
debugTrace('use():' + num)
return true
}
// x |-----|
if (low > num) return false
// |x----|
if (low === num) {
// x|----|
this.ss.updateKeyByIterator(it, new Interval(low + 1, high))
debugTrace('use():' + num)
return true
}
// |----x|
if (high === num) {
// |----|x
this.ss.updateKeyByIterator(it, new Interval(low, high - 1))
debugTrace('use():' + num)
return true
}
// |--x--|
// x|--|
this.ss.updateKeyByIterator(it, new Interval(num + 1, high))
// |--|x|--|
this.ss.insert(new Interval(low, num - 1))
debugTrace('use():' + num)
return true
}
debugTrace('use():failed')
return false
}
/**
* Deallocate the number. The number become vacant status.
* Time Complexity O(logN) : N is the number of intervals (not numbers)
* @param {Number} num - The number to deallocate. The number must be occupied status.
* In other words, the number must be allocated by alloc() or occupied be use().
*/
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.upperBound(key)
if (it.equals(this.ss.end())) {
// ....v
if (it.equals(this.ss.begin())) {
// Insert new interval
this.ss.insert(key)
return
}
it.pre()
const low = it.pointer.high
const high = it.pointer.high
if (high + 1 === num) {
// Concat to left
this.ss.updateKeyByIterator(it, new Interval(low, num))
} else {
// Insert new interval
this.ss.insert(key)
}
} else {
if (it.equals(this.ss.begin())) {
// v....
if (num + 1 === it.pointer.low) {
// Concat to right
const high = it.pointer.high
this.ss.updateKeyByIterator(it, new Interval(num, high))
} else {
// Insert new interval
this.ss.insert(key)
}
} else {
// ..v..
const rLow = it.pointer.low
const rHigh = it.pointer.high
it.pre()
const lLow = it.pointer.low
const lHigh = it.pointer.high
if (lHigh + 1 === num) {
if (num + 1 === rLow) {
// Concat to left and right
this.ss.eraseElementByIterator(it)
this.ss.updateKeyByIterator(it, new Interval(lLow, rHigh))
} else {
// Concat to left
this.ss.updateKeyByIterator(it, new Interval(lLow, num))
}
} else {
if (num + 1 === rLow) {
// Concat to right
this.ss.eraseElementByIterator(it.next())
this.ss.insert(new Interval(num, rHigh))
} else {
// Insert new interval
this.ss.insert(key)
}
}
}
}
debugTrace('free():' + num)
}
/**
* Clear all occupied numbers.
* The all numbers are set to vacant status.
* Time Complexity O(1)
*/
NumberAllocator.prototype.clear = function () {
debugTrace('clear()')
this.ss.clear()
this.ss.insert(new Interval(this.min, this.max))
}
/**
* Get the number of intervals. Interval is internal structure of this library.
* This function is for debugging.
* Time Complexity O(1)
* @return {Number} - The number of intervals.
*/
NumberAllocator.prototype.intervalCount = function () {
return this.ss.size()
}
/**
* Dump the internal structor of the library.
* This function is for debugging.
* Time Complexity O(N) : N is the number of intervals (not numbers)
*/
NumberAllocator.prototype.dump = function () {
console.log('length:' + this.ss.size())
for (const element of this.ss) {
console.log(element)
}
}
module.exports = NumberAllocator

69
VApp/node_modules/number-allocator/package.json generated vendored Normal file
View File

@ -0,0 +1,69 @@
{
"name": "number-allocator",
"version": "1.0.14",
"description": "A library for the unique number allocator",
"main": "index.js",
"types": "types/index.d.ts",
"directories": {
"lib": "lib",
"test": "test"
},
"scripts": {
"test": "node_modules/.bin/nyc --reporter=lcov --reporter=text ./node_modules/mocha/bin/_mocha",
"pretest": "standard ./*.js ./lib/**/*.js ./test/*.js | snazzy",
"codecov": "codecov",
"browser-build": "rimraf dist/ && mkdirp dist/ && browserify index.js --standalone number-allocator > dist/number-allocator.js && uglifyjs dist/number-allocator.js --compress --mangle --output dist/number-allocator.min.js",
"typescript-compile-test": "tsc -p test/typescript/tsconfig.json",
"typescript-compile-execute": "node test/typescript/*.js",
"typescript-test": "npm run typescript-compile-test && npm run typescript-compile-execute",
"test-web": "karma start",
"generate-docs": "node_modules/.bin/jsdoc -d docs lib"
},
"repository": {
"type": "git",
"url": "git+https://github.com/redboltz/number-allocator.git"
},
"author": "Takatoshi Kondo",
"license": "MIT",
"keywords": [
"unique",
"number",
"id",
"value",
"allocator"
],
"bugs": {
"url": "https://github.com/redboltz/number-allocator/issues"
},
"homepage": "https://github.com/redboltz/number-allocator#readme",
"dependencies": {
"debug": "^4.3.1",
"js-sdsl": "4.3.0"
},
"devDependencies": {
"airtap": "^4.0.1",
"browserify": "^17.0.0",
"chai": "^4.2.0",
"codecov": "^3.8.1",
"eslint": "^7.18.0",
"eslint-config-standard": "16.0.2",
"jsdoc": "^3.6.6",
"karma": "^6.0.3",
"karma-browserify": "^8.0.0",
"karma-chrome-launcher": "^3.1.0",
"karma-mocha": "^2.0.1",
"mocha": "^10.2.0",
"nyc": "^15.1.0",
"snazzy": "^9.0.0",
"standard": "^16.0.3",
"tslint": "^5.11.0",
"tslint-config-standard": "^9.0.0",
"typescript": "^4.1.3",
"uglify-es": "^3.3.9"
},
"standard": {
"env": [
"mocha"
]
}
}

337
VApp/node_modules/number-allocator/test/test.js generated vendored Normal file
View File

@ -0,0 +1,337 @@
// Copyright Takatoshi Kondo 2021
//
// Distributed under the MIT License
'use strict'
const NumberAllocator = require('..').NumberAllocator
const assert = require('chai').assert
describe('number-allocator', function () {
it('should create without number', function (done) {
const a = NumberAllocator(0, 0)
assert.equal(a.intervalCount(), 1)
done()
})
it('should work with one number', function (done) {
const a = new NumberAllocator(0, 0)
assert.equal(a.intervalCount(), 1)
assert.equal(a.firstVacant(), 0)
assert.equal(a.alloc(), 0)
assert.equal(a.intervalCount(), 0)
assert.equal(a.alloc(), null)
assert.equal(a.firstVacant(), null)
a.free(0)
assert.equal(a.intervalCount(), 1)
assert.equal(a.alloc(), 0)
assert.equal(a.intervalCount(), 0)
assert.equal(a.alloc(), null)
assert.equal(a.use(0), false)
assert.equal(a.use(1), false)
assert.equal(a.use(-1), false)
a.free(0)
assert.equal(a.intervalCount(), 1)
assert.equal(a.use(0), true)
assert.equal(a.intervalCount(), 0)
assert.equal(a.use(1), false)
assert.equal(a.alloc(), null)
a.free(0)
assert.equal(a.intervalCount(), 1)
assert.equal(a.alloc(), 0)
assert.equal(a.intervalCount(), 0)
done()
})
it('should work with one number (offset)', function (done) {
const a = new NumberAllocator(5, 5)
assert.equal(a.intervalCount(), 1)
assert.equal(a.firstVacant(), 5)
assert.equal(a.alloc(), 5)
assert.equal(a.intervalCount(), 0)
assert.equal(a.alloc(), null)
assert.equal(a.firstVacant(), null)
a.free(5)
assert.equal(a.intervalCount(), 1)
assert.equal(a.alloc(), 5)
assert.equal(a.intervalCount(), 0)
assert.equal(a.alloc(), null)
assert.equal(a.use(5), false)
assert.equal(a.use(1), false)
a.free(5)
assert.equal(a.intervalCount(), 1)
assert.equal(a.use(5), true)
assert.equal(a.intervalCount(), 0)
assert.equal(a.use(1), false)
assert.equal(a.alloc(), null)
a.free(5)
assert.equal(a.intervalCount(), 1)
assert.equal(a.alloc(), 5)
assert.equal(a.intervalCount(), 0)
done()
})
it('should alloc/free work well on interval', function (done) {
const a = new NumberAllocator(0, 4)
assert.equal(a.intervalCount(), 1)
assert.equal(a.alloc(), 0)
assert.equal(a.intervalCount(), 1)
assert.equal(a.alloc(), 1)
assert.equal(a.intervalCount(), 1)
assert.equal(a.alloc(), 2)
assert.equal(a.intervalCount(), 1)
assert.equal(a.alloc(), 3)
assert.equal(a.intervalCount(), 1)
assert.equal(a.alloc(), 4)
assert.equal(a.intervalCount(), 0)
assert.equal(a.alloc(), null)
a.free(2)
assert.equal(a.intervalCount(), 1)
assert.equal(a.alloc(), 2)
assert.equal(a.intervalCount(), 0)
done()
})
it('should use/free work well on interval', function (done) {
const a = new NumberAllocator(0, 4)
assert.equal(a.intervalCount(), 1)
assert.equal(a.use(0), true)
assert.equal(a.intervalCount(), 1)
assert.equal(a.use(4), true)
assert.equal(a.intervalCount(), 1)
assert.equal(a.use(2), true)
assert.equal(a.intervalCount(), 2)
assert.equal(a.use(1), true)
assert.equal(a.intervalCount(), 1)
assert.equal(a.use(3), true)
assert.equal(a.intervalCount(), 0)
assert.equal(a.use(0), false)
assert.equal(a.use(1), false)
assert.equal(a.use(2), false)
assert.equal(a.use(3), false)
assert.equal(a.use(4), false)
a.free(2)
assert.equal(a.intervalCount(), 1)
assert.equal(a.use(2), true)
assert.equal(a.intervalCount(), 0)
done()
})
it('should clear work well and interval be updated well', function (done) {
const a = new NumberAllocator(0, 4)
assert.equal(a.alloc(), 0)
assert.equal(a.intervalCount(), 1)
assert.equal(a.use(1), true)
assert.equal(a.alloc(), 2)
assert.equal(a.intervalCount(), 1)
assert.equal(a.use(3), true)
assert.equal(a.alloc(), 4)
assert.equal(a.intervalCount(), 0)
a.clear()
assert.equal(a.intervalCount(), 1)
assert.equal(a.alloc(), 0)
assert.equal(a.intervalCount(), 1)
assert.equal(a.use(1), true)
assert.equal(a.alloc(), 2)
assert.equal(a.intervalCount(), 1)
assert.equal(a.use(3), true)
assert.equal(a.alloc(), 4)
assert.equal(a.intervalCount(), 0)
done()
})
it('should interval be concatinated well', function (done) {
const prepare = function () {
const a = new NumberAllocator(0, 4)
assert.equal(a.use(0), true)
assert.equal(a.use(1), true)
assert.equal(a.use(2), true)
assert.equal(a.use(3), true)
assert.equal(a.use(4), true)
return a
}
let a = prepare()
a.free(0)
assert.equal(a.intervalCount(), 1)
a.free(4)
assert.equal(a.intervalCount(), 2)
a.free(2)
assert.equal(a.intervalCount(), 3)
a.free(1)
assert.equal(a.intervalCount(), 2)
// concat left and right
a.free(3)
assert.equal(a.intervalCount(), 1)
a = prepare()
a.free(3)
assert.equal(a.intervalCount(), 1)
// ....v
// end concat right
a.free(4)
assert.equal(a.intervalCount(), 1)
a = prepare()
a.free(1)
assert.equal(a.intervalCount(), 1)
// begin concat left
a.free(0)
assert.equal(a.intervalCount(), 1)
a = prepare()
a.free(2)
assert.equal(a.intervalCount(), 1)
// begin no concat
a.free(0)
assert.equal(a.intervalCount(), 2)
a = prepare()
a.free(1)
assert.equal(a.intervalCount(), 1)
a.free(4)
assert.equal(a.intervalCount(), 2)
// concat left
a.free(2)
assert.equal(a.intervalCount(), 2)
a = prepare()
a.free(4)
assert.equal(a.intervalCount(), 1)
a.free(1)
assert.equal(a.intervalCount(), 2)
// concat right
a.free(3)
assert.equal(a.intervalCount(), 2)
done()
})
it('should work well with negative numbers', function (done) {
const a = new NumberAllocator(-2, 3)
assert.equal(a.intervalCount(), 1)
assert.equal(a.use(2), true)
assert.equal(a.intervalCount(), 2)
const value = a.alloc()
assert.notEqual(value, null)
assert.equal(value, -2)
assert.equal(a.intervalCount(), 2)
assert.equal(a.use(0), true)
assert.equal(a.intervalCount(), 3)
done()
})
it('should dump', function (done) {
const a = new NumberAllocator(0, 4)
a.dump()
assert.equal(a.use(0), true)
a.dump()
assert.equal(a.use(1), true)
a.dump()
assert.equal(a.use(2), true)
a.dump()
a.free(0)
a.dump()
a.free(2)
a.dump()
a.free(1)
a.dump()
done()
})
it('should fail use the same number twice in the middle of interval', function (done) {
const a = new NumberAllocator(0, 4)
assert.equal(a.use(1), true)
assert.equal(a.use(1), false)
done()
})
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()
})
it('should concat to right on free', function (done) {
const a = NumberAllocator(0, 3)
assert.equal(a.alloc(), 0)
assert.equal(a.alloc(), 1)
a.free(1)
assert.equal(a.alloc(), 1)
done()
})
it('should concat to left on free', function (done) {
const a = NumberAllocator(0, 3)
assert.equal(a.alloc(), 0)
assert.equal(a.alloc(), 1)
assert.equal(a.alloc(), 2)
a.free(0)
a.free(1)
assert.equal(a.alloc(), 0)
assert.equal(a.alloc(), 1)
done()
})
it('should concat to left and right on free', function (done) {
const a = NumberAllocator(0, 3)
assert.equal(a.alloc(), 0)
assert.equal(a.alloc(), 1)
assert.equal(a.alloc(), 2)
a.free(0)
a.free(2)
a.free(1)
assert.equal(a.alloc(), 0)
assert.equal(a.alloc(), 1)
assert.equal(a.alloc(), 2)
done()
})
it('should insert new interval on free', function (done) {
const a = NumberAllocator(0, 4)
assert.equal(a.alloc(), 0)
assert.equal(a.alloc(), 1)
assert.equal(a.alloc(), 2)
assert.equal(a.alloc(), 3)
assert.equal(a.alloc(), 4)
a.free(0)
a.free(4)
a.free(2)
assert.equal(a.alloc(), 0)
assert.equal(a.alloc(), 2)
assert.equal(a.alloc(), 4)
done()
})
})

View File

@ -0,0 +1,24 @@
import { NumberAllocator } from '../..'
const a: NumberAllocator = new NumberAllocator(1, 5)
const num1: Number | null = a.firstVacant()
console.log(num1)
const num2: Number | null = a.alloc()
console.log(num2)
const ret: Boolean = a.use(3)
console.log(ret)
a.free(2)
const ic1: Number = a.intervalCount()
console.log(ic1)
a.dump()
a.clear()
const ic2: Number = a.intervalCount()
console.log(ic2)

View File

@ -0,0 +1,14 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"moduleResolution": "node",
"noEmitOnError": true,
"noImplicitAny": true,
"alwaysStrict": true,
"strictNullChecks": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"sourceMap": true
}
}

1
VApp/node_modules/number-allocator/types/index.d.ts generated vendored Normal file
View File

@ -0,0 +1 @@
export { NumberAllocator } from './lib/number-allocator'

View File

@ -0,0 +1,65 @@
export declare class NumberAllocator {
/**
* NumberAllocator constructor.
* The all numbers are set to vacant status.
* Time Complexity O(1)
* @constructor
* @param {number} min - The maximum number of allocatable. The number must be integer.
* @param {number} maxh - The minimum number of allocatable. The number must be integer.
*/
constructor (min: number, max: number)
/**
* Get the first vacant number. The status of the number is not updated.
* Time Complexity O(1)
* @return {number} - The first vacant number. If all numbers are occupied, return null.
* When alloc() is called then the same value will be allocated.
*/
public firstVacant (): number | null
/**
* Allocate the first vacant number. The number become occupied status.
* Time Complexity O(1)
* @return {number} - The first vacant number. If all numbers are occupied, return null.
*/
public alloc (): number | null
/**
* Use the number. The number become occupied status.
* If the number has already been occupied, then return false.
* Time Complexity O(logN) : N is the number of intervals (not numbers)
* @param {number} num - The number to request use.
* @return {Boolean} - If `num` was not occupied, then return true, otherwise return false.
*/
public use (num: number): Boolean
/**
* Deallocate the number. The number become vacant status.
* Time Complexity O(logN) : N is the number of intervals (not numbers)
* @param {number} num - The number to deallocate. The number must be occupied status.
* In other words, the number must be allocated by alloc() or occupied be use().
*/
public free (num: number): void
/**
* Clear all occupied numbers.
* The all numbers are set to vacant status.
* Time Complexity O(1)
*/
public clear (): void
/**
* Get the number of intervals. Interval is internal structure of this library.
* This function is for debugging.
* Time Complexity O(1)
* @return {number} - The number of intervals.
*/
public intervalCount (): number
/**
* Dump the internal structor of the library.
* This function is for debugging.
* Time Complexity O(N) : N is the number of intervals (not numbers)
*/
dump (): void
}