Options
All
  • Public
  • Public/Protected
  • All
Menu

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)
see

DoubleLinkedList

see

SingleLinkedList

Type parameters

  • T

    The type of element in the collection.

Hierarchy

  • Vector

Implements

Index

Constructors

constructor

  • new Vector<T>(opts?: Iterable<T> | VectorOptions<T>): Vector<T>
  • 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> | VectorOptions<T> = ...

      Additional options to customize the newly created vector.

    Returns Vector<T>

Accessors

capacity

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

    see

    Vector.size

    Returns number

size

  • get size(): number

Methods

[Symbol.iterator]

  • [Symbol.iterator](): Generator<T, void, unknown>
  • 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

  • add(el: T): [boolean, VectorCursor<T>]
  • Parameters

    • el: T

    Returns [boolean, VectorCursor<T>]

allocate

  • allocate(count: number): void
  • 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

  • append(el: T): VectorCursor<T>
  • Parameters

    • el: T

    Returns VectorCursor<T>

at

  • at(position: number): VectorCursor<T>
  • Return a cursor that is placed at the index given by position in the sequence.

    Parameters

    • position: number

    Returns VectorCursor<T>

clear

  • clear(): void
  • Remove all elements from this collection, effectively setting the collection to the empty collection.

    Returns void

clone

delete

  • delete(el: T): boolean

deleteAll

  • deleteAll(element: T): number

deleteAt

  • deleteAt(pos: VectorCursor<T>): void
  • Parameters

    • pos: VectorCursor<T>

    Returns void

deleteAtIndex

  • deleteAtIndex(index: number): void

first

  • first(): T

getAt

  • getAt(index: number): T
  • 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

has

  • has(element: T): boolean

insertAfter

  • insertAfter(pos: VectorCursor<T>, element: T): VectorCursor<T>
  • Parameters

    • pos: VectorCursor<T>
    • element: T

    Returns VectorCursor<T>

insertBefore

  • insertBefore(pos: VectorCursor<T>, element: T): VectorCursor<T>
  • Parameters

    • pos: VectorCursor<T>
    • element: T

    Returns VectorCursor<T>

last

  • last(): T

prepend

  • prepend(el: T): VectorCursor<T>
  • Parameters

    • el: T

    Returns VectorCursor<T>

replace

  • replace(index: number, element: T): void
  • Parameters

    • index: number
    • element: T

    Returns void

shrinkFit

  • shrinkFit(): void

slice

  • slice(a: number, b: number): VectorRange<T>
  • Parameters

    • a: number
    • b: number

    Returns VectorRange<T>

swap

  • swap(a: number, b: number): void
  • Parameters

    • a: number
    • b: number

    Returns void

toRange

  • toRange(): VectorRange<T>

Legend

  • Property
  • Method
  • Accessor
  • Property
  • Method
  • Inherited property
  • Inherited method

Generated using TypeDoc