The constructor initializes a Deque object with optional iterable of elements and options.
An iterable object (such as an array or a Set) that contains the initial
elements to be added to the deque. It can also be an object with a length
or size
property
that represents the number of elements in the iterable object. If no elements are provided, an
empty deque
Optional
options: DequeOptions<E, R>The options
parameter is an optional object that can contain
configuration options for the deque. In this code, it is used to set the bucketSize
option,
which determines the size of each bucket in the deque. If the bucketSize
option is not provided
or is not a number
Protected
_createProtected
_getTime Complexity: O(1) Space Complexity: O(1)
The function calculates the bucket index and index within the bucket based on the given position.
The pos
parameter represents the position within the data structure. It is
a number that indicates the index or position of an element within the structure.
an object with two properties: "bucketIndex" and "indexInBucket".
Protected
_getProtected
_getProtected
_reallocateTime Complexity: O(n) Space Complexity: O(n)
The _reallocate
function reallocates the buckets in an array, adding new buckets if needed.
Optional
needBucketNum: numberThe needBucketNum
parameter is an optional number that
specifies the number of new buckets needed. If not provided, it will default to half of the
current bucket count (this._bucketCount >> 1
) or 1 if the current bucket count is less than 2.
Time Complexity: O(n) Space Complexity: O(1)
The function is an implementation of the Symbol.iterator method that returns an IterableIterator.
Rest
...args: any[]The args
parameter in the code snippet represents a rest parameter. It
allows the function to accept any number of arguments as an array. In this case, the args
parameter is used to pass any number of arguments to the _getIterator
method.
Time Complexity: O(n) Space Complexity: O(n)
The addAt
function inserts one or more elements at a specified position in an array-like data
structure.
The pos
parameter represents the position at which the element(s) should
be inserted. It is of type number
.
The element
parameter represents the element that you want to insert into
the array at the specified position.
Optional
num: number = 1The num
parameter represents the number of times the element
should be
inserted at the specified position (pos
). By default, it is set to 1, meaning that the element
will be inserted once. However, you can provide a different value for num
if you want
The size of the array after the insertion is being returned.
Time Complexity: O(1) Space Complexity: O(1)
The at
function retrieves an element at a specified position in an array-like data structure.
The pos
parameter represents the position of the element that you want to
retrieve from the data structure. It is of type number
and should be a valid index within the
range of the data structure.
The element at the specified position in the data structure is being returned.
Time Complexity: O(n) Space Complexity: O(n)
The clone()
function returns a new instance of the Deque
class with the same elements and
bucket size as the original instance.
The clone()
method is returning a new instance of the Deque
class with the same
elements as the original deque (this
) and the same bucket size.
Time Complexity: O(1) Space Complexity: O(1)
The cut
function updates the state of the object based on the given position and returns the
updated size.
The pos
parameter represents the position at which the string should be
cut. It is a number that indicates the index of the character where the cut should be made.
If true, the original deque will not be cut, and return a new deque
The method is returning the updated size of the data structure.
Time Complexity: O(1) Space Complexity: O(1) or O(n)
The cutRest
function cuts the elements from a specified position in a deque and returns a new
deque with the cut elements.
The pos
parameter represents the position from which to cut the Deque. It
is a number that indicates the index of the element in the Deque where the cut should start.
Optional
isCutSelf: boolean = falseisCutSelf is a boolean parameter that determines whether the original Deque should be modified or a new Deque should be created. If isCutSelf is true, the original Deque will be modified by cutting off elements starting from the specified position. If isCutSelf is false, a new De
The function cutRest
returns either the modified original deque (this
) or a new deque
(newDeque
) depending on the value of the isCutSelf
parameter.
Time Complexity: O(n) Space Complexity: O(1)
The delete
function removes all occurrences of a specified element from an array-like data
structure.
The element
parameter represents the element that you want to delete from
the data structure.
The size of the data structure after the element has been deleted.
Time Complexity: O(n) Space Complexity: O(1) or O(n)
The deleteAt
function removes an element at a specified position in an array-like data
structure.
The pos
parameter in the deleteAt
function represents the position at
which an element needs to be deleted from the data structure. It is of type number
and indicates
the index of the element to be deleted.
The size of the data structure after the deletion operation is performed.
Time Complexity: O(n) Space Complexity: O(1)
The every
function checks if every element in the array satisfies a given predicate.
The predicate
parameter is a callback function that takes three arguments:
the current element being processed, its index, and the array it belongs to. It should return a
boolean value indicating whether the element satisfies a certain condition or not.
Optional
thisArg: anyThe thisArg
parameter is an optional argument that specifies the value
to be used as this
when executing the predicate
function. If thisArg
is provided, it will be
passed as the this
value to the predicate
function. If thisArg
is
The every
method is returning a boolean value. It returns true
if every element in
the array satisfies the provided predicate function, and false
otherwise.
Time Complexity: O(n) Space Complexity: O(1)
The fill
function in TypeScript fills a specified range in an array-like object with a given
value.
The value
parameter in the fill
method represents the element that will be
used to fill the specified range in the array.
Optional
start: number = 0The start
parameter specifies the index at which to start filling the array
with the specified value. If not provided, it defaults to 0, indicating the beginning of the
array.
The end
parameter in the fill
function represents the index at which the filling
of values should stop. It specifies the end of the range within the array where the value
should
be filled.
The fill
method is returning the modified object (this
) after filling the specified
range with the provided value.
Time Complexity: O(n) Space Complexity: O(n)
The filter
function creates a new deque containing elements from the original deque that satisfy
a given predicate function.
The predicate
parameter is a callback function that takes three arguments:
the current element being iterated over, the index of the current element, and the deque itself.
It should return a boolean value indicating whether the element should be included in the filtered
deque or not.
Optional
thisArg: anyThe thisArg
parameter is an optional argument that specifies the value
to be used as this
when executing the predicate
function. If thisArg
is provided, it will be
passed as the this
value to the predicate
function. If thisArg
is
The filter
method is returning a new Deque
object that contains the elements that
satisfy the given predicate function.
Time Complexity: O(n) Space Complexity: O(1)
The findIndex
function iterates over an array and returns the index of the first element that
satisfies the provided predicate function.
The predicate
parameter in the findIndex
function is a callback function
that takes three arguments: item
, index
, and the array this
. It should return a boolean
value indicating whether the current element satisfies the condition being checked for.
Optional
thisArg: anyThe thisArg
parameter in the findIndex
function is an optional
parameter that specifies the value to use as this
when executing the predicate
function. If
provided, the predicate
function will be called with thisArg
as its this
value. If @returns The
findIndex` method is returning the index of the first element in the array that
satisfies the provided predicate function. If no such element is found, it returns -1.
Time Complexity: O(n) Space Complexity: O(1)
The forEach
function iterates over each element in an array-like object and calls a callback
function for each element.
The callbackfn parameter is a function that will be called for each element in the array. It takes three arguments: the current element being processed, the index of the current element, and the array that forEach was called upon.
Optional
thisArg: anyThe thisArg
parameter is an optional argument that specifies the value
to be used as this
when executing the callbackfn
function. If thisArg
is provided, it will
be passed as the this
value to the callbackfn
function. If `thisArg
Time Complexity: O(n) Space Complexity: O(1)
The function checks if a given element exists in a collection.
The parameter "element" is of type E, which means it can be any type. It represents the element that we want to check for existence in the collection.
a boolean value. It returns true if the element is found in the collection, and false otherwise.
Time Complexity: O(n) Space Complexity: O(1)
The function indexOf searches for a specified element starting from a given index in an array-like object and returns the index of the first occurrence, or -1 if not found.
The searchElement
parameter in the indexOf
function represents the
element that you want to find within the array. The function will search for this element starting
from the fromIndex
(if provided) up to the end of the array. If the searchElement
is found
within the
Optional
fromIndex: number = 0The fromIndex
parameter in the indexOf
function represents the
index at which to start searching for the searchElement
within the array. If provided, the
search will begin at this index and continue to the end of the array. If fromIndex
is not
specified, the default
The indexOf
method is returning the index of the searchElement
if it is found in the
array starting from the fromIndex
. If the searchElement
is not found, it returns -1.
Time Complexity: O(n) Space Complexity: O(1)
The join
function in TypeScript returns a string by joining the elements of an array with a
specified separator.
Optional
separator: string = ','The separator
parameter is a string that specifies the character
or characters that will be used to separate each element when joining them into a single string.
By default, the separator is set to a comma (,
), but you can provide a different separator if
needed.
The join
method is being returned, which takes an optional separator
parameter
(defaulting to a comma) and returns a string created by joining all elements of the array after
converting it to an array.
Time Complexity: O(n) Space Complexity: O(1)
The function lastIndexOf
in TypeScript returns the index of the last occurrence of a specified
element in an array.
The searchElement
parameter is the element that you want to find the
last index of within the array. The lastIndexOf
method will search the array starting from the
fromIndex
(or the end of the array if not specified) and return the index of the last occurrence
of the
The fromIndex
parameter in the lastIndexOf
method specifies the
index at which to start searching for the searchElement
in the array. By default, it starts
searching from the last element of the array (this.length - 1
). If a specific fromIndex
is
provided
The last index of the searchElement
in the array is being returned. If the
searchElement
is not found in the array, -1 is returned.
Time Complexity: O(n) Space Complexity: O(n)
The map
function takes a callback function and applies it to each element in the deque,
returning a new deque with the results.
The callback parameter is a function that will be called for each element in the deque. It takes three arguments: the current element, the index of the element, and the deque itself. It should return a value of type EM.
Optional
toElementFn: ((rawElement: RM) => EM)The toElementFn
parameter is an optional function that can be used to
transform the raw element (RM
) into a new element (EM
) before adding it to the new deque. If
provided, this function will be called for each raw element in the original deque.
Optional
thisArg: anyThe thisArg
parameter is an optional argument that allows you to
specify the value of this
within the callback function. It is used to set the context or scope
in which the callback function will be executed. If thisArg
is provided, it will be used as the
value of
a new Deque object with elements of type EM and raw elements of type RM.
Time Complexity - Amortized O(1) (possible reallocation), Space Complexity - O(n) (due to potential resizing).
The push function adds an element to a data structure and reallocates memory if necessary.
The element
parameter represents the value that you want to add to the data
structure.
The size of the data structure after the element has been pushed.
Time Complexity: O(k) Space Complexity: O(k)
The function pushMany
iterates over elements and pushes them into an array after applying a
transformation function if provided.
The elements
parameter in the pushMany
function is expected to be an iterable containing elements of type E
or R
. It can be either an IterableWithSizeOrLength<E>
or an IterableWithSizeOrLength<R>
. The
function iterates over each element
The pushMany
function is returning an array of boolean values, where each value
represents the result of calling the push
method on the current object instance with the
corresponding element from the input elements
iterable.
Time Complexity: O(n) Space Complexity: O(1)
The reverse() function reverses the order of the buckets and the elements within each bucket in a data structure.
The reverse() method is returning the object itself (this) after performing the reverse operation on the buckets and updating the relevant properties.
Time Complexity: O(1) Space Complexity: O(1)
The setAt
function sets an element at a specific position in an array-like data structure.
The pos
parameter represents the position at which the element needs to be
set. It is of type number
.
The element
parameter is the value that you want to set at the specified
position in the data structure.
Time Complexity: O(1) Space Complexity: O(1)
The shift()
function removes and returns the first element from a data structure, updating the
internal state variables accordingly.
The element that is being removed from the beginning of the data structure is being returned.
Time Complexity: O(n) Space Complexity: O(n)
The shrinkToFit
function reorganizes the elements in an array-like data structure to minimize
memory usage.
Nothing is being returned. The function is using the return
statement to exit early if
this._length
is 0, but it does not return any value.
Time Complexity: O(m) Space Complexity: O(m)
The slice
function in TypeScript creates a new instance by extracting a portion of elements from
the original instance based on the specified start and end indices.
Optional
start: number = 0The start
parameter in the slice
method represents the index at
which to begin extracting elements from an array-like object. If no start
parameter is provided,
the default value is 0, meaning the extraction will start from the beginning of the array.
The end
parameter in the slice
method represents the index at which to
end the slicing. By default, if no end
parameter is provided, it will slice until the end of the
array (i.e., this.length
).
The slice
method is returning a new instance of the object with elements sliced from
the specified start index (default is 0) to the specified end index (default is the length of the
object).
Time Complexity: O(n) Space Complexity: O(1)
The "some" function checks if at least one element in a collection satisfies a given predicate.
The predicate
parameter is a callback function that takes three arguments:
value
, index
, and array
. It should return a boolean value indicating whether the current
element satisfies the condition.
Optional
thisArg: anyThe thisArg
parameter is an optional argument that specifies the value
to be used as the this
value when executing the predicate
function. If thisArg
is provided,
it will be passed as the this
value to the predicate
function. If `thisArg
a boolean value. It returns true if the predicate function returns true for any element in the collection, and false otherwise.
Time Complexity: O(n log n) Space Complexity: O(n)
The sort
function in TypeScript sorts the elements of a collection using a specified comparison
function.
Optional
compareFn: ((a: E, b: E) => number)The compareFn
parameter is a function that defines the sort order. It takes
two elements a
and b
as input and returns a number indicating their relative order. If the
returned value is negative, a
comes before b
. If the returned value is positive, @returns The
sort` method is returning the instance of the object on which it is called (this),
after sorting the elements based on the provided comparison function (compareFn).
Time Complexity: O(n) Space Complexity: O(1)
The splice
function in TypeScript overrides the default behavior to remove and insert elements
in a Deque data structure while ensuring the starting position and delete count are within bounds.
The start
parameter in the splice
method represents the index at which
to start changing the array. Items will be removed or added starting from this index.
The deleteCount
parameter in the splice
method represents the
number of elements to remove from the array starting at the specified start
index. If
deleteCount
is not provided, it defaults to the number of elements from the start
index to the
end of the array (@param {E[]} items - The
itemsparameter in the
splicemethod represents the elements that will be inserted into the deque at the specified
startindex. These elements will be inserted in place of the elements that are removed based on the
startand
deleteCountparameters. @returns The
splicemethod is returning the array
deletedElements` which contains the elements
that were removed from the Deque during the splice operation.
Rest
...items: E[]Time Complexity: Amortized O(1) Space Complexity: O(n)
The unshift
function adds an element to the beginning of an array-like data structure and
returns the new size of the structure.
The element
parameter represents the element that you want to add to the
beginning of the data structure.
The size of the data structure after the element has been added.
Time Complexity: O(k) Space Complexity: O(k)
The unshiftMany
function in TypeScript iterates over elements and adds them to the beginning of
an array, optionally converting them using a provided function.
The elements
parameter in the unshiftMany
function is an iterable containing elements of type E
or R
. It
can be an array or any other iterable data structure that has a known size or length. The function
iterates over each element in the elements
iterable and
The unshiftMany
function returns an array of boolean values indicating whether each
element was successfully added to the beginning of the array.
Example
Example