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

sorted-set

v0.3.0

Published

a sorted set based heavily upon redis' skip list implementation

Downloads

1,346

Readme

sorted-set

Build Status

A sorted set based heavily upon redis' skip list implementation. The module named sorted-map separates keys and values similar to redis' implementation.

Test

Run any of the following:

$ mocha
$ npm test
$ make test

Note: remember to npm install!

Install

$ npm install sorted-set

API

var Set = require('sorted-set');

var set = new Set();

// average O(log(N))
set.add('5a600e16');
set.add('5a600e17');
set.add('5a600e18'); // => null
set.add('5a600e17'); // => '5a600e17'

// average O(1)
set.has('5a600e17'); // => true

// average O(1)
set.get('5a600e17'); // => '5a600e17'

// average O(log(N))
set.del('5a600e16'); // => '5a600e16'

// average O(1)
set.del('5a600e16'); // => null

set.add('5a600e10');
set.add('5a600e11');
set.add('5a600e12');
set.add('5a600e13');
set.add('5a600e14');
set.add('5a600e15');
set.add('5a600e16');

// average O(log(N)+M) where M is the number of elements between min and max
set.range('5a600e13', '5a600e14'); // inclusive
// => ['5a600e13', '5a600e14']

set.range('5a600e15');
// => ['5a600e15', '5a600e16', '5a600e17', '5a600e18']

// average O(log(N)+log(M)) where M as in range
set.count('5a600e13', '5a600e14'); // => 2

// more or less indexOf for the sorted values
// average O(log(N))
set.rank('5a600e16'); // => 6
set.rank('5a600e13'); // => 3
set.rank('5a600e14'); // => 4
set.rank('5a600e15'); // => 5
set.rank('5a600e19'); // => -1

// average O(log(N)+M) where M as in range
set.slice(0, 3);
// => ['5a600e10', '5a600e11', '5a600e12']

set.slice(-1); // => ['5a600e18']

set.length; // => 9

set.shift(); // => '5a600e10'

set.pop(); // => '5a600e18'

set.head(); // => '5a600e11'

set.tail(); // => '5a600e17'

Intersection

This form of intersection intersects based on the comparison of values.

var a = new Set(), b = new Set();

a.add('5a600e10');
a.add('5a600e12');
a.add('5a600e14');
a.add('5a600e15');
a.add('5a600e17');
a.add('5a600e18');
a.add('5a600e19');
a.add('5a600e1a');
a.add('5a600e1b');
a.add('5a600e1c');
a.add('5a600e1e');

b.add('5a600e10');
b.add('5a600e11');
b.add('5a600e13');
b.add('5a600e14');
b.add('5a600e15');
b.add('5a600e17');
b.add('5a600e19');
b.add('5a600e1b');
b.add('5a600e1c');
b.add('5a600e1d');
b.add('5a600e1f');

Set.intersect(a, b);
// => ['5a600e10', '5a600e14', '5a600e15', '5a600e17', '5a600e19', '5a600e1b', '5a600e1c']

Set.intersect(b, a);
// => ['5a600e10', '5a600e14', '5a600e15', '5a600e17', '5a600e19', '5a600e1b', '5a600e1c']

// works, but not preferred
a.intersect(b);
// => ['5a600e10', '5a600e14', '5a600e15', '5a600e17', '5a600e19', '5a600e1b', '5a600e1c']

var c = new Set();

c.add('5a600e10');
c.add('5a600e12');
c.add('5a600e13');
c.add('5a600e14');
c.add('5a600e16');
c.add('5a600e17');
c.add('5a600e18');
c.add('5a600e1a');
c.add('5a600e1c');
c.add('5a600e1f');

// for best performance, the smallest set should be first
Set.intersect(c, a, b);
// => ['5a600e10', '5a600e14', '5a600e17', '5a600e1c']

This form of intersect intersects based on the hash of the value. This version is also slower, because the set is not necessarily ordered by the hash of its values.

var a = new Set(), b = new Set();

a.add('5a600e10');
a.add('5a600e12');
a.add('5a600e13');
a.add('5a600e14');
a.add('5a600e16');
a.add('5a600e17');
a.add('5a600e18');
a.add('5a600e1a');
a.add('5a600e1c');
a.add('5a600e1f');

b.add('5a600e10');
b.add('5a600e11');
b.add('5a600e13');
b.add('5a600e14');
b.add('5a600e15');
b.add('5a600e17');
b.add('5a600e19');
b.add('5a600e1b');
b.add('5a600e1c');
b.add('5a600e1d');
b.add('5a600e1f');

Set.intersectKeys(a, b);

Custom Hashing and Comparison

Caution: this often involves adding objects to the set. When you add an object to the set, make sure to not change the data in the object by reference without updating the set, unless the update doesn't alter the result of hashing or comparison.

var set = new Set({
  // what the primary index should be (defaults to identity function)
  hash: function(entry) {
    return entry % 50;
  },
  // how to order the set (defaults to string-friendly comparator)
  compare: function(a, b) {
    // descending numeric sort
    return b - a;
  }
});

set.add(22);
set.add(72); // replaces 22 because it hashes to the same value
set.add(40);

// descending order
set.values(); // [72, 40]

Unique

You can enable unique values with the unique option, which causes set to throw an error if the value provided already belongs to a different key.

var set = new Set({
  unique: true,
  hash: function(item) {
    return item.key;
  },
  compare: function(a, b) {
    return a.value - b.value;
  }
});

set.add({key: '5a600e10', value: 16});
set.add({key: '5a600e11', value: 6});
set.add({key: '5a600e12', value: 17});
set.add({key: '5a600e13', value: 11});
set.add({key: '5a600e14', value: 14});
set.add({key: '5a600e15', value: 19});
set.add({key: '5a600e16', value: 3});
set.add({key: '5a600e17', value: 12});
set.add({key: '5a600e18', value: 10});

// currently O(log(N)) because it needs to attempt to insert the value
set.add({key: '5a600e19', value: 11}); // throws
set.add({key: '5a600e14', value: 14}); // => {key: '5a600e14', value: 14}

TODO: all tests need to check compare and hash support.

License

The MIT License (MIT)

Copyright © 2014 GlobeSherpa

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.