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

quick-sorted-list

v1.0.6

Published

A sorted list using AVL the balanced binary search tree.

Downloads

9

Readme

quick-sorted-list

A javascript sorted list using AVL binary tree for quick search.

Installation

npm install quick-sorted-list

Usage

var SortedList = require('quick-sorted-list');
var sortedList = new SortedList();

Instantiation

The quick can be instantiated with an optional parameter object to specify a comparer to sort the elements.

var sortedList = new SortedList(function(a,b){return a.key-b.key;});

Properties

- length

Return the number of values stored in the sorted list.

var sortedList = new SortedList();
sortedList.insert(1);
sortedList.insert(3);
console.log(sortedList.length); // 2

Methods

- insert

sortedList.insert({key:1});

- toArray

Converts the sorted list to an Array

var valueArray = sortedList.toArray();
console.log(valueArray.toString());

- cut

Cut the sorted list by specified value. The first parameter 'element', specify the value to cut the sorted list. Second parameter 'side', specify the side of sorted list to cut, either 'left' or 'right'. The third parameter 'inclusive', specify if the elements which equal to the value should be cut or not.

var sorted = new SortedList();
sorted.insertBatch([10, 5, 22, 2, -2, 0, 11, -1, 7, 2, 8, -3]);

var removed = sorted.cut(-1, 'left', true);
console.log(removed);         // [-3, -2, -1];
console.log(sorted.toArray()) //[0, 2, 2, 5, 7, 8, 10, 11, 22]);

- locateByIndex

Find the element located at the index position of the list.

var sorted = new SortedList();
sorted.insertBatch([10, 5, 22, 2, -2, 0, 11, -1, 7, 2, 8, -3]);
// Now they are sorted as [-3, -2, -1, 0, 2, 2, 5, 7, 8, 10, 11, 22]

sorted.locateByIndex(5); // the 6th element in the sorted list is 2.

- locate

Find the index in the sorted list where the given element could be inserted. The second parameter is to specify at which side we want to insert the given element, accept either 'left' or 'right'.

var sorted = new SortedList();
sorted.insertBatch([10, 5, 22, 2, -2, 0, 11, -1, 7, 2, 8, -3]); 
// Now they are sorted as [-3, -2, -1, 0, 2, 2, 5, 7, 8, 10, 11, 22]

sorted.locate(5, 'left'); // the first place (by insert to the 'left' side of same values) to insert is 6.
sorted.locate(7, 'right'); // the last place (by insert to the 'right' side of same values) to insert is 8.

- removeByIndex

Remove the element at the given position of the list and return the element.

var sorted = new SortedList();
sorted.insertBatch([10, 5, 22, 2, -2, 0, 11, -1, 7, 2, 8, -3]); 
// Now they are sorted as [-3, -2, -1, 0, 2, 2, 5, 7, 8, 10, 11, 22]

sorted.removeByIndex(5); // the second instance of '2' is removed, the returned value is 2. 
sorted.toArray();        // the remaining values are [-3, -2, -1, 0, 2, 5, 7, 8, 10, 11, 22]

- remove

Remove elements in the sorted list which equals to the given element, and return the removed ones.

var sorted = new SortedList();
sorted.insertBatch([10, 5, 22, 2, -2, 0, 11, -1, 7, 2, 8, -3]); 
// Now they are sorted as [-3, -2, -1, 0, 2, 2, 5, 7, 8, 10, 11, 22]

sorted.remove(2);        // Two instances of '2' are removed, the returned value is [2, 2]. 
sorted.remove(13);       // There is no '13' in the list, the returned value is [];
sorted.toArray();        // the remaining values are [-3, -2, -1, 0, 5, 7, 8, 10, 11, 22]