Class HeapAsync<T>

Heap

Type Parameters

  • T

Hierarchy

  • HeapAsync

Implements

  • Iterable<Promise<T>>

Constructors

  • Heap instance constructor.

    Type Parameters

    • T

    Parameters

    • compare: AsyncComparator<T> = HeapAsync.minComparator

      Optional comparison function, defaults to Heap.minComparator

    Returns HeapAsync<T>

Properties

_limit: number = 0
compare: AsyncComparator<T> = HeapAsync.minComparator

Optional comparison function, defaults to Heap.minComparator

element: (() => undefined | T) = ...

Type declaration

    • (): undefined | T
    • Alias of peek

      Returns undefined | T

heapArray: T[] = []
offer: ((element) => Promise<boolean>) = ...

Type declaration

    • (element): Promise<boolean>
    • Alias of add

      Parameters

      • element: T

      Returns Promise<boolean>

poll: (() => Promise<undefined | T>) = ...

Type declaration

    • (): Promise<undefined | T>
    • Alias of pop

      Returns Promise<undefined | T>

Accessors

  • get length(): number
  • Length of the heap.

    Returns number

  • get limit(): number
  • Get length limit of the heap.

    Returns number

  • set limit(_l): void
  • Set length limit of the heap.

    Parameters

    • _l: number

    Returns void

Methods

  • Iterator interface

    Returns Iterator<Promise<T>, any, undefined>

  • Limit heap size if needed

    Returns void

  • Return the bottom (lowest value) N elements of the heap, without corner cases, unsorted

    Parameters

    • n: number

      Number of elements.

    Returns Promise<T[]>

    Array of length <= N.

  • Returns the inverse to the comparison function.

    Parameters

    • a: T
    • b: T

    Returns Promise<number>

  • Move a node to a new index, switching places

    Parameters

    • j: number

      First node index

    • k: number

      Another node index

    Returns void

  • Move a node down the tree (to the leaves) to find a place where the heap is sorted.

    Parameters

    • i: number

      Index of the node

    Returns Promise<void>

  • Move a node up the tree (to the root) to find a place where the heap is sorted.

    Parameters

    • i: number

      Index of the node

    Returns Promise<void>

  • Return index of the top element

    Parameters

    • list: T[]

    Returns Promise<number>

  • Return the top (highest value) N elements of the heap, without corner cases, unsorted Implementation: init + push.

    Parameters

    • n: number

      Number of elements.

    Returns Promise<T[]>

    Array of length <= N.

  • Return the top (highest value) N elements of the heap, without corner cases, unsorted Implementation: heap.

    Parameters

    • n: number

      Number of elements.

    Returns Promise<T[]>

    Array of length <= N.

  • Return the top (highest value) N elements of the heap, without corner cases, unsorted Implementation: push.

    Parameters

    • n: number

      Number of elements.

    Returns Promise<T[]>

    Array of length <= N.

  • Return the top element

    Parameters

    • Rest ...list: T[]

    Returns Promise<undefined | T>

  • Adds an element to the heap. Aliases: offer. Same as: push(element)

    Parameters

    • element: T

      Element to be added

    Returns Promise<boolean>

    true

  • Adds an array of elements to the heap. Similar as: push(element, element, ...).

    Parameters

    • elements: T[]

      Elements to be added

    Returns Promise<boolean>

    true

  • Return the bottom (lowest value) N elements of the heap.

    Parameters

    • n: number = 1

      Number of elements.

    Returns Promise<T[]>

    Array of length <= N.

  • Check if the heap is sorted, useful for testing purposes.

    Returns Promise<undefined | T>

    Returns an element if something wrong is found, otherwise it's undefined

  • Remove all of the elements from this heap.

    Returns void

  • Returns true if this queue contains the specified element.

    Parameters

    • o: T

      Element to be found

    • fn: AsyncIsEqual<T> = HeapAsync.defaultIsEqual

      Optional comparison function, receives (element, needle)

    Returns Promise<boolean>

  • Get the element at the given index.

    Parameters

    • i: number

      Index to get

    Returns T

    Element at that index

  • Get the elements of these node's children

    Parameters

    • idx: number

      Node index

    Returns T[]

    Children elements

  • Get the element of this node's parent

    Parameters

    • idx: number

      Node index

    Returns undefined | T

    Parent element

  • Initialise a heap, sorting nodes

    Parameters

    • Optional array: T[]

      Optional initial state array

    Returns Promise<void>

  • Test if the heap has no elements.

    Returns boolean

    True if no elements on the heap

  • Returns an iterator. To comply with Java interface.

    Returns Iterable<Promise<T>>

  • Get the leafs of the tree (no children nodes)

    Returns T[]

  • Top node. Aliases: element. Same as: top(1)[0]

    Returns undefined | T

    Top node

  • Extract the top node (root). Aliases: poll.

    Returns Promise<undefined | T>

    Extracted top node, undefined if empty

  • Pushes element(s) to the heap.

    Parameters

    • Rest ...elements: T[]

      Elements to insert

    Returns Promise<boolean>

    True if elements are present

  • Same as push & pop in sequence, but faster

    Parameters

    • element: T

      Element to insert

    Returns Promise<T>

    Extracted top node

  • Remove an element from the heap.

    Parameters

    • Optional o: T

      Element to be found

    • fn: AsyncIsEqual<T> = HeapAsync.defaultIsEqual

      Optional function to compare

    Returns Promise<boolean>

    True if the heap was modified

  • Pop the current peek value, and add the new item.

    Parameters

    • element: T

      Element to replace peek

    Returns Promise<T>

    Old peek

  • Clone the heap's internal array

    Returns T[]

  • String output, call to Array.prototype.toString()

    Returns string

  • Return the top (highest value) N elements of the heap.

    Parameters

    • n: number = 1

      Number of elements.

    Returns Promise<T[]>

    Array of length <= N.

  • Default equality function.

    Type Parameters

    • N

    Parameters

    • a: N

      First element

    • b: N

      Second element

    Returns Promise<boolean>

    True if equal, false otherwise

  • Gets children indices for given index.

    Parameters

    • idx: number

      Parent index

    Returns number[]

    Array of children indices

  • Gets parent index for given index.

    Parameters

    • idx: number

      Children index

    Returns number

    Parent index, -1 if idx is 0

  • Gets sibling index for given index.

    Parameters

    • idx: number

      Children index

    Returns number

    Sibling index, -1 if idx is 0

  • Return the n least valuable elements of a heap-like Array

    Type Parameters

    • N

    Parameters

    • heapArr: N[]

      Array, should be an array-heap

    • n: number = 1

      Max number of elements

    • Optional compare: AsyncComparator<N>

      Optional compare function

    Returns Promise<N[]>

    Elements

  • Converts an array into an array-heap, in place

    Type Parameters

    • N

    Parameters

    • arr: N[]

      Array to be modified

    • Optional compare: AsyncComparator<N>

      Optional compare function

    Returns Promise<HeapAsync<N>>

    For convenience, it returns a Heap instance

  • Extract the peek of an array-heap

    Type Parameters

    • N

    Parameters

    • heapArr: N[]

      Array to be modified, should be a heap

    • Optional compare: AsyncComparator<N>

      Optional compare function

    Returns Promise<undefined | N>

    Returns the extracted peek

  • Pushes a item into an array-heap

    Type Parameters

    • N

    Parameters

    • heapArr: N[]

      Array to be modified, should be a heap

    • item: N

      Item to push

    • Optional compare: AsyncComparator<N>

      Optional compare function

    Returns Promise<void>

  • Push followed by pop, faster

    Type Parameters

    • N

    Parameters

    • heapArr: N[]

      Array to be modified, should be a heap

    • item: N

      Item to push

    • Optional compare: AsyncComparator<N>

      Optional compare function

    Returns Promise<N>

    Returns the extracted peek

  • Replace peek with item

    Type Parameters

    • N

    Parameters

    • heapArr: N[]

      Array to be modified, should be a heap

    • item: N

      Item as replacement

    • Optional compare: AsyncComparator<N>

      Optional compare function

    Returns Promise<N>

    Returns the extracted peek

  • Return the n most valuable elements of a heap-like Array

    Type Parameters

    • N

    Parameters

    • heapArr: N[]

      Array, should be an array-heap

    • n: number = 1

      Max number of elements

    • Optional compare: AsyncComparator<N>

      Optional compare function

    Returns Promise<N[]>

    Elements

  • Max heap comparison function.

    Type Parameters

    • N

    Parameters

    • a: N

      First element

    • b: N

      Second element

    Returns Promise<number>

    0 if they're equal, positive if a goes up, negative if b goes up

  • Max number heap comparison function.

    Parameters

    • a: number

      First element

    • b: number

      Second element

    Returns Promise<number>

    0 if they're equal, positive if a goes up, negative if b goes up

  • Min heap comparison function, default.

    Type Parameters

    • N

    Parameters

    • a: N

      First element

    • b: N

      Second element

    Returns Promise<number>

    0 if they're equal, positive if a goes up, negative if b goes up

  • Min number heap comparison function, default.

    Parameters

    • a: number

      First element

    • b: number

      Second element

    Returns Promise<number>

    0 if they're equal, positive if a goes up, negative if b goes up

  • Return the n most valuable elements of an iterable

    Type Parameters

    • N

    Parameters

    • n: number

      Max number of elements

    • iterable: Iterable<N>
    • Optional compare: AsyncComparator<N>

      Optional compare function

    Returns Promise<N[]>

    Elements

  • Return the n least valuable elements of an iterable

    Type Parameters

    • N

    Parameters

    • n: number

      Max number of elements

    • iterable: Iterable<N>
    • Optional compare: AsyncComparator<N>

      Optional compare function

    Returns Promise<N[]>

    Elements

  • Prints a heap.

    Type Parameters

    • N

    Parameters

    Returns string

Generated using TypeDoc