08ed855fa1d5da60632a6322781d4e15f1eb35183f29c76d00e9e4a53de17217b0fa3e901be1510afc9a42182e06a467051bd7ae2b2dc098a7e09244e7654e 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. # Unique number allocator for JavaScript.
  2. 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)
  3. ## How to use
  4. ```js
  5. const NumberAllocator = require('number-allocator').NumberAllocator
  6. // Construct a NumerAllocator that has [0-10] numbers.
  7. // All numbers are vacant.
  8. const a = new NumberAllocator(0, 10)
  9. // Allocate the least vacant number.
  10. const num0 = a.alloc()
  11. console.log(num0) // 0
  12. // Allocate the least vacant number.
  13. const num1 = a.alloc()
  14. console.log(num1) // 1
  15. // Use any vacant number.
  16. const ret1 = a.use(5) // 5 is marked as used(occupied) in the NumberAllocator.
  17. console.log(ret1) // true
  18. // If use the used number, then return false.
  19. const ret2 = a.use(1) // 1 has already been used, then return false
  20. console.log(ret2) // false
  21. // Free the used number.
  22. a.free(1)
  23. // Now 1 becomes vacant again.
  24. const ret3 = a.use(1)
  25. console.log(ret3) // true
  26. // Get the lowest vacant number without marking used.
  27. const num2 = a.firstVacant()
  28. console.log(num2) // 2
  29. // Clear all used mark. Now [0-10] are allocatable again.
  30. a.clear()
  31. ```
  32. ## Reference
  33. ### NumberAllocator(min, max)
  34. Constructor
  35. - min: Number
  36. - The maximum number of allocatable. The number must be integer.
  37. - max: Number
  38. - The minimum number of allocatable. The number must be integer.
  39. The all numbers are set to vacant status.
  40. Time Complexity O(1)
  41. ### firstVacant()
  42. Get the first vacant number. The status of the number is not updated.
  43. Time Complexity O(1)
  44. - return: Number
  45. - The first vacant number. If all numbers are occupied, return null.
  46. When alloc() is called then the same value will be allocated.
  47. ### alloc()
  48. Allocate the first vacant number. The number become occupied status.
  49. Time Complexity O(1)
  50. - return: Number
  51. - The first vacant number. If all numbers are occupied, return null.
  52. ### use(num)
  53. Use the number. The number become occupied status.
  54. If the number has already been occupied, then return false.
  55. Time Complexity O(logN) : N is the number of intervals (not numbers)
  56. - num: Number
  57. - The number to request use.
  58. - return: Boolean
  59. - If `num` was not occupied, then return true, otherwise return false.
  60. ### free(num)
  61. Deallocate the number. The number become vacant status.
  62. Time Complexity O(logN) : N is the number of intervals (not numbers)
  63. - num: Number
  64. - The number to deallocate. The number must be occupied status.
  65. In other words, the number must be allocated by alloc() or occupied be use().
  66. ### clear()
  67. Clear all occupied numbers.
  68. The all numbers are set to vacant status.
  69. Time Complexity O(1)
  70. ### intervalCount()
  71. Get the number of intervals. Interval is internal structure of this library.
  72. This function is for debugging.
  73. Time Complexity O(1)
  74. - return: Number
  75. - The number of intervals.
  76. ### dump()
  77. Dump the internal structor of the library.
  78. This function is for debugging.
  79. Time Complexity O(N) : N is the number of intervals (not numbers)
  80. ## Internal structure
  81. NumberAllocator has a sorted-set of Interval.
  82. Interval has `low` and `high` property.
  83. I use `[low-high]` notation to describe Interval.
  84. When NumberAllocator is constructed, it has only one Interval(min, max).
  85. Let's say `new NumberAllocator(1, 9)` then the internal structure become as follows:
  86. ```
  87. [1-------9]
  88. ```
  89. When `alloc()` is called, the first Interval.low is returned.
  90. And then the interval is shrinked.
  91. ```
  92. alloc()
  93. return 1
  94. [2------9]
  95. ```
  96. When `use(5)` is called, the interval is separated to the two intervals.
  97. ```
  98. use(5)
  99. [2-4] [6--9]
  100. ```
  101. When `alloc()` is called again, the first Interval.low is returned.
  102. And then the interval is shrinked.
  103. ```
  104. alloc()
  105. return 2
  106. [3-4] [6--9]
  107. ```
  108. When `free(1)` is called. the interval is inseted.
  109. ```
  110. free(1)
  111. [1] [3-4] [6--9]
  112. ```
  113. When `free(2)` is called. the interval is inseted.
  114. And check the left and right intervals. If they are continuours, then concatinate to them.
  115. ```
  116. free(1)
  117. [1][2][3-4] [6--9]
  118. [1-------4] [6--9]
  119. ```
  120. When `clear()` is called, then reset the interval as follows:
  121. ```
  122. [1-------9]
  123. ```
  124. When `intervalCount()` is called, then the number of intervals is retuned.
  125. In the following case, return 3.
  126. ```
  127. intervalCount()
  128. return 3
  129. [1] [3-4] [6--9]
  130. ```
  131. Interval management (insertion/concatination/shrinking) is using efficient way.
  132. Insert/Delete operation to sorted-set is minimized.
  133. Some of operations requires O(logN) time complexity. N is number of intervals.
  134. If the maximum count of allocatable values is M, N is at most floor((M + 1) / 2),
  135. In this example, M is 9 so N is at most 5 as follows:
  136. ```
  137. [1] [3] [5] [7] [9]
  138. ```