docs: Deque docs enriched, big o notations

This commit is contained in:
Revone 2023-11-19 08:56:37 +08:00
parent a4a35731d2
commit 5c6803b1a9
5 changed files with 485 additions and 13 deletions

View file

@ -8,7 +8,7 @@ All notable changes to this project will be documented in this file.
- [Semantic Versioning](https://semver.org/spec/v2.0.0.html)
- [`auto-changelog`](https://github.com/CookPete/auto-changelog)
## [v1.45.3](https://github.com/zrwusa/data-structure-typed/compare/v1.35.0...main) (upcoming)
## [v1.46.1](https://github.com/zrwusa/data-structure-typed/compare/v1.35.0...main) (upcoming)
### Changes

View file

@ -807,8 +807,53 @@ Array.from(dijkstraResult?.seen ?? []).map(vertex => vertex.key) // ['A', 'B', '
[//]: # (No deletion!!! Start of Replace Section)
<div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>comparison</span></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>SRC 10,000 add</td><td>0.57</td><td>1745.23</td><td>4.83e-6</td></tr><tr><td>CJS 10,000 add</td><td>0.57</td><td>1752.47</td><td>4.64e-6</td></tr><tr><td>MJS 10,000 add</td><td>0.59</td><td>1687.89</td><td>1.40e-4</td></tr><tr><td>SRC PQ 10,000 add & pop</td><td>3.41</td><td>293.00</td><td>2.65e-5</td></tr><tr><td>CJS PQ 10,000 add & pop</td><td>4.92</td><td>203.31</td><td>3.60e-5</td></tr><tr><td>MJS PQ 10,000 add & pop</td><td>4.88</td><td>204.72</td><td>4.35e-5</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>avl-tree</span></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>10,000 add randomly</td><td>31.02</td><td>32.24</td><td>2.83e-4</td></tr><tr><td>10,000 add & delete randomly</td><td>71.45</td><td>14.00</td><td>0.00</td></tr><tr><td>10,000 addMany</td><td>40.21</td><td>24.87</td><td>4.47e-4</td></tr><tr><td>10,000 get</td><td>28.34</td><td>35.29</td><td>5.15e-4</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>binary-tree</span></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000 add randomly</td><td>13.04</td><td>76.71</td><td>1.56e-4</td></tr><tr><td>1,000 add & delete randomly</td><td>15.79</td><td>63.33</td><td>1.40e-4</td></tr><tr><td>1,000 addMany</td><td>10.25</td><td>97.60</td><td>1.08e-4</td></tr><tr><td>1,000 get</td><td>18.31</td><td>54.60</td><td>1.50e-4</td></tr><tr><td>1,000 dfs</td><td>155.22</td><td>6.44</td><td>8.11e-4</td></tr><tr><td>1,000 bfs</td><td>56.66</td><td>17.65</td><td>6.70e-4</td></tr><tr><td>1,000 morris</td><td>254.79</td><td>3.92</td><td>5.27e-4</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>bst</span></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>10,000 add randomly</td><td>28.23</td><td>35.42</td><td>3.20e-4</td></tr><tr><td>10,000 add & delete randomly</td><td>68.35</td><td>14.63</td><td>8.46e-4</td></tr><tr><td>10,000 addMany</td><td>28.94</td><td>34.56</td><td>0.00</td></tr><tr><td>10,000 get</td><td>28.97</td><td>34.51</td><td>4.57e-4</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>rb-tree</span></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>100,000 add</td><td>89.12</td><td>11.22</td><td>0.00</td></tr><tr><td>100,000 add & delete randomly</td><td>219.00</td><td>4.57</td><td>0.00</td></tr><tr><td>100,000 getNode</td><td>105.74</td><td>9.46</td><td>6.09e-4</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>directed-graph</span></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000 addVertex</td><td>0.10</td><td>9902.14</td><td>9.12e-7</td></tr><tr><td>1,000 addEdge</td><td>6.11</td><td>163.67</td><td>1.29e-4</td></tr><tr><td>1,000 getVertex</td><td>0.05</td><td>2.16e+4</td><td>3.51e-7</td></tr><tr><td>1,000 getEdge</td><td>23.63</td><td>42.33</td><td>0.00</td></tr><tr><td>tarjan</td><td>222.85</td><td>4.49</td><td>0.01</td></tr><tr><td>tarjan all</td><td>223.79</td><td>4.47</td><td>0.00</td></tr><tr><td>topologicalSort</td><td>181.77</td><td>5.50</td><td>0.00</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>hash-map</span></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>10,000 set</td><td>1.00</td><td>1001.93</td><td>2.06e-5</td></tr><tr><td>10,000 set & get</td><td>1.51</td><td>661.53</td><td>2.65e-5</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>heap</span></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>10,000 add & pop</td><td>5.82</td><td>171.79</td><td>5.13e-5</td></tr><tr><td>10,000 fib add & pop</td><td>359.24</td><td>2.78</td><td>0.00</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>doubly-linked-list</span></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000,000 push</td><td>214.50</td><td>4.66</td><td>0.05</td></tr><tr><td>1,000,000 unshift</td><td>205.89</td><td>4.86</td><td>0.05</td></tr><tr><td>1,000,000 unshift & shift</td><td>168.93</td><td>5.92</td><td>0.04</td></tr><tr><td>1,000,000 insertBefore</td><td>291.32</td><td>3.43</td><td>0.05</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>singly-linked-list</span></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>10,000 push & pop</td><td>215.82</td><td>4.63</td><td>0.02</td></tr><tr><td>10,000 insertBefore</td><td>248.56</td><td>4.02</td><td>0.01</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>max-priority-queue</span></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>10,000 refill & poll</td><td>8.85</td><td>113.01</td><td>1.90e-4</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>priority-queue</span></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>100,000 add & pop</td><td>104.23</td><td>9.59</td><td>0.00</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>deque</span></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000,000 push</td><td>18.17</td><td>55.04</td><td>0.00</td></tr><tr><td>1,000,000 push & pop</td><td>26.06</td><td>38.37</td><td>0.00</td></tr><tr><td>1,000,000 push & shift</td><td>26.79</td><td>37.32</td><td>0.00</td></tr><tr><td>1,000,000 unshift & shift</td><td>25.13</td><td>39.80</td><td>0.00</td></tr></table></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000,000 push</td><td>14.25</td><td>70.18</td><td>1.63e-4</td></tr><tr><td>1,000,000 push & pop</td><td>23.15</td><td>43.21</td><td>2.07e-4</td></tr><tr><td>1,000,000 push & shift</td><td>24.16</td><td>41.40</td><td>1.78e-4</td></tr><tr><td>1,000,000 unshift & shift</td><td>22.28</td><td>44.88</td><td>1.56e-4</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>queue</span></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000,000 push</td><td>45.72</td><td>21.87</td><td>0.01</td></tr><tr><td>1,000,000 push & shift</td><td>81.75</td><td>12.23</td><td>0.00</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>stack</span></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000,000 push</td><td>44.30</td><td>22.57</td><td>0.01</td></tr><tr><td>1,000,000 push & pop</td><td>49.69</td><td>20.12</td><td>0.01</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>trie</span></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>100,000 push</td><td>43.10</td><td>23.20</td><td>6.46e-4</td></tr><tr><td>100,000 getWords</td><td>85.45</td><td>11.70</td><td>0.00</td></tr></table></div>
</div>
[//]: # (No deletion!!! End of Replace Section)

View file

@ -1,6 +1,6 @@
{
"name": "data-structure-typed",
"version": "1.45.4",
"version": "1.46.2",
"description": "Data Structures of Javascript & TypeScript. Binary Tree, BST, Graph, Heap, Priority Queue, Linked List, Queue, Deque, Stack, AVL Tree, Tree Multiset, Trie, Directed Graph, Undirected Graph, Singly Linked List, Doubly Linked List, Max Heap, Max Priority Queue, Min Heap, Min Priority Queue.",
"main": "dist/cjs/index.js",
"module": "dist/mjs/index.js",

View file

@ -124,7 +124,7 @@ export class HashMapIterator<K, V> {
}
clone() {
return new HashMapIterator(this._node, this._sentinel, this.hashMap, this.iterateDirection)
return new HashMapIterator<K, V>(this._node, this._sentinel, this.hashMap, this.iterateDirection)
}
}

View file

@ -23,6 +23,20 @@ export class DequeIterator<E> {
index: number;
readonly deque: Deque<E>;
/**
* The constructor initializes the index, iterate direction, and prev/next functions for a
* DequeIterator object.
* @param {number} index - The index parameter represents the current index position of the iterator
* within the deque. It is a number that indicates the position of the element that the iterator is
* currently pointing to.
* @param deque - The `deque` parameter is an instance of the `Deque` class. It represents a
* double-ended queue data structure, which allows elements to be added or removed from both ends.
* @param iterateDirection - The `iterateDirection` parameter is an optional parameter that specifies
* the direction in which the iterator should iterate over the elements of the `deque`. It has a
* default value of `IterateDirection.DEFAULT`.
* @returns The constructor is not returning anything. It is used to initialize the properties of the
* object being created.
*/
constructor(index: number, deque: Deque<E>, iterateDirection = IterateDirection.DEFAULT) {
this.index = index;
this.iterateDirection = iterateDirection;
@ -94,6 +108,15 @@ export class Deque<E> {
protected _bucketCount = 0;
protected readonly _bucketSize: number;
/**
* The constructor initializes a data structure with a specified bucket size and populates it with
* elements from an iterable.
* @param elements - The `elements` parameter is an iterable object (such as an array or a Set) that
* contains the initial elements to be stored in the data structure. It can also be an object with a
* `length` property or a `size` property, which represents the number of elements in the iterable.
* @param bucketSize - The `bucketSize` parameter is the maximum number of elements that can be
* stored in each bucket. It determines the size of each bucket in the data structure.
*/
constructor(elements: IterableWithSizeOrLength<E> = [], bucketSize = (1 << 12)) {
let _size;
@ -129,6 +152,11 @@ export class Deque<E> {
return this._size;
}
/**
* The function returns the first element in a collection if it exists, otherwise it returns
* undefined.
* @returns The first element of the collection, of type E, is being returned.
*/
get first(): E | undefined {
if (this.size === 0) return;
return this._buckets[this._bucketFirst][this._firstInBucket];
@ -139,15 +167,6 @@ export class Deque<E> {
return this._buckets[this._bucketLast][this._lastInBucket];
}
/**
* Time Complexity: Amortized O(1) - Generally constant time, but resizing when the deque is full leads to O(n).
* Space Complexity: O(n) - In worst case, resizing doubles the array size.
*/
empty() {
return this._size === 0;
}
/**
* Time Complexity: O(1) - Removes the last element.
* Space Complexity: O(1) - Operates in-place.
@ -214,6 +233,10 @@ export class Deque<E> {
return this.shift();
}
/**
* The clear() function resets the state of the object by initializing all variables to their default
* values.
*/
clear() {
this._buckets = [new Array(this._bucketSize)];
this._bucketCount = 1;
@ -221,22 +244,55 @@ export class Deque<E> {
this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
}
/**
* The `begin()` function returns a new iterator for a deque starting from the first element.
* @returns A new instance of the DequeIterator class is being returned.
*/
begin() {
return new DequeIterator<E>(0, this);
}
/**
* The `end()` function returns a new `DequeIterator` object with the size and reference to the
* current deque.
* @returns A new instance of the DequeIterator class is being returned.
*/
end() {
return new DequeIterator<E>(this.size, this);
}
/**
* The reverseBegin function returns a new DequeIterator object that starts at the last element of
* the deque and iterates in reverse direction.
* @returns A new instance of the DequeIterator class is being returned.
*/
reverseBegin() {
return new DequeIterator<E>(this.size - 1, this, IterateDirection.REVERSE);
}
/**
* The reverseEnd() function returns a new DequeIterator object that iterates over the elements of a
* Deque in reverse order.
* @returns A new instance of the DequeIterator class is being returned.
*/
reverseEnd() {
return new DequeIterator<E>(-1, this, IterateDirection.REVERSE);
}
/**
* Time Complexity - Amortized O(1) (possible reallocation)
* Space Complexity - O(n) (due to potential resizing).
*/
/**
* 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.
* @param {E} element - The `element` parameter represents the value that you want to add to the data
* structure.
* @returns The size of the data structure after the element has been pushed.
*/
push(element: E) {
if (this.size) {
if (this._lastInBucket < this._bucketSize - 1) {
@ -258,6 +314,19 @@ export class Deque<E> {
return this.size;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*
* The `pop()` function removes and returns the last element from a data structure, updating the
* internal state variables accordingly.
* @returns The element that was removed from the data structure is being returned.
*/
pop() {
if (this.size === 0) return;
const element = this._buckets[this._bucketLast][this._lastInBucket];
@ -276,6 +345,21 @@ export class Deque<E> {
return element;
}
/**
* Time Complexity: Amortized O(1)
* Space Complexity: O(n)
*/
/**
* 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.
* @param {E} element - The `element` parameter represents the element that you want to add to the
* beginning of the data structure.
* @returns The size of the data structure after the element has been added.
*/
unshift(element: E) {
if (this.size) {
if (this._firstInBucket > 0) {
@ -297,6 +381,21 @@ export class Deque<E> {
return this.size;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* 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.
* @returns The element that is being removed from the beginning of the data structure is being
* returned.
*/
shift() {
if (this.size === 0) return;
const element = this._buckets[this._bucketFirst][this._firstInBucket];
@ -315,6 +414,22 @@ export class Deque<E> {
return element;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*
* The `getAt` function retrieves an element at a specified position in an array-like data structure.
* @param {number} pos - 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.
* @returns The element at the specified position in the data structure is being returned.
*/
getAt(pos: number): E {
rangeCheck!(pos, 0, this.size - 1);
const {
@ -324,6 +439,22 @@ export class Deque<E> {
return this._buckets[bucketIndex][indexInBucket]!;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*
* The `setAt` function sets an element at a specific position in an array-like data structure.
* @param {number} pos - The `pos` parameter represents the position at which the element needs to be
* set. It is of type `number`.
* @param {E} element - The `element` parameter is the value that you want to set at the specified
* position in the data structure.
*/
setAt(pos: number, element: E) {
rangeCheck!(pos, 0, this.size - 1);
const {
@ -333,6 +464,26 @@ export class Deque<E> {
this._buckets[bucketIndex][indexInBucket] = element;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*
* The `insertAt` function inserts one or more elements at a specified position in an array-like data
* structure.
* @param {number} pos - The `pos` parameter represents the position at which the element(s) should
* be inserted. It is of type `number`.
* @param {E} element - The `element` parameter represents the element that you want to insert into
* the array at the specified position.
* @param [num=1] - The `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
* @returns The size of the array after the insertion is being returned.
*/
insertAt(pos: number, element: E, num = 1) {
const length = this.size;
rangeCheck!(pos, 0, length);
@ -352,6 +503,21 @@ export class Deque<E> {
return this.size;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* 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.
* @param {number} pos - 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.
* @returns The method is returning the updated size of the data structure.
*/
cut(pos: number) {
if (pos < 0) {
this.clear();
@ -367,6 +533,22 @@ export class Deque<E> {
return this.size;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The `deleteAt` function removes an element at a specified position in an array-like data
* structure.
* @param {number} pos - 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.
* @returns The size of the data structure after the deletion operation is performed.
*/
deleteAt(pos: number) {
rangeCheck!(pos, 0, this.size - 1);
if (pos === 0) this.shift();
@ -391,6 +573,21 @@ export class Deque<E> {
return this.size;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The `delete` function removes all occurrences of a specified element from an array-like data
* structure.
* @param {E} element - The `element` parameter represents the element that you want to delete from
* the data structure.
* @returns The size of the data structure after the element has been deleted.
*/
delete(element: E) {
const size = this.size;
if (size === 0) return 0;
@ -408,6 +605,20 @@ export class Deque<E> {
return this.size;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The function deletes an element from a deque using an iterator and returns the next iterator.
* @param iter - The parameter `iter` is of type `DequeIterator<E>`. It represents an iterator object
* that is used to iterate over elements in a deque (double-ended queue).
* @returns the updated iterator after deleting an element from the deque.
*/
deleteByIterator(iter: DequeIterator<E>) {
const index = iter.index;
this.deleteAt(index);
@ -415,6 +626,20 @@ export class Deque<E> {
return iter;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The function `findIterator` searches for an element in a deque and returns an iterator pointing to
* the element if found, otherwise it returns an iterator pointing to the end of the deque.
* @param {E} element - The `element` parameter is the element that you want to find in the deque.
* @returns The method `findIterator(element: E)` returns a `DequeIterator<E>` object.
*/
findIterator(element: E) {
for (let i = 0; i < this.size; ++i) {
if (this.getAt(i) === element) {
@ -424,6 +649,20 @@ export class Deque<E> {
return this.end();
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* 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.
* @returns The reverse() method is returning the object itself (this) after performing the reverse
* operation on the buckets and updating the relevant properties.
*/
reverse() {
this._buckets.reverse().forEach(function (bucket) {
bucket.reverse();
@ -436,6 +675,19 @@ export class Deque<E> {
return this;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The `unique()` function removes duplicate elements from an array-like data structure and returns
* the number of unique elements.
* @returns The size of the modified array is being returned.
*/
unique() {
if (this.size <= 1) {
return this.size;
@ -453,6 +705,21 @@ export class Deque<E> {
return this.size;
}
/**
* Time Complexity: O(n log n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n log n)
* Space Complexity: O(n)
*
* The `sort` function sorts the elements in a data structure using a provided comparator function.
* @param [comparator] - The `comparator` parameter is a function that takes in two elements `x` and
* `y` of type `E` and returns a number. The comparator function is used to determine the order of
* the elements in the sorted array.
* @returns The method is returning the sorted instance of the object on which the method is called.
*/
sort(comparator?: (x: E, y: E) => number) {
const arr: E[] = [];
for (let i = 0; i < this.size; ++i) {
@ -465,6 +732,20 @@ export class Deque<E> {
return this;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*
* The `shrinkToFit` function reorganizes the elements in an array-like data structure to minimize
* memory usage.
* @returns Nothing is being returned. The function is using the `return` statement to exit early if
* `this.size` is 0, but it does not return any value.
*/
shrinkToFit() {
if (this.size === 0) return;
const newBuckets = [];
@ -486,12 +767,42 @@ export class Deque<E> {
this._buckets = newBuckets;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The `forEach` function iterates over each element in a deque and applies a callback function to
* each element.
* @param callback - The callback parameter is a function that will be called for each element in the
* deque. It takes three parameters:
*/
forEach(callback: (element: E, index: number, deque: Deque<E>) => void) {
for (let i = 0; i < this.size; ++i) {
callback(this.getAt(i), i, this);
}
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The `find` function iterates over the elements in a deque and returns the first element for which
* the callback function returns true, or undefined if no such element is found.
* @param callback - A function that takes three parameters: element, index, and deque. It should
* return a boolean value indicating whether the element satisfies a certain condition.
* @returns The method `find` returns the first element in the deque that satisfies the condition
* specified by the callback function. If no element satisfies the condition, it returns `undefined`.
*/
find(callback: (element: E, index: number, deque: Deque<E>) => boolean): E | undefined {
for (let i = 0; i < this.size; ++i) {
const element = this.getAt(i);
@ -502,6 +813,18 @@ export class Deque<E> {
return undefined;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*
* The `toArray` function converts the elements of a data structure into an array.
* @returns The `toArray()` method is returning an array of elements of type `E`.
*/
toArray(): E[] {
const arr: E[] = [];
for (let i = 0; i < this.size; ++i) {
@ -510,6 +833,20 @@ export class Deque<E> {
return arr;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* 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.
* @param callback - The `callback` parameter is a function that takes three arguments:
* @returns The `map` method is returning a new `Deque` object with the transformed elements.
*/
map<T>(callback: (element: E, index: number, deque: Deque<E>) => T): Deque<T> {
const newDeque = new Deque<T>([], this._bucketSize);
for (let i = 0; i < this.size; ++i) {
@ -518,6 +855,22 @@ export class Deque<E> {
return newDeque;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*
* The `filter` function creates a new deque containing only the elements that satisfy the given
* predicate function.
* @param predicate - The `predicate` parameter is a function that takes three arguments: `element`,
* `index`, and `deque`.
* @returns The `filter` method is returning a new `Deque` object that contains only the elements
* that satisfy the given `predicate` function.
*/
filter(predicate: (element: E, index: number, deque: Deque<E>) => boolean): Deque<E> {
const newDeque = new Deque<E>([], this._bucketSize);
for (let i = 0; i < this.size; ++i) {
@ -529,6 +882,24 @@ export class Deque<E> {
return newDeque;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The `reduce` function iterates over the elements of a deque and applies a callback function to
* each element, accumulating a single value.
* @param callback - The `callback` parameter is a function that takes four arguments:
* @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It
* is the value that will be passed as the first argument to the `callback` function when reducing
* the elements of the deque.
* @returns the final value of the accumulator after iterating over all elements in the deque and
* applying the callback function to each element.
*/
reduce<T>(callback: (accumulator: T, element: E, index: number, deque: Deque<E>) => T, initialValue: T): T {
let accumulator = initialValue;
for (let i = 0; i < this.size; ++i) {
@ -537,6 +908,22 @@ export class Deque<E> {
return accumulator;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The function "indexOf" returns the index of the first occurrence of a given element in an array,
* or -1 if the element is not found.
* @param {E} element - The "element" parameter represents the element that you want to find the
* index of in the data structure.
* @returns The indexOf function returns the index of the first occurrence of the specified element
* in the data structure. If the element is not found, it returns -1.
*/
indexOf(element: E): number {
for (let i = 0; i < this.size; ++i) {
if (this.getAt(i) === element) {
@ -546,12 +933,38 @@ export class Deque<E> {
return -1;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The above function is an implementation of the iterator protocol in TypeScript, allowing the
* object to be iterated over using a for...of loop.
*/
* [Symbol.iterator]() {
for (let i = 0; i < this.size; ++i) {
yield this.getAt(i);
}
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*
* The `_reallocate` function reallocates the buckets in an array, adding new buckets if needed.
* @param {number} [needBucketNum] - The `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.
*/
protected _reallocate(needBucketNum?: number) {
const newBuckets = [];
const addBucketNum = needBucketNum || this._bucketCount >> 1 || 1;
@ -574,6 +987,20 @@ export class Deque<E> {
this._bucketCount = newBuckets.length;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*
* The function calculates the bucket index and index within the bucket based on the given position.
* @param {number} pos - 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.
* @returns an object with two properties: "bucketIndex" and "indexInBucket".
*/
protected _getBucketAndPosition(pos: number) {
let bucketIndex: number;
let indexInBucket: number;