UNPKG

version-vector-with-exceptions

Version:
36 lines (28 loc) 752 B
'use strict'; module.exports = SortedArray var search = require('binary-search') function SortedArray(cmp, arr) { if (typeof cmp != 'function') throw new TypeError('comparator must be a function') this.arr = arr || [] this.cmp = cmp } SortedArray.prototype.insert = function(element) { var index = search(this.arr, element, this.cmp) if (index < 0) index = ~index this.arr.splice(index, 0, element) } SortedArray.prototype.indexOf = function(element) { var index = search(this.arr, element, this.cmp) return index >= 0 ? index : -1 } SortedArray.prototype.remove = function(element) { var index = search(this.arr, element, this.cmp) if (index < 0) return false this.arr.splice(index, 1) return true }