A collection of values with the largest always on top.
A heap is a binary tree where each node is greater than both its leaves. The tree itself is complete or nearly complete at all times, so the heap is backed by a compact array. When values are added or removed, the tree rotates until the value has sunk until its parent is greater, or floated until all children are less.
Values are presumed to not change in relative position without first being
removed, and perhaps added back, or adjusted after mutation using sink(index)
or float(index)
.
The number of items in this collection.
Adds values to the end of a collection.
Removes a value from the end of a collection, and returns that value.
Returns the value at the beginning of a collection, the value that would be returned by shift()
.
Deletes the first equivalent value. Returns whether the key was found and successfully deleted.
Copies values or entries from another collection into this collection, and then returns this.
Deletes every value or every value for each key. Returns the number of successful deletions.
Deletes all of the values in the collection.
Returns the position of a value, or -1 if the value is not found.
Iterates every value in this collection.
Calls the callback for each entry in the collection.
Returns an array of the respective return values of a callback for each entry in this collection.
Returns an array with each value from this collection that passes the given test.
Aggregates every value in this collection with the result collected up to that index.
Aggregates every value in this collection, from right to left.
Returns an array of [key, class] entries where every value from the collection is placed into the same equivalence class if they return the same key through the given callback.
Returns whether any entry in this collection passes a given test.
Returns whether every entry in this collection passes a given test.
Returns whether any value in the collection is truthy.
Returns whether all values in the collection are truthy.
Returns one, arbitrary value from this collection, or undefined if there are none.
Returns the only value in this collection, or undefined if there is more than one value, or if there are no values in the collection.
Returns a sorted array of the values in this collection.
Returns a copy of this collection with the values in reverse order.
Returns a string of all the values in the collection delimited by the given string.
Returns the sum of all values in this collection.
Returns the arithmetic mean of the collection, by computing its sum and the count of values and returning the quotient.
Returns the smallest value in this collection.
Returns the largest value in this collection.
Returns an array of the respective values in this collection and in each collection provided as an argument.
Returns an array of [index, value] entries for each value in this collection, counting all values from the given index.
Returns a new collection of the same type containing all the values of itself and the values of any number of other iterable collections in order.
Assuming that this is a collection of collections, returns a new collection that contains all the values of each nested collection in order.
Returns an array of each value in this collection.
Returns an object with each property name and value corresponding to the entries in this collection.
Used by JSON.stringify
to create a JSON representation of the collection.
Creates a deep replica of this collection.
Creates a shallow clone of this collection.
The compare
function used by this collection to determine how to order its own values.
The equals
function used to check whether values in this collection are equivalent.
Adds a listener for when values are added or removed at any position.
Unregisters a range change listener provided by addRangeChangeListener.
Informs range change listeners that values were removed then added at an index.
Adds a listener for before values are added or removed at any position.
Unregisters a range change listener provided by addBeforeRangeChangeListener or addRangeChangeListener with the beforeChange
flag.
Informs range change listeners that values will be removed then added at an index.
Adds a listener for when the value for a key changes, or when entries are added or removed.
Adds a listener for before map entries are created, deleted, or updated.
Unregisters a map change listener provided by addMapChangeListener.
Unregisters a map change listener provided by addBeforeMapChangeListener or addMapChangeListener with the beforeChange
flag.
Informs map change listeners that an entry was created, deleted, or updated.
Informs map change listeners that an entry will be created, deleted, or updated.
Adds a listener for an owned property with the given name.
Adds a listener for before a property changes.
Unregisters a property change listener provided by addOwnPropertyChangeListener.
Unregisters a property change listener provided by addBeforeOwnPropertyChangeListener or addOwnPropertyChangeListener with the beforeChange
flag.
Informs property change listeners that the value for a property name has changed.
Informs property change listeners that the value for a property name will change.
May perform internal changes necessary to dispatch property changes for a particular name.
var Heap = require("collections/heap");
var heap = new Heap([2, 8, 5]); heap.push(6) heap.pop(); heap.peek();
var minHeap = new Heap([2, 8, 5], null, function (a, b) { return b - a ; }); minHeap.push(6) minHeap.pop(); minHeap.peek();