A vector is a sequence with fast member access by sequence number.

import { Vector } from "scl"

Inserting elements anywhere else than at the end is very slow and should be avoided. When inserting, the vector may need to re-allocate to provide enough room for the new element.

The following table summarises the time complexity of the most commonly used properties.

Property name Worst-case
append() O(n)
at() O(1)
getAt() O(1)
insertAfter() O(n)
insertBefore() O(n)
deleteAt() O(n)
prepend() O(n)
size O(1)
const v = new Vector<number>()
v.append(1)
v.append(2)
v.append(3)
assert.strictEqual(v.size, 3)

Type Parameters

  • T

    The type of element in the collection.

Implements

Constructors

  • Construct a new vector.

    const v1 = new Vector<number>([1, 2, 3, 4, 5])
    assert.strictEqual(v.size, 5)
    const v2 = new Vector<number>({ capacity: 1024 })
    for (let i = 0; i < 1024; i++) {
    v2.append(i)
    }

    Type Parameters

    • T

    Parameters

    • opts: Iterable<T, any, any> | VectorOptions<T> = {}

      Additional options to customize the newly created vector.

    Returns Vector<T>

Accessors

  • get capacity(): number
  • Get how much elements this vector can hold before it needs to re-allocate.

    Returns number

Methods

  • Returns an object which is able to sift through the values in this collection.

    The order by which the elements are traversed depends on the kind of collection. For unordered collections, the iteration order is unspecified and may even differ between two iterations on the same collection.

    Returns Generator<T, void, unknown>

  • Add an element to the collection. If the element already exists, update its value.

    The location where the element is placed depends on the collection type, and in the generic case there is no guarantee on the location where it is inserted.

    This method returns a pair with the first element indicating whether the element was added, while the second element refers to the actual location of the element.

    Parameters

    • el: T

    Returns [boolean, VectorCursor<T>]

  • Ensure the vector can store at least count amount of elements.

    The size property of this vector is never changed during this call.

    Parameters

    • count: number

    Returns void

  • Append an item at the end of the collection. The element will be given the highest order.

    Parameters

    • el: T

    Returns VectorCursor<T>

  • Return a cursor that is placed at the index given by position in the sequence.

    Parameters

    • position: number

    Returns VectorCursor<T>

  • Remove all elements from this collection, effectively setting the collection to the empty collection.

    Returns void

  • Remove an element from the collection. If multiple elements are matched, the collection picks one of them.

    Parameters

    • el: T

    Returns boolean

    true if the element was found, false otherwise.

  • Remove an element from the collection. If multiple elements are matched, the collection removes all of them.

    Parameters

    • element: T

    Returns number

    The amount of elements that was removed.

  • Remove the element pointed to by the iterator result from this collection.

    Parameters

    • pos: VectorCursor<T>

    Returns void

  • Parameters

    • index: number

    Returns void

  • Get the first element in the sequence.

    Returns T

    An error object if the collection is empty.

  • Allows taking an element directly out of the collection at a given position.

    This method might be faster than at because it is not forced to construct a cursor object.

    Parameters

    • index: number

    Returns T

  • Checks if the collection holds the given element.

    Parameters

    • element: T

      The element to check membership of.

    Returns boolean

    True if the collections holds the given element, false otherwise.

  • Insert an element after the element at the given position. The position is deduced from the iterator that is given to the method.

    Parameters

    • pos: VectorCursor<T>
    • element: T

    Returns VectorCursor<T>

  • Insert an element before the element at the given position. The position is deduced from the iterator that is goven to the method.

    Parameters

    • pos: VectorCursor<T>
    • element: T

    Returns VectorCursor<T>

  • Get the last element in the collection.

    Returns T

    An error object if the collection is empty.

  • Prepend an item to the beginning of the collection. The element will be given the lowest order.

    Parameters

    • el: T

    Returns VectorCursor<T>

  • Parameters

    • index: number
    • element: T

    Returns void

  • Parameters

    • a: number
    • b: number

    Returns VectorRange<T>

  • Parameters

    • a: number
    • b: number

    Returns void