Refactor: Added the _ensureCallback method to eliminate redundant code.

This commit is contained in:
Revone 2024-01-19 18:02:56 +08:00
parent 3fa7155c97
commit ae11bca83c
7 changed files with 34 additions and 26 deletions

View file

@ -736,45 +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.31</td><td>158.42</td><td>1.69e-4</td></tr><tr><td>100,000 add & poll</td><td>32.21</td><td>31.05</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>6.42</td><td>155.78</td><td>1.88e-4</td></tr><tr><td>100,000 add & poll</td><td>31.60</td><td>31.65</td><td>9.13e-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>59.70</td><td>16.75</td><td>7.70e-4</td></tr><tr><td>100,000 add randomly</td><td>72.55</td><td>13.78</td><td>0.00</td></tr><tr><td>100,000 get</td><td>104.48</td><td>9.57</td><td>0.00</td></tr><tr><td>100,000 iterator</td><td>27.70</td><td>36.10</td><td>0.01</td></tr><tr><td>100,000 add & delete orderly</td><td>126.07</td><td>7.93</td><td>0.00</td></tr><tr><td>100,000 add & delete randomly</td><td>212.96</td><td>4.70</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>59.28</td><td>16.87</td><td>7.81e-4</td></tr><tr><td>100,000 add randomly</td><td>71.61</td><td>13.96</td><td>9.02e-4</td></tr><tr><td>100,000 get</td><td>104.13</td><td>9.60</td><td>0.00</td></tr><tr><td>100,000 iterator</td><td>27.03</td><td>37.00</td><td>0.00</td></tr><tr><td>100,000 add & delete orderly</td><td>126.42</td><td>7.91</td><td>0.00</td></tr><tr><td>100,000 add & delete randomly</td><td>214.36</td><td>4.66</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>44.49</td><td>22.48</td><td>0.01</td></tr><tr><td>100,000 push & shift</td><td>4.82</td><td>207.40</td><td>5.25e-4</td></tr><tr><td>Native JS Array 100,000 push & shift</td><td>2201.76</td><td>0.45</td><td>0.10</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>44.98</td><td>22.23</td><td>0.01</td></tr><tr><td>100,000 push & shift</td><td>4.89</td><td>204.39</td><td>6.15e-4</td></tr><tr><td>Native JS Array 100,000 push & shift</td><td>2251.13</td><td>0.44</td><td>0.26</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>12.34</td><td>81.06</td><td>1.27e-4</td></tr><tr><td>1,000,000 push & pop</td><td>21.35</td><td>46.84</td><td>1.44e-4</td></tr><tr><td>1,000,000 push & shift</td><td>21.54</td><td>46.42</td><td>0.00</td></tr><tr><td>100,000 push & shift</td><td>2.09</td><td>478.80</td><td>1.60e-4</td></tr><tr><td>Native JS Array 100,000 push & shift</td><td>2233.46</td><td>0.45</td><td>0.12</td></tr><tr><td>100,000 unshift & shift</td><td>1.99</td><td>502.42</td><td>1.55e-4</td></tr><tr><td>Native JS Array 100,000 unshift & shift</td><td>4153.08</td><td>0.24</td><td>0.34</td></tr></table></div>
</div><div id="json-to-html"><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;"><tbody><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>129.84</td><td>7.70</td><td>0.04</td></tr><tr><td>Native JS Map 1,000,000 set</td><td>238.66</td><td>4.19</td><td>0.03</td></tr><tr><td>Native JS Set 1,000,000 add</td><td>194.68</td><td>5.14</td><td>0.01</td></tr><tr><td>1,000,000 set &amp; get</td><td>134.95</td><td>7.41</td><td>0.03</td></tr><tr><td>Native JS Map 1,000,000 set &amp; get</td><td>320.94</td><td>3.12</td><td>0.04</td></tr><tr><td>Native JS Set 1,000,000 add &amp; has</td><td>261.56</td><td>3.82</td><td>0.07</td></tr><tr><td>1,000,000 ObjKey set &amp; get</td><td>363.11</td><td>2.75</td><td>0.06</td></tr><tr><td>Native JS Map 1,000,000 ObjKey set &amp; get</td><td>344.03</td><td>2.91</td><td>0.05</td></tr><tr><td>Native JS Set 1,000,000 ObjKey add &amp; has</td><td>351.35</td><td>2.85</td><td>0.10</td></tr></tbody></table></div>
</div>
</div><div class="json-to-html-collapse clearfix 0">
<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>22.21</td><td>45.02</td><td>0.00</td></tr><tr><td>1,000,000 push & pop</td><td>30.97</td><td>32.29</td><td>0.00</td></tr><tr><td>1,000,000 push & shift</td><td>30.81</td><td>32.46</td><td>0.00</td></tr><tr><td>100,000 push & shift</td><td>3.24</td><td>308.25</td><td>2.08e-4</td></tr><tr><td>Native JS Array 100,000 push & shift</td><td>2279.95</td><td>0.44</td><td>0.21</td></tr><tr><td>100,000 unshift & shift</td><td>2.93</td><td>341.78</td><td>3.21e-4</td></tr><tr><td>Native JS Array 100,000 unshift & shift</td><td>4076.04</td><td>0.25</td><td>0.42</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>118.11</td><td>8.47</td><td>0.02</td></tr><tr><td>Native JS Map 1,000,000 set</td><td>206.18</td><td>4.85</td><td>0.01</td></tr><tr><td>Native JS Set 1,000,000 add</td><td>167.50</td><td>5.97</td><td>0.01</td></tr><tr><td>1,000,000 set & get</td><td>116.15</td><td>8.61</td><td>0.01</td></tr><tr><td>Native JS Map 1,000,000 set & get</td><td>269.11</td><td>3.72</td><td>0.02</td></tr><tr><td>Native JS Set 1,000,000 add & has</td><td>168.02</td><td>5.95</td><td>0.01</td></tr><tr><td>1,000,000 ObjKey set & get</td><td>335.36</td><td>2.98</td><td>0.04</td></tr><tr><td>Native JS Map 1,000,000 ObjKey set & get</td><td>327.78</td><td>3.05</td><td>0.06</td></tr><tr><td>Native JS Set 1,000,000 ObjKey add & has</td><td>281.64</td><td>3.55</td><td>0.04</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>44.75</td><td>22.35</td><td>6.31e-4</td></tr><tr><td>100,000 getWords</td><td>83.96</td><td>11.91</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>43.64</td><td>22.91</td><td>8.01e-4</td></tr><tr><td>100,000 getWords</td><td>82.95</td><td>12.06</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'>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>262.23</td><td>3.81</td><td>0.00</td></tr><tr><td>100,000 add randomly</td><td>323.18</td><td>3.09</td><td>0.00</td></tr><tr><td>100,000 get</td><td>130.85</td><td>7.64</td><td>0.00</td></tr><tr><td>100,000 iterator</td><td>31.79</td><td>31.46</td><td>0.01</td></tr><tr><td>100,000 add & delete orderly</td><td>423.80</td><td>2.36</td><td>0.00</td></tr><tr><td>100,000 add & delete randomly</td><td>585.79</td><td>1.71</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>263.01</td><td>3.80</td><td>0.02</td></tr><tr><td>100,000 add randomly</td><td>353.89</td><td>2.83</td><td>0.01</td></tr><tr><td>100,000 get</td><td>131.65</td><td>7.60</td><td>0.00</td></tr><tr><td>100,000 iterator</td><td>31.62</td><td>31.63</td><td>0.01</td></tr><tr><td>100,000 add & delete orderly</td><td>468.81</td><td>2.13</td><td>0.00</td></tr><tr><td>100,000 add & delete randomly</td><td>628.30</td><td>1.59</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>5.84</td><td>171.13</td><td>8.92e-5</td></tr><tr><td>10,000 RBTree get randomly</td><td>9.48</td><td>105.49</td><td>5.40e-4</td></tr><tr><td>10,000 RBTree add & delete randomly</td><td>18.19</td><td>54.98</td><td>1.42e-4</td></tr><tr><td>10,000 AVLTree add randomly</td><td>26.45</td><td>37.80</td><td>2.66e-4</td></tr><tr><td>10,000 AVLTree get randomly</td><td>10.63</td><td>94.09</td><td>1.36e-4</td></tr><tr><td>10,000 AVLTree add & delete randomly</td><td>49.07</td><td>20.38</td><td>3.49e-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>5.79</td><td>172.73</td><td>7.35e-5</td></tr><tr><td>10,000 RBTree get randomly</td><td>9.58</td><td>104.37</td><td>9.96e-5</td></tr><tr><td>10,000 RBTree add & delete randomly</td><td>17.82</td><td>56.12</td><td>1.90e-4</td></tr><tr><td>10,000 AVLTree add randomly</td><td>27.85</td><td>35.91</td><td>3.16e-4</td></tr><tr><td>10,000 AVLTree get randomly</td><td>10.73</td><td>93.19</td><td>6.38e-5</td></tr><tr><td>10,000 AVLTree add & delete randomly</td><td>48.98</td><td>20.42</td><td>3.91e-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>1.01e+4</td><td>1.05e-6</td></tr><tr><td>1,000 addEdge</td><td>6.18</td><td>161.88</td><td>7.72e-4</td></tr><tr><td>1,000 getVertex</td><td>0.05</td><td>2.17e+4</td><td>3.58e-7</td></tr><tr><td>1,000 getEdge</td><td>23.03</td><td>43.43</td><td>0.00</td></tr><tr><td>tarjan</td><td>203.18</td><td>4.92</td><td>0.02</td></tr><tr><td>topologicalSort</td><td>174.78</td><td>5.72</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>9624.60</td><td>7.61e-7</td></tr><tr><td>1,000 addEdge</td><td>6.02</td><td>166.18</td><td>1.45e-4</td></tr><tr><td>1,000 getVertex</td><td>0.05</td><td>2.17e+4</td><td>3.52e-7</td></tr><tr><td>1,000 getEdge</td><td>23.88</td><td>41.88</td><td>0.00</td></tr><tr><td>tarjan</td><td>215.08</td><td>4.65</td><td>0.01</td></tr><tr><td>topologicalSort</td><td>187.83</td><td>5.32</td><td>0.00</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>doubly-linked-list</span></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000,000 push</td><td>227.39</td><td>4.40</td><td>0.03</td></tr><tr><td>1,000,000 unshift</td><td>211.70</td><td>4.72</td><td>0.02</td></tr><tr><td>1,000,000 unshift & shift</td><td>171.84</td><td>5.82</td><td>0.02</td></tr><tr><td>1,000,000 addBefore</td><td>318.69</td><td>3.14</td><td>0.06</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>221.69</td><td>4.51</td><td>0.03</td></tr><tr><td>1,000,000 unshift</td><td>227.33</td><td>4.40</td><td>0.07</td></tr><tr><td>1,000,000 unshift & shift</td><td>170.08</td><td>5.88</td><td>0.02</td></tr><tr><td>1,000,000 addBefore</td><td>321.29</td><td>3.11</td><td>0.07</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>201.65</td><td>4.96</td><td>0.04</td></tr><tr><td>10,000 push & pop</td><td>216.76</td><td>4.61</td><td>0.01</td></tr><tr><td>10,000 addBefore</td><td>248.82</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>213.14</td><td>4.69</td><td>0.04</td></tr><tr><td>10,000 push & pop</td><td>217.84</td><td>4.59</td><td>0.01</td></tr><tr><td>10,000 addBefore</td><td>250.81</td><td>3.99</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'>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.35</td><td>36.56</td><td>0.00</td></tr><tr><td>100,000 add & poll</td><td>78.76</td><td>12.70</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>26.91</td><td>37.16</td><td>1.96e-4</td></tr><tr><td>100,000 add & poll</td><td>75.09</td><td>13.32</td><td>5.04e-4</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>38.79</td><td>25.78</td><td>0.00</td></tr><tr><td>1,000,000 push & pop</td><td>47.01</td><td>21.27</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>39.92</td><td>25.05</td><td>0.01</td></tr><tr><td>1,000,000 push & pop</td><td>48.08</td><td>20.80</td><td>0.01</td></tr></table></div>
</div>
[//]: # (No deletion!!! End of Replace Section)

View file

@ -245,6 +245,7 @@ export class AVLTreeMultiMap<
): BinaryTreeDeleteResult<NODE>[] {
const deletedResult: BinaryTreeDeleteResult<NODE>[] = [];
if (!this.root) return deletedResult;
callback = this._ensureCallback(identifier, callback);
const curr: NODE | undefined = this.getNode(identifier, callback) ?? undefined;
if (!curr) return deletedResult;

View file

@ -172,7 +172,6 @@ export class AVLTree<
identifier: ReturnType<C>,
callback: C = this._DEFAULT_CALLBACK as C
): BinaryTreeDeleteResult<NODE>[] {
if ((identifier as any) instanceof AVLTreeNode) callback = (node => node) as C;
const deletedResults = super.delete(identifier, callback);
for (const { needBalanced } of deletedResults) {
if (needBalanced) {

View file

@ -508,8 +508,7 @@ export class BinaryTree<
): BinaryTreeDeleteResult<NODE>[] {
const deletedResult: BinaryTreeDeleteResult<NODE>[] = [];
if (!this.root) return deletedResult;
if ((!callback || callback === this._DEFAULT_CALLBACK) && (identifier as any) instanceof BinaryTreeNode)
callback = (node => node) as C;
callback = this._ensureCallback(identifier, callback);
const curr = this.getNode(identifier, callback);
if (!curr) return deletedResult;
@ -612,10 +611,9 @@ export class BinaryTree<
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
iterationType: IterationType = this.iterationType
): NODE[] {
if ((!callback || callback === this._DEFAULT_CALLBACK) && (identifier as any) instanceof BinaryTreeNode)
callback = (node => node) as C;
beginRoot = this.ensureNode(beginRoot);
if (!beginRoot) return [];
callback = this._ensureCallback(identifier, callback);
const ans: NODE[] = [];
@ -838,8 +836,7 @@ export class BinaryTree<
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
iterationType: IterationType = this.iterationType
): boolean {
if ((!callback || callback === this._DEFAULT_CALLBACK) && (identifier as any) instanceof BinaryTreeNode)
callback = (node => node) as C;
callback = this._ensureCallback(identifier, callback);
return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
}
@ -2051,4 +2048,15 @@ export class BinaryTree<
}
this._root = v;
}
protected _ensureCallback<C extends BTNCallback<NODE>>(
identifier: ReturnType<C> | null | undefined,
callback: C = this._DEFAULT_CALLBACK as C
): C {
if ((!callback || callback === this._DEFAULT_CALLBACK) && this.isNode(identifier)) {
callback = (node => node) as C;
}
return callback;
}
}

View file

@ -448,6 +448,7 @@ export class BST<
): NODE[] {
beginRoot = this.ensureNode(beginRoot);
if (!beginRoot) return [];
callback = this._ensureCallback(identifier, callback);
const ans: NODE[] = [];
if (iterationType === 'RECURSIVE') {

View file

@ -252,7 +252,7 @@ export class RedBlackTree<
): BinaryTreeDeleteResult<NODE>[] {
if (identifier === null) return [];
const results: BinaryTreeDeleteResult<NODE>[] = [];
callback = this._ensureCallback(identifier, callback);
const nodeToDelete = this.isRealNode(identifier) ? identifier : this.getNode(identifier, callback);
if (!nodeToDelete) {

View file

@ -257,6 +257,7 @@ export class TreeMultiMap<
): BinaryTreeDeleteResult<NODE>[] {
if (identifier === null) return [];
const results: BinaryTreeDeleteResult<NODE>[] = [];
callback = this._ensureCallback(identifier, callback);
const nodeToDelete = this.isRealNode(identifier) ? identifier : this.getNode(identifier, callback);