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

circularray

v0.2.2

Published

A fast circular array implementation in JavaScript.

Downloads

34

Readme

Circularray

Portemanteau of “circular array”. Circularray is a fast circular array implementation in JavaScript based on a doubly linked list.

Read a write up on how it works.

Usage

The concept of “pointer” is important to understand. A circularray a list of nodes that loops on itself, with a node marked as “the pointer.” Inserting nodes and remove nodes is done around that pointer, and that pointer can be moved at will.

In the example below, the pointer is marked in brackets.

const circle = new Circularray([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
// -> … 8 9 [0] 1 2 …

// Adding items (front and back)
circle.push(10)
// -> … 8 9 10 [0] 1 2 …
circle.unshift(-1)
// -> … 8 9 10 [-1] 0 1 2 …

// Removing items (front and back)
circle.shift()
// Yields -1 ->  … 8 9 10 [0] 1 2 …
circle.pop()
// Yields 10 -> … 8 9 [0] 1 2 …

// Rotating the circle
circle.rotate(-3)
// -> … 1 2 [3] 4 5 …
circle.rotate(4)
// -> … 7 8 [9] 0 1 …

// Array representation
circle.toArray()
// -> [9, 0, 1, 2, 3, 4, 5, 6, 7, 8]
circle.toArray('prev')
// -> [9, 8, 7, 6, 5, 4, 3, 2, 1], not the same as .reverse()!

// Length reading
circle.length
// -> 5

// Length cutting
circle.length = 5
// -> ↬ 2 3 [9] 0 1 ↫
circle.length = 0
// -> Empty

Example

A perfect example of when a circular array is handy is for the Josephus problem.

To put it simply: counting begins at a specified point in the circle and proceeds around the circle in a specified direction (typically clockwise). After a specified number of items are skipped, the next item is removed. The procedure is repeated with the remaining items, starting with the next one, going in the same direction and skipping the same number of items, until only one item remains.

Considering we would skip one item out of 2, this is how it would be implemented. At every iteration, we rotate the circle clockwise by 1 and drop the first one, until we have only one item remaining.

const circle = new Circularray([1, 2, 3, 4, 5, 6, 7, 8, 9])
while (circle.length > 1) circle.rotate(-1).shift()
console.log('Remaining item is', circle.pop()) // 3

With an offset of 3:

const circle = new Circularray([1, 2, 3, 4, 5, 6, 7, 8, 9])
while (circle.length > 1) circle.rotate(-2).shift()
console.log('Remaining item is', circle.pop()) // 1

With an offset of half the size of the circle (as suggested in Advent of Code 2016 Day 19):

const circle = new Circularray([1, 2, 3, 4, 5, 6, 7, 8, 9])
// Maintain a second pointer halfway through the circle to avoid having to
// rotate the circle back and forth at every round. Unnecessary for small sets,
// but vital for large ones (like the one in AoC).
let offset = Math.floor(circle.size / 2) - 1
let mirror = circle.pointer
while (offset--) mirror = mirror.next

while (circle.size > 1) {
  mirror = mirror.next
  mirror.remove()
  circle.size--
  if (circle.size % 2 === 0) mirror = mirror.next
  circle.rotate(-1)
}

console.log('Remaining item is', circle.pop()) // 9

Benchmark

| Josephus with k=2 | Circularray | Array | | :---------------- | :---------- | :---- | | 10_000 items | ~10ms | ~4ms | | 100_000 items | ~25ms | ~1.8s | | 500_000 items | ~100ms | ~45s | | 1_000_000 items | ~250ms | ~4mn |

Development

npm i
npm test