mirror of
https://github.com/zrwusa/data-structure-typed.git
synced 2024-11-23 12:54:04 +00:00
feat: Provide the toElementFn method during instantiation for the entire data structure series, allowing the mapping of raw data to the desired data type and direct insertion of data into the data structure to be completed within a single loop.
fix: Override the corresponding clone, filter, and map methods in the Heap and PriorityQueue data structures, ensuring the accurate return of the respective data structures. #84 fixed.
This commit is contained in:
parent
6539e50598
commit
1ae9e929b1
26
README.md
26
README.md
|
@ -736,43 +736,43 @@ Version 11.7.9
|
|||
[//]: # (No deletion!!! Start of Replace Section)
|
||||
<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>100,000 add</td><td>6.38</td><td>156.71</td><td>2.05e-4</td></tr><tr><td>100,000 add & poll</td><td>31.53</td><td>31.71</td><td>8.78e-4</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>100,000 add</td><td>6.43</td><td>155.58</td><td>1.69e-4</td></tr><tr><td>100,000 add & poll</td><td>34.95</td><td>28.61</td><td>7.35e-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>58.68</td><td>17.04</td><td>9.39e-4</td></tr><tr><td>100,000 add randomly</td><td>75.49</td><td>13.25</td><td>0.00</td></tr><tr><td>100,000 get</td><td>90.48</td><td>11.05</td><td>0.00</td></tr><tr><td>100,000 iterator</td><td>29.71</td><td>33.66</td><td>0.01</td></tr><tr><td>100,000 add & delete orderly</td><td>122.02</td><td>8.20</td><td>0.00</td></tr><tr><td>100,000 add & delete randomly</td><td>214.39</td><td>4.66</td><td>0.02</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>100,000 add</td><td>78.90</td><td>12.67</td><td>0.00</td></tr><tr><td>100,000 add randomly</td><td>82.74</td><td>12.09</td><td>0.00</td></tr><tr><td>100,000 get</td><td>111.79</td><td>8.95</td><td>0.00</td></tr><tr><td>100,000 iterator</td><td>26.62</td><td>37.56</td><td>0.01</td></tr><tr><td>100,000 add & delete orderly</td><td>152.36</td><td>6.56</td><td>0.00</td></tr><tr><td>100,000 add & delete randomly</td><td>230.54</td><td>4.34</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'>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>42.67</td><td>23.44</td><td>0.01</td></tr><tr><td>100,000 push & shift</td><td>4.92</td><td>203.05</td><td>7.10e-4</td></tr><tr><td>Native JS Array 100,000 push & shift</td><td>2380.40</td><td>0.42</td><td>0.24</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>42.92</td><td>23.30</td><td>0.01</td></tr><tr><td>100,000 push & shift</td><td>4.90</td><td>203.91</td><td>5.57e-4</td></tr><tr><td>Native JS Array 100,000 push & shift</td><td>2372.32</td><td>0.42</td><td>0.25</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>24.97</td><td>40.04</td><td>0.00</td></tr><tr><td>1,000,000 push & pop</td><td>30.75</td><td>32.52</td><td>0.00</td></tr><tr><td>1,000,000 push & shift</td><td>30.60</td><td>32.67</td><td>0.00</td></tr><tr><td>100,000 push & shift</td><td>3.22</td><td>310.21</td><td>2.47e-4</td></tr><tr><td>Native JS Array 100,000 push & shift</td><td>2153.50</td><td>0.46</td><td>0.13</td></tr><tr><td>100,000 unshift & shift</td><td>2.89</td><td>345.71</td><td>2.85e-4</td></tr><tr><td>Native JS Array 100,000 unshift & shift</td><td>4177.84</td><td>0.24</td><td>0.28</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>24.61</td><td>40.64</td><td>0.00</td></tr><tr><td>1,000,000 push & pop</td><td>31.09</td><td>32.17</td><td>0.00</td></tr><tr><td>1,000,000 push & shift</td><td>30.79</td><td>32.48</td><td>0.00</td></tr><tr><td>100,000 push & shift</td><td>3.35</td><td>298.19</td><td>6.23e-4</td></tr><tr><td>Native JS Array 100,000 push & shift</td><td>2214.50</td><td>0.45</td><td>0.36</td></tr><tr><td>100,000 unshift & shift</td><td>2.90</td><td>344.25</td><td>2.69e-4</td></tr><tr><td>Native JS Array 100,000 unshift & shift</td><td>4256.15</td><td>0.23</td><td>0.15</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>1,000,000 set</td><td>116.95</td><td>8.55</td><td>0.03</td></tr><tr><td>Native JS Map 1,000,000 set</td><td>206.87</td><td>4.83</td><td>0.01</td></tr><tr><td>Native JS Set 1,000,000 add</td><td>167.65</td><td>5.96</td><td>0.01</td></tr><tr><td>1,000,000 set & get</td><td>122.70</td><td>8.15</td><td>0.03</td></tr><tr><td>Native JS Map 1,000,000 set & get</td><td>263.26</td><td>3.80</td><td>0.01</td></tr><tr><td>Native JS Set 1,000,000 add & has</td><td>167.72</td><td>5.96</td><td>0.01</td></tr><tr><td>1,000,000 ObjKey set & get</td><td>320.17</td><td>3.12</td><td>0.03</td></tr><tr><td>Native JS Map 1,000,000 ObjKey set & get</td><td>292.05</td><td>3.42</td><td>0.02</td></tr><tr><td>Native JS Set 1,000,000 ObjKey add & has</td><td>267.27</td><td>3.74</td><td>0.02</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 set</td><td>116.37</td><td>8.59</td><td>0.02</td></tr><tr><td>Native JS Map 1,000,000 set</td><td>210.43</td><td>4.75</td><td>0.02</td></tr><tr><td>Native JS Set 1,000,000 add</td><td>181.96</td><td>5.50</td><td>0.02</td></tr><tr><td>1,000,000 set & get</td><td>121.63</td><td>8.22</td><td>0.02</td></tr><tr><td>Native JS Map 1,000,000 set & get</td><td>269.23</td><td>3.71</td><td>0.01</td></tr><tr><td>Native JS Set 1,000,000 add & has</td><td>185.33</td><td>5.40</td><td>0.03</td></tr><tr><td>1,000,000 ObjKey set & get</td><td>325.11</td><td>3.08</td><td>0.03</td></tr><tr><td>Native JS Map 1,000,000 ObjKey set & get</td><td>308.21</td><td>3.24</td><td>0.03</td></tr><tr><td>Native JS Set 1,000,000 ObjKey add & has</td><td>270.59</td><td>3.70</td><td>0.03</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.93</td><td>22.76</td><td>5.85e-4</td></tr><tr><td>100,000 getWords</td><td>83.42</td><td>11.99</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>100,000 push</td><td>46.94</td><td>21.30</td><td>0.00</td></tr><tr><td>100,000 getWords</td><td>105.45</td><td>9.48</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'>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>100,000 add</td><td>232.60</td><td>4.30</td><td>0.00</td></tr><tr><td>100,000 add randomly</td><td>306.92</td><td>3.26</td><td>0.00</td></tr><tr><td>100,000 get</td><td>113.28</td><td>8.83</td><td>0.00</td></tr><tr><td>100,000 iterator</td><td>33.20</td><td>30.12</td><td>0.01</td></tr><tr><td>100,000 add & delete orderly</td><td>399.10</td><td>2.51</td><td>0.00</td></tr><tr><td>100,000 add & delete randomly</td><td>551.57</td><td>1.81</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>100,000 add</td><td>274.90</td><td>3.64</td><td>0.01</td></tr><tr><td>100,000 add randomly</td><td>398.94</td><td>2.51</td><td>0.02</td></tr><tr><td>100,000 get</td><td>141.33</td><td>7.08</td><td>0.01</td></tr><tr><td>100,000 iterator</td><td>32.25</td><td>31.01</td><td>0.00</td></tr><tr><td>100,000 add & delete orderly</td><td>446.47</td><td>2.24</td><td>0.00</td></tr><tr><td>100,000 add & delete randomly</td><td>625.71</td><td>1.60</td><td>0.03</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-overall</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 RBTree add randomly</td><td>6.24</td><td>160.31</td><td>1.80e-4</td></tr><tr><td>10,000 RBTree get randomly</td><td>7.93</td><td>126.09</td><td>8.91e-5</td></tr><tr><td>10,000 RBTree add & delete randomly</td><td>17.05</td><td>58.65</td><td>1.66e-4</td></tr><tr><td>10,000 AVLTree add randomly</td><td>21.90</td><td>45.66</td><td>2.09e-4</td></tr><tr><td>10,000 AVLTree get randomly</td><td>8.68</td><td>115.15</td><td>0.00</td></tr><tr><td>10,000 AVLTree add & delete randomly</td><td>41.66</td><td>24.00</td><td>6.11e-4</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>10,000 RBTree add randomly</td><td>6.69</td><td>149.44</td><td>1.04e-4</td></tr><tr><td>10,000 RBTree get randomly</td><td>9.22</td><td>108.47</td><td>1.36e-4</td></tr><tr><td>10,000 RBTree add & delete randomly</td><td>18.59</td><td>53.79</td><td>0.00</td></tr><tr><td>10,000 AVLTree add randomly</td><td>24.73</td><td>40.43</td><td>2.93e-4</td></tr><tr><td>10,000 AVLTree get randomly</td><td>9.77</td><td>102.31</td><td>9.04e-5</td></tr><tr><td>10,000 AVLTree add & delete randomly</td><td>46.18</td><td>21.65</td><td>7.76e-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>9614.91</td><td>7.24e-6</td></tr><tr><td>1,000 addEdge</td><td>6.68</td><td>149.81</td><td>5.43e-4</td></tr><tr><td>1,000 getVertex</td><td>0.05</td><td>2.18e+4</td><td>3.02e-7</td></tr><tr><td>1,000 getEdge</td><td>21.46</td><td>46.60</td><td>0.00</td></tr><tr><td>tarjan</td><td>204.99</td><td>4.88</td><td>0.03</td></tr><tr><td>topologicalSort</td><td>166.17</td><td>6.02</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 addVertex</td><td>0.10</td><td>9860.11</td><td>8.96e-7</td></tr><tr><td>1,000 addEdge</td><td>6.23</td><td>160.44</td><td>4.50e-4</td></tr><tr><td>1,000 getVertex</td><td>0.05</td><td>2.09e+4</td><td>1.05e-5</td></tr><tr><td>1,000 getEdge</td><td>25.18</td><td>39.72</td><td>0.01</td></tr><tr><td>tarjan</td><td>205.33</td><td>4.87</td><td>0.01</td></tr><tr><td>topologicalSort</td><td>186.14</td><td>5.37</td><td>0.02</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>217.48</td><td>4.60</td><td>0.04</td></tr><tr><td>1,000,000 unshift</td><td>221.64</td><td>4.51</td><td>0.02</td></tr><tr><td>1,000,000 unshift & shift</td><td>175.58</td><td>5.70</td><td>0.03</td></tr><tr><td>1,000,000 addBefore</td><td>311.89</td><td>3.21</td><td>0.04</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>228.04</td><td>4.39</td><td>0.03</td></tr><tr><td>1,000,000 unshift</td><td>235.67</td><td>4.24</td><td>0.07</td></tr><tr><td>1,000,000 unshift & shift</td><td>198.31</td><td>5.04</td><td>0.02</td></tr><tr><td>1,000,000 addBefore</td><td>314.03</td><td>3.18</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>1,000,000 push & shift</td><td>211.02</td><td>4.74</td><td>0.04</td></tr><tr><td>10,000 push & pop</td><td>216.29</td><td>4.62</td><td>0.01</td></tr><tr><td>10,000 addBefore</td><td>248.75</td><td>4.02</td><td>0.01</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 & shift</td><td>236.11</td><td>4.24</td><td>0.05</td></tr><tr><td>10,000 push & pop</td><td>220.24</td><td>4.54</td><td>0.01</td></tr><tr><td>10,000 addBefore</td><td>246.87</td><td>4.05</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'>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</td><td>27.53</td><td>36.32</td><td>0.00</td></tr><tr><td>100,000 add & poll</td><td>76.95</td><td>13.00</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>100,000 add</td><td>27.34</td><td>36.57</td><td>9.52e-4</td></tr><tr><td>100,000 add & poll</td><td>77.85</td><td>12.85</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'>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>39.39</td><td>25.39</td><td>0.01</td></tr><tr><td>1,000,000 push & pop</td><td>47.72</td><td>20.96</td><td>0.01</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>38.50</td><td>25.97</td><td>0.00</td></tr><tr><td>1,000,000 push & pop</td><td>46.82</td><td>21.36</td><td>0.00</td></tr></table></div>
|
||||
</div>
|
||||
|
||||
[//]: # (No deletion!!! End of Replace Section)
|
||||
|
|
|
@ -1 +1,2 @@
|
|||
export * from './iterable-base';
|
||||
export * from './iterable-entry-base';
|
||||
export * from './iterable-element-base';
|
||||
|
|
250
src/data-structures/base/iterable-element-base.ts
Normal file
250
src/data-structures/base/iterable-element-base.ts
Normal file
|
@ -0,0 +1,250 @@
|
|||
import { ElementCallback, IterableElementBaseOptions, ReduceElementCallback } from '../../types';
|
||||
|
||||
export abstract class IterableElementBase<E, R, C> {
|
||||
/**
|
||||
* The protected constructor initializes the options for the IterableElementBase class, including the
|
||||
* toElementFn function.
|
||||
* @param [options] - An optional object that contains the following properties:
|
||||
*/
|
||||
protected constructor(options?: IterableElementBaseOptions<E, R>) {
|
||||
if (options) {
|
||||
const { toElementFn } = options;
|
||||
if (typeof toElementFn === 'function') this._toElementFn = toElementFn;
|
||||
else if (toElementFn) throw new TypeError('toElementFn must be a function type');
|
||||
}
|
||||
}
|
||||
|
||||
abstract get size(): number;
|
||||
|
||||
protected _toElementFn?: (rawElement: R) => E;
|
||||
|
||||
/**
|
||||
* The function returns the _toElementFn property, which is a function that converts a raw element to
|
||||
* a specific type.
|
||||
* @returns The function `get toElementFn()` is returning either a function that takes a raw element
|
||||
* `rawElement` of type `R` and returns an element `E`, or `undefined` if no function is assigned to
|
||||
* `_toElementFn`.
|
||||
*/
|
||||
get toElementFn(): ((rawElement: R) => E) | undefined {
|
||||
return this._toElementFn;
|
||||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(1)
|
||||
*/
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(1)
|
||||
*
|
||||
* The function is an implementation of the Symbol.iterator method that returns an IterableIterator.
|
||||
* @param {any[]} args - 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.
|
||||
*/
|
||||
* [Symbol.iterator](...args: any[]): IterableIterator<E> {
|
||||
yield* this._getIterator(...args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(n)
|
||||
*/
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(n)
|
||||
*
|
||||
* The function returns an iterator that yields all the values in the object.
|
||||
*/
|
||||
* values(): IterableIterator<E> {
|
||||
for (const item of this) {
|
||||
yield item;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(1)
|
||||
*/
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(1)
|
||||
*
|
||||
* The `every` function checks if every element in the array satisfies a given predicate.
|
||||
* @param 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.
|
||||
* @param {any} [thisArg] - The `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
|
||||
* @returns 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.
|
||||
*/
|
||||
every(predicate: ElementCallback<E, R, boolean, C>, thisArg?: any): boolean {
|
||||
let index = 0;
|
||||
for (const item of this) {
|
||||
if (!predicate.call(thisArg, item, index++, this)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(1)
|
||||
*/
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(1)
|
||||
*/
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(1)
|
||||
*
|
||||
* The "some" function checks if at least one element in a collection satisfies a given predicate.
|
||||
* @param 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.
|
||||
* @param {any} [thisArg] - The `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
|
||||
* @returns a boolean value. It returns true if the predicate function returns true for any element
|
||||
* in the collection, and false otherwise.
|
||||
*/
|
||||
some(predicate: ElementCallback<E, R, boolean, C>, thisArg?: any): boolean {
|
||||
let index = 0;
|
||||
for (const item of this) {
|
||||
if (predicate.call(thisArg, item, index++, this)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(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.
|
||||
* @param callbackfn - 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.
|
||||
* @param {any} [thisArg] - The `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
|
||||
*/
|
||||
forEach(callbackfn: ElementCallback<E, R, void, C>, thisArg?: any): void {
|
||||
let index = 0;
|
||||
for (const item of this) {
|
||||
callbackfn.call(thisArg, item, index++, this);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(1)
|
||||
*/
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(1)
|
||||
*
|
||||
* The `find` function iterates over the elements of an array-like object and returns the first
|
||||
* element that satisfies the provided callback function.
|
||||
* @param callbackfn - 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 itself. The function should return a boolean value indicating whether the
|
||||
* current element matches the desired condition.
|
||||
* @param {any} [thisArg] - The `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
|
||||
* @returns The `find` method returns the first element in the array that satisfies the provided
|
||||
* callback function. If no element satisfies the callback function, `undefined` is returned.
|
||||
*/
|
||||
find(callbackfn: ElementCallback<E, R, boolean, C>, thisArg?: any): E | undefined {
|
||||
let index = 0;
|
||||
for (const item of this) {
|
||||
if (callbackfn.call(thisArg, item, index++, this)) return item;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(1)
|
||||
*
|
||||
* The function checks if a given element exists in a collection.
|
||||
* @param {E} element - 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.
|
||||
* @returns a boolean value. It returns true if the element is found in the collection, and false
|
||||
* otherwise.
|
||||
*/
|
||||
has(element: E): boolean {
|
||||
for (const ele of this) {
|
||||
if (ele === element) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(1)
|
||||
*/
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(1)
|
||||
*
|
||||
* The `reduce` function iterates over the elements of an array-like object and applies a callback
|
||||
* function to reduce them into a single value.
|
||||
* @param callbackfn - The callbackfn parameter is a function that will be called for each element in
|
||||
* the array. It takes four arguments:
|
||||
* @param {U} initialValue - The initialValue parameter is the initial value of the accumulator. It
|
||||
* is the value that the accumulator starts with before the reduction operation begins.
|
||||
* @returns The `reduce` method is returning the final value of the accumulator after iterating over
|
||||
* all the elements in the array and applying the callback function to each element.
|
||||
*/
|
||||
reduce<U>(callbackfn: ReduceElementCallback<E, R, U, C>, initialValue: U): U {
|
||||
let accumulator = initialValue;
|
||||
let index = 0;
|
||||
for (const item of this) {
|
||||
accumulator = callbackfn(accumulator, item as E, index++, this);
|
||||
}
|
||||
return accumulator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(n)
|
||||
*/
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(n)
|
||||
*
|
||||
* The print function logs the elements of an array to the console.
|
||||
*/
|
||||
print(): void {
|
||||
console.log([...this]);
|
||||
}
|
||||
|
||||
abstract isEmpty(): boolean;
|
||||
|
||||
abstract clear(): void;
|
||||
|
||||
abstract clone(): C;
|
||||
|
||||
abstract map(...args: any[]): any;
|
||||
|
||||
abstract filter(...args: any[]): any;
|
||||
|
||||
protected abstract _getIterator(...args: any[]): IterableIterator<E>;
|
||||
}
|
|
@ -1,6 +1,14 @@
|
|||
import { ElementCallback, EntryCallback, ReduceElementCallback, ReduceEntryCallback } from '../../types';
|
||||
import { EntryCallback, ReduceEntryCallback } from '../../types';
|
||||
|
||||
export abstract class IterableEntryBase<K = any, V = any> {
|
||||
// protected constructor(options?: IterableEntryBaseOptions<K, V, R>) {
|
||||
// if (options) {
|
||||
// const { toEntryFn } = options;
|
||||
// if (typeof toEntryFn === 'function') this._toEntryFn = toEntryFn
|
||||
// else throw new TypeError('toEntryFn must be a function type');
|
||||
// }
|
||||
// }
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(1)
|
||||
|
@ -8,6 +16,16 @@ export abstract class IterableEntryBase<K = any, V = any> {
|
|||
|
||||
abstract get size(): number;
|
||||
|
||||
// protected _toEntryFn?: (rawElement: R) => BTNEntry<K, V>;
|
||||
//
|
||||
// /**
|
||||
// * The function returns the value of the _toEntryFn property.
|
||||
// * @returns The function being returned is `this._toEntryFn`.
|
||||
// */
|
||||
// get toEntryFn() {
|
||||
// return this._toEntryFn;
|
||||
// }
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(1)
|
||||
|
@ -285,6 +303,13 @@ export abstract class IterableEntryBase<K = any, V = any> {
|
|||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(n)
|
||||
*/
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(n)
|
||||
*
|
||||
* The print function logs the elements of an array to the console.
|
||||
*/
|
||||
print(): void {
|
||||
console.log([...this]);
|
||||
}
|
||||
|
@ -301,219 +326,3 @@ export abstract class IterableEntryBase<K = any, V = any> {
|
|||
|
||||
protected abstract _getIterator(...args: any[]): IterableIterator<[K, V]>;
|
||||
}
|
||||
|
||||
export abstract class IterableElementBase<E = any, C = any> {
|
||||
abstract get size(): number;
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(1)
|
||||
*/
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(1)
|
||||
*
|
||||
* The function is an implementation of the Symbol.iterator method that returns an IterableIterator.
|
||||
* @param {any[]} args - 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.
|
||||
*/
|
||||
* [Symbol.iterator](...args: any[]): IterableIterator<E> {
|
||||
yield* this._getIterator(...args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(n)
|
||||
*/
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(n)
|
||||
*
|
||||
* The function returns an iterator that yields all the values in the object.
|
||||
*/
|
||||
* values(): IterableIterator<E> {
|
||||
for (const item of this) {
|
||||
yield item;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(1)
|
||||
*/
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(1)
|
||||
*
|
||||
* The `every` function checks if every element in the array satisfies a given predicate.
|
||||
* @param 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.
|
||||
* @param {any} [thisArg] - The `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
|
||||
* @returns 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.
|
||||
*/
|
||||
every(predicate: ElementCallback<E, boolean>, thisArg?: any): boolean {
|
||||
let index = 0;
|
||||
for (const item of this) {
|
||||
if (!predicate.call(thisArg, item, index++, this)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(1)
|
||||
*/
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(1)
|
||||
*/
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(1)
|
||||
*
|
||||
* The "some" function checks if at least one element in a collection satisfies a given predicate.
|
||||
* @param 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.
|
||||
* @param {any} [thisArg] - The `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
|
||||
* @returns a boolean value. It returns true if the predicate function returns true for any element
|
||||
* in the collection, and false otherwise.
|
||||
*/
|
||||
some(predicate: ElementCallback<E, boolean>, thisArg?: any): boolean {
|
||||
let index = 0;
|
||||
for (const item of this) {
|
||||
if (predicate.call(thisArg, item, index++, this)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(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.
|
||||
* @param callbackfn - 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.
|
||||
* @param {any} [thisArg] - The `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
|
||||
*/
|
||||
forEach(callbackfn: ElementCallback<E, void>, thisArg?: any): void {
|
||||
let index = 0;
|
||||
for (const item of this) {
|
||||
callbackfn.call(thisArg, item, index++, this);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(1)
|
||||
*/
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(1)
|
||||
*
|
||||
* The `find` function iterates over the elements of an array-like object and returns the first
|
||||
* element that satisfies the provided callback function.
|
||||
* @param callbackfn - 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 itself. The function should return a boolean value indicating whether the
|
||||
* current element matches the desired condition.
|
||||
* @param {any} [thisArg] - The `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
|
||||
* @returns The `find` method returns the first element in the array that satisfies the provided
|
||||
* callback function. If no element satisfies the callback function, `undefined` is returned.
|
||||
*/
|
||||
find(callbackfn: ElementCallback<E, boolean>, thisArg?: any): E | undefined {
|
||||
let index = 0;
|
||||
for (const item of this) {
|
||||
if (callbackfn.call(thisArg, item, index++, this)) return item;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(1)
|
||||
*
|
||||
* The function checks if a given element exists in a collection.
|
||||
* @param {E} element - 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.
|
||||
* @returns a boolean value. It returns true if the element is found in the collection, and false
|
||||
* otherwise.
|
||||
*/
|
||||
has(element: E): boolean {
|
||||
for (const ele of this) {
|
||||
if (ele === element) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(1)
|
||||
*/
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(1)
|
||||
*
|
||||
* The `reduce` function iterates over the elements of an array-like object and applies a callback
|
||||
* function to reduce them into a single value.
|
||||
* @param callbackfn - The callbackfn parameter is a function that will be called for each element in
|
||||
* the array. It takes four arguments:
|
||||
* @param {U} initialValue - The initialValue parameter is the initial value of the accumulator. It
|
||||
* is the value that the accumulator starts with before the reduction operation begins.
|
||||
* @returns The `reduce` method is returning the final value of the accumulator after iterating over
|
||||
* all the elements in the array and applying the callback function to each element.
|
||||
*/
|
||||
reduce<U>(callbackfn: ReduceElementCallback<E, U>, initialValue: U): U {
|
||||
let accumulator = initialValue;
|
||||
let index = 0;
|
||||
for (const item of this) {
|
||||
accumulator = callbackfn(accumulator, item as E, index++, this);
|
||||
}
|
||||
return accumulator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(n)
|
||||
*/
|
||||
print(): void {
|
||||
console.log([...this]);
|
||||
}
|
||||
|
||||
abstract isEmpty(): boolean;
|
||||
|
||||
abstract clear(): void;
|
||||
|
||||
abstract clone(): C;
|
||||
|
||||
abstract map(...args: any[]): any;
|
||||
|
||||
abstract filter(...args: any[]): any;
|
||||
|
||||
protected abstract _getIterator(...args: any[]): IterableIterator<E>;
|
||||
}
|
|
@ -158,6 +158,7 @@ export class BinaryTree<
|
|||
const { iterationType, toEntryFn } = options;
|
||||
if (iterationType) this.iterationType = iterationType;
|
||||
if (typeof toEntryFn === 'function') this._toEntryFn = toEntryFn;
|
||||
else if (toEntryFn) throw TypeError('toEntryFn must be a function type');
|
||||
}
|
||||
|
||||
if (keysOrNodesOrEntriesOrRawElements) this.addMany(keysOrNodesOrEntriesOrRawElements);
|
||||
|
|
|
@ -270,8 +270,8 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|||
* @returns The `map` method is returning a new `HashMap` object with the transformed values based on
|
||||
* the provided callback function.
|
||||
*/
|
||||
map<U>(callbackfn: EntryCallback<K, V, U>, thisArg?: any): HashMap<K, U> {
|
||||
const resultMap = new HashMap<K, U>();
|
||||
map<VM>(callbackfn: EntryCallback<K, V, VM>, thisArg?: any): HashMap<K, VM> {
|
||||
const resultMap = new HashMap<K, VM>();
|
||||
let index = 0;
|
||||
for (const [key, value] of this) {
|
||||
resultMap.set(key, callbackfn.call(thisArg, value, key, index++, this));
|
||||
|
@ -898,8 +898,8 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
|
|||
* @returns a new `LinkedHashMap` object with the values mapped according to the provided callback
|
||||
* function.
|
||||
*/
|
||||
map<NV>(callback: EntryCallback<K, V, NV>, thisArg?: any): LinkedHashMap<K, NV> {
|
||||
const mappedMap = new LinkedHashMap<K, NV>();
|
||||
map<VM>(callback: EntryCallback<K, V, VM>, thisArg?: any): LinkedHashMap<K, VM> {
|
||||
const mappedMap = new LinkedHashMap<K, VM>();
|
||||
let index = 0;
|
||||
for (const [key, value] of this) {
|
||||
const newValue = callback.call(thisArg, value, key, index, this);
|
||||
|
|
|
@ -20,7 +20,7 @@ import { IterableElementBase } from '../base';
|
|||
* 7. Efficient Sorting Algorithms: For example, heap sort. Heap sort uses the properties of a heap to sort elements.
|
||||
* 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prime's minimum-spanning tree algorithm, which use heaps to improve performance.
|
||||
*/
|
||||
export class Heap<E = any, R = any> extends IterableElementBase<E> {
|
||||
export class Heap<E = any, R = any> extends IterableElementBase<E, R, Heap<E, R>> {
|
||||
/**
|
||||
* The constructor initializes a heap data structure with optional elements and options.
|
||||
* @param elements - The `elements` parameter is an iterable object that contains the initial
|
||||
|
@ -35,16 +35,16 @@ export class Heap<E = any, R = any> extends IterableElementBase<E> {
|
|||
* order of elements in the heap.
|
||||
*/
|
||||
constructor(elements: Iterable<E> | Iterable<R> = [], options?: HeapOptions<E, R>) {
|
||||
super();
|
||||
super(options);
|
||||
|
||||
if (options) {
|
||||
const { comparator, toElementFn } = options;
|
||||
const { comparator } = options;
|
||||
if (comparator) this._comparator = comparator;
|
||||
if (toElementFn) this._toElementFn = toElementFn;
|
||||
}
|
||||
|
||||
if (elements) {
|
||||
for (const el of elements) {
|
||||
if (this._toElementFn) this.add(this._toElementFn(el as R));
|
||||
if (this.toElementFn) this.add(this.toElementFn(el as R));
|
||||
else this.add(el as E);
|
||||
}
|
||||
}
|
||||
|
@ -60,12 +60,6 @@ export class Heap<E = any, R = any> extends IterableElementBase<E> {
|
|||
return this._elements;
|
||||
}
|
||||
|
||||
protected _toElementFn?: (rawElement: R) => E;
|
||||
|
||||
get toElementFn() {
|
||||
return this._toElementFn;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the size (number of elements) of the heap.
|
||||
*/
|
||||
|
@ -132,12 +126,6 @@ export class Heap<E = any, R = any> extends IterableElementBase<E> {
|
|||
return this.elements[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(log n)
|
||||
* Space Complexity: O(1)
|
||||
* where n is the number of elements in the heap.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Check if the heap is empty.
|
||||
* @returns True if the heap is empty, otherwise false.
|
||||
|
@ -146,12 +134,6 @@ export class Heap<E = any, R = any> extends IterableElementBase<E> {
|
|||
return this.size === 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(log n)
|
||||
* Space Complexity: O(1)
|
||||
* where n is the number of elements in the heap.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Reset the elements of the heap. Make the elements empty.
|
||||
*/
|
||||
|
@ -159,11 +141,6 @@ export class Heap<E = any, R = any> extends IterableElementBase<E> {
|
|||
this._elements = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(1)
|
||||
* Space Complexity: O(1)
|
||||
*/
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(n)
|
||||
|
@ -214,11 +191,6 @@ export class Heap<E = any, R = any> extends IterableElementBase<E> {
|
|||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(n)
|
||||
*/
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(log n)
|
||||
|
@ -256,11 +228,6 @@ export class Heap<E = any, R = any> extends IterableElementBase<E> {
|
|||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(1)
|
||||
*/
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(n)
|
||||
|
@ -272,12 +239,6 @@ export class Heap<E = any, R = any> extends IterableElementBase<E> {
|
|||
return [...this.elements];
|
||||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(1)
|
||||
* The worst-case O(n) This is because, in the worst case, the element to be deleted is located at the end of the heap (not the root), and after deletion, we may need to reorganize the elements by performing a sinkDown operation.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(n)
|
||||
|
@ -285,18 +246,10 @@ export class Heap<E = any, R = any> extends IterableElementBase<E> {
|
|||
* Clone the heap, creating a new heap with the same elements.
|
||||
* @returns A new Heap instance containing the same elements.
|
||||
*/
|
||||
clone(): Heap<E> {
|
||||
const clonedHeap = new Heap<E>([], { comparator: this.comparator });
|
||||
clonedHeap._elements = [...this.elements];
|
||||
return clonedHeap;
|
||||
clone(): Heap<E, R> {
|
||||
return new Heap<E, R>(this, { comparator: this.comparator, toElementFn: this.toElementFn });
|
||||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(log n)
|
||||
* where log n is the height of the heap.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n log n)
|
||||
* Space Complexity: O(n)
|
||||
|
@ -306,19 +259,14 @@ export class Heap<E = any, R = any> extends IterableElementBase<E> {
|
|||
*/
|
||||
sort(): E[] {
|
||||
const visitedNode: E[] = [];
|
||||
const cloned = this.clone();
|
||||
const cloned = new Heap<E, R>(this, { comparator: this.comparator });
|
||||
while (cloned.size !== 0) {
|
||||
const top = cloned.poll();
|
||||
if (top) visitedNode.push(top);
|
||||
if (top !== undefined) visitedNode.push(top);
|
||||
}
|
||||
return visitedNode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(n)
|
||||
*/
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n log n)
|
||||
* Space Complexity: O(n)
|
||||
|
@ -331,11 +279,6 @@ export class Heap<E = any, R = any> extends IterableElementBase<E> {
|
|||
return results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(n)
|
||||
*/
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(n)
|
||||
|
@ -352,8 +295,8 @@ export class Heap<E = any, R = any> extends IterableElementBase<E> {
|
|||
* @returns The `filter` method is returning a new `Heap` object that contains the elements that pass
|
||||
* the filter condition specified by the `callback` function.
|
||||
*/
|
||||
filter(callback: ElementCallback<E, boolean>, thisArg?: any): Heap<E> {
|
||||
const filteredList = new Heap<E>();
|
||||
filter(callback: ElementCallback<E, R, boolean, Heap<E, R>>, thisArg?: any): Heap<E, R> {
|
||||
const filteredList = new Heap<E, R>([], { toElementFn: this.toElementFn, comparator: this.comparator });
|
||||
let index = 0;
|
||||
for (const current of this) {
|
||||
if (callback.call(thisArg, current, index, this)) {
|
||||
|
@ -367,30 +310,31 @@ export class Heap<E = any, R = any> extends IterableElementBase<E> {
|
|||
/**
|
||||
* Time Complexity: O(n log n)
|
||||
* Space Complexity: O(n)
|
||||
*/
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(n)
|
||||
*
|
||||
* The `map` function creates a new heap by applying a callback function to each element of the
|
||||
* original heap.
|
||||
* @param callback - The callback parameter is a function that will be called for each element in the
|
||||
* original heap. It takes three arguments: the current element, the index of the current element,
|
||||
* and the original heap itself. The callback function should return a value of type T, which will be
|
||||
* added to the mapped heap.
|
||||
* @param comparator - The `comparator` parameter is a function that is used to compare elements in
|
||||
* the heap. It takes two arguments, `a` and `b`, and returns a negative number if `a` is less than
|
||||
* `b`, a positive number if `a` is greater than `b`, or
|
||||
* @param callback - The `callback` parameter is a function that will be called for each element in
|
||||
* the heap. It takes three arguments: `el` (the current element), `index` (the index of the current
|
||||
* element), and `this` (the heap itself). The callback function should return a value of
|
||||
* @param comparator - The `comparator` parameter is a function that defines the order of the
|
||||
* elements in the heap. It takes two elements `a` and `b` as arguments and returns a negative number
|
||||
* if `a` should be placed before `b`, a positive number if `a` should be placed after
|
||||
* @param [toElementFn] - The `toElementFn` parameter is an optional function that converts the raw
|
||||
* element `RR` to the desired type `T`. It takes a single argument `rawElement` of type `RR` and
|
||||
* returns a value of type `T`. This function is used to transform the elements of the original
|
||||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
||||
* specify the value of `this` within the callback function. It is used when you want to bind a
|
||||
* specific object as the context for the callback function. If `thisArg` is not provided,
|
||||
* `undefined` is used as
|
||||
* @returns a new instance of the Heap class, which is created using the mapped elements from the
|
||||
* original Heap.
|
||||
* 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
|
||||
* @returns a new instance of the `Heap` class with the mapped elements.
|
||||
*/
|
||||
map<T>(callback: ElementCallback<E, T>, comparator: Comparator<T>, thisArg?: any): Heap<T> {
|
||||
const mappedHeap: Heap<T> = new Heap<T>([], { comparator: comparator });
|
||||
map<EM, RM>(
|
||||
callback: ElementCallback<E, R, EM, Heap<E, R>>,
|
||||
comparator: Comparator<EM>,
|
||||
toElementFn?: (rawElement: RM) => EM,
|
||||
thisArg?: any
|
||||
): Heap<EM, RM> {
|
||||
const mappedHeap: Heap<EM, RM> = new Heap<EM, RM>([], { comparator, toElementFn });
|
||||
let index = 0;
|
||||
for (const el of this) {
|
||||
mappedHeap.add(callback.call(thisArg, el, index, this));
|
||||
|
@ -429,11 +373,6 @@ export class Heap<E = any, R = any> extends IterableElementBase<E> {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(log n)
|
||||
* Space Complexity: O(1)
|
||||
*/
|
||||
|
||||
/**
|
||||
* Time Complexity: O(log n)
|
||||
* Space Complexity: O(1)
|
||||
|
@ -454,11 +393,6 @@ export class Heap<E = any, R = any> extends IterableElementBase<E> {
|
|||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(log n)
|
||||
* Space Complexity: O(1)
|
||||
*/
|
||||
|
||||
/**
|
||||
* Time Complexity: O(log n)
|
||||
* Space Complexity: O(1)
|
||||
|
@ -580,11 +514,6 @@ export class FibonacciHeap<E> {
|
|||
this._size = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(1)
|
||||
* Space Complexity: O(1)
|
||||
*/
|
||||
|
||||
/**
|
||||
* Time Complexity: O(1)
|
||||
* Space Complexity: O(1)
|
||||
|
@ -597,11 +526,6 @@ export class FibonacciHeap<E> {
|
|||
return this.push(element);
|
||||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(1)
|
||||
* Space Complexity: O(1)
|
||||
*/
|
||||
|
||||
/**
|
||||
* Time Complexity: O(1)
|
||||
* Space Complexity: O(1)
|
||||
|
@ -624,11 +548,6 @@ export class FibonacciHeap<E> {
|
|||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(1)
|
||||
* Space Complexity: O(1)
|
||||
*/
|
||||
|
||||
/**
|
||||
* Time Complexity: O(1)
|
||||
* Space Complexity: O(1)
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
* @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
|
||||
* @license MIT License
|
||||
*/
|
||||
import type { HeapOptions } from '../../types';
|
||||
import type { Comparator, ElementCallback, HeapOptions } from '../../types';
|
||||
import { Heap } from './heap';
|
||||
|
||||
/**
|
||||
|
@ -18,7 +18,7 @@ import { Heap } from './heap';
|
|||
* 7. Efficient Sorting Algorithms: For example, heap sort. Heap sort uses the properties of a heap to sort elements.
|
||||
* 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum-spanning tree algorithm, which use heaps to improve performance.
|
||||
*/
|
||||
export class MaxHeap<E = any, R = any> extends Heap<E> {
|
||||
export class MaxHeap<E = any, R = any> extends Heap<E, R> {
|
||||
constructor(elements: Iterable<E> | Iterable<R> = [], options?: HeapOptions<E, R>) {
|
||||
super(elements, {
|
||||
comparator: (a: E, b: E): number => {
|
||||
|
@ -34,4 +34,78 @@ export class MaxHeap<E = any, R = any> extends Heap<E> {
|
|||
...options
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* The `clone` function returns a new instance of the `MaxHeap` class with the same properties as the
|
||||
* current instance.
|
||||
* @returns The `clone()` method is returning a new instance of the `MaxHeap` class with the same
|
||||
* properties as the current instance.
|
||||
*/
|
||||
override clone(): MaxHeap<E, R> {
|
||||
return new MaxHeap<E, R>(this, { comparator: this.comparator, toElementFn: this.toElementFn });
|
||||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(n)
|
||||
*
|
||||
* The `filter` function creates a new MaxHeap object containing elements that pass a given callback
|
||||
* function.
|
||||
* @param callback - The `callback` parameter is a function that will be called for each element in
|
||||
* the heap. It takes three arguments: the current element, the index of the current element, and the
|
||||
* heap itself. The callback function should return a boolean value indicating whether the current
|
||||
* element should be included in the filtered list
|
||||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
||||
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
||||
* passed as the `this` value to the `callback` function. If `thisArg` is
|
||||
* @returns The `filter` method is returning a new `MaxHeap` object that contains the elements that pass
|
||||
* the filter condition specified by the `callback` function.
|
||||
*/
|
||||
override filter(callback: ElementCallback<E, R, boolean, MaxHeap<E, R>>, thisArg?: any): MaxHeap<E, R> {
|
||||
const filteredList = new MaxHeap<E, R>([], { toElementFn: this.toElementFn, comparator: this.comparator });
|
||||
let index = 0;
|
||||
for (const current of this) {
|
||||
if (callback.call(thisArg, current, index, this)) {
|
||||
filteredList.add(current);
|
||||
}
|
||||
index++;
|
||||
}
|
||||
return filteredList;
|
||||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n log n)
|
||||
* Space Complexity: O(n)
|
||||
*
|
||||
* The `map` function creates a new heap by applying a callback function to each element of the
|
||||
* original heap.
|
||||
* @param callback - The `callback` parameter is a function that will be called for each element in
|
||||
* the heap. It takes three arguments: `el` (the current element), `index` (the index of the current
|
||||
* element), and `this` (the heap itself). The callback function should return a value of
|
||||
* @param comparator - The `comparator` parameter is a function that defines the order of the
|
||||
* elements in the heap. It takes two elements `a` and `b` as arguments and returns a negative number
|
||||
* if `a` should be placed before `b`, a positive number if `a` should be placed after
|
||||
* @param [toElementFn] - The `toElementFn` parameter is an optional function that converts the raw
|
||||
* element `RR` to the desired type `T`. It takes a single argument `rawElement` of type `RR` and
|
||||
* returns a value of type `T`. This function is used to transform the elements of the original
|
||||
* @param {any} [thisArg] - The `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
|
||||
* @returns a new instance of the `MaxHeap` class with the mapped elements.
|
||||
*/
|
||||
override map<EM, RM>(
|
||||
callback: ElementCallback<E, R, EM, MaxHeap<E, R>>,
|
||||
comparator: Comparator<EM>,
|
||||
toElementFn?: (rawElement: RM) => EM,
|
||||
thisArg?: any
|
||||
): MaxHeap<EM, RM> {
|
||||
const mappedHeap: MaxHeap<EM, RM> = new MaxHeap<EM, RM>([], { comparator, toElementFn });
|
||||
let index = 0;
|
||||
for (const el of this) {
|
||||
mappedHeap.add(callback.call(thisArg, el, index, this));
|
||||
index++;
|
||||
}
|
||||
return mappedHeap;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,21 +5,95 @@
|
|||
* @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
|
||||
* @license MIT License
|
||||
*/
|
||||
import type { HeapOptions } from '../../types';
|
||||
import type { Comparator, ElementCallback, HeapOptions } from '../../types';
|
||||
import { Heap } from './heap';
|
||||
|
||||
/**
|
||||
* 1. Complete Binary Tree: Heaps are typically complete binary trees, meaning every level is fully filled except possibly for the last level, which has nodes as far left as possible.
|
||||
* 2. Heap Properties: The value of each parent node is less than or equal to the value of its children.
|
||||
* 2. MinHeap Properties: The value of each parent node is less than or equal to the value of its children.
|
||||
* 3. Root Node Access: In a heap, the largest element (in a max heap) or the smallest element (in a min heap) is always at the root of the tree.
|
||||
* 4. Efficient Insertion and Deletion: Due to its structure, a heap allows for insertion and deletion operations in logarithmic time (O(log n)).
|
||||
* 5. Managing Dynamic Data Sets: Heaps effectively manage dynamic data sets, especially when frequent access to the largest or smallest elements is required.
|
||||
* 6. Non-linear Search: While a heap allows rapid access to its largest or smallest element, it is less efficient for other operations, such as searching for a specific element, as it is not designed for these tasks.
|
||||
* 7. Efficient Sorting Algorithms: For example, heap sort. Heap sort uses the properties of a heap to sort elements.
|
||||
* 7. Efficient Sorting Algorithms: For example, heap sort. MinHeap sort uses the properties of a heap to sort elements.
|
||||
* 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance.
|
||||
*/
|
||||
export class MinHeap<E = any, R = any> extends Heap<E> {
|
||||
export class MinHeap<E = any, R = any> extends Heap<E, R> {
|
||||
constructor(elements: Iterable<E> | Iterable<R> = [], options?: HeapOptions<E, R>) {
|
||||
super(elements, options);
|
||||
}
|
||||
|
||||
/**
|
||||
* The `clone` function returns a new instance of the `MinHeap` class with the same comparator and
|
||||
* toElementFn as the original instance.
|
||||
* @returns The `clone()` method is returning a new instance of the `MinHeap` class with the same
|
||||
* properties as the current instance.
|
||||
*/
|
||||
override clone(): MinHeap<E, R> {
|
||||
return new MinHeap<E, R>(this, { comparator: this.comparator, toElementFn: this.toElementFn });
|
||||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(n)
|
||||
*
|
||||
* The `filter` function creates a new MinHeap object containing elements that pass a given callback
|
||||
* function.
|
||||
* @param callback - The `callback` parameter is a function that will be called for each element in
|
||||
* the heap. It takes three arguments: the current element, the index of the current element, and the
|
||||
* heap itself. The callback function should return a boolean value indicating whether the current
|
||||
* element should be included in the filtered list
|
||||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
||||
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
||||
* passed as the `this` value to the `callback` function. If `thisArg` is
|
||||
* @returns The `filter` method is returning a new `MinHeap` object that contains the elements that pass
|
||||
* the filter condition specified by the `callback` function.
|
||||
*/
|
||||
override filter(callback: ElementCallback<E, R, boolean, MinHeap<E, R>>, thisArg?: any): MinHeap<E, R> {
|
||||
const filteredList = new MinHeap<E, R>([], { toElementFn: this.toElementFn, comparator: this.comparator });
|
||||
let index = 0;
|
||||
for (const current of this) {
|
||||
if (callback.call(thisArg, current, index, this)) {
|
||||
filteredList.add(current);
|
||||
}
|
||||
index++;
|
||||
}
|
||||
return filteredList;
|
||||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n log n)
|
||||
* Space Complexity: O(n)
|
||||
*
|
||||
* The `map` function creates a new heap by applying a callback function to each element of the
|
||||
* original heap.
|
||||
* @param callback - The `callback` parameter is a function that will be called for each element in
|
||||
* the heap. It takes three arguments: `el` (the current element), `index` (the index of the current
|
||||
* element), and `this` (the heap itself). The callback function should return a value of
|
||||
* @param comparator - The `comparator` parameter is a function that defines the order of the
|
||||
* elements in the heap. It takes two elements `a` and `b` as arguments and returns a negative number
|
||||
* if `a` should be placed before `b`, a positive number if `a` should be placed after
|
||||
* @param [toElementFn] - The `toElementFn` parameter is an optional function that converts the raw
|
||||
* element `RR` to the desired type `T`. It takes a single argument `rawElement` of type `RR` and
|
||||
* returns a value of type `T`. This function is used to transform the elements of the original
|
||||
* @param {any} [thisArg] - The `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
|
||||
* @returns a new instance of the `MinHeap` class with the mapped elements.
|
||||
*/
|
||||
override map<EM, RM>(
|
||||
callback: ElementCallback<E, R, EM, MinHeap<E, R>>,
|
||||
comparator: Comparator<EM>,
|
||||
toElementFn?: (rawElement: RM) => EM,
|
||||
thisArg?: any
|
||||
): MinHeap<EM, RM> {
|
||||
const mappedHeap: MinHeap<EM, RM> = new MinHeap<EM, RM>([], { comparator, toElementFn });
|
||||
let index = 0;
|
||||
for (const el of this) {
|
||||
mappedHeap.add(callback.call(thisArg, el, index, this));
|
||||
index++;
|
||||
}
|
||||
return mappedHeap;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
||||
* @license MIT License
|
||||
*/
|
||||
import type { ElementCallback } from '../../types';
|
||||
import type { DoublyLinkedListOptions, ElementCallback } from '../../types';
|
||||
import { IterableElementBase } from '../base';
|
||||
|
||||
export class DoublyLinkedListNode<E = any> {
|
||||
|
@ -87,21 +87,17 @@ export class DoublyLinkedListNode<E = any> {
|
|||
* 3. No Centralized Index: Unlike arrays, elements in a linked list are not stored contiguously, so there is no centralized index. Accessing elements in a linked list typically requires traversing from the head or tail node.
|
||||
* 4. High Efficiency in Insertion and Deletion: Adding or removing elements in a linked list does not require moving other elements, making these operations more efficient than in arrays.
|
||||
*/
|
||||
export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
||||
/**
|
||||
* The constructor initializes a linked list with optional elements.
|
||||
* @param elements - The `elements` parameter is an optional iterable object that contains the
|
||||
* initial elements to be added to the data structure. It defaults to an empty array if no elements
|
||||
* are provided.
|
||||
*/
|
||||
constructor(elements: Iterable<E> = []) {
|
||||
super();
|
||||
export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R, DoublyLinkedList<E, R>> {
|
||||
constructor(elements: Iterable<E> | Iterable<R> = [], options?: DoublyLinkedListOptions<E, R>) {
|
||||
super(options);
|
||||
this._head = undefined;
|
||||
this._tail = undefined;
|
||||
this._size = 0;
|
||||
if (elements) {
|
||||
for (const el of elements) {
|
||||
this.push(el);
|
||||
if (this.toElementFn) {
|
||||
this.push(this.toElementFn(el as R));
|
||||
} else this.push(el as E);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -729,8 +725,8 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|||
* @returns The `clone()` method is returning a new instance of the `DoublyLinkedList` class, which
|
||||
* is a copy of the original list.
|
||||
*/
|
||||
clone(): DoublyLinkedList<E> {
|
||||
return new DoublyLinkedList(this.values());
|
||||
clone(): DoublyLinkedList<E, R> {
|
||||
return new DoublyLinkedList<E, R>(this);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -755,8 +751,8 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|||
* @returns The `filter` method is returning a new `DoublyLinkedList` object that contains the
|
||||
* elements that pass the filter condition specified by the `callback` function.
|
||||
*/
|
||||
filter(callback: ElementCallback<E, boolean>, thisArg?: any): DoublyLinkedList<E> {
|
||||
const filteredList = new DoublyLinkedList<E>();
|
||||
filter(callback: ElementCallback<E, R, boolean, DoublyLinkedList<E, R>>, thisArg?: any): DoublyLinkedList<E, R> {
|
||||
const filteredList = new DoublyLinkedList<E, R>([], { toElementFn: this.toElementFn });
|
||||
let index = 0;
|
||||
for (const current of this) {
|
||||
if (callback.call(thisArg, current, index, this)) {
|
||||
|
@ -773,24 +769,28 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|||
*/
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(n)
|
||||
*
|
||||
* The `map` function creates a new DoublyLinkedList by applying a callback function to each element
|
||||
* in the original list.
|
||||
* The `map` function takes a callback function and returns a new DoublyLinkedList with the results
|
||||
* of applying the callback to each element in the original list.
|
||||
* @param callback - The callback parameter is a function that will be called for each element in the
|
||||
* DoublyLinkedList. It takes three arguments: the current element, the index of the current element,
|
||||
* and the DoublyLinkedList itself. The callback function should return a value that will be added to
|
||||
* the new DoublyLinkedList that
|
||||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
||||
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
||||
* passed as the `this` value to the `callback` function. If `thisArg` is
|
||||
* @returns The `map` function is returning a new `DoublyLinkedList` object that contains the results
|
||||
* of applying the provided `callback` function to each element in the original `DoublyLinkedList`
|
||||
* object.
|
||||
* original DoublyLinkedList. It takes three arguments: current (the current element being
|
||||
* processed), index (the index of the current element), and this (the original DoublyLinkedList).
|
||||
* The callback function should return a value of type
|
||||
* @param [toElementFn] - The `toElementFn` parameter is an optional function that can be used to
|
||||
* convert the raw element (`RR`) to the desired element type (`T`). It takes the raw element as
|
||||
* input and returns the converted element. If this parameter is not provided, the raw element will
|
||||
* be used as is.
|
||||
* @param {any} [thisArg] - The `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
|
||||
* @returns a new instance of the `DoublyLinkedList` class with elements of type `T` and `RR`.
|
||||
*/
|
||||
map<T>(callback: ElementCallback<E, T>, thisArg?: any): DoublyLinkedList<T> {
|
||||
const mappedList = new DoublyLinkedList<T>();
|
||||
map<EM, RM>(
|
||||
callback: ElementCallback<E, R, EM, DoublyLinkedList<E, R>>,
|
||||
toElementFn?: (rawElement: RM) => EM,
|
||||
thisArg?: any
|
||||
): DoublyLinkedList<EM, RM> {
|
||||
const mappedList = new DoublyLinkedList<EM, RM>([], { toElementFn });
|
||||
let index = 0;
|
||||
for (const current of this) {
|
||||
mappedList.push(callback.call(thisArg, current, index, this));
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
||||
* @license MIT License
|
||||
*/
|
||||
import type { ElementCallback } from '../../types';
|
||||
import type { ElementCallback, SinglyLinkedListOptions } from '../../types';
|
||||
import { IterableElementBase } from '../base';
|
||||
|
||||
export class SinglyLinkedListNode<E = any> {
|
||||
|
@ -59,17 +59,17 @@ export class SinglyLinkedListNode<E = any> {
|
|||
}
|
||||
}
|
||||
|
||||
export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
||||
/**
|
||||
* The constructor initializes a new instance of a class with an optional iterable of elements.
|
||||
* @param elements - The `elements` parameter is an optional iterable object that contains the
|
||||
* initial elements to be added to the instance of the class. If no `elements` are provided, an empty
|
||||
* array will be used as the default value.
|
||||
*/
|
||||
constructor(elements: Iterable<E> = []) {
|
||||
super();
|
||||
export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R, SinglyLinkedList<E, R>> {
|
||||
constructor(elements: Iterable<E> | Iterable<R> = [], options?: SinglyLinkedListOptions<E, R>) {
|
||||
super(options);
|
||||
if (elements) {
|
||||
for (const el of elements) this.push(el);
|
||||
for (const el of elements) {
|
||||
if (this.toElementFn) {
|
||||
this.push(this.toElementFn(el as R));
|
||||
} else {
|
||||
this.push(el as E);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -673,8 +673,8 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|||
* @returns The `clone()` method is returning a new instance of the `SinglyLinkedList` class, which
|
||||
* is a clone of the original list.
|
||||
*/
|
||||
clone(): SinglyLinkedList<E> {
|
||||
return new SinglyLinkedList<E>(this.values());
|
||||
clone(): SinglyLinkedList<E, R> {
|
||||
return new SinglyLinkedList<E, R>(this, { toElementFn: this.toElementFn });
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -699,8 +699,8 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|||
* @returns The `filter` method is returning a new `SinglyLinkedList` object that contains the
|
||||
* elements that pass the filter condition specified by the `callback` function.
|
||||
*/
|
||||
filter(callback: ElementCallback<E, boolean>, thisArg?: any): SinglyLinkedList<E> {
|
||||
const filteredList = new SinglyLinkedList<E>();
|
||||
filter(callback: ElementCallback<E, R, boolean, SinglyLinkedList<E, R>>, thisArg?: any): SinglyLinkedList<E, R> {
|
||||
const filteredList = new SinglyLinkedList<E, R>([], { toElementFn: this.toElementFn });
|
||||
let index = 0;
|
||||
for (const current of this) {
|
||||
if (callback.call(thisArg, current, index, this)) {
|
||||
|
@ -715,22 +715,30 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(n)
|
||||
*/
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(n)
|
||||
*
|
||||
* The `map` function creates a new SinglyLinkedList by applying a callback function to each element
|
||||
* of the original list.
|
||||
* The `map` function takes a callback function and returns a new SinglyLinkedList with the results
|
||||
* of applying the callback to each element in the original list.
|
||||
* @param callback - The `callback` parameter is a function that will be called for each element in
|
||||
* the linked list. It takes three arguments:
|
||||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
||||
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
||||
* passed as the `this` value to the `callback` function. If `thisArg` is
|
||||
* @returns The `map` function is returning a new `SinglyLinkedList` object that contains the results
|
||||
* of applying the provided `callback` function to each element in the original list.
|
||||
* the original list. It takes three arguments: `current` (the current element being processed),
|
||||
* `index` (the index of the current element), and `this` (the original list). It should return a
|
||||
* value
|
||||
* @param [toElementFn] - The `toElementFn` parameter is an optional function that can be used to
|
||||
* convert the raw element (`RR`) to the desired element type (`T`). It takes the raw element as
|
||||
* input and returns the converted element. If this parameter is not provided, the raw element will
|
||||
* be used as is.
|
||||
* @param {any} [thisArg] - The `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
|
||||
* @returns a new instance of the `SinglyLinkedList` class with the mapped elements.
|
||||
*/
|
||||
map<T>(callback: ElementCallback<E, T>, thisArg?: any): SinglyLinkedList<T> {
|
||||
const mappedList = new SinglyLinkedList<T>();
|
||||
map<EM, RM>(
|
||||
callback: ElementCallback<E, R, EM, SinglyLinkedList<E, R>>,
|
||||
toElementFn?: (rawElement: RM) => EM,
|
||||
thisArg?: any
|
||||
): SinglyLinkedList<EM, RM> {
|
||||
const mappedList = new SinglyLinkedList<EM, RM>([], { toElementFn });
|
||||
let index = 0;
|
||||
for (const current of this) {
|
||||
mappedList.push(callback.call(thisArg, current, index, this));
|
||||
|
|
|
@ -5,10 +5,10 @@
|
|||
* @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
|
||||
* @license MIT License
|
||||
*/
|
||||
import type { PriorityQueueOptions } from '../../types';
|
||||
import type { Comparator, ElementCallback, PriorityQueueOptions } from '../../types';
|
||||
import { PriorityQueue } from './priority-queue';
|
||||
|
||||
export class MaxPriorityQueue<E = any, R = any> extends PriorityQueue<E> {
|
||||
export class MaxPriorityQueue<E = any, R = any> extends PriorityQueue<E, R> {
|
||||
/**
|
||||
* The constructor initializes a PriorityQueue with optional elements and options, including a
|
||||
* comparator function.
|
||||
|
@ -34,4 +34,84 @@ export class MaxPriorityQueue<E = any, R = any> extends PriorityQueue<E> {
|
|||
...options
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* The `clone` function returns a new instance of the `MaxPriorityQueue` class with the same
|
||||
* comparator and toElementFn as the current instance.
|
||||
* @returns The method is returning a new instance of the MaxPriorityQueue class with the same
|
||||
* comparator and toElementFn as the current instance.
|
||||
*/
|
||||
override clone(): MaxPriorityQueue<E, R> {
|
||||
return new MaxPriorityQueue<E, R>(this, { comparator: this.comparator, toElementFn: this.toElementFn });
|
||||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(n)
|
||||
*
|
||||
* The `filter` function creates a new MaxPriorityQueue object containing elements that pass a given callback
|
||||
* function.
|
||||
* @param callback - The `callback` parameter is a function that will be called for each element in
|
||||
* the heap. It takes three arguments: the current element, the index of the current element, and the
|
||||
* heap itself. The callback function should return a boolean value indicating whether the current
|
||||
* element should be included in the filtered list
|
||||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
||||
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
||||
* passed as the `this` value to the `callback` function. If `thisArg` is
|
||||
* @returns The `filter` method is returning a new `MaxPriorityQueue` object that contains the elements that pass
|
||||
* the filter condition specified by the `callback` function.
|
||||
*/
|
||||
override filter(
|
||||
callback: ElementCallback<E, R, boolean, MaxPriorityQueue<E, R>>,
|
||||
thisArg?: any
|
||||
): MaxPriorityQueue<E, R> {
|
||||
const filteredPriorityQueue = new MaxPriorityQueue<E, R>([], {
|
||||
toElementFn: this.toElementFn,
|
||||
comparator: this.comparator
|
||||
});
|
||||
let index = 0;
|
||||
for (const current of this) {
|
||||
if (callback.call(thisArg, current, index, this)) {
|
||||
filteredPriorityQueue.add(current);
|
||||
}
|
||||
index++;
|
||||
}
|
||||
return filteredPriorityQueue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n log n)
|
||||
* Space Complexity: O(n)
|
||||
*
|
||||
* The `map` function creates a new heap by applying a callback function to each element of the
|
||||
* original heap.
|
||||
* @param callback - The `callback` parameter is a function that will be called for each element in
|
||||
* the heap. It takes three arguments: `el` (the current element), `index` (the index of the current
|
||||
* element), and `this` (the heap itself). The callback function should return a value of
|
||||
* @param comparator - The `comparator` parameter is a function that defines the order of the
|
||||
* elements in the heap. It takes two elements `a` and `b` as arguments and returns a negative number
|
||||
* if `a` should be placed before `b`, a positive number if `a` should be placed after
|
||||
* @param [toElementFn] - The `toElementFn` parameter is an optional function that converts the raw
|
||||
* element `RR` to the desired type `T`. It takes a single argument `rawElement` of type `RR` and
|
||||
* returns a value of type `T`. This function is used to transform the elements of the original
|
||||
* @param {any} [thisArg] - The `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
|
||||
* @returns a new instance of the `MaxPriorityQueue` class with the mapped elements.
|
||||
*/
|
||||
override map<EM, RM>(
|
||||
callback: ElementCallback<E, R, EM, MaxPriorityQueue<E, R>>,
|
||||
comparator: Comparator<EM>,
|
||||
toElementFn?: (rawElement: RM) => EM,
|
||||
thisArg?: any
|
||||
): MaxPriorityQueue<EM, RM> {
|
||||
const mappedPriorityQueue = new MaxPriorityQueue<EM, RM>([], { comparator, toElementFn });
|
||||
let index = 0;
|
||||
for (const el of this) {
|
||||
mappedPriorityQueue.add(callback.call(thisArg, el, index, this));
|
||||
index++;
|
||||
}
|
||||
return mappedPriorityQueue;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,10 +5,10 @@
|
|||
* @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
|
||||
* @license MIT License
|
||||
*/
|
||||
import type { PriorityQueueOptions } from '../../types';
|
||||
import type { Comparator, ElementCallback, PriorityQueueOptions } from '../../types';
|
||||
import { PriorityQueue } from './priority-queue';
|
||||
|
||||
export class MinPriorityQueue<E = any, R = any> extends PriorityQueue<E> {
|
||||
export class MinPriorityQueue<E = any, R = any> extends PriorityQueue<E, R> {
|
||||
/**
|
||||
* The constructor initializes a PriorityQueue with optional elements and options, including a
|
||||
* comparator function.
|
||||
|
@ -23,4 +23,84 @@ export class MinPriorityQueue<E = any, R = any> extends PriorityQueue<E> {
|
|||
constructor(elements: Iterable<E> | Iterable<R> = [], options?: PriorityQueueOptions<E, R>) {
|
||||
super(elements, options);
|
||||
}
|
||||
|
||||
/**
|
||||
* The `clone` function returns a new instance of the `MinPriorityQueue` class with the same
|
||||
* comparator and toElementFn as the original instance.
|
||||
* @returns The method is returning a new instance of the `MinPriorityQueue` class with the same
|
||||
* properties as the current instance.
|
||||
*/
|
||||
override clone(): MinPriorityQueue<E, R> {
|
||||
return new MinPriorityQueue<E, R>(this, { comparator: this.comparator, toElementFn: this.toElementFn });
|
||||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(n)
|
||||
*
|
||||
* The `filter` function creates a new MinPriorityQueue object containing elements that pass a given callback
|
||||
* function.
|
||||
* @param callback - The `callback` parameter is a function that will be called for each element in
|
||||
* the heap. It takes three arguments: the current element, the index of the current element, and the
|
||||
* heap itself. The callback function should return a boolean value indicating whether the current
|
||||
* element should be included in the filtered list
|
||||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
||||
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
||||
* passed as the `this` value to the `callback` function. If `thisArg` is
|
||||
* @returns The `filter` method is returning a new `MinPriorityQueue` object that contains the elements that pass
|
||||
* the filter condition specified by the `callback` function.
|
||||
*/
|
||||
override filter(
|
||||
callback: ElementCallback<E, R, boolean, MinPriorityQueue<E, R>>,
|
||||
thisArg?: any
|
||||
): MinPriorityQueue<E, R> {
|
||||
const filteredPriorityQueue = new MinPriorityQueue<E, R>([], {
|
||||
toElementFn: this.toElementFn,
|
||||
comparator: this.comparator
|
||||
});
|
||||
let index = 0;
|
||||
for (const current of this) {
|
||||
if (callback.call(thisArg, current, index, this)) {
|
||||
filteredPriorityQueue.add(current);
|
||||
}
|
||||
index++;
|
||||
}
|
||||
return filteredPriorityQueue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n log n)
|
||||
* Space Complexity: O(n)
|
||||
*
|
||||
* The `map` function creates a new heap by applying a callback function to each element of the
|
||||
* original heap.
|
||||
* @param callback - The `callback` parameter is a function that will be called for each element in
|
||||
* the heap. It takes three arguments: `el` (the current element), `index` (the index of the current
|
||||
* element), and `this` (the heap itself). The callback function should return a value of
|
||||
* @param comparator - The `comparator` parameter is a function that defines the order of the
|
||||
* elements in the heap. It takes two elements `a` and `b` as arguments and returns a negative number
|
||||
* if `a` should be placed before `b`, a positive number if `a` should be placed after
|
||||
* @param [toElementFn] - The `toElementFn` parameter is an optional function that converts the raw
|
||||
* element `RR` to the desired type `T`. It takes a single argument `rawElement` of type `RR` and
|
||||
* returns a value of type `T`. This function is used to transform the elements of the original
|
||||
* @param {any} [thisArg] - The `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
|
||||
* @returns a new instance of the `MinPriorityQueue` class with the mapped elements.
|
||||
*/
|
||||
override map<EM, RM>(
|
||||
callback: ElementCallback<E, R, EM, MinPriorityQueue<E, R>>,
|
||||
comparator: Comparator<EM>,
|
||||
toElementFn?: (rawElement: RM) => EM,
|
||||
thisArg?: any
|
||||
): MinPriorityQueue<EM, RM> {
|
||||
const mappedPriorityQueue: MinPriorityQueue<EM, RM> = new MinPriorityQueue<EM, RM>([], { comparator, toElementFn });
|
||||
let index = 0;
|
||||
for (const el of this) {
|
||||
mappedPriorityQueue.add(callback.call(thisArg, el, index, this));
|
||||
index++;
|
||||
}
|
||||
return mappedPriorityQueue;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
* @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
|
||||
* @license MIT License
|
||||
*/
|
||||
import type { PriorityQueueOptions } from '../../types';
|
||||
import type { Comparator, ElementCallback, PriorityQueueOptions } from '../../types';
|
||||
import { Heap } from '../heap';
|
||||
|
||||
/**
|
||||
|
@ -28,4 +28,81 @@ export class PriorityQueue<E = any, R = any> extends Heap<E, R> {
|
|||
constructor(elements: Iterable<E> | Iterable<R> = [], options?: PriorityQueueOptions<E, R>) {
|
||||
super(elements, options);
|
||||
}
|
||||
|
||||
/**
|
||||
* The `clone` function returns a new instance of the `PriorityQueue` class with the same comparator
|
||||
* and toElementFn as the original instance.
|
||||
* @returns The method is returning a new instance of the `PriorityQueue` class with the same
|
||||
* elements and properties as the current instance.
|
||||
*/
|
||||
override clone(): PriorityQueue<E, R> {
|
||||
return new PriorityQueue<E, R>(this, { comparator: this.comparator, toElementFn: this.toElementFn });
|
||||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(n)
|
||||
*
|
||||
* The `filter` function creates a new PriorityQueue object containing elements that pass a given callback
|
||||
* function.
|
||||
* @param callback - The `callback` parameter is a function that will be called for each element in
|
||||
* the heap. It takes three arguments: the current element, the index of the current element, and the
|
||||
* heap itself. The callback function should return a boolean value indicating whether the current
|
||||
* element should be included in the filtered list
|
||||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
||||
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
||||
* passed as the `this` value to the `callback` function. If `thisArg` is
|
||||
* @returns The `filter` method is returning a new `PriorityQueue` object that contains the elements that pass
|
||||
* the filter condition specified by the `callback` function.
|
||||
*/
|
||||
override filter(callback: ElementCallback<E, R, boolean, PriorityQueue<E, R>>, thisArg?: any): PriorityQueue<E, R> {
|
||||
const filteredPriorityQueue = new PriorityQueue<E, R>([], {
|
||||
toElementFn: this.toElementFn,
|
||||
comparator: this.comparator
|
||||
});
|
||||
let index = 0;
|
||||
for (const current of this) {
|
||||
if (callback.call(thisArg, current, index, this)) {
|
||||
filteredPriorityQueue.add(current);
|
||||
}
|
||||
index++;
|
||||
}
|
||||
return filteredPriorityQueue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n log n)
|
||||
* Space Complexity: O(n)
|
||||
*
|
||||
* The `map` function creates a new heap by applying a callback function to each element of the
|
||||
* original heap.
|
||||
* @param callback - The `callback` parameter is a function that will be called for each element in
|
||||
* the heap. It takes three arguments: `el` (the current element), `index` (the index of the current
|
||||
* element), and `this` (the heap itself). The callback function should return a value of
|
||||
* @param comparator - The `comparator` parameter is a function that defines the order of the
|
||||
* elements in the heap. It takes two elements `a` and `b` as arguments and returns a negative number
|
||||
* if `a` should be placed before `b`, a positive number if `a` should be placed after
|
||||
* @param [toElementFn] - The `toElementFn` parameter is an optional function that converts the raw
|
||||
* element `RR` to the desired type `T`. It takes a single argument `rawElement` of type `RR` and
|
||||
* returns a value of type `T`. This function is used to transform the elements of the original
|
||||
* @param {any} [thisArg] - The `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
|
||||
* @returns a new instance of the `PriorityQueue` class with the mapped elements.
|
||||
*/
|
||||
override map<EM, RM>(
|
||||
callback: ElementCallback<E, R, EM, PriorityQueue<E, R>>,
|
||||
comparator: Comparator<EM>,
|
||||
toElementFn?: (rawElement: RM) => EM,
|
||||
thisArg?: any
|
||||
): PriorityQueue<EM, RM> {
|
||||
const mappedPriorityQueue: PriorityQueue<EM, RM> = new PriorityQueue<EM, RM>([], { comparator, toElementFn });
|
||||
let index = 0;
|
||||
for (const el of this) {
|
||||
mappedPriorityQueue.add(callback.call(thisArg, el, index, this));
|
||||
index++;
|
||||
}
|
||||
return mappedPriorityQueue;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,9 +16,9 @@ import { calcMinUnitsRequired, rangeCheck } from '../../utils';
|
|||
* 4. Efficiency: Adding and removing elements at both ends of a deque is usually very fast. However, when the dynamic array needs to expand, it may involve copying the entire array to a larger one, and this operation has a time complexity of O(n).
|
||||
* 5. Performance jitter: Deque may experience performance jitter, but DoublyLinkedList will not
|
||||
*/
|
||||
export class Deque<E> extends IterableElementBase<E> {
|
||||
export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E, R>> {
|
||||
/**
|
||||
* The constructor initializes a Deque object with an optional iterable of elements and options.
|
||||
* The constructor initializes a Deque object with optional iterable of elements and options.
|
||||
* @param elements - 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
|
||||
|
@ -28,8 +28,8 @@ export class Deque<E> extends IterableElementBase<E> {
|
|||
* which determines the size of each bucket in the deque. If the `bucketSize` option is not provided
|
||||
* or is not a number
|
||||
*/
|
||||
constructor(elements: IterableWithSizeOrLength<E> = [], options?: DequeOptions) {
|
||||
super();
|
||||
constructor(elements: IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R> = [], options?: DequeOptions<E, R>) {
|
||||
super(options);
|
||||
|
||||
if (options) {
|
||||
const { bucketSize } = options;
|
||||
|
@ -53,8 +53,12 @@ export class Deque<E> extends IterableElementBase<E> {
|
|||
this._bucketFirst = this._bucketLast = (this._bucketCount >> 1) - (needBucketNum >> 1);
|
||||
this._firstInBucket = this._lastInBucket = (this._bucketSize - (_size % this._bucketSize)) >> 1;
|
||||
|
||||
for (const element of elements) {
|
||||
this.push(element);
|
||||
for (const el of elements) {
|
||||
if (this.toElementFn) {
|
||||
this.push(this.toElementFn(el as R));
|
||||
} else {
|
||||
this.push(el as E);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -747,8 +751,8 @@ export class Deque<E> extends IterableElementBase<E> {
|
|||
* @returns 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.
|
||||
*/
|
||||
clone(): Deque<E> {
|
||||
return new Deque<E>([...this], { bucketSize: this.bucketSize });
|
||||
clone(): Deque<E, R> {
|
||||
return new Deque<E, R>(this, { bucketSize: this.bucketSize, toElementFn: this.toElementFn });
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -772,8 +776,8 @@ export class Deque<E> extends IterableElementBase<E> {
|
|||
* @returns The `filter` method is returning a new `Deque` object that contains the elements that
|
||||
* satisfy the given predicate function.
|
||||
*/
|
||||
filter(predicate: ElementCallback<E, boolean>, thisArg?: any): Deque<E> {
|
||||
const newDeque = new Deque<E>([], { bucketSize: this._bucketSize });
|
||||
filter(predicate: ElementCallback<E, R, boolean, Deque<E, R>>, thisArg?: any): Deque<E, R> {
|
||||
const newDeque = new Deque<E, R>([], { bucketSize: this._bucketSize, toElementFn: this.toElementFn });
|
||||
let index = 0;
|
||||
for (const el of this) {
|
||||
if (predicate.call(thisArg, el, index, this)) {
|
||||
|
@ -788,21 +792,28 @@ export class Deque<E> extends IterableElementBase<E> {
|
|||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(n)
|
||||
*/
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(n)
|
||||
*
|
||||
* The `map` function creates a new Deque by applying a callback function to each element of the
|
||||
* original Deque.
|
||||
* @param callback - The `callback` parameter is a function that will be called for each element in
|
||||
* the deque. It takes three arguments:
|
||||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
||||
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
||||
* passed as the `this` value to the `callback` function. If `thisArg` is
|
||||
* @returns a new Deque object with the mapped values.
|
||||
* 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 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.
|
||||
* @param [toElementFn] - 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.
|
||||
* @param {any} [thisArg] - The `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
|
||||
* @returns a new Deque object with elements of type EM and raw elements of type RM.
|
||||
*/
|
||||
map<T>(callback: ElementCallback<E, T>, thisArg?: any): Deque<T> {
|
||||
const newDeque = new Deque<T>([], { bucketSize: this._bucketSize });
|
||||
map<EM, RM>(
|
||||
callback: ElementCallback<E, R, EM, Deque<E, R>>,
|
||||
toElementFn?: (rawElement: RM) => EM,
|
||||
thisArg?: any
|
||||
): Deque<EM, RM> {
|
||||
const newDeque = new Deque<EM, RM>([], { bucketSize: this._bucketSize, toElementFn });
|
||||
let index = 0;
|
||||
for (const el of this) {
|
||||
newDeque.push(callback.call(thisArg, el, index, this));
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
* @copyright Tyler Zeng <zrwusa@gmail.com>
|
||||
* @class
|
||||
*/
|
||||
import type { ElementCallback } from '../../types';
|
||||
import type { ElementCallback, QueueOptions } from '../../types';
|
||||
import { IterableElementBase } from '../base';
|
||||
import { SinglyLinkedList } from '../linked-list';
|
||||
|
||||
|
@ -16,17 +16,14 @@ import { SinglyLinkedList } from '../linked-list';
|
|||
* 6. Breadth-First Search (BFS): In traversal algorithms for graphs and trees, queues store elements that are to be visited.
|
||||
* 7. Real-time Queuing: Like queuing systems in banks or supermarkets.
|
||||
*/
|
||||
export class Queue<E = any> extends IterableElementBase<E> {
|
||||
/**
|
||||
* The constructor initializes an instance of a class with an optional array of elements and sets the offset to 0.
|
||||
* @param {E[]} [elements] - The `elements` parameter is an optional array of elements of type `E`. If provided, it
|
||||
* will be used to initialize the `_elements` property of the class. If not provided, the `_elements` property will be
|
||||
* initialized as an empty array.
|
||||
*/
|
||||
constructor(elements: Iterable<E> = []) {
|
||||
super();
|
||||
export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E, R>> {
|
||||
constructor(elements: Iterable<E> | Iterable<R> = [], options?: QueueOptions<E, R>) {
|
||||
super(options);
|
||||
if (elements) {
|
||||
for (const el of elements) this.push(el);
|
||||
for (const el of elements) {
|
||||
if (this.toElementFn) this.push(this.toElementFn(el as R));
|
||||
else this.push(el as E);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -190,7 +187,7 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|||
* @param index
|
||||
*/
|
||||
at(index: number): E | undefined {
|
||||
return this.elements[index];
|
||||
return this.elements[index + this._offset];
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -254,8 +251,8 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|||
* The `clone()` function returns a new Queue object with the same elements as the original Queue.
|
||||
* @returns The `clone()` method is returning a new instance of the `Queue` class.
|
||||
*/
|
||||
clone(): Queue<E> {
|
||||
return new Queue(this.elements.slice(this.offset));
|
||||
clone(): Queue<E, R> {
|
||||
return new Queue(this.elements.slice(this.offset), { toElementFn: this.toElementFn });
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -279,8 +276,8 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|||
* @returns The `filter` method is returning a new `Queue` object that contains the elements that
|
||||
* satisfy the given predicate function.
|
||||
*/
|
||||
filter(predicate: ElementCallback<E, boolean>, thisArg?: any): Queue<E> {
|
||||
const newDeque = new Queue<E>([]);
|
||||
filter(predicate: ElementCallback<E, R, boolean, Queue<E, R>>, thisArg?: any): Queue<E, R> {
|
||||
const newDeque = new Queue<E, R>([], { toElementFn: this.toElementFn });
|
||||
let index = 0;
|
||||
for (const el of this) {
|
||||
if (predicate.call(thisArg, el, index, this)) {
|
||||
|
@ -296,22 +293,12 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|||
* 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 queue,
|
||||
* returning a new queue with the results.
|
||||
* @param callback - The callback parameter is a function that will be called for each element in the
|
||||
* queue. It takes three arguments: the current element, the index of the current element, and the
|
||||
* queue itself. The callback function should return a new value that will be added to the new queue.
|
||||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
||||
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
||||
* passed as the `this` value to the `callback` function. If `thisArg` is
|
||||
* @returns The `map` function is returning a new `Queue` object with the transformed elements.
|
||||
*/
|
||||
map<T>(callback: ElementCallback<E, T>, thisArg?: any): Queue<T> {
|
||||
const newDeque = new Queue<T>([]);
|
||||
map<EM, RM>(
|
||||
callback: ElementCallback<E, R, EM, Queue<E, R>>,
|
||||
toElementFn?: (rawElement: RM) => EM,
|
||||
thisArg?: any
|
||||
): Queue<EM, RM> {
|
||||
const newDeque = new Queue<EM, RM>([], { toElementFn });
|
||||
let index = 0;
|
||||
for (const el of this) {
|
||||
newDeque.push(callback.call(thisArg, el, index, this));
|
||||
|
@ -332,7 +319,7 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|||
* The function `_getIterator` returns an iterable iterator for the elements in the class.
|
||||
*/
|
||||
protected* _getIterator(): IterableIterator<E> {
|
||||
for (const item of this.elements) {
|
||||
for (const item of this.elements.slice(this.offset)) {
|
||||
yield item;
|
||||
}
|
||||
}
|
||||
|
@ -344,7 +331,7 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|||
* 3. Memory Usage: Since each element requires additional space to store a pointer to the next element, linked lists may use more memory compared to arrays.
|
||||
* 4. Frequent Enqueuing and Dequeuing Operations: If your application involves frequent enqueuing and dequeuing operations and is less concerned with random access, then LinkedListQueue is a good choice.
|
||||
*/
|
||||
export class LinkedListQueue<E = any> extends SinglyLinkedList<E> {
|
||||
export class LinkedListQueue<E = any, R = any> extends SinglyLinkedList<E, R> {
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(n)
|
||||
|
@ -358,7 +345,7 @@ export class LinkedListQueue<E = any> extends SinglyLinkedList<E> {
|
|||
* @returns The `clone()` method is returning a new instance of `LinkedListQueue` with the same
|
||||
* values as the original `LinkedListQueue`.
|
||||
*/
|
||||
override clone(): LinkedListQueue<E> {
|
||||
return new LinkedListQueue<E>(this.values());
|
||||
override clone(): LinkedListQueue<E, R> {
|
||||
return new LinkedListQueue<E, R>(this, { toElementFn: this.toElementFn });
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
||||
* @license MIT License
|
||||
*/
|
||||
import type { ElementCallback } from '../../types';
|
||||
import type { ElementCallback, StackOptions } from '../../types';
|
||||
import { IterableElementBase } from '../base';
|
||||
|
||||
/**
|
||||
|
@ -16,17 +16,17 @@ import { IterableElementBase } from '../base';
|
|||
* 5. Expression Evaluation: Used for the evaluation of arithmetic or logical expressions, especially when dealing with parenthesis matching and operator precedence.
|
||||
* 6. Backtracking Algorithms: In problems where multiple branches need to be explored but only one branch can be explored at a time, stacks can be used to save the state at each branching point.
|
||||
*/
|
||||
export class Stack<E = any> extends IterableElementBase<E> {
|
||||
/**
|
||||
* The constructor initializes an array of elements, which can be provided as an optional parameter.
|
||||
* @param {E[]} [elements] - The `elements` parameter is an optional parameter of type `E[]`, which represents an array
|
||||
* of elements of type `E`. It is used to initialize the `_elements` property of the class. If the `elements` parameter
|
||||
* is provided and is an array, it is assigned to the `_elements
|
||||
*/
|
||||
constructor(elements: Iterable<E> = []) {
|
||||
super();
|
||||
export class Stack<E = any, R = any> extends IterableElementBase<E, R, Stack<E, R>> {
|
||||
constructor(elements: Iterable<E> | Iterable<R> = [], options?: StackOptions<E, R>) {
|
||||
super(options);
|
||||
if (elements) {
|
||||
for (const el of elements) this.push(el);
|
||||
for (const el of elements) {
|
||||
if (this.toElementFn) {
|
||||
this.push(this.toElementFn(el as R));
|
||||
} else {
|
||||
this.push(el as E);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -192,8 +192,8 @@ export class Stack<E = any> extends IterableElementBase<E> {
|
|||
* The `clone()` function returns a new `Stack` object with the same elements as the original stack.
|
||||
* @returns The `clone()` method is returning a new `Stack` object with a copy of the `_elements` array.
|
||||
*/
|
||||
clone(): Stack<E> {
|
||||
return new Stack(this.elements.slice());
|
||||
clone(): Stack<E, R> {
|
||||
return new Stack<E, R>(this, { toElementFn: this.toElementFn });
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -217,8 +217,8 @@ export class Stack<E = any> extends IterableElementBase<E> {
|
|||
* @returns The `filter` method is returning a new `Stack` object that contains the elements that
|
||||
* satisfy the given predicate function.
|
||||
*/
|
||||
filter(predicate: ElementCallback<E, boolean>, thisArg?: any): Stack<E> {
|
||||
const newStack = new Stack<E>();
|
||||
filter(predicate: ElementCallback<E, R, boolean, Stack<E, R>>, thisArg?: any): Stack<E, R> {
|
||||
const newStack = new Stack<E, R>([], { toElementFn: this.toElementFn });
|
||||
let index = 0;
|
||||
for (const el of this) {
|
||||
if (predicate.call(thisArg, el, index, this)) {
|
||||
|
@ -235,20 +235,25 @@ export class Stack<E = any> extends IterableElementBase<E> {
|
|||
*/
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(n)
|
||||
*
|
||||
* The `map` function takes a callback function and applies it to each element in the stack,
|
||||
* returning a new stack with the results.
|
||||
* @param callback - The `callback` parameter is a function that will be called for each element in
|
||||
* the stack. It takes three arguments:
|
||||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
||||
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
||||
* passed as the `this` value to the `callback` function. If `thisArg` is
|
||||
* @returns The `map` method is returning a new `Stack` object.
|
||||
* @param callback - The callback parameter is a function that will be called for each element in the
|
||||
* stack. It takes three arguments: the current element, the index of the element, and the stack
|
||||
* itself. It should return a new value that will be added to the new stack.
|
||||
* @param [toElementFn] - The `toElementFn` parameter is an optional function that can be used to
|
||||
* transform the raw element (`RM`) into a new element (`EM`) before pushing it into the new stack.
|
||||
* @param {any} [thisArg] - The `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
|
||||
* @returns a new Stack object with elements of type EM and raw elements of type RM.
|
||||
*/
|
||||
map<T>(callback: ElementCallback<E, T>, thisArg?: any): Stack<T> {
|
||||
const newStack = new Stack<T>();
|
||||
map<EM, RM>(
|
||||
callback: ElementCallback<E, R, EM, Stack<E, R>>,
|
||||
toElementFn?: (rawElement: RM) => EM,
|
||||
thisArg?: any
|
||||
): Stack<EM, RM> {
|
||||
const newStack = new Stack<EM, RM>([], { toElementFn });
|
||||
let index = 0;
|
||||
for (const el of this) {
|
||||
newStack.push(callback.call(thisArg, el, index, this));
|
||||
|
|
|
@ -93,21 +93,27 @@ export class TrieNode {
|
|||
* 10. IP Routing: Used in certain types of IP routing algorithms.
|
||||
* 11. Text Word Frequency Count: Counting and storing the frequency of words in a large amount of text data.
|
||||
*/
|
||||
export class Trie extends IterableElementBase<string, Trie> {
|
||||
export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
|
||||
/**
|
||||
* The constructor function for the Trie class.
|
||||
* @param words: Iterable string Initialize the trie with a set of words
|
||||
* @param options?: TrieOptions Allow the user to pass in options for the trie
|
||||
* @return This
|
||||
*/
|
||||
constructor(words: Iterable<string> = [], options?: TrieOptions) {
|
||||
super();
|
||||
constructor(words: Iterable<string> | Iterable<R> = [], options?: TrieOptions<R>) {
|
||||
super(options);
|
||||
if (options) {
|
||||
const { caseSensitive } = options;
|
||||
if (caseSensitive !== undefined) this._caseSensitive = caseSensitive;
|
||||
}
|
||||
if (words) {
|
||||
for (const word of words) this.add(word);
|
||||
for (const word of words) {
|
||||
if (this.toElementFn) {
|
||||
this.add(this.toElementFn(word as R));
|
||||
} else {
|
||||
this.add(word as string);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -470,8 +476,8 @@ export class Trie extends IterableElementBase<string, Trie> {
|
|||
* sensitivity as the original Trie.
|
||||
* @returns A new instance of the Trie class is being returned.
|
||||
*/
|
||||
clone(): Trie {
|
||||
return new Trie(this.values(), { caseSensitive: this.caseSensitive });
|
||||
clone(): Trie<R> {
|
||||
return new Trie<R>(this, { caseSensitive: this.caseSensitive, toElementFn: this.toElementFn });
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -493,8 +499,8 @@ export class Trie extends IterableElementBase<string, Trie> {
|
|||
* specific object as the context for the `predicate` function. If `thisArg` is provided, it will be
|
||||
* @returns The `filter` method is returning an array of strings (`string[]`).
|
||||
*/
|
||||
filter(predicate: ElementCallback<string, boolean>, thisArg?: any): Trie {
|
||||
const results: Trie = new Trie();
|
||||
filter(predicate: ElementCallback<string, R, boolean, Trie<R>>, thisArg?: any): Trie<R> {
|
||||
const results = new Trie<R>([], { toElementFn: this.toElementFn, caseSensitive: this.caseSensitive });
|
||||
let index = 0;
|
||||
for (const word of this) {
|
||||
if (predicate.call(thisArg, word, index, this)) {
|
||||
|
@ -514,17 +520,26 @@ export class Trie extends IterableElementBase<string, Trie> {
|
|||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(n)
|
||||
*
|
||||
* The `map` function creates a new Trie by applying a callback function to each element in the Trie.
|
||||
* The `map` function creates a new Trie by applying a callback function to each element in the
|
||||
* current Trie.
|
||||
* @param callback - The callback parameter is a function that will be called for each element in the
|
||||
* Trie. It takes three arguments: the current element in the Trie, the index of the current element,
|
||||
* and the Trie itself. The callback function should return a new value for the element.
|
||||
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
||||
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
||||
* passed as the `this` value to the `callback` function. If `thisArg` is
|
||||
* @returns The `map` function is returning a new Trie object.
|
||||
* Trie. It takes four arguments:
|
||||
* @param [toElementFn] - The `toElementFn` parameter is an optional function that can be used to
|
||||
* convert the raw element (`RM`) into a string representation. This can be useful if the raw element
|
||||
* is not already a string or if you want to customize how the element is converted into a string. If
|
||||
* this parameter is
|
||||
* @param {any} [thisArg] - The `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
|
||||
* @returns a new Trie object.
|
||||
*/
|
||||
map(callback: ElementCallback<string, string>, thisArg?: any): Trie {
|
||||
const newTrie = new Trie();
|
||||
map<RM>(
|
||||
callback: ElementCallback<string, R, string, Trie<R>>,
|
||||
toElementFn?: (rawElement: RM) => string,
|
||||
thisArg?: any
|
||||
): Trie<RM> {
|
||||
const newTrie = new Trie<RM>([], { toElementFn, caseSensitive: this.caseSensitive });
|
||||
let index = 0;
|
||||
for (const word of this) {
|
||||
newTrie.add(callback.call(thisArg, word, index, this));
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import { IterableElementBase, IterableEntryBase } from '../../../data-structures';
|
||||
|
||||
export type EntryCallback<K, V, R> = (value: V, key: K, index: number, container: IterableEntryBase<K, V>) => R;
|
||||
export type ElementCallback<V, R> = (element: V, index: number, container: IterableElementBase<V>) => R;
|
||||
export type ElementCallback<E, R, RT, C> = (element: E, index: number, container: IterableElementBase<E, R, C>) => RT;
|
||||
export type ReduceEntryCallback<K, V, R> = (
|
||||
accumulator: R,
|
||||
value: V,
|
||||
|
@ -9,9 +9,17 @@ export type ReduceEntryCallback<K, V, R> = (
|
|||
index: number,
|
||||
container: IterableEntryBase<K, V>
|
||||
) => R;
|
||||
export type ReduceElementCallback<V, R> = (
|
||||
accumulator: R,
|
||||
element: V,
|
||||
export type ReduceElementCallback<E, R, RT, C> = (
|
||||
accumulator: RT,
|
||||
element: E,
|
||||
index: number,
|
||||
container: IterableElementBase<V>
|
||||
) => R;
|
||||
container: IterableElementBase<E, R, C>
|
||||
) => RT;
|
||||
|
||||
// export type IterableEntryBaseOptions<K, V, R> = {
|
||||
// toEntryFn?: (rawElement: R) => BTNEntry<K, V>;
|
||||
// };
|
||||
|
||||
export type IterableElementBaseOptions<E, R> = {
|
||||
toElementFn?: (rawElement: R) => E;
|
||||
};
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import { Comparator } from '../../common';
|
||||
import { IterableElementBaseOptions } from '../base';
|
||||
|
||||
export type HeapOptions<E, R> = {
|
||||
export type HeapOptions<E, R> = IterableElementBaseOptions<E, R> & {
|
||||
comparator?: Comparator<E>;
|
||||
toElementFn?: (rawElement: R) => E;
|
||||
};
|
||||
|
|
|
@ -1 +1,3 @@
|
|||
export {};
|
||||
import { IterableElementBaseOptions } from '../base';
|
||||
|
||||
export type DoublyLinkedListOptions<E, R> = IterableElementBaseOptions<E, R> & {};
|
||||
|
|
|
@ -1 +1,3 @@
|
|||
export {};
|
||||
import { IterableElementBaseOptions } from '../base';
|
||||
|
||||
export type SinglyLinkedListOptions<E, R> = IterableElementBaseOptions<E, R> & {};
|
||||
|
|
|
@ -1 +1,3 @@
|
|||
export type DequeOptions = { bucketSize?: number };
|
||||
import { IterableElementBaseOptions } from '../base';
|
||||
|
||||
export type DequeOptions<E, R> = { bucketSize?: number } & IterableElementBaseOptions<E, R>;
|
||||
|
|
|
@ -1 +1,3 @@
|
|||
export {};
|
||||
import { IterableElementBaseOptions } from '../base';
|
||||
|
||||
export type QueueOptions<E, R> = IterableElementBaseOptions<E, R> & {};
|
||||
|
|
|
@ -1 +1,3 @@
|
|||
export {};
|
||||
import { IterableElementBaseOptions } from '../base';
|
||||
|
||||
export type StackOptions<E, R> = IterableElementBaseOptions<E, R> & {};
|
||||
|
|
|
@ -1 +1,3 @@
|
|||
export type TrieOptions = { caseSensitive?: boolean };
|
||||
import { IterableElementBaseOptions } from '../base';
|
||||
|
||||
export type TrieOptions<R> = { caseSensitive?: boolean } & IterableElementBaseOptions<string, R>;
|
||||
|
|
|
@ -88,6 +88,44 @@ describe('Heap Operation Test', () => {
|
|||
}
|
||||
});
|
||||
|
||||
it('should object heap map & filter', function () {
|
||||
const minHeap = new MinHeap<{ a: string; key: number }>(
|
||||
[
|
||||
{ key: 1, a: 'a1' },
|
||||
{ key: 6, a: 'a6' },
|
||||
{ key: 5, a: 'a5' },
|
||||
{ key: 3, a: 'a3' },
|
||||
{ key: 2, a: 'a2' },
|
||||
{ key: 4, a: 'a4' },
|
||||
{ key: 0, a: 'a0' }
|
||||
],
|
||||
{ comparator: (a, b) => a.key - b.key }
|
||||
);
|
||||
|
||||
const mappedMinHeap = minHeap.map(
|
||||
item => item.key,
|
||||
(a, b) => a - b
|
||||
);
|
||||
expect(mappedMinHeap.peek()).toBe(0);
|
||||
expect(mappedMinHeap.sort()).toEqual([0, 1, 2, 3, 4, 5, 6]);
|
||||
|
||||
const mappedToElementFnMinHeap = minHeap.map<string, { id: string }>(
|
||||
item => item.key.toString(),
|
||||
(a, b) => Number(a) - Number(b),
|
||||
rawElement => rawElement.id
|
||||
);
|
||||
expect(mappedToElementFnMinHeap.peek()).toBe('0');
|
||||
expect(mappedToElementFnMinHeap.sort()).toEqual(['0', '1', '2', '3', '4', '5', '6']);
|
||||
|
||||
const filteredHeap = minHeap.filter(item => item.key > 3);
|
||||
expect(filteredHeap.peek()).toEqual({ a: 'a4', key: 4 });
|
||||
expect(filteredHeap.sort()).toEqual([
|
||||
{ a: 'a4', key: 4 },
|
||||
{ a: 'a5', key: 5 },
|
||||
{ a: 'a6', key: 6 }
|
||||
]);
|
||||
});
|
||||
|
||||
it('should object heap', () => {
|
||||
const heap = new Heap<{ rawItem: { id: number } }>(
|
||||
[
|
||||
|
|
|
@ -5,7 +5,7 @@ describe('MaxHeap', () => {
|
|||
let maxHeap: MaxHeap<number>;
|
||||
|
||||
beforeEach(() => {
|
||||
maxHeap = new MaxHeap([], { comparator: numberComparator });
|
||||
maxHeap = new MaxHeap<number>([], { comparator: numberComparator });
|
||||
});
|
||||
|
||||
it('add and poll elements in descending order', () => {
|
||||
|
@ -49,4 +49,47 @@ describe('MaxHeap', () => {
|
|||
maxHeap.poll();
|
||||
expect(maxHeap.isEmpty()).toBe(true);
|
||||
});
|
||||
|
||||
it('should object heap map & filter', function () {
|
||||
const maxHeap = new MaxHeap<{ a: string; key: number }>(
|
||||
[
|
||||
{ key: 1, a: 'a1' },
|
||||
{ key: 6, a: 'a6' },
|
||||
{ key: 5, a: 'a5' },
|
||||
{ key: 3, a: 'a3' },
|
||||
{ key: 2, a: 'a2' },
|
||||
{ key: 4, a: 'a4' },
|
||||
{ key: 0, a: 'a0' }
|
||||
],
|
||||
{ comparator: (a, b) => b.key - a.key }
|
||||
);
|
||||
|
||||
const mappedMaxHeap = maxHeap.map(
|
||||
item => item.key,
|
||||
(a, b) => b - a
|
||||
);
|
||||
expect(mappedMaxHeap.peek()).toBe(6);
|
||||
expect(mappedMaxHeap.sort()).toEqual([6, 5, 4, 3, 2, 1, 0]);
|
||||
|
||||
const mappedToElementFnMaxHeap = maxHeap.map<
|
||||
string,
|
||||
{
|
||||
id: string;
|
||||
}
|
||||
>(
|
||||
item => item.key.toString(),
|
||||
(a, b) => Number(b) - Number(a),
|
||||
rawElement => rawElement.id
|
||||
);
|
||||
expect(mappedToElementFnMaxHeap.peek()).toBe('6');
|
||||
expect(mappedToElementFnMaxHeap.sort()).toEqual(['6', '5', '4', '3', '2', '1', '0']);
|
||||
|
||||
const filteredHeap = maxHeap.filter(item => item.key > 3);
|
||||
expect(filteredHeap.peek()).toEqual({ a: 'a6', key: 6 });
|
||||
expect(filteredHeap.sort()).toEqual([
|
||||
{ a: 'a6', key: 6 },
|
||||
{ a: 'a5', key: 5 },
|
||||
{ a: 'a4', key: 4 }
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -5,7 +5,7 @@ describe('MinHeap', () => {
|
|||
let minHeap: MinHeap<number>;
|
||||
|
||||
beforeEach(() => {
|
||||
minHeap = new MinHeap([], { comparator: numberComparator });
|
||||
minHeap = new MinHeap<number>([], { comparator: numberComparator });
|
||||
});
|
||||
|
||||
it('add and poll elements in ascending order', () => {
|
||||
|
@ -40,6 +40,23 @@ describe('MinHeap', () => {
|
|||
expect(sortedArray).toEqual([1, 2, 3, 4]);
|
||||
});
|
||||
|
||||
it('should clone', function () {
|
||||
const minHeap = new MinHeap<string>();
|
||||
minHeap.add('1');
|
||||
minHeap.add('6');
|
||||
minHeap.add('2');
|
||||
minHeap.add('0');
|
||||
minHeap.add('5');
|
||||
minHeap.add('9');
|
||||
minHeap.delete('2');
|
||||
expect([...minHeap]).toEqual(['0', '1', '9', '6', '5']);
|
||||
const cloned = minHeap.clone();
|
||||
expect([...cloned]).toEqual(['0', '1', '9', '6', '5']);
|
||||
minHeap.delete('5');
|
||||
expect([...minHeap]).toEqual(['0', '1', '9', '6']);
|
||||
expect([...cloned]).toEqual(['0', '1', '9', '6', '5']);
|
||||
});
|
||||
|
||||
it('check if the heap is empty', () => {
|
||||
expect(minHeap.isEmpty()).toBe(true);
|
||||
|
||||
|
|
|
@ -50,6 +50,36 @@ describe('Deque - Basic Operations', () => {
|
|||
expect(deque.at(0)).toBe(3);
|
||||
});
|
||||
|
||||
test('should at after shifting', () => {
|
||||
deque.clear();
|
||||
for (let i = 0; i < 100; i++) {
|
||||
deque.push(i);
|
||||
}
|
||||
|
||||
for (let i = 0; i < 10; i++) {
|
||||
expect(deque.shift()).toBe(i);
|
||||
}
|
||||
|
||||
for (let i = 0; i < 90; i++) {
|
||||
expect(deque.at(i)).toBe(i + 10);
|
||||
}
|
||||
});
|
||||
|
||||
test('should at after popping', () => {
|
||||
deque.clear();
|
||||
for (let i = 0; i < 100; i++) {
|
||||
deque.push(i);
|
||||
}
|
||||
|
||||
for (let i = 0; i < 10; i++) {
|
||||
expect(deque.pop()).toBe(99 - i);
|
||||
}
|
||||
|
||||
for (let i = 0; i < 90; i++) {
|
||||
expect(deque.at(i)).toBe(i);
|
||||
}
|
||||
});
|
||||
|
||||
it('should clone', function () {
|
||||
const deque = new Deque<string>();
|
||||
deque.push('1');
|
||||
|
|
|
@ -90,6 +90,62 @@ describe('Queue', () => {
|
|||
expect(queue.isEmpty()).toBeTruthy();
|
||||
});
|
||||
|
||||
test('should at after shifting', () => {
|
||||
for (let i = 0; i < 100; i++) {
|
||||
queue.push(i);
|
||||
}
|
||||
|
||||
for (let i = 0; i < 10; i++) {
|
||||
expect(queue.shift()).toBe(i);
|
||||
}
|
||||
|
||||
for (let i = 0; i < 90; i++) {
|
||||
expect(queue.at(i)).toBe(i + 10);
|
||||
}
|
||||
});
|
||||
|
||||
test('should toElementFn', () => {
|
||||
const queue = new Queue<string, { id: string }>([{ id: '1' }, { id: '5' }, { id: '3' }, { id: '4' }, { id: '2' }], {
|
||||
toElementFn: rawElement => rawElement.id
|
||||
});
|
||||
|
||||
expect(queue.size).toBe(5);
|
||||
queue.shift();
|
||||
expect(queue.size).toBe(4);
|
||||
expect(queue.at(1)).toBe('3');
|
||||
});
|
||||
|
||||
it('should object queue map & filter', function () {
|
||||
const queue = new Queue<{ a: string; key: number }>([
|
||||
{ key: 1, a: 'a1' },
|
||||
{ key: 6, a: 'a6' },
|
||||
{ key: 5, a: 'a5' },
|
||||
{ key: 3, a: 'a3' },
|
||||
{ key: 2, a: 'a2' },
|
||||
{ key: 4, a: 'a4' },
|
||||
{ key: 0, a: 'a0' }
|
||||
]);
|
||||
|
||||
const mappedQueue = queue.map(item => item.key);
|
||||
expect(mappedQueue.at(0)).toBe(1);
|
||||
expect([...mappedQueue]).toEqual([1, 6, 5, 3, 2, 4, 0]);
|
||||
|
||||
const mappedToElementFnQueue = queue.map<string, { id: string }>(
|
||||
item => item.key.toString(),
|
||||
rawElement => rawElement.id
|
||||
);
|
||||
expect(mappedToElementFnQueue.at(0)).toBe('1');
|
||||
expect([...mappedToElementFnQueue]).toEqual(['1', '6', '5', '3', '2', '4', '0']);
|
||||
|
||||
const filteredQueue = queue.filter(item => item.key > 3);
|
||||
expect(filteredQueue.at(0)).toEqual({ a: 'a6', key: 6 });
|
||||
expect([...filteredQueue]).toEqual([
|
||||
{ a: 'a6', key: 6 },
|
||||
{ a: 'a5', key: 5 },
|
||||
{ a: 'a4', key: 4 }
|
||||
]);
|
||||
});
|
||||
|
||||
it('should clone', function () {
|
||||
const queue = new Queue<string>();
|
||||
queue.push('1');
|
||||
|
|
Loading…
Reference in a new issue