sortedset
A fast, simple sorted set implementation for javascript (browser and node) that allows a custom compare function.
Similar API to javascript's Set.
npm install sortedset
Compare Function
The compare function is the same thing that works with Array#sort().
- If
compare(a, b) < 0
,a
comes beforeb
. - If
compare(a, b) === 0
,a
andb
are considered identical in the set. - If
compare(a, b) > 0
,b
comes beforea
.
If no compare function is provided, a default ascending comparator is used.
const SortedSet = const set = 2 1 3 console // [1, 2, 3]
Or pass in a custom comparator.
const SortedSet = { if a === b return 0 return a < b ? 1 : -1} const set = 2 1 3 reverse console // [3, 2, 1]
API
Constructor:
new SortedSet()
new SortedSet([2, 1, 3])
new SortedSet(customCompare)
new SortedSet([2, 1, 3], customCompare)
Properties:
SortedSet#size
Methods:
SortedSet#add(item)
SortedSet#delete(item)
Note: Returns the deleted item if it existed, else undefinedSortedSet#clear()
SortedSet#has(item)
SortedSet is iterable:
const set = 2 1 3 for const item of set console