{"name":"number-allocator","version":"1.0.10","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":"^2.1.2"},"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":"^8.2.1","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":"9bfe5095dfc69c8921a44d5789b8e3248fc75d56","_id":"number-allocator@1.0.10","_nodeVersion":"14.17.1","_npmVersion":"6.14.13","dist":{"integrity":"sha512-K4AvNGKo9lP6HqsZyfSr9KDaqnwFzW203inhQEOwFrmFaYevpdX4VNwdOLk197aHujzbT//z6pCBrCOUYSM5iw==","shasum":"efc4c665e45bf60f0ad172aca1540e093b5292e8","tarball":"http://registry-npm.ywwl.com/number-allocator/download/number-allocator-1.0.10.tgz","fileCount":13,"unpackedSize":28552,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJiHZBSACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmpZSg/+Pv394h5ySOuvBjrnm9l/c4dOit2HVSMnCRDF1KU4qCQg45VD\r\nNAz/G5Djn+q21oAjYmvILKn4C/EMKzLXkm5mMl8jl6TLnxLr0l8McPOoRGSq\r\nC0wQJo+kfz+q6lkPDOIeQsowheYgbIR6l7CilFhb94VC9S+x822PuyqhE+FJ\r\nDkR5Y6O6wPCIz4HCcbU5xCmnC2aaJkvd0/ROATtruGRsbkZW1SqfUUS8XY9z\r\nC+JXU7qBlBIi5PwzYqwXjEvQuQ9ViYJZ+/X8CUzJ71hvoALEpyBKLuDxKYY7\r\nuG9rXgpDqPb5cmuasvIptx+tzIYpMlmtAF+RjXGo3uH9/Yn4Vfx3Fc52LQjk\r\nY1LV6CYUH4ty65t+49j6SzOAvDhCJ3kObVSqOgmCNTzz3TbjFgjReZswbdrz\r\nKawPiEZRRRu8C8my4QAtlK2iajCLEdlvVeQHz0LuYYdTAIDM/vmFx4s/06r8\r\nhqjXhev6uFxZobs5gUsqM28riQ1oDE2jr/WUBMIQs6AXKiuT5aYYoDe74cnR\r\nLBba9aVGLZMYKdFIXeHh0x+eHRCjX/dIgsDf3ZyQPPBEKxKsMCqf2yPMvIFO\r\n3v22Z4niKv8G2KLDvA69+G22RwZ2PHcEBXualJbBdeFOJrAsrAYAljK6X3zV\r\n75dVxn0WT6OnLs14Axw8IAgI2Awqb/i9+kA=\r\n=iWPn\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCICy6zRIXgaEmPH0cjvN5GVzT25OcV1mNf/aqoVzQMJ/fAiBdU3aekJGo9Svg2bShAPdnVFZLt2aRx9ensQGTIP1Fbg=="}],"size":7133,"noattachment":false,"key":"/number-allocator/-/number-allocator-1.0.10.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.10_1646104658127_0.18143587022834895"},"_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":1646104658260,"_cnpm_publish_time":1646104658260,"dist-tags":{"latest":"1.0.14"}}