sorted-map
A sorted map based heavily upon redis' skip list implementation.
Test
Run any of the following:
$ mocha$ npm test$ make test
Note: remember to npm install
!
Install
$ npm install sorted-map
API
var Map = ; var map = ; // average O(log(N))map;map;map; // => nullmap; // => 9 // average O(1)map; // => true // average O(1)map; // => 12 // average O(log(N))map; // => 8 // average O(1)map; // => null map;map;map;map;map;map;map; // average O(log(N)+M) where M is the number of elements between min and maxmap; // [14-16]// => [{key: '5a600e14', value: 14}, {key: '5a600e10', value: 16}] map; // [17-∞)// => [{key: '5a600e12', value: 17}, {key: '5a600e15', value: 19}] // average O(log(N)+log(M)) where M as in rangemap; // => 2 // more or less indexOf for the sorted values// average O(log(N))map; // => 0map; // => 3map; // => 5map; // => 8map; // => -1 // average O(log(N)+M) where M as in rangemap;// => [{key: '5a600e16', value: 3},// {key: '5a600e11', value: 6},// {key: '5a600e18', value: 10}] map; // => [{key: '5a600e16', value: 3}] maplength; // => 9
Intersection
var a = b = ; a;a;a;a;a;a;a;a;a;a;a; b;b;b;b;b;b;b;b;b;b;b; Map;// => ['5a600e10', '5a600e14', '5a600e17', '5a600e19', '5a600e1c', '5a600e15', '5a600e1b'] Map;// => ['5a600e1b', '5a600e14', '5a600e1c', '5a600e15', '5a600e19', '5a600e10', '5a600e17'] // works, but not preferreda;// => ['5a600e10', '5a600e14', '5a600e17', '5a600e19', '5a600e1c', '5a600e15', '5a600e1b'] var c = ; c;c;c;c;c;c;c;c;c;c; // for best performance, the smallest set should be firstMap;// => ['5a600e10', '5a600e14', '5a600e17', '5a600e1c']
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 map = unique: true; map;map;map;map;map;map;map;map;map; // currently O(log(N)) because it needs to attempt to insert the valuemap; // throwsmap; // => 14
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.