Skip to main content
Module

x/collections/vector.ts>Vector

Collection data structures that are not standard built-in objects in JavaScript. This includes a vector (double-ended queue), binary heap (priority queue), binary search tree, and a red black tree.
Very Popular
Go to Latest
class Vector
implements Iterable<T>
import { Vector } from "https://deno.land/x/collections@v0.8.0/vector.ts";

A double-ended queue implemented with a growable ring buffer. Vector is faster than JavaScript's built in Array class for shifting and unshifting because it only requires reallocation when increasing the capacity.

Constructors

new
Vector(capacity?: number)

Properties

private
_capacity: number
private
_length: number
private
data: (T | undefined)[]
private
end
private
start: number
capacity: number

The vector will be able to hold this many values without reallocating. If the length exceeds the capacity, then the capacity will be increased. Changing the capacity may trigger reallocation. Changing the capacity to less than the length will change the length to be equal to the new capacity.

length: number

The amount of values stored in the vector. You can set the length to truncate the vector. If you increase the length by setting it, the new slots will be empty.

Methods

clear(): void

Removes all values from the vector.

concat<U>(...values: (Vector<U> | ConcatArray<U>)[]): Vector<U>

Merges two or more iterables together. This does not change existing Iterables, it returns a new Vector.

delete(index: number): T | undefined

Removes and returns the value at index from the vector. If the value is negative, it will be subtracted from the end. The values between the index and the end will be shifted to the left.

drain(): IterableIterator<T>

Returns an iterator for retrieving and removing values from the vector (from left-to-right).

drainRight(): IterableIterator<T>

Returns an iterator for retrieving and removing values from the vector (from right-to-left).

get(index: number): T | undefined

Returns the value at the given index. If the value is negative, it will be subtracted from the end. The index 0 would return the first value in the vector. The index -1 would return the last value in the vector.

isEmpty(): boolean

Checks if the vector is empty.

join(separator?): string

Creates and returns a new string concatenating all of the values in the Vector, separated by commas or a specified separator string.

peek(): T | undefined

Returns the first value in the vector, or undefined if it is empty.

peekRight(): T | undefined

Returns the last value in the vector, or undefined if it is empty.

pop(): T | undefined

Removes the last value from the vector and returns it, or undefined if it is empty.

push(...values: T[]): number

Adds values to the end of the vector.

reduce<U>(callback: (
previousValue: U,
currentValue: T,
currentIndex: number,
) => U
, initialValue?: U
): U

Applies a function against an accumulator and each value of the vector (from left-to-right) to reduce it to a single value. If no initial value is supplied, the first value in the vector will be used and skipped. Calling reduce on an empty array without an initial value creates a TypeError.

reduceRight<U>(callback: (
previousValue: U,
currentValue: T,
currentIndex: number,
) => U
, initialValue?: U
): U

Applies a function against an accumulator and each value of the vector (from right-to-left) to reduce it to a single value. If no initial value is supplied, the last value in the vector will be used and skipped. Calling reduce on an empty array without an initial value creates a TypeError.

set(index: number, value: T)

Sets the value at the given index, then returns the value. If the value is negative, it will be subtracted from the end. The index 0 would set the first value in the vector. The index -1 would set the last value in the vector. If the absolute index value is greater than the length, the size will be increased to match before setting the value.

shift(): T | undefined

Removes the first value from the vector and returns it, or undefined if it is empty.

shrinkToFit(): void

Shrinks the capacity to be equal to the length.

slice(start?, end?: number): Vector<T>

Returns a shallow copy of a portion of the vector into a new vector. The start and end represent the index of values in the vector. The end is exclusive meaning it will not be included. If the index value is negative, it will be subtracted from the end of the vector. For example, vector.slice(-2) would return a new vector containing the last 2 values.

toArray(): T[]

Converts the vector to an array. It's recommended to use this instead of Array.from because this method is significantly faster.

unshift(...values: T[]): number

Adds values to the start of the vector.

values(): IterableIterator<T>

Returns an iterator for retrieving values from the vector (from left-to-right).

valuesRight(): IterableIterator<T>

Returns an iterator for retrieving values from the vector (from right-to-left).

[Symbol.iterator](): IterableIterator<T>

Returns an iterator for retrieving values from the vector (from left-to-right).

Static Methods

from<T, U, V>(collection: ArrayLike<T> | Iterable<T>): Vector<U>

Creates a new vector from an array like or iterable object.

from<T, U, V>(collection: ArrayLike<T> | Iterable<T>, options: { map: map<T, U>; thisArg?: V; }): Vector<U>