{"name":"number-allocator","version":"1.0.5","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","bugs":{"url":"https://github.com/redboltz/number-allocator/issues"},"homepage":"https://github.com/redboltz/number-allocator#readme","dependencies":{"collections":"^5.1.12","debug":"^4.3.1"},"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":"fccf339711f30b707b46832932b59b5ff2ec2297","_id":"number-allocator@1.0.5","_nodeVersion":"14.15.4","_npmVersion":"6.14.10","dist":{"integrity":"sha512-IbGLgKe1np/9MxvMTtZ3SrFfb7kg9SVL8DFNkxkVN8CV511VDtgfO0WssH5KV53+FbVanbLaSKWnJXxRrwo7Yg==","shasum":"e5bda011e5cf158769ebb4b0fc5ccb35f0760efa","tarball":"http://registry-npm.ywwl.com/number-allocator/download/number-allocator-1.0.5.tgz","fileCount":13,"unpackedSize":28274,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJgH2qaCRA9TVsSAnZWagAAMQUP/37SWR1lenTrDVpsbBaY\nWgcmg7tuW07y6T+MZuqCvly9SW2wCqhwf9ydhFmBenAtZM7sOx+7iIIUk9P0\nvNTz/Uk2Tsu3l9n2bytIStr7qN35ASfM8NR5sATf+93gleUOxzhoEQTMUEuq\ncUXucmr6kNz5nT5c5TcGhaUt3hn4JEOBLa6DI/VDMImUpID1Ccxs5OULJYDn\nFYYZvdFWKBc08XA2ViwbEATfrneDsByfFLwDjgptXyCA3ipTkK7sihAFFRmA\nDO7bp8tjj8W4F/B3ejb1fEvlkqPD0k7oe+YPfeAR1egEg2kHQtZ7SUB7f9In\nrSKjeHqh/Gm+gHPmpocDSEmi4+ntVCU97Xo0yn2USKvmAzxtO+9RtIRsoLFu\nTQCHreCF9V7ktonunFPRE997xuTLaodnrUnXWgbGcY3sJEA1PNY8CGTTw4QN\ndykl6sZ0YQwbFHXb9DxhxykjmKHO5IXfeJZb26gKBDrAhUUutxUs5b7uI0CQ\nM0irGfhTKs+NsEAaTYsg4Vw/QMI5YbnFQFxsxk3XPwsrd5Qhlery/yhyLf4g\n86EGdameMEbC0rvhHEQ1T3Y3TjutlK7ZGt2+llPTU9Y2TUCz6lsHYT5gYALo\noM0wbf5oi+RWF0azkxdgvgofU7uwuP17lEudilfRTq43A3Z4WO/WarRvBkAB\niOh4\r\n=l5ek\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCfRMNN1iYBbL1jr8mnNCgwxs2PJMqqUDeFmHNPMC6MtwIhANJ0qAa2xclOa/ecBh054kH0SmCU0BH84onvSM1xzJJs"}],"size":6976,"noattachment":false,"key":"/number-allocator/-/number-allocator-1.0.5.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.5_1612671641824_0.4691527353180869"},"_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":1612671642010,"_cnpm_publish_time":1612671642010,"dist-tags":{"latest":"1.0.14"}}