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])
circle.push(10)
circle.unshift(-1)
circle.shift()
circle.pop()
circle.rotate(-3)
circle.rotate(4)
circle.toArray()
circle.toArray('prev')
circle.length
circle.length = 5
circle.length = 0
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())
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())
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])
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())
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