npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

number-allocator

v1.0.14

Published

A library for the unique number allocator

Downloads

3,173,724

Readme

Unique number allocator for JavaScript.

Version 1.0.14 number-allocator CI codecov

How to use

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]