{"name":"number-allocator","version":"1.0.13","description":"A library for the unique number allocator","main":"index.js","types":"types/index.d.ts","directories":{"lib":"lib","test":"test"},"scripts":{"test":"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":"jsdoc -d docs lib"},"repository":{"type":"git","url":"git+https://github.com/redboltz/number-allocator.git"},"author":{"name":"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"]},"gitHead":"dc9806445e4cab165b1e9b74f46b404dee685bcd","_id":"number-allocator@1.0.13","_nodeVersion":"19.4.0","_npmVersion":"9.2.0","dist":{"integrity":"sha512-f8lIcu5m61qzhbPfvVvenwtA6XkiIrbllQEBriTxjEGUna4oOXEOF8eJ720onvKhFtgmVdFGQxd8Jw8zV1tQRw==","shasum":"3805a8b0907052da843e3ed6977ecb12587ce372","tarball":"http://registry-npm.ywwl.com/number-allocator/download/number-allocator-1.0.13.tgz","fileCount":13,"unpackedSize":29672,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIAk7XSf5dnPITHOCI5eNEP9N4eCefkprTG7BDnTmHfCmAiEA5HrW6Wa5RUUZ1pCd3rFZ0atbTymeRXOW+OJjPAR9rMA="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjzlRmACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmqWrhAAh5WmySQ+4qsaC2LJMk6EJ/5gMxQo1aw9KXUgqbSVFIpsKVe5\r\nJjrAG9HI+NRly95EPMdLOoMdxkUOUOlVXoBQsKqnzEIdAqZyMC12SzL9LWdh\r\n5l65fmjQZ/ARsLPXSvu7mnc9uE3eqgPn4NFBSljoy5anaUMZgNvLVT41ErcN\r\nvGAEXCwmtoglEP+lQtvU8WmuuEctrQTsgcVoGZhCkJDbF9kRnBcaWMhWVlIr\r\nxxrBKjgIgZRqZbKxMkcPV74n4Gwo1+507kOmPYIPtb9qO6a9HrqYYZcwXJpU\r\ns2g6YiBwRwO6SDYnjAE0mcF+raz2c+TyGjaSJITN30Awqn1tuQXEir7kJ9un\r\np1+v3KJ8l2CKDkVCko7kkHd8RZ6SfCz8Nqf97CoaL0ba5bqkx6gM3zC3iAwW\r\nu14XvOcv5L5NRyFskAmcvCVqzgyLLmAfwZqWYskPVe/WW/w/TwjaH+1H/zO+\r\nOHeOHIuMKXdMpFEhbuR19pwwEuPQwluuR1p/yysCa3fO3kGfn3ACGfzXrRdI\r\npwGr8zrnp2rwjBPyLVcvtSrZ+ariKlZaJTOgS1ztGkkYGKS4zSEUGPWbzcwz\r\nwfFC+BamsJhcIAs3PXMFfR5nAOMFzCcoECYwHno4QDx0TqEOTZ2isqaSahdT\r\nfSve9Da1Zev25kWf2W3vw2XB3f4JrF+cp2w=\r\n=Mrh+\r\n-----END PGP SIGNATURE-----\r\n","size":7178,"noattachment":false,"key":"/number-allocator/-/number-allocator-1.0.13.tgz"},"_npmUser":{"name":"redboltz","email":"redboltz@gmail.com"},"maintainers":[{"name":"redboltz","email":"redboltz@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/number-allocator_1.0.13_1674466406533_0.5458236049032845"},"_hasShrinkwrap":false,"readme":"# Unique number allocator for JavaScript.\n\nVersion 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)\n\n## How to use\n\n```js\nconst NumberAllocator = require('number-allocator').NumberAllocator\n\n// Construct a NumerAllocator that has [0-10] numbers.\n// All numbers are vacant.\nconst a = new NumberAllocator(0, 10)\n\n// Allocate the least vacant number.\nconst num0 = a.alloc()\nconsole.log(num0) // 0\n\n// Allocate the least vacant number.\nconst num1 = a.alloc()\nconsole.log(num1) // 1\n\n// Use any vacant number.\nconst ret1 = a.use(5) // 5 is marked as used(occupied) in the NumberAllocator.\nconsole.log(ret1) // true\n\n// If use the used number, then return false.\nconst ret2 = a.use(1) // 1 has already been used, then return false\nconsole.log(ret2) // false\n\n// Free the used number.\na.free(1)\n// Now 1 becomes vacant again.\n\nconst ret3 = a.use(1)\nconsole.log(ret3) // true\n\n// Get the lowest vacant number without marking used.\nconst num2 = a.firstVacant()\nconsole.log(num2) // 2\n\n// Clear all used mark. Now [0-10] are allocatable again.\na.clear()\n```\n\n## Reference\n\n### NumberAllocator(min, max)\nConstructor\n\n- min: Number\n   - The maximum number of allocatable. The number must be integer.\n- max: Number\n   - The minimum number of allocatable. The number must be integer.\n\nThe all numbers are set to vacant status.\n\nTime Complexity O(1)\n\n### firstVacant()\nGet the first vacant number. The status of the number is not updated.\n\nTime Complexity O(1)\n\n- return: Number\n   - The first vacant number. If all numbers are occupied, return null.\n     When alloc() is called then the same value will be allocated.\n\n### alloc()\nAllocate the first vacant number. The number become occupied status.\n\nTime Complexity O(1)\n\n- return: Number\n   - The first vacant number. If all numbers are occupied, return null.\n\n### use(num)\nUse the number. The number become occupied status.\n\nIf the number has already been occupied, then return false.\n\nTime Complexity O(logN) : N is the number of intervals (not numbers)\n\n- num: Number\n   - The number to request use.\n- return: Boolean\n   - If `num` was not occupied, then return true, otherwise return false.\n\n### free(num)\nDeallocate the number. The number become vacant status.\n\nTime Complexity O(logN) : N is the number of intervals (not numbers)\n\n- num: Number\n   - The number to deallocate. The number must be occupied status.\n     In other words, the number must be allocated by alloc() or occupied be use().\n\n### clear()\nClear all occupied numbers.\nThe all numbers are set to vacant status.\nTime Complexity O(1)\n\n### intervalCount()\nGet the number of intervals. Interval is internal structure of this library.\n\nThis function is for debugging.\n\nTime Complexity O(1)\n\n- return: Number\n   - The number of intervals.\n\n### dump()\nDump the internal structor of the library.\n\nThis function is for debugging.\n\nTime Complexity O(N) : N is the number of intervals (not numbers)\n\n## Internal structure\nNumberAllocator has a sorted-set of Interval.\n\nInterval has `low` and `high` property.\n\nI use `[low-high]` notation to describe Interval.\n\nWhen NumberAllocator is constructed, it has only one Interval(min, max).\n\nLet's say `new NumberAllocator(1, 9)` then the internal structure become as follows:\n\n```\n[1-------9]\n```\n\nWhen `alloc()` is called, the first Interval.low is returned.\n\nAnd then the interval is shrinked.\n\n```\nalloc()\nreturn 1\n [2------9]\n```\n\nWhen `use(5)` is called, the interval is separated to the two intervals.\n\n```\nuse(5)\n [2-4] [6--9]\n```\n\nWhen `alloc()` is called again, the first Interval.low is returned.\n\nAnd then the interval is shrinked.\n\n```\nalloc()\nreturn 2\n  [3-4] [6--9]\n```\n\nWhen `free(1)` is called. the interval is inseted.\n\n```\nfree(1)\n[1]  [3-4] [6--9]\n```\n\nWhen `free(2)` is called. the interval is inseted.\n\nAnd check the left and right intervals. If they are continuours, then concatinate to them.\n\n```\nfree(1)\n[1][2][3-4] [6--9]\n[1-------4] [6--9]\n```\n\nWhen `clear()` is called, then reset the interval as follows:\n\n```\n[1-------9]\n```\n\nWhen `intervalCount()` is called, then the number of intervals is retuned.\n\nIn the following case, return 3.\n\n```\nintervalCount()\nreturn 3\n[1]  [3-4] [6--9]\n```\n\nInterval management (insertion/concatination/shrinking) is using efficient way.\nInsert/Delete operation to sorted-set is minimized.\nSome of operations requires O(logN) time complexity. N is number of intervals.\nIf the maximum count of allocatable values is M, N is at most floor((M + 1) / 2),\n\nIn this example, M is 9 so N is at most 5 as follows:\n\n```\n[1] [3] [5] [7] [9]\n```\n","publish_time":1674466406711,"_cnpm_publish_time":1674466406711,"dist-tags":{"latest":"1.0.14"}}