sorted-set
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();
set.add('5a600e16');
set.add('5a600e17');
set.add('5a600e18');
set.add('5a600e17');
set.has('5a600e17');
set.get('5a600e17');
set.del('5a600e16');
set.del('5a600e16');
set.add('5a600e10');
set.add('5a600e11');
set.add('5a600e12');
set.add('5a600e13');
set.add('5a600e14');
set.add('5a600e15');
set.add('5a600e16');
set.range('5a600e13', '5a600e14');
set.range('5a600e15');
set.count('5a600e13', '5a600e14');
set.rank('5a600e16');
set.rank('5a600e13');
set.rank('5a600e14');
set.rank('5a600e15');
set.rank('5a600e19');
set.slice(0, 3);
set.slice(-1);
set.length;
set.shift();
set.pop();
set.head();
set.tail();
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);
Set.intersect(b, a);
a.intersect(b);
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');
Set.intersect(c, a, b);
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({
hash: function(entry) {
return entry % 50;
},
compare: function(a, b) {
return b - a;
}
});
set.add(22);
set.add(72);
set.add(40);
set.values();
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});
set.add({key: '5a600e19', value: 11});
set.add({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.