Skip to main content
Module

x/collections/mod.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
Latest
class Vector
implements Iterable<T>
Deprecated
Deprecated

Use Array instead. Arrays are generally faster for all other operations besides shifting and unshifting.

import { Vector } from "https://deno.land/x/collections@0.12.1/mod.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).

every(
callback: (
value: T,
index: number,
vector: Vector<T>,
) => unknown
,
start?: number,
end?: number,
): boolean

Returns true if a value in the vector satisfies the provided testing function or false if it is not found. Optionally, you can search a subset of the vector by providing an index range. 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.

every<U>(
callback: (
value: T,
index: number,
vector: Vector<T>,
) => unknown
,
thisArg?: U,
start?: number,
end?: number,
): boolean
find(
callback: (
value: T,
index: number,
vector: Vector<T>,
) => unknown
,
start?: number,
end?: number,
): T | undefined

Returns the first value in the vector that satisfies the provided testing function or undefined if it is not found. Optionally, you can search a subset of the vector by providing an index range. 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.

find<U>(
callback: (
value: T,
index: number,
vector: Vector<T>,
) => unknown
,
thisArg?: U,
start?: number,
end?: number,
): T | undefined
findIndex(
callback: (
value: T,
index: number,
vector: Vector<T>,
) => unknown
,
start?: number,
end?: number,
): number

Returns the index of the first value in the vector that satisfies the provided testing function or 1 if it is not found. Optionally, you can search a subset of the vector by providing an index range. 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.

findIndex<U>(
callback: (
value: T,
index: number,
vector: Vector<T>,
) => unknown
,
thisArg?: U,
start?: number,
end?: number,
): number
findLast(
callback: (
value: T,
index: number,
vector: Vector<T>,
) => unknown
,
start?: number,
end?: number,
): T | undefined

Returns the last value in the vector that satisfies the provided testing function or undefined if it is not found. Optionally, you can search a subset of the vector by providing an index range. 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.

findLast<U>(
callback: (
value: T,
index: number,
vector: Vector<T>,
) => unknown
,
thisArg?: U,
start?: number,
end?: number,
): T | undefined
findLastIndex(
callback: (
value: T,
index: number,
vector: Vector<T>,
) => unknown
,
start?: number,
end?: number,
): number

Returns the index of the last value in the vector that satisfies the provided testing function or -1 if it is not found. Optionally, you can search a subset of the vector by providing an index range. 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.

findLastIndex<U>(
callback: (
value: T,
index: number,
vector: Vector<T>,
) => unknown
,
thisArg?: U,
start?: number,
end?: number,
): number
forEach(
callback: (
value: T | undefined,
index: number,
vector: Vector<T>,
) => void
,
start?: number,
end?: number,
): void

Executes the provided function once for each value in the vector. Optionally, you can iterate a subset of the vector by providing an index range. 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.

forEach<U>(
callback: (
value: T | undefined,
index: number,
vector: Vector<T>,
) => void
,
thisArg?: U,
start?: number,
end?: number,
): void
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.

includes(
searchValue: T,
start?: number,
end?: number,
): boolean

Returns true if the search value can be found in the vector, or false if it is not found. This uses strict equality checks. Optionally, you can search a subset of the vector by providing an index range. 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.

indexOf(
searchValue: T,
start?: number,
end?: number,
): number

Returns the first index at which the search value can be found in the vector, or -1 if it is not found. This uses strict equality checks. Optionally, you can search a subset of the vector by providing an index range. 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.

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, converted to strings using their toString methods and separated by commas or a specified separator string.

lastIndexOf(
searchValue: T,
start?: number,
end?: number,
): number

Returns the last index at which the search value can be found in the vector, or -1 if it is not found. This uses strict equality checks. Optionally, you can search a subset of the vector by providing an index range. 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.

map<U>(
callback: (
v: T,
k: number,
vector: Vector<T>,
) => U
,
start?: number,
end?: number,
): Vector<U>

Creates a new vector from the results of executing the provided function for each value in the vector. Optionally, you can iterate a subset of the vector by providing an index range. 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.

map<U, V>(
callback: (
v: T,
k: number,
vector: Vector<T>,
) => U
,
thisArg?: V,
start?: number,
end?: number,
): Vector<U>
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.

Reverses the vector in place then returns it.

set(index: number, value: T | undefined)

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.

some(
callback: (
value: T,
index: number,
vector: Vector<T>,
) => unknown
,
start?: number,
end?: number,
): boolean

Returns true if a value in the vector satisfies the provided testing function or false if it is not found. Optionally, you can search a subset of the vector by providing an index range. 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.

some<U>(
callback: (
value: T,
index: number,
vector: Vector<T>,
) => unknown
,
thisArg?: U,
start?: number,
end?: number,
): boolean
sort(compare?: (a: T, b: T) => number)

Sorts the values of the vector in place then returns it. This uses Array sort method internally. If the vector has been shifted it may trigger reallocation before sorting.

splice(start: number, deleteCount?: number): Vector<T>

Changes the contents of an array in place by removing or replacing existing elements or inserting new values. Then returns an Vector of the values that were removed. Returns a shallow copy of a portion of the vector into a new vector. The start represents the index of value you may insert values before or delete values starting from. The deleteCount is the number of values you would like to delete from the vector. The deleteCount would default to the number of values between the index and the end of the vector. If the start value is negative, it will be subtracted from the end of the vector. If the deleteCount is less than 0, no values will be deleted. If any insert values are specified, they will be inserted before the start index.

splice(
start: number,
deleteCount: number,
...insertValues: (T | undefined)[],
): Vector<T>
toArray(): T[]

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

toLocaleString(): string

Creates and returns a new string concatenating all of the values in the Vector, converted to strings using their toLocaleString methods and separated by a locale-specific string.

toString(): string

Creates and returns a new string concatenating all of the values in the Vector, converted to strings using their toString methods and separated by commas.

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<T>): Vector<U>

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

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