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

sequenced-array

v2.0.0

Published

The sequenced array class which maintains sorted order with time complexity O(logN)

Downloads

31

Readme

Build Status Coverage

sequenced-array

The sequenced array class which maintains sorted order with time complexity O(logN) by using binary search algorithm.

In most situations, the worst-case performance is O(logN) except for the cases that there are <empty item>s in the array.

If each item of the array is an empty item which is the worst case, the time complexity is O(N), because we need to compare all items of the array to determine an insert point.

Install

$ npm i sequenced-array

Usage

import SequencedArray from 'sequenced-array'

new SequencedArray(arrayLength, {desc, compare})

new SequencedArray(array, {desc, compare})

class SequencedArray extends Array {}

SequencedArray is a subclass of Array, so that its instances inherit all methods, getters and setters of normal arrays.

  • desc ?boolean=false Whether the array should be sorted in decending order. By default SequencedArrays are in ascending order.
  • compare ?Function=(a, b) => a - b The compare function as the same as the compareFunction of Array.prototype.filter(compareFunction). So that we can compare array items which are not numbers.

Creates a SequencedArray

// creates an empty array
new SequencedArray([])
// or
new SequencedArray()

// creates an array of length 10 with 10 empty items.
new SequencedArray(10)

// creates an array of [1, 2, 3]
new SequencedArray([1, 2, 3])

// creates an order book which puts the highest bid at the top
const orderBook = new SequencedArray([], {
  desc: true,
  compare: (a, b) => a.price - b.price
})
orderBook.insert({price: 2, amount: 1})
orderBook.insert({price: 3, amount: 2})
orderBook.insert({price: 1, amount: 1})

console.log(orderBook[0])   // {price: 3, amount: 2}
console.log(orderBook[1])   // {price: 2, amount: 1}
console.log(orderBook[2])   // {price: 1, amount: 2}

array.match(item, index): number | undefined

  • item any
  • index number

Matches the item with array[index]

Returns

  • < 0 indicates that item should be before the index index
  • = 0 indicates that item equals the item at index index
  • > 0 indicates that item should be after the index index
  • undefined indicates that array[index] is an empty item, so it is not matchable.
const arr = new SequencedArray(4)
arr[2] = 2
arr.match(5, 0)     // undefined
arr.match(1, 2)     // -1
arr.match(3, 2)     // 1
arr.match(2, 2)     // 0

array.locate(item): [number, number]

Finds which location should item be located at.

Returns [min, max]

  • If min equals to max, it indicates that item is equals to array[min]
  • Otherwise, it indicates that item could be inserted between index min and index max
new SequencedArray([1, 2, 3, 4]).locate(2.5)  // [1, 2]
new SequencedArray([1, 2, 3, 4]).locate(2)    // [1, 1]

new SequencedArray([1, 2, 3, 4]).locate(0)
// [-1, 0]
// `0` should be placed before `1`(array[0])

array.insert(item): {index: number, inserted: boolean}

Insert item into the array and maintains the sorting order.

Returns Object

  • index number The item has been located at index index
  • inserted boolean
    • true the new item has been inserted into the array
    • false the item equals to array[index] and it is unnecessary to insert the item as a new one.
const a = new SequencedArray([1, 2, 3, 4])
a.insert(2.5)
// {
//   index: 2,
//   inserted: true
// }

// Then, the array is:
// [1, 2, 2.5, 3, 4]
const b = new SequencedArray([1, 2, 3, 4])
b.insert(2)
// {
//   index: 1,
//   inserted: false
// }

// `2` is already located at index 1
const c = new SequencedArray([])
c.insert(3)
c.insert(1)
c.insert(2)
// Then the array is: [1, 2, 3]

const d = new SequencedArray([], {desc: true})
c.insert(3)
c.insert(1)
c.insert(2)
// Then the array is: [3, 2, 1]

License

MIT