mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-26 12:16:27 +08:00
deploy
This commit is contained in:
parent
1b9759b17f
commit
34d765dbec
16 changed files with 559 additions and 443 deletions
|
@ -5074,124 +5074,127 @@
|
||||||
<a id="__codelineno-39-6" name="__codelineno-39-6" href="#__codelineno-39-6"></a>
|
<a id="__codelineno-39-6" name="__codelineno-39-6" href="#__codelineno-39-6"></a>
|
||||||
<a id="__codelineno-39-7" name="__codelineno-39-7" href="#__codelineno-39-7"></a><span class="cm">/* 基于数组实现的哈希表 */</span>
|
<a id="__codelineno-39-7" name="__codelineno-39-7" href="#__codelineno-39-7"></a><span class="cm">/* 基于数组实现的哈希表 */</span>
|
||||||
<a id="__codelineno-39-8" name="__codelineno-39-8" href="#__codelineno-39-8"></a><span class="k">typedef</span><span class="w"> </span><span class="k">struct</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-39-8" name="__codelineno-39-8" href="#__codelineno-39-8"></a><span class="k">typedef</span><span class="w"> </span><span class="k">struct</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-39-9" name="__codelineno-39-9" href="#__codelineno-39-9"></a><span class="w"> </span><span class="n">Pair</span><span class="w"> </span><span class="o">*</span><span class="n">buckets</span><span class="p">[</span><span class="n">HASHTABLE_CAPACITY</span><span class="p">];</span>
|
<a id="__codelineno-39-9" name="__codelineno-39-9" href="#__codelineno-39-9"></a><span class="w"> </span><span class="n">Pair</span><span class="w"> </span><span class="o">*</span><span class="n">buckets</span><span class="p">[</span><span class="n">MAX_SIZE</span><span class="p">];</span>
|
||||||
<a id="__codelineno-39-10" name="__codelineno-39-10" href="#__codelineno-39-10"></a><span class="p">}</span><span class="w"> </span><span class="n">ArrayHashMap</span><span class="p">;</span>
|
<a id="__codelineno-39-10" name="__codelineno-39-10" href="#__codelineno-39-10"></a><span class="p">}</span><span class="w"> </span><span class="n">ArrayHashMap</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-11" name="__codelineno-39-11" href="#__codelineno-39-11"></a>
|
<a id="__codelineno-39-11" name="__codelineno-39-11" href="#__codelineno-39-11"></a>
|
||||||
<a id="__codelineno-39-12" name="__codelineno-39-12" href="#__codelineno-39-12"></a><span class="cm">/* 构造函数 */</span>
|
<a id="__codelineno-39-12" name="__codelineno-39-12" href="#__codelineno-39-12"></a><span class="cm">/* 构造函数 */</span>
|
||||||
<a id="__codelineno-39-13" name="__codelineno-39-13" href="#__codelineno-39-13"></a><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="nf">newArrayHashMap</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-39-13" name="__codelineno-39-13" href="#__codelineno-39-13"></a><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="nf">newArrayHashMap</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-39-14" name="__codelineno-39-14" href="#__codelineno-39-14"></a><span class="w"> </span><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">hmap</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="n">ArrayHashMap</span><span class="p">));</span>
|
<a id="__codelineno-39-14" name="__codelineno-39-14" href="#__codelineno-39-14"></a><span class="w"> </span><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">hmap</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="n">ArrayHashMap</span><span class="p">));</span>
|
||||||
<a id="__codelineno-39-15" name="__codelineno-39-15" href="#__codelineno-39-15"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">hmap</span><span class="p">;</span>
|
<a id="__codelineno-39-15" name="__codelineno-39-15" href="#__codelineno-39-15"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">MAX_SIZE</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-39-16" name="__codelineno-39-16" href="#__codelineno-39-16"></a><span class="p">}</span>
|
<a id="__codelineno-39-16" name="__codelineno-39-16" href="#__codelineno-39-16"></a><span class="w"> </span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-17" name="__codelineno-39-17" href="#__codelineno-39-17"></a>
|
<a id="__codelineno-39-17" name="__codelineno-39-17" href="#__codelineno-39-17"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-39-18" name="__codelineno-39-18" href="#__codelineno-39-18"></a><span class="cm">/* 析构函数 */</span>
|
<a id="__codelineno-39-18" name="__codelineno-39-18" href="#__codelineno-39-18"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">hmap</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-19" name="__codelineno-39-19" href="#__codelineno-39-19"></a><span class="kt">void</span><span class="w"> </span><span class="nf">delArrayHashMap</span><span class="p">(</span><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">hmap</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-39-19" name="__codelineno-39-19" href="#__codelineno-39-19"></a><span class="p">}</span>
|
||||||
<a id="__codelineno-39-20" name="__codelineno-39-20" href="#__codelineno-39-20"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">HASHTABLE_CAPACITY</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-39-20" name="__codelineno-39-20" href="#__codelineno-39-20"></a>
|
||||||
<a id="__codelineno-39-21" name="__codelineno-39-21" href="#__codelineno-39-21"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-39-21" name="__codelineno-39-21" href="#__codelineno-39-21"></a><span class="cm">/* 析构函数 */</span>
|
||||||
<a id="__codelineno-39-22" name="__codelineno-39-22" href="#__codelineno-39-22"></a><span class="w"> </span><span class="n">free</span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">-></span><span class="n">val</span><span class="p">);</span>
|
<a id="__codelineno-39-22" name="__codelineno-39-22" href="#__codelineno-39-22"></a><span class="kt">void</span><span class="w"> </span><span class="nf">delArrayHashMap</span><span class="p">(</span><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">hmap</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-39-23" name="__codelineno-39-23" href="#__codelineno-39-23"></a><span class="w"> </span><span class="n">free</span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]);</span>
|
<a id="__codelineno-39-23" name="__codelineno-39-23" href="#__codelineno-39-23"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">MAX_SIZE</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-39-24" name="__codelineno-39-24" href="#__codelineno-39-24"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-39-24" name="__codelineno-39-24" href="#__codelineno-39-24"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-39-25" name="__codelineno-39-25" href="#__codelineno-39-25"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-39-25" name="__codelineno-39-25" href="#__codelineno-39-25"></a><span class="w"> </span><span class="n">free</span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">-></span><span class="n">val</span><span class="p">);</span>
|
||||||
<a id="__codelineno-39-26" name="__codelineno-39-26" href="#__codelineno-39-26"></a><span class="w"> </span><span class="n">free</span><span class="p">(</span><span class="n">hmap</span><span class="p">);</span>
|
<a id="__codelineno-39-26" name="__codelineno-39-26" href="#__codelineno-39-26"></a><span class="w"> </span><span class="n">free</span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]);</span>
|
||||||
<a id="__codelineno-39-27" name="__codelineno-39-27" href="#__codelineno-39-27"></a><span class="p">}</span>
|
<a id="__codelineno-39-27" name="__codelineno-39-27" href="#__codelineno-39-27"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-39-28" name="__codelineno-39-28" href="#__codelineno-39-28"></a>
|
<a id="__codelineno-39-28" name="__codelineno-39-28" href="#__codelineno-39-28"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-39-29" name="__codelineno-39-29" href="#__codelineno-39-29"></a><span class="cm">/* 添加操作 */</span>
|
<a id="__codelineno-39-29" name="__codelineno-39-29" href="#__codelineno-39-29"></a><span class="w"> </span><span class="n">free</span><span class="p">(</span><span class="n">hmap</span><span class="p">);</span>
|
||||||
<a id="__codelineno-39-30" name="__codelineno-39-30" href="#__codelineno-39-30"></a><span class="kt">void</span><span class="w"> </span><span class="nf">put</span><span class="p">(</span><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">hmap</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">key</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="n">val</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-39-30" name="__codelineno-39-30" href="#__codelineno-39-30"></a><span class="p">}</span>
|
||||||
<a id="__codelineno-39-31" name="__codelineno-39-31" href="#__codelineno-39-31"></a><span class="w"> </span><span class="n">Pair</span><span class="w"> </span><span class="o">*</span><span class="n">Pair</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="n">Pair</span><span class="p">));</span>
|
<a id="__codelineno-39-31" name="__codelineno-39-31" href="#__codelineno-39-31"></a>
|
||||||
<a id="__codelineno-39-32" name="__codelineno-39-32" href="#__codelineno-39-32"></a><span class="w"> </span><span class="n">Pair</span><span class="o">-></span><span class="n">key</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">key</span><span class="p">;</span>
|
<a id="__codelineno-39-32" name="__codelineno-39-32" href="#__codelineno-39-32"></a><span class="cm">/* 添加操作 */</span>
|
||||||
<a id="__codelineno-39-33" name="__codelineno-39-33" href="#__codelineno-39-33"></a><span class="w"> </span><span class="n">Pair</span><span class="o">-></span><span class="n">val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="n">strlen</span><span class="p">(</span><span class="n">val</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">);</span>
|
<a id="__codelineno-39-33" name="__codelineno-39-33" href="#__codelineno-39-33"></a><span class="kt">void</span><span class="w"> </span><span class="nf">put</span><span class="p">(</span><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">hmap</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">key</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="n">val</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-39-34" name="__codelineno-39-34" href="#__codelineno-39-34"></a><span class="w"> </span><span class="n">strcpy</span><span class="p">(</span><span class="n">Pair</span><span class="o">-></span><span class="n">val</span><span class="p">,</span><span class="w"> </span><span class="n">val</span><span class="p">);</span>
|
<a id="__codelineno-39-34" name="__codelineno-39-34" href="#__codelineno-39-34"></a><span class="w"> </span><span class="n">Pair</span><span class="w"> </span><span class="o">*</span><span class="n">Pair</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="n">Pair</span><span class="p">));</span>
|
||||||
<a id="__codelineno-39-35" name="__codelineno-39-35" href="#__codelineno-39-35"></a>
|
<a id="__codelineno-39-35" name="__codelineno-39-35" href="#__codelineno-39-35"></a><span class="w"> </span><span class="n">Pair</span><span class="o">-></span><span class="n">key</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">key</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-36" name="__codelineno-39-36" href="#__codelineno-39-36"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">hashFunc</span><span class="p">(</span><span class="n">key</span><span class="p">);</span>
|
<a id="__codelineno-39-36" name="__codelineno-39-36" href="#__codelineno-39-36"></a><span class="w"> </span><span class="n">Pair</span><span class="o">-></span><span class="n">val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="n">strlen</span><span class="p">(</span><span class="n">val</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">);</span>
|
||||||
<a id="__codelineno-39-37" name="__codelineno-39-37" href="#__codelineno-39-37"></a><span class="w"> </span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Pair</span><span class="p">;</span>
|
<a id="__codelineno-39-37" name="__codelineno-39-37" href="#__codelineno-39-37"></a><span class="w"> </span><span class="n">strcpy</span><span class="p">(</span><span class="n">Pair</span><span class="o">-></span><span class="n">val</span><span class="p">,</span><span class="w"> </span><span class="n">val</span><span class="p">);</span>
|
||||||
<a id="__codelineno-39-38" name="__codelineno-39-38" href="#__codelineno-39-38"></a><span class="p">}</span>
|
<a id="__codelineno-39-38" name="__codelineno-39-38" href="#__codelineno-39-38"></a>
|
||||||
<a id="__codelineno-39-39" name="__codelineno-39-39" href="#__codelineno-39-39"></a>
|
<a id="__codelineno-39-39" name="__codelineno-39-39" href="#__codelineno-39-39"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">hashFunc</span><span class="p">(</span><span class="n">key</span><span class="p">);</span>
|
||||||
<a id="__codelineno-39-40" name="__codelineno-39-40" href="#__codelineno-39-40"></a><span class="cm">/* 删除操作 */</span>
|
<a id="__codelineno-39-40" name="__codelineno-39-40" href="#__codelineno-39-40"></a><span class="w"> </span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Pair</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-41" name="__codelineno-39-41" href="#__codelineno-39-41"></a><span class="kt">void</span><span class="w"> </span><span class="nf">removeItem</span><span class="p">(</span><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">hmap</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">key</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-39-41" name="__codelineno-39-41" href="#__codelineno-39-41"></a><span class="p">}</span>
|
||||||
<a id="__codelineno-39-42" name="__codelineno-39-42" href="#__codelineno-39-42"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">hashFunc</span><span class="p">(</span><span class="n">key</span><span class="p">);</span>
|
<a id="__codelineno-39-42" name="__codelineno-39-42" href="#__codelineno-39-42"></a>
|
||||||
<a id="__codelineno-39-43" name="__codelineno-39-43" href="#__codelineno-39-43"></a><span class="w"> </span><span class="n">free</span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="o">-></span><span class="n">val</span><span class="p">);</span>
|
<a id="__codelineno-39-43" name="__codelineno-39-43" href="#__codelineno-39-43"></a><span class="cm">/* 删除操作 */</span>
|
||||||
<a id="__codelineno-39-44" name="__codelineno-39-44" href="#__codelineno-39-44"></a><span class="w"> </span><span class="n">free</span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">index</span><span class="p">]);</span>
|
<a id="__codelineno-39-44" name="__codelineno-39-44" href="#__codelineno-39-44"></a><span class="kt">void</span><span class="w"> </span><span class="nf">removeItem</span><span class="p">(</span><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">hmap</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">key</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-39-45" name="__codelineno-39-45" href="#__codelineno-39-45"></a><span class="w"> </span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">;</span>
|
<a id="__codelineno-39-45" name="__codelineno-39-45" href="#__codelineno-39-45"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">hashFunc</span><span class="p">(</span><span class="n">key</span><span class="p">);</span>
|
||||||
<a id="__codelineno-39-46" name="__codelineno-39-46" href="#__codelineno-39-46"></a><span class="p">}</span>
|
<a id="__codelineno-39-46" name="__codelineno-39-46" href="#__codelineno-39-46"></a><span class="w"> </span><span class="n">free</span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="o">-></span><span class="n">val</span><span class="p">);</span>
|
||||||
<a id="__codelineno-39-47" name="__codelineno-39-47" href="#__codelineno-39-47"></a>
|
<a id="__codelineno-39-47" name="__codelineno-39-47" href="#__codelineno-39-47"></a><span class="w"> </span><span class="n">free</span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">index</span><span class="p">]);</span>
|
||||||
<a id="__codelineno-39-48" name="__codelineno-39-48" href="#__codelineno-39-48"></a><span class="cm">/* 获取所有键值对 */</span>
|
<a id="__codelineno-39-48" name="__codelineno-39-48" href="#__codelineno-39-48"></a><span class="w"> </span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-49" name="__codelineno-39-49" href="#__codelineno-39-49"></a><span class="kt">void</span><span class="w"> </span><span class="nf">pairSet</span><span class="p">(</span><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">hmap</span><span class="p">,</span><span class="w"> </span><span class="n">MapSet</span><span class="w"> </span><span class="o">*</span><span class="n">set</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-39-49" name="__codelineno-39-49" href="#__codelineno-39-49"></a><span class="p">}</span>
|
||||||
<a id="__codelineno-39-50" name="__codelineno-39-50" href="#__codelineno-39-50"></a><span class="w"> </span><span class="n">Pair</span><span class="w"> </span><span class="o">*</span><span class="n">entries</span><span class="p">;</span>
|
<a id="__codelineno-39-50" name="__codelineno-39-50" href="#__codelineno-39-50"></a>
|
||||||
<a id="__codelineno-39-51" name="__codelineno-39-51" href="#__codelineno-39-51"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
<a id="__codelineno-39-51" name="__codelineno-39-51" href="#__codelineno-39-51"></a><span class="cm">/* 获取所有键值对 */</span>
|
||||||
<a id="__codelineno-39-52" name="__codelineno-39-52" href="#__codelineno-39-52"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">total</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
<a id="__codelineno-39-52" name="__codelineno-39-52" href="#__codelineno-39-52"></a><span class="kt">void</span><span class="w"> </span><span class="nf">pairSet</span><span class="p">(</span><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">hmap</span><span class="p">,</span><span class="w"> </span><span class="n">MapSet</span><span class="w"> </span><span class="o">*</span><span class="n">set</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-39-53" name="__codelineno-39-53" href="#__codelineno-39-53"></a><span class="w"> </span><span class="cm">/* 统计有效键值对数量 */</span>
|
<a id="__codelineno-39-53" name="__codelineno-39-53" href="#__codelineno-39-53"></a><span class="w"> </span><span class="n">Pair</span><span class="w"> </span><span class="o">*</span><span class="n">entries</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-54" name="__codelineno-39-54" href="#__codelineno-39-54"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">HASHTABLE_CAPACITY</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-39-54" name="__codelineno-39-54" href="#__codelineno-39-54"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-55" name="__codelineno-39-55" href="#__codelineno-39-55"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-39-55" name="__codelineno-39-55" href="#__codelineno-39-55"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">total</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-56" name="__codelineno-39-56" href="#__codelineno-39-56"></a><span class="w"> </span><span class="n">total</span><span class="o">++</span><span class="p">;</span>
|
<a id="__codelineno-39-56" name="__codelineno-39-56" href="#__codelineno-39-56"></a><span class="w"> </span><span class="cm">/* 统计有效键值对数量 */</span>
|
||||||
<a id="__codelineno-39-57" name="__codelineno-39-57" href="#__codelineno-39-57"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-39-57" name="__codelineno-39-57" href="#__codelineno-39-57"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">MAX_SIZE</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-39-58" name="__codelineno-39-58" href="#__codelineno-39-58"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-39-58" name="__codelineno-39-58" href="#__codelineno-39-58"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-39-59" name="__codelineno-39-59" href="#__codelineno-39-59"></a><span class="w"> </span><span class="n">entries</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="n">Pair</span><span class="p">)</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">total</span><span class="p">);</span>
|
<a id="__codelineno-39-59" name="__codelineno-39-59" href="#__codelineno-39-59"></a><span class="w"> </span><span class="n">total</span><span class="o">++</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-60" name="__codelineno-39-60" href="#__codelineno-39-60"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">HASHTABLE_CAPACITY</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-39-60" name="__codelineno-39-60" href="#__codelineno-39-60"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-39-61" name="__codelineno-39-61" href="#__codelineno-39-61"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-39-61" name="__codelineno-39-61" href="#__codelineno-39-61"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-39-62" name="__codelineno-39-62" href="#__codelineno-39-62"></a><span class="w"> </span><span class="n">entries</span><span class="p">[</span><span class="n">index</span><span class="p">].</span><span class="n">key</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">-></span><span class="n">key</span><span class="p">;</span>
|
<a id="__codelineno-39-62" name="__codelineno-39-62" href="#__codelineno-39-62"></a><span class="w"> </span><span class="n">entries</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="n">Pair</span><span class="p">)</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">total</span><span class="p">);</span>
|
||||||
<a id="__codelineno-39-63" name="__codelineno-39-63" href="#__codelineno-39-63"></a><span class="w"> </span><span class="n">entries</span><span class="p">[</span><span class="n">index</span><span class="p">].</span><span class="n">val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="n">strlen</span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">-></span><span class="n">val</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">);</span>
|
<a id="__codelineno-39-63" name="__codelineno-39-63" href="#__codelineno-39-63"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">MAX_SIZE</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-39-64" name="__codelineno-39-64" href="#__codelineno-39-64"></a><span class="w"> </span><span class="n">strcpy</span><span class="p">(</span><span class="n">entries</span><span class="p">[</span><span class="n">index</span><span class="p">].</span><span class="n">val</span><span class="p">,</span><span class="w"> </span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">-></span><span class="n">val</span><span class="p">);</span>
|
<a id="__codelineno-39-64" name="__codelineno-39-64" href="#__codelineno-39-64"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-39-65" name="__codelineno-39-65" href="#__codelineno-39-65"></a><span class="w"> </span><span class="n">index</span><span class="o">++</span><span class="p">;</span>
|
<a id="__codelineno-39-65" name="__codelineno-39-65" href="#__codelineno-39-65"></a><span class="w"> </span><span class="n">entries</span><span class="p">[</span><span class="n">index</span><span class="p">].</span><span class="n">key</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">-></span><span class="n">key</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-66" name="__codelineno-39-66" href="#__codelineno-39-66"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-39-66" name="__codelineno-39-66" href="#__codelineno-39-66"></a><span class="w"> </span><span class="n">entries</span><span class="p">[</span><span class="n">index</span><span class="p">].</span><span class="n">val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="n">strlen</span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">-></span><span class="n">val</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">);</span>
|
||||||
<a id="__codelineno-39-67" name="__codelineno-39-67" href="#__codelineno-39-67"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-39-67" name="__codelineno-39-67" href="#__codelineno-39-67"></a><span class="w"> </span><span class="n">strcpy</span><span class="p">(</span><span class="n">entries</span><span class="p">[</span><span class="n">index</span><span class="p">].</span><span class="n">val</span><span class="p">,</span><span class="w"> </span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">-></span><span class="n">val</span><span class="p">);</span>
|
||||||
<a id="__codelineno-39-68" name="__codelineno-39-68" href="#__codelineno-39-68"></a><span class="w"> </span><span class="n">set</span><span class="o">-></span><span class="n">set</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">entries</span><span class="p">;</span>
|
<a id="__codelineno-39-68" name="__codelineno-39-68" href="#__codelineno-39-68"></a><span class="w"> </span><span class="n">index</span><span class="o">++</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-69" name="__codelineno-39-69" href="#__codelineno-39-69"></a><span class="w"> </span><span class="n">set</span><span class="o">-></span><span class="n">len</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">total</span><span class="p">;</span>
|
<a id="__codelineno-39-69" name="__codelineno-39-69" href="#__codelineno-39-69"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-39-70" name="__codelineno-39-70" href="#__codelineno-39-70"></a><span class="p">}</span>
|
<a id="__codelineno-39-70" name="__codelineno-39-70" href="#__codelineno-39-70"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-39-71" name="__codelineno-39-71" href="#__codelineno-39-71"></a>
|
<a id="__codelineno-39-71" name="__codelineno-39-71" href="#__codelineno-39-71"></a><span class="w"> </span><span class="n">set</span><span class="o">-></span><span class="n">set</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">entries</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-72" name="__codelineno-39-72" href="#__codelineno-39-72"></a><span class="cm">/* 获取所有键 */</span>
|
<a id="__codelineno-39-72" name="__codelineno-39-72" href="#__codelineno-39-72"></a><span class="w"> </span><span class="n">set</span><span class="o">-></span><span class="n">len</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">total</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-73" name="__codelineno-39-73" href="#__codelineno-39-73"></a><span class="kt">void</span><span class="w"> </span><span class="nf">keySet</span><span class="p">(</span><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">hmap</span><span class="p">,</span><span class="w"> </span><span class="n">MapSet</span><span class="w"> </span><span class="o">*</span><span class="n">set</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-39-73" name="__codelineno-39-73" href="#__codelineno-39-73"></a><span class="p">}</span>
|
||||||
<a id="__codelineno-39-74" name="__codelineno-39-74" href="#__codelineno-39-74"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="o">*</span><span class="n">keys</span><span class="p">;</span>
|
<a id="__codelineno-39-74" name="__codelineno-39-74" href="#__codelineno-39-74"></a>
|
||||||
<a id="__codelineno-39-75" name="__codelineno-39-75" href="#__codelineno-39-75"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
<a id="__codelineno-39-75" name="__codelineno-39-75" href="#__codelineno-39-75"></a><span class="cm">/* 获取所有键 */</span>
|
||||||
<a id="__codelineno-39-76" name="__codelineno-39-76" href="#__codelineno-39-76"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">total</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
<a id="__codelineno-39-76" name="__codelineno-39-76" href="#__codelineno-39-76"></a><span class="kt">void</span><span class="w"> </span><span class="nf">keySet</span><span class="p">(</span><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">hmap</span><span class="p">,</span><span class="w"> </span><span class="n">MapSet</span><span class="w"> </span><span class="o">*</span><span class="n">set</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-39-77" name="__codelineno-39-77" href="#__codelineno-39-77"></a><span class="w"> </span><span class="cm">/* 统计有效键值对数量 */</span>
|
<a id="__codelineno-39-77" name="__codelineno-39-77" href="#__codelineno-39-77"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="o">*</span><span class="n">keys</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-78" name="__codelineno-39-78" href="#__codelineno-39-78"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">HASHTABLE_CAPACITY</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-39-78" name="__codelineno-39-78" href="#__codelineno-39-78"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-79" name="__codelineno-39-79" href="#__codelineno-39-79"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-39-79" name="__codelineno-39-79" href="#__codelineno-39-79"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">total</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-80" name="__codelineno-39-80" href="#__codelineno-39-80"></a><span class="w"> </span><span class="n">total</span><span class="o">++</span><span class="p">;</span>
|
<a id="__codelineno-39-80" name="__codelineno-39-80" href="#__codelineno-39-80"></a><span class="w"> </span><span class="cm">/* 统计有效键值对数量 */</span>
|
||||||
<a id="__codelineno-39-81" name="__codelineno-39-81" href="#__codelineno-39-81"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-39-81" name="__codelineno-39-81" href="#__codelineno-39-81"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">MAX_SIZE</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-39-82" name="__codelineno-39-82" href="#__codelineno-39-82"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-39-82" name="__codelineno-39-82" href="#__codelineno-39-82"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-39-83" name="__codelineno-39-83" href="#__codelineno-39-83"></a><span class="w"> </span><span class="n">keys</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="n">total</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="kt">int</span><span class="p">));</span>
|
<a id="__codelineno-39-83" name="__codelineno-39-83" href="#__codelineno-39-83"></a><span class="w"> </span><span class="n">total</span><span class="o">++</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-84" name="__codelineno-39-84" href="#__codelineno-39-84"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">HASHTABLE_CAPACITY</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-39-84" name="__codelineno-39-84" href="#__codelineno-39-84"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-39-85" name="__codelineno-39-85" href="#__codelineno-39-85"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-39-85" name="__codelineno-39-85" href="#__codelineno-39-85"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-39-86" name="__codelineno-39-86" href="#__codelineno-39-86"></a><span class="w"> </span><span class="n">keys</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">-></span><span class="n">key</span><span class="p">;</span>
|
<a id="__codelineno-39-86" name="__codelineno-39-86" href="#__codelineno-39-86"></a><span class="w"> </span><span class="n">keys</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="n">total</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="kt">int</span><span class="p">));</span>
|
||||||
<a id="__codelineno-39-87" name="__codelineno-39-87" href="#__codelineno-39-87"></a><span class="w"> </span><span class="n">index</span><span class="o">++</span><span class="p">;</span>
|
<a id="__codelineno-39-87" name="__codelineno-39-87" href="#__codelineno-39-87"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">MAX_SIZE</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-39-88" name="__codelineno-39-88" href="#__codelineno-39-88"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-39-88" name="__codelineno-39-88" href="#__codelineno-39-88"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-39-89" name="__codelineno-39-89" href="#__codelineno-39-89"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-39-89" name="__codelineno-39-89" href="#__codelineno-39-89"></a><span class="w"> </span><span class="n">keys</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">-></span><span class="n">key</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-90" name="__codelineno-39-90" href="#__codelineno-39-90"></a><span class="w"> </span><span class="n">set</span><span class="o">-></span><span class="n">set</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">keys</span><span class="p">;</span>
|
<a id="__codelineno-39-90" name="__codelineno-39-90" href="#__codelineno-39-90"></a><span class="w"> </span><span class="n">index</span><span class="o">++</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-91" name="__codelineno-39-91" href="#__codelineno-39-91"></a><span class="w"> </span><span class="n">set</span><span class="o">-></span><span class="n">len</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">total</span><span class="p">;</span>
|
<a id="__codelineno-39-91" name="__codelineno-39-91" href="#__codelineno-39-91"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-39-92" name="__codelineno-39-92" href="#__codelineno-39-92"></a><span class="p">}</span>
|
<a id="__codelineno-39-92" name="__codelineno-39-92" href="#__codelineno-39-92"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-39-93" name="__codelineno-39-93" href="#__codelineno-39-93"></a>
|
<a id="__codelineno-39-93" name="__codelineno-39-93" href="#__codelineno-39-93"></a><span class="w"> </span><span class="n">set</span><span class="o">-></span><span class="n">set</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">keys</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-94" name="__codelineno-39-94" href="#__codelineno-39-94"></a><span class="cm">/* 获取所有值 */</span>
|
<a id="__codelineno-39-94" name="__codelineno-39-94" href="#__codelineno-39-94"></a><span class="w"> </span><span class="n">set</span><span class="o">-></span><span class="n">len</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">total</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-95" name="__codelineno-39-95" href="#__codelineno-39-95"></a><span class="kt">void</span><span class="w"> </span><span class="nf">valueSet</span><span class="p">(</span><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">hmap</span><span class="p">,</span><span class="w"> </span><span class="n">MapSet</span><span class="w"> </span><span class="o">*</span><span class="n">set</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-39-95" name="__codelineno-39-95" href="#__codelineno-39-95"></a><span class="p">}</span>
|
||||||
<a id="__codelineno-39-96" name="__codelineno-39-96" href="#__codelineno-39-96"></a><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="o">**</span><span class="n">vals</span><span class="p">;</span>
|
<a id="__codelineno-39-96" name="__codelineno-39-96" href="#__codelineno-39-96"></a>
|
||||||
<a id="__codelineno-39-97" name="__codelineno-39-97" href="#__codelineno-39-97"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
<a id="__codelineno-39-97" name="__codelineno-39-97" href="#__codelineno-39-97"></a><span class="cm">/* 获取所有值 */</span>
|
||||||
<a id="__codelineno-39-98" name="__codelineno-39-98" href="#__codelineno-39-98"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">total</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
<a id="__codelineno-39-98" name="__codelineno-39-98" href="#__codelineno-39-98"></a><span class="kt">void</span><span class="w"> </span><span class="nf">valueSet</span><span class="p">(</span><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">hmap</span><span class="p">,</span><span class="w"> </span><span class="n">MapSet</span><span class="w"> </span><span class="o">*</span><span class="n">set</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-39-99" name="__codelineno-39-99" href="#__codelineno-39-99"></a><span class="w"> </span><span class="cm">/* 统计有效键值对数量 */</span>
|
<a id="__codelineno-39-99" name="__codelineno-39-99" href="#__codelineno-39-99"></a><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="o">**</span><span class="n">vals</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-100" name="__codelineno-39-100" href="#__codelineno-39-100"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">HASHTABLE_CAPACITY</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-39-100" name="__codelineno-39-100" href="#__codelineno-39-100"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-101" name="__codelineno-39-101" href="#__codelineno-39-101"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-39-101" name="__codelineno-39-101" href="#__codelineno-39-101"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">total</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-102" name="__codelineno-39-102" href="#__codelineno-39-102"></a><span class="w"> </span><span class="n">total</span><span class="o">++</span><span class="p">;</span>
|
<a id="__codelineno-39-102" name="__codelineno-39-102" href="#__codelineno-39-102"></a><span class="w"> </span><span class="cm">/* 统计有效键值对数量 */</span>
|
||||||
<a id="__codelineno-39-103" name="__codelineno-39-103" href="#__codelineno-39-103"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-39-103" name="__codelineno-39-103" href="#__codelineno-39-103"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">MAX_SIZE</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-39-104" name="__codelineno-39-104" href="#__codelineno-39-104"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-39-104" name="__codelineno-39-104" href="#__codelineno-39-104"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-39-105" name="__codelineno-39-105" href="#__codelineno-39-105"></a><span class="w"> </span><span class="n">vals</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="n">total</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="p">));</span>
|
<a id="__codelineno-39-105" name="__codelineno-39-105" href="#__codelineno-39-105"></a><span class="w"> </span><span class="n">total</span><span class="o">++</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-106" name="__codelineno-39-106" href="#__codelineno-39-106"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">HASHTABLE_CAPACITY</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-39-106" name="__codelineno-39-106" href="#__codelineno-39-106"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-39-107" name="__codelineno-39-107" href="#__codelineno-39-107"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-39-107" name="__codelineno-39-107" href="#__codelineno-39-107"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-39-108" name="__codelineno-39-108" href="#__codelineno-39-108"></a><span class="w"> </span><span class="n">vals</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">-></span><span class="n">val</span><span class="p">;</span>
|
<a id="__codelineno-39-108" name="__codelineno-39-108" href="#__codelineno-39-108"></a><span class="w"> </span><span class="n">vals</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="n">total</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="p">));</span>
|
||||||
<a id="__codelineno-39-109" name="__codelineno-39-109" href="#__codelineno-39-109"></a><span class="w"> </span><span class="n">index</span><span class="o">++</span><span class="p">;</span>
|
<a id="__codelineno-39-109" name="__codelineno-39-109" href="#__codelineno-39-109"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">MAX_SIZE</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-39-110" name="__codelineno-39-110" href="#__codelineno-39-110"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-39-110" name="__codelineno-39-110" href="#__codelineno-39-110"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-39-111" name="__codelineno-39-111" href="#__codelineno-39-111"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-39-111" name="__codelineno-39-111" href="#__codelineno-39-111"></a><span class="w"> </span><span class="n">vals</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">-></span><span class="n">val</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-112" name="__codelineno-39-112" href="#__codelineno-39-112"></a><span class="w"> </span><span class="n">set</span><span class="o">-></span><span class="n">set</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">vals</span><span class="p">;</span>
|
<a id="__codelineno-39-112" name="__codelineno-39-112" href="#__codelineno-39-112"></a><span class="w"> </span><span class="n">index</span><span class="o">++</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-113" name="__codelineno-39-113" href="#__codelineno-39-113"></a><span class="w"> </span><span class="n">set</span><span class="o">-></span><span class="n">len</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">total</span><span class="p">;</span>
|
<a id="__codelineno-39-113" name="__codelineno-39-113" href="#__codelineno-39-113"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-39-114" name="__codelineno-39-114" href="#__codelineno-39-114"></a><span class="p">}</span>
|
<a id="__codelineno-39-114" name="__codelineno-39-114" href="#__codelineno-39-114"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-39-115" name="__codelineno-39-115" href="#__codelineno-39-115"></a>
|
<a id="__codelineno-39-115" name="__codelineno-39-115" href="#__codelineno-39-115"></a><span class="w"> </span><span class="n">set</span><span class="o">-></span><span class="n">set</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">vals</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-116" name="__codelineno-39-116" href="#__codelineno-39-116"></a><span class="cm">/* 打印哈希表 */</span>
|
<a id="__codelineno-39-116" name="__codelineno-39-116" href="#__codelineno-39-116"></a><span class="w"> </span><span class="n">set</span><span class="o">-></span><span class="n">len</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">total</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-117" name="__codelineno-39-117" href="#__codelineno-39-117"></a><span class="kt">void</span><span class="w"> </span><span class="nf">print</span><span class="p">(</span><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">hmap</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-39-117" name="__codelineno-39-117" href="#__codelineno-39-117"></a><span class="p">}</span>
|
||||||
<a id="__codelineno-39-118" name="__codelineno-39-118" href="#__codelineno-39-118"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="p">;</span>
|
<a id="__codelineno-39-118" name="__codelineno-39-118" href="#__codelineno-39-118"></a>
|
||||||
<a id="__codelineno-39-119" name="__codelineno-39-119" href="#__codelineno-39-119"></a><span class="w"> </span><span class="n">MapSet</span><span class="w"> </span><span class="n">set</span><span class="p">;</span>
|
<a id="__codelineno-39-119" name="__codelineno-39-119" href="#__codelineno-39-119"></a><span class="cm">/* 打印哈希表 */</span>
|
||||||
<a id="__codelineno-39-120" name="__codelineno-39-120" href="#__codelineno-39-120"></a><span class="w"> </span><span class="n">pairSet</span><span class="p">(</span><span class="n">hmap</span><span class="p">,</span><span class="w"> </span><span class="o">&</span><span class="n">set</span><span class="p">);</span>
|
<a id="__codelineno-39-120" name="__codelineno-39-120" href="#__codelineno-39-120"></a><span class="kt">void</span><span class="w"> </span><span class="nf">print</span><span class="p">(</span><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">hmap</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-39-121" name="__codelineno-39-121" href="#__codelineno-39-121"></a><span class="w"> </span><span class="n">Pair</span><span class="w"> </span><span class="o">*</span><span class="n">entries</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="n">Pair</span><span class="w"> </span><span class="o">*</span><span class="p">)</span><span class="n">set</span><span class="p">.</span><span class="n">set</span><span class="p">;</span>
|
<a id="__codelineno-39-121" name="__codelineno-39-121" href="#__codelineno-39-121"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-122" name="__codelineno-39-122" href="#__codelineno-39-122"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">set</span><span class="p">.</span><span class="n">len</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-39-122" name="__codelineno-39-122" href="#__codelineno-39-122"></a><span class="w"> </span><span class="n">MapSet</span><span class="w"> </span><span class="n">set</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-123" name="__codelineno-39-123" href="#__codelineno-39-123"></a><span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="s">"%d -> %s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span><span class="w"> </span><span class="n">entries</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">key</span><span class="p">,</span><span class="w"> </span><span class="n">entries</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">val</span><span class="p">);</span>
|
<a id="__codelineno-39-123" name="__codelineno-39-123" href="#__codelineno-39-123"></a><span class="w"> </span><span class="n">pairSet</span><span class="p">(</span><span class="n">hmap</span><span class="p">,</span><span class="w"> </span><span class="o">&</span><span class="n">set</span><span class="p">);</span>
|
||||||
<a id="__codelineno-39-124" name="__codelineno-39-124" href="#__codelineno-39-124"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-39-124" name="__codelineno-39-124" href="#__codelineno-39-124"></a><span class="w"> </span><span class="n">Pair</span><span class="w"> </span><span class="o">*</span><span class="n">entries</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="n">Pair</span><span class="w"> </span><span class="o">*</span><span class="p">)</span><span class="n">set</span><span class="p">.</span><span class="n">set</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-125" name="__codelineno-39-125" href="#__codelineno-39-125"></a><span class="w"> </span><span class="n">free</span><span class="p">(</span><span class="n">set</span><span class="p">.</span><span class="n">set</span><span class="p">);</span>
|
<a id="__codelineno-39-125" name="__codelineno-39-125" href="#__codelineno-39-125"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">set</span><span class="p">.</span><span class="n">len</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-39-126" name="__codelineno-39-126" href="#__codelineno-39-126"></a><span class="p">}</span>
|
<a id="__codelineno-39-126" name="__codelineno-39-126" href="#__codelineno-39-126"></a><span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="s">"%d -> %s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span><span class="w"> </span><span class="n">entries</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">key</span><span class="p">,</span><span class="w"> </span><span class="n">entries</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">val</span><span class="p">);</span>
|
||||||
|
<a id="__codelineno-39-127" name="__codelineno-39-127" href="#__codelineno-39-127"></a><span class="w"> </span><span class="p">}</span>
|
||||||
|
<a id="__codelineno-39-128" name="__codelineno-39-128" href="#__codelineno-39-128"></a><span class="w"> </span><span class="n">free</span><span class="p">(</span><span class="n">set</span><span class="p">.</span><span class="n">set</span><span class="p">);</span>
|
||||||
|
<a id="__codelineno-39-129" name="__codelineno-39-129" href="#__codelineno-39-129"></a><span class="p">}</span>
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
|
|
|
@ -3975,7 +3975,27 @@
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
<div class="highlight"><span class="filename">binary_search.rb</span><pre><span></span><code><a id="__codelineno-12-1" name="__codelineno-12-1" href="#__codelineno-12-1"></a><span class="o">[</span><span class="n">class</span><span class="o">]</span><span class="p">{}</span><span class="o">-[</span><span class="n">func</span><span class="o">]</span><span class="p">{</span><span class="n">binary_search</span><span class="p">}</span>
|
<div class="highlight"><span class="filename">binary_search.rb</span><pre><span></span><code><a id="__codelineno-12-1" name="__codelineno-12-1" href="#__codelineno-12-1"></a><span class="c1">### 二分查找(双闭区间) ###</span>
|
||||||
|
<a id="__codelineno-12-2" name="__codelineno-12-2" href="#__codelineno-12-2"></a><span class="k">def</span><span class="w"> </span><span class="nf">binary_search</span><span class="p">(</span><span class="n">nums</span><span class="p">,</span><span class="w"> </span><span class="n">target</span><span class="p">)</span>
|
||||||
|
<a id="__codelineno-12-3" name="__codelineno-12-3" href="#__codelineno-12-3"></a><span class="w"> </span><span class="c1"># 初始化双闭区间 [0, n-1] ,即 i, j 分别指向数组首元素、尾元素</span>
|
||||||
|
<a id="__codelineno-12-4" name="__codelineno-12-4" href="#__codelineno-12-4"></a><span class="w"> </span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">nums</span><span class="o">.</span><span class="n">length</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span>
|
||||||
|
<a id="__codelineno-12-5" name="__codelineno-12-5" href="#__codelineno-12-5"></a>
|
||||||
|
<a id="__codelineno-12-6" name="__codelineno-12-6" href="#__codelineno-12-6"></a><span class="w"> </span><span class="c1"># 循环,当搜索区间为空时跳出(当 i > j 时为空)</span>
|
||||||
|
<a id="__codelineno-12-7" name="__codelineno-12-7" href="#__codelineno-12-7"></a><span class="w"> </span><span class="k">while</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><=</span><span class="w"> </span><span class="n">j</span>
|
||||||
|
<a id="__codelineno-12-8" name="__codelineno-12-8" href="#__codelineno-12-8"></a><span class="w"> </span><span class="c1"># 理论上 Ruby 的数字可以无限大(取决于内存大小),无须考虑大数越界问题</span>
|
||||||
|
<a id="__codelineno-12-9" name="__codelineno-12-9" href="#__codelineno-12-9"></a><span class="w"> </span><span class="n">m</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">j</span><span class="p">)</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="c1"># 计算中点索引 m</span>
|
||||||
|
<a id="__codelineno-12-10" name="__codelineno-12-10" href="#__codelineno-12-10"></a>
|
||||||
|
<a id="__codelineno-12-11" name="__codelineno-12-11" href="#__codelineno-12-11"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">nums</span><span class="o">[</span><span class="n">m</span><span class="o">]</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">target</span>
|
||||||
|
<a id="__codelineno-12-12" name="__codelineno-12-12" href="#__codelineno-12-12"></a><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">m</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="c1"># 此情况说明 target 在区间 [m+1, j] 中</span>
|
||||||
|
<a id="__codelineno-12-13" name="__codelineno-12-13" href="#__codelineno-12-13"></a><span class="w"> </span><span class="k">elsif</span><span class="w"> </span><span class="n">nums</span><span class="o">[</span><span class="n">m</span><span class="o">]</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="n">target</span>
|
||||||
|
<a id="__codelineno-12-14" name="__codelineno-12-14" href="#__codelineno-12-14"></a><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">m</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="c1"># 此情况说明 target 在区间 [i, m-1] 中</span>
|
||||||
|
<a id="__codelineno-12-15" name="__codelineno-12-15" href="#__codelineno-12-15"></a><span class="w"> </span><span class="k">else</span>
|
||||||
|
<a id="__codelineno-12-16" name="__codelineno-12-16" href="#__codelineno-12-16"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">m</span><span class="w"> </span><span class="c1"># 找到目标元素,返回其索引</span>
|
||||||
|
<a id="__codelineno-12-17" name="__codelineno-12-17" href="#__codelineno-12-17"></a><span class="w"> </span><span class="k">end</span>
|
||||||
|
<a id="__codelineno-12-18" name="__codelineno-12-18" href="#__codelineno-12-18"></a><span class="w"> </span><span class="k">end</span>
|
||||||
|
<a id="__codelineno-12-19" name="__codelineno-12-19" href="#__codelineno-12-19"></a>
|
||||||
|
<a id="__codelineno-12-20" name="__codelineno-12-20" href="#__codelineno-12-20"></a><span class="w"> </span><span class="o">-</span><span class="mi">1</span><span class="w"> </span><span class="c1"># 未找到目标元素,返回 -1</span>
|
||||||
|
<a id="__codelineno-12-21" name="__codelineno-12-21" href="#__codelineno-12-21"></a><span class="k">end</span>
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
|
@ -4274,7 +4294,27 @@
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
<div class="highlight"><span class="filename">binary_search.rb</span><pre><span></span><code><a id="__codelineno-26-1" name="__codelineno-26-1" href="#__codelineno-26-1"></a><span class="o">[</span><span class="n">class</span><span class="o">]</span><span class="p">{}</span><span class="o">-[</span><span class="n">func</span><span class="o">]</span><span class="p">{</span><span class="n">binary_search_lcro</span><span class="p">}</span>
|
<div class="highlight"><span class="filename">binary_search.rb</span><pre><span></span><code><a id="__codelineno-26-1" name="__codelineno-26-1" href="#__codelineno-26-1"></a><span class="c1">### 二分查找(左闭右开区间) ###</span>
|
||||||
|
<a id="__codelineno-26-2" name="__codelineno-26-2" href="#__codelineno-26-2"></a><span class="k">def</span><span class="w"> </span><span class="nf">binary_search_lcro</span><span class="p">(</span><span class="n">nums</span><span class="p">,</span><span class="w"> </span><span class="n">target</span><span class="p">)</span>
|
||||||
|
<a id="__codelineno-26-3" name="__codelineno-26-3" href="#__codelineno-26-3"></a><span class="w"> </span><span class="c1"># 初始化左闭右开区间 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1</span>
|
||||||
|
<a id="__codelineno-26-4" name="__codelineno-26-4" href="#__codelineno-26-4"></a><span class="w"> </span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">nums</span><span class="o">.</span><span class="n">length</span>
|
||||||
|
<a id="__codelineno-26-5" name="__codelineno-26-5" href="#__codelineno-26-5"></a>
|
||||||
|
<a id="__codelineno-26-6" name="__codelineno-26-6" href="#__codelineno-26-6"></a><span class="w"> </span><span class="c1"># 循环,当搜索区间为空时跳出(当 i = j 时为空)</span>
|
||||||
|
<a id="__codelineno-26-7" name="__codelineno-26-7" href="#__codelineno-26-7"></a><span class="w"> </span><span class="k">while</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">j</span>
|
||||||
|
<a id="__codelineno-26-8" name="__codelineno-26-8" href="#__codelineno-26-8"></a><span class="w"> </span><span class="c1"># 计算中点索引 m</span>
|
||||||
|
<a id="__codelineno-26-9" name="__codelineno-26-9" href="#__codelineno-26-9"></a><span class="w"> </span><span class="n">m</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">j</span><span class="p">)</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mi">2</span>
|
||||||
|
<a id="__codelineno-26-10" name="__codelineno-26-10" href="#__codelineno-26-10"></a>
|
||||||
|
<a id="__codelineno-26-11" name="__codelineno-26-11" href="#__codelineno-26-11"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">nums</span><span class="o">[</span><span class="n">m</span><span class="o">]</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">target</span>
|
||||||
|
<a id="__codelineno-26-12" name="__codelineno-26-12" href="#__codelineno-26-12"></a><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">m</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="c1"># 此情况说明 target 在区间 [m+1, j) 中</span>
|
||||||
|
<a id="__codelineno-26-13" name="__codelineno-26-13" href="#__codelineno-26-13"></a><span class="w"> </span><span class="k">elsif</span><span class="w"> </span><span class="n">nums</span><span class="o">[</span><span class="n">m</span><span class="o">]</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="n">target</span>
|
||||||
|
<a id="__codelineno-26-14" name="__codelineno-26-14" href="#__codelineno-26-14"></a><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">m</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="c1"># 此情况说明 target 在区间 [i, m) 中</span>
|
||||||
|
<a id="__codelineno-26-15" name="__codelineno-26-15" href="#__codelineno-26-15"></a><span class="w"> </span><span class="k">else</span>
|
||||||
|
<a id="__codelineno-26-16" name="__codelineno-26-16" href="#__codelineno-26-16"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">m</span><span class="w"> </span><span class="c1"># 找到目标元素,返回其索引</span>
|
||||||
|
<a id="__codelineno-26-17" name="__codelineno-26-17" href="#__codelineno-26-17"></a><span class="w"> </span><span class="k">end</span>
|
||||||
|
<a id="__codelineno-26-18" name="__codelineno-26-18" href="#__codelineno-26-18"></a><span class="w"> </span><span class="k">end</span>
|
||||||
|
<a id="__codelineno-26-19" name="__codelineno-26-19" href="#__codelineno-26-19"></a>
|
||||||
|
<a id="__codelineno-26-20" name="__codelineno-26-20" href="#__codelineno-26-20"></a><span class="w"> </span><span class="o">-</span><span class="mi">1</span><span class="w"> </span><span class="c1"># 未找到目标元素,返回 -1</span>
|
||||||
|
<a id="__codelineno-26-21" name="__codelineno-26-21" href="#__codelineno-26-21"></a><span class="k">end</span>
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
|
|
|
@ -3893,7 +3893,16 @@
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
<div class="highlight"><span class="filename">binary_search_edge.rb</span><pre><span></span><code><a id="__codelineno-12-1" name="__codelineno-12-1" href="#__codelineno-12-1"></a><span class="o">[</span><span class="n">class</span><span class="o">]</span><span class="p">{}</span><span class="o">-[</span><span class="n">func</span><span class="o">]</span><span class="p">{</span><span class="n">binary_search_left_edge</span><span class="p">}</span>
|
<div class="highlight"><span class="filename">binary_search_edge.rb</span><pre><span></span><code><a id="__codelineno-12-1" name="__codelineno-12-1" href="#__codelineno-12-1"></a><span class="c1">### 二分查找最左一个 target ###</span>
|
||||||
|
<a id="__codelineno-12-2" name="__codelineno-12-2" href="#__codelineno-12-2"></a><span class="k">def</span><span class="w"> </span><span class="nf">binary_search_left_edge</span><span class="p">(</span><span class="n">nums</span><span class="p">,</span><span class="w"> </span><span class="n">target</span><span class="p">)</span>
|
||||||
|
<a id="__codelineno-12-3" name="__codelineno-12-3" href="#__codelineno-12-3"></a><span class="w"> </span><span class="c1"># 等价于查找 target 的插入点</span>
|
||||||
|
<a id="__codelineno-12-4" name="__codelineno-12-4" href="#__codelineno-12-4"></a><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">binary_search_insertion</span><span class="p">(</span><span class="n">nums</span><span class="p">,</span><span class="w"> </span><span class="n">target</span><span class="p">)</span>
|
||||||
|
<a id="__codelineno-12-5" name="__codelineno-12-5" href="#__codelineno-12-5"></a>
|
||||||
|
<a id="__codelineno-12-6" name="__codelineno-12-6" href="#__codelineno-12-6"></a><span class="w"> </span><span class="c1"># 未找到 target ,返回 -1</span>
|
||||||
|
<a id="__codelineno-12-7" name="__codelineno-12-7" href="#__codelineno-12-7"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="o">-</span><span class="mi">1</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">nums</span><span class="o">.</span><span class="n">length</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="n">nums</span><span class="o">[</span><span class="n">i</span><span class="o">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="n">target</span>
|
||||||
|
<a id="__codelineno-12-8" name="__codelineno-12-8" href="#__codelineno-12-8"></a>
|
||||||
|
<a id="__codelineno-12-9" name="__codelineno-12-9" href="#__codelineno-12-9"></a><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="c1"># 找到 target ,返回索引 i</span>
|
||||||
|
<a id="__codelineno-12-10" name="__codelineno-12-10" href="#__codelineno-12-10"></a><span class="k">end</span>
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
|
@ -4110,7 +4119,19 @@
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
<div class="highlight"><span class="filename">binary_search_edge.rb</span><pre><span></span><code><a id="__codelineno-26-1" name="__codelineno-26-1" href="#__codelineno-26-1"></a><span class="o">[</span><span class="n">class</span><span class="o">]</span><span class="p">{}</span><span class="o">-[</span><span class="n">func</span><span class="o">]</span><span class="p">{</span><span class="n">binary_search_right_edge</span><span class="p">}</span>
|
<div class="highlight"><span class="filename">binary_search_edge.rb</span><pre><span></span><code><a id="__codelineno-26-1" name="__codelineno-26-1" href="#__codelineno-26-1"></a><span class="c1">### 二分查找最右一个 target ###</span>
|
||||||
|
<a id="__codelineno-26-2" name="__codelineno-26-2" href="#__codelineno-26-2"></a><span class="k">def</span><span class="w"> </span><span class="nf">binary_search_right_edge</span><span class="p">(</span><span class="n">nums</span><span class="p">,</span><span class="w"> </span><span class="n">target</span><span class="p">)</span>
|
||||||
|
<a id="__codelineno-26-3" name="__codelineno-26-3" href="#__codelineno-26-3"></a><span class="w"> </span><span class="c1"># 转化为查找最左一个 target + 1</span>
|
||||||
|
<a id="__codelineno-26-4" name="__codelineno-26-4" href="#__codelineno-26-4"></a><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">binary_search_insertion</span><span class="p">(</span><span class="n">nums</span><span class="p">,</span><span class="w"> </span><span class="n">target</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span>
|
||||||
|
<a id="__codelineno-26-5" name="__codelineno-26-5" href="#__codelineno-26-5"></a>
|
||||||
|
<a id="__codelineno-26-6" name="__codelineno-26-6" href="#__codelineno-26-6"></a><span class="w"> </span><span class="c1"># j 指向最右一个 target ,i 指向首个大于 target 的元素</span>
|
||||||
|
<a id="__codelineno-26-7" name="__codelineno-26-7" href="#__codelineno-26-7"></a><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span>
|
||||||
|
<a id="__codelineno-26-8" name="__codelineno-26-8" href="#__codelineno-26-8"></a>
|
||||||
|
<a id="__codelineno-26-9" name="__codelineno-26-9" href="#__codelineno-26-9"></a><span class="w"> </span><span class="c1"># 未找到 target ,返回 -1</span>
|
||||||
|
<a id="__codelineno-26-10" name="__codelineno-26-10" href="#__codelineno-26-10"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="o">-</span><span class="mi">1</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="o">-</span><span class="mi">1</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="n">nums</span><span class="o">[</span><span class="n">j</span><span class="o">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="n">target</span>
|
||||||
|
<a id="__codelineno-26-11" name="__codelineno-26-11" href="#__codelineno-26-11"></a>
|
||||||
|
<a id="__codelineno-26-12" name="__codelineno-26-12" href="#__codelineno-26-12"></a><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="c1"># 找到 target ,返回索引 j</span>
|
||||||
|
<a id="__codelineno-26-13" name="__codelineno-26-13" href="#__codelineno-26-13"></a><span class="k">end</span>
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
|
|
|
@ -3920,7 +3920,26 @@
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
<div class="highlight"><span class="filename">binary_search_insertion.rb</span><pre><span></span><code><a id="__codelineno-12-1" name="__codelineno-12-1" href="#__codelineno-12-1"></a><span class="o">[</span><span class="n">class</span><span class="o">]</span><span class="p">{}</span><span class="o">-[</span><span class="n">func</span><span class="o">]</span><span class="p">{</span><span class="n">binary_search_insertion_simple</span><span class="p">}</span>
|
<div class="highlight"><span class="filename">binary_search_insertion.rb</span><pre><span></span><code><a id="__codelineno-12-1" name="__codelineno-12-1" href="#__codelineno-12-1"></a><span class="c1">### 二分查找插入点(无重复元素) ###</span>
|
||||||
|
<a id="__codelineno-12-2" name="__codelineno-12-2" href="#__codelineno-12-2"></a><span class="k">def</span><span class="w"> </span><span class="nf">binary_search_insertion_simple</span><span class="p">(</span><span class="n">nums</span><span class="p">,</span><span class="w"> </span><span class="n">target</span><span class="p">)</span>
|
||||||
|
<a id="__codelineno-12-3" name="__codelineno-12-3" href="#__codelineno-12-3"></a><span class="w"> </span><span class="c1"># 初始化双闭区间 [0, n-1]</span>
|
||||||
|
<a id="__codelineno-12-4" name="__codelineno-12-4" href="#__codelineno-12-4"></a><span class="w"> </span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">nums</span><span class="o">.</span><span class="n">length</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span>
|
||||||
|
<a id="__codelineno-12-5" name="__codelineno-12-5" href="#__codelineno-12-5"></a>
|
||||||
|
<a id="__codelineno-12-6" name="__codelineno-12-6" href="#__codelineno-12-6"></a><span class="w"> </span><span class="k">while</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><=</span><span class="w"> </span><span class="n">j</span>
|
||||||
|
<a id="__codelineno-12-7" name="__codelineno-12-7" href="#__codelineno-12-7"></a><span class="w"> </span><span class="c1"># 计算中点索引 m</span>
|
||||||
|
<a id="__codelineno-12-8" name="__codelineno-12-8" href="#__codelineno-12-8"></a><span class="w"> </span><span class="n">m</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">j</span><span class="p">)</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mi">2</span>
|
||||||
|
<a id="__codelineno-12-9" name="__codelineno-12-9" href="#__codelineno-12-9"></a>
|
||||||
|
<a id="__codelineno-12-10" name="__codelineno-12-10" href="#__codelineno-12-10"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">nums</span><span class="o">[</span><span class="n">m</span><span class="o">]</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">target</span>
|
||||||
|
<a id="__codelineno-12-11" name="__codelineno-12-11" href="#__codelineno-12-11"></a><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">m</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="c1"># target 在区间 [m+1, j] 中</span>
|
||||||
|
<a id="__codelineno-12-12" name="__codelineno-12-12" href="#__codelineno-12-12"></a><span class="w"> </span><span class="k">elsif</span><span class="w"> </span><span class="n">nums</span><span class="o">[</span><span class="n">m</span><span class="o">]</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="n">target</span>
|
||||||
|
<a id="__codelineno-12-13" name="__codelineno-12-13" href="#__codelineno-12-13"></a><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">m</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="c1"># target 在区间 [i, m-1] 中</span>
|
||||||
|
<a id="__codelineno-12-14" name="__codelineno-12-14" href="#__codelineno-12-14"></a><span class="w"> </span><span class="k">else</span>
|
||||||
|
<a id="__codelineno-12-15" name="__codelineno-12-15" href="#__codelineno-12-15"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">m</span><span class="w"> </span><span class="c1"># 找到 target ,返回插入点 m</span>
|
||||||
|
<a id="__codelineno-12-16" name="__codelineno-12-16" href="#__codelineno-12-16"></a><span class="w"> </span><span class="k">end</span>
|
||||||
|
<a id="__codelineno-12-17" name="__codelineno-12-17" href="#__codelineno-12-17"></a><span class="w"> </span><span class="k">end</span>
|
||||||
|
<a id="__codelineno-12-18" name="__codelineno-12-18" href="#__codelineno-12-18"></a>
|
||||||
|
<a id="__codelineno-12-19" name="__codelineno-12-19" href="#__codelineno-12-19"></a><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="c1"># 未找到 target ,返回插入点 i</span>
|
||||||
|
<a id="__codelineno-12-20" name="__codelineno-12-20" href="#__codelineno-12-20"></a><span class="k">end</span>
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
|
@ -4225,7 +4244,26 @@
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
<div class="highlight"><span class="filename">binary_search_insertion.rb</span><pre><span></span><code><a id="__codelineno-26-1" name="__codelineno-26-1" href="#__codelineno-26-1"></a><span class="o">[</span><span class="n">class</span><span class="o">]</span><span class="p">{}</span><span class="o">-[</span><span class="n">func</span><span class="o">]</span><span class="p">{</span><span class="n">binary_search_insertion</span><span class="p">}</span>
|
<div class="highlight"><span class="filename">binary_search_insertion.rb</span><pre><span></span><code><a id="__codelineno-26-1" name="__codelineno-26-1" href="#__codelineno-26-1"></a><span class="c1">### 二分查找插入点(存在重复元素) ###</span>
|
||||||
|
<a id="__codelineno-26-2" name="__codelineno-26-2" href="#__codelineno-26-2"></a><span class="k">def</span><span class="w"> </span><span class="nf">binary_search_insertion</span><span class="p">(</span><span class="n">nums</span><span class="p">,</span><span class="w"> </span><span class="n">target</span><span class="p">)</span>
|
||||||
|
<a id="__codelineno-26-3" name="__codelineno-26-3" href="#__codelineno-26-3"></a><span class="w"> </span><span class="c1"># 初始化双闭区间 [0, n-1]</span>
|
||||||
|
<a id="__codelineno-26-4" name="__codelineno-26-4" href="#__codelineno-26-4"></a><span class="w"> </span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">nums</span><span class="o">.</span><span class="n">length</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span>
|
||||||
|
<a id="__codelineno-26-5" name="__codelineno-26-5" href="#__codelineno-26-5"></a>
|
||||||
|
<a id="__codelineno-26-6" name="__codelineno-26-6" href="#__codelineno-26-6"></a><span class="w"> </span><span class="k">while</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><=</span><span class="w"> </span><span class="n">j</span>
|
||||||
|
<a id="__codelineno-26-7" name="__codelineno-26-7" href="#__codelineno-26-7"></a><span class="w"> </span><span class="c1"># 计算中点索引 m</span>
|
||||||
|
<a id="__codelineno-26-8" name="__codelineno-26-8" href="#__codelineno-26-8"></a><span class="w"> </span><span class="n">m</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">j</span><span class="p">)</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mi">2</span>
|
||||||
|
<a id="__codelineno-26-9" name="__codelineno-26-9" href="#__codelineno-26-9"></a>
|
||||||
|
<a id="__codelineno-26-10" name="__codelineno-26-10" href="#__codelineno-26-10"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">nums</span><span class="o">[</span><span class="n">m</span><span class="o">]</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">target</span>
|
||||||
|
<a id="__codelineno-26-11" name="__codelineno-26-11" href="#__codelineno-26-11"></a><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">m</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="c1"># target 在区间 [m+1, j] 中</span>
|
||||||
|
<a id="__codelineno-26-12" name="__codelineno-26-12" href="#__codelineno-26-12"></a><span class="w"> </span><span class="k">elsif</span><span class="w"> </span><span class="n">nums</span><span class="o">[</span><span class="n">m</span><span class="o">]</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="n">target</span>
|
||||||
|
<a id="__codelineno-26-13" name="__codelineno-26-13" href="#__codelineno-26-13"></a><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">m</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="c1"># target 在区间 [i, m-1] 中</span>
|
||||||
|
<a id="__codelineno-26-14" name="__codelineno-26-14" href="#__codelineno-26-14"></a><span class="w"> </span><span class="k">else</span>
|
||||||
|
<a id="__codelineno-26-15" name="__codelineno-26-15" href="#__codelineno-26-15"></a><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">m</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="c1"># 首个小于 target 的元素在区间 [i, m-1] 中</span>
|
||||||
|
<a id="__codelineno-26-16" name="__codelineno-26-16" href="#__codelineno-26-16"></a><span class="w"> </span><span class="k">end</span>
|
||||||
|
<a id="__codelineno-26-17" name="__codelineno-26-17" href="#__codelineno-26-17"></a><span class="w"> </span><span class="k">end</span>
|
||||||
|
<a id="__codelineno-26-18" name="__codelineno-26-18" href="#__codelineno-26-18"></a>
|
||||||
|
<a id="__codelineno-26-19" name="__codelineno-26-19" href="#__codelineno-26-19"></a><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="c1"># 返回插入点 i</span>
|
||||||
|
<a id="__codelineno-26-20" name="__codelineno-26-20" href="#__codelineno-26-20"></a><span class="k">end</span>
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
|
|
|
@ -3859,7 +3859,17 @@
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
<div class="highlight"><span class="filename">two_sum.rb</span><pre><span></span><code><a id="__codelineno-12-1" name="__codelineno-12-1" href="#__codelineno-12-1"></a><span class="o">[</span><span class="n">class</span><span class="o">]</span><span class="p">{}</span><span class="o">-[</span><span class="n">func</span><span class="o">]</span><span class="p">{</span><span class="n">two_sum_brute_force</span><span class="p">}</span>
|
<div class="highlight"><span class="filename">two_sum.rb</span><pre><span></span><code><a id="__codelineno-12-1" name="__codelineno-12-1" href="#__codelineno-12-1"></a><span class="c1">### 方法一:暴力枚举 ###</span>
|
||||||
|
<a id="__codelineno-12-2" name="__codelineno-12-2" href="#__codelineno-12-2"></a><span class="k">def</span><span class="w"> </span><span class="nf">two_sum_brute_force</span><span class="p">(</span><span class="n">nums</span><span class="p">,</span><span class="w"> </span><span class="n">target</span><span class="p">)</span>
|
||||||
|
<a id="__codelineno-12-3" name="__codelineno-12-3" href="#__codelineno-12-3"></a><span class="w"> </span><span class="c1"># 两层循环,时间复杂度为 O(n^2)</span>
|
||||||
|
<a id="__codelineno-12-4" name="__codelineno-12-4" href="#__codelineno-12-4"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="mi">0</span><span class="o">...</span><span class="p">(</span><span class="n">nums</span><span class="o">.</span><span class="n">length</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span>
|
||||||
|
<a id="__codelineno-12-5" name="__codelineno-12-5" href="#__codelineno-12-5"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span><span class="o">...</span><span class="n">nums</span><span class="o">.</span><span class="n">length</span>
|
||||||
|
<a id="__codelineno-12-6" name="__codelineno-12-6" href="#__codelineno-12-6"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="o">[</span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="n">j</span><span class="o">]</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">nums</span><span class="o">[</span><span class="n">i</span><span class="o">]</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">nums</span><span class="o">[</span><span class="n">j</span><span class="o">]</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">target</span>
|
||||||
|
<a id="__codelineno-12-7" name="__codelineno-12-7" href="#__codelineno-12-7"></a><span class="w"> </span><span class="k">end</span>
|
||||||
|
<a id="__codelineno-12-8" name="__codelineno-12-8" href="#__codelineno-12-8"></a><span class="w"> </span><span class="k">end</span>
|
||||||
|
<a id="__codelineno-12-9" name="__codelineno-12-9" href="#__codelineno-12-9"></a>
|
||||||
|
<a id="__codelineno-12-10" name="__codelineno-12-10" href="#__codelineno-12-10"></a><span class="w"> </span><span class="o">[]</span>
|
||||||
|
<a id="__codelineno-12-11" name="__codelineno-12-11" href="#__codelineno-12-11"></a><span class="k">end</span>
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
|
@ -4139,7 +4149,19 @@
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
<div class="highlight"><span class="filename">two_sum.rb</span><pre><span></span><code><a id="__codelineno-26-1" name="__codelineno-26-1" href="#__codelineno-26-1"></a><span class="o">[</span><span class="n">class</span><span class="o">]</span><span class="p">{}</span><span class="o">-[</span><span class="n">func</span><span class="o">]</span><span class="p">{</span><span class="n">two_sum_hash_table</span><span class="p">}</span>
|
<div class="highlight"><span class="filename">two_sum.rb</span><pre><span></span><code><a id="__codelineno-26-1" name="__codelineno-26-1" href="#__codelineno-26-1"></a><span class="c1">### 方法二:辅助哈希表 ###</span>
|
||||||
|
<a id="__codelineno-26-2" name="__codelineno-26-2" href="#__codelineno-26-2"></a><span class="k">def</span><span class="w"> </span><span class="nf">two_sum_hash_table</span><span class="p">(</span><span class="n">nums</span><span class="p">,</span><span class="w"> </span><span class="n">target</span><span class="p">)</span>
|
||||||
|
<a id="__codelineno-26-3" name="__codelineno-26-3" href="#__codelineno-26-3"></a><span class="w"> </span><span class="c1"># 辅助哈希表,空间复杂度为 O(n)</span>
|
||||||
|
<a id="__codelineno-26-4" name="__codelineno-26-4" href="#__codelineno-26-4"></a><span class="w"> </span><span class="n">dic</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{}</span>
|
||||||
|
<a id="__codelineno-26-5" name="__codelineno-26-5" href="#__codelineno-26-5"></a><span class="w"> </span><span class="c1"># 单层循环,时间复杂度为 O(n)</span>
|
||||||
|
<a id="__codelineno-26-6" name="__codelineno-26-6" href="#__codelineno-26-6"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="mi">0</span><span class="o">...</span><span class="n">nums</span><span class="o">.</span><span class="n">length</span>
|
||||||
|
<a id="__codelineno-26-7" name="__codelineno-26-7" href="#__codelineno-26-7"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="o">[</span><span class="n">dic</span><span class="o">[</span><span class="n">target</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="n">nums</span><span class="o">[</span><span class="n">i</span><span class="o">]]</span><span class="p">,</span><span class="w"> </span><span class="n">i</span><span class="o">]</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">dic</span><span class="o">.</span><span class="n">has_key?</span><span class="p">(</span><span class="n">target</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="n">nums</span><span class="o">[</span><span class="n">i</span><span class="o">]</span><span class="p">)</span>
|
||||||
|
<a id="__codelineno-26-8" name="__codelineno-26-8" href="#__codelineno-26-8"></a>
|
||||||
|
<a id="__codelineno-26-9" name="__codelineno-26-9" href="#__codelineno-26-9"></a><span class="w"> </span><span class="n">dic</span><span class="o">[</span><span class="n">nums</span><span class="o">[</span><span class="n">i</span><span class="o">]]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">i</span>
|
||||||
|
<a id="__codelineno-26-10" name="__codelineno-26-10" href="#__codelineno-26-10"></a><span class="w"> </span><span class="k">end</span>
|
||||||
|
<a id="__codelineno-26-11" name="__codelineno-26-11" href="#__codelineno-26-11"></a>
|
||||||
|
<a id="__codelineno-26-12" name="__codelineno-26-12" href="#__codelineno-26-12"></a><span class="w"> </span><span class="o">[]</span>
|
||||||
|
<a id="__codelineno-26-13" name="__codelineno-26-13" href="#__codelineno-26-13"></a><span class="k">end</span>
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
|
|
|
@ -3993,52 +3993,38 @@
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
<div class="highlight"><span class="filename">bucket_sort.c</span><pre><span></span><code><a id="__codelineno-10-1" name="__codelineno-10-1" href="#__codelineno-10-1"></a><span class="cm">/* 桶排序 */</span>
|
<div class="highlight"><span class="filename">bucket_sort.c</span><pre><span></span><code><a id="__codelineno-10-1" name="__codelineno-10-1" href="#__codelineno-10-1"></a><span class="cm">/* 桶排序 */</span>
|
||||||
<a id="__codelineno-10-2" name="__codelineno-10-2" href="#__codelineno-10-2"></a><span class="kt">void</span><span class="w"> </span><span class="nf">bucketSort</span><span class="p">(</span><span class="kt">float</span><span class="w"> </span><span class="n">nums</span><span class="p">[],</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">size</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-10-2" name="__codelineno-10-2" href="#__codelineno-10-2"></a><span class="kt">void</span><span class="w"> </span><span class="nf">bucketSort</span><span class="p">(</span><span class="kt">float</span><span class="w"> </span><span class="n">nums</span><span class="p">[],</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-10-3" name="__codelineno-10-3" href="#__codelineno-10-3"></a><span class="w"> </span><span class="c1">// 初始化 k = n/2 个桶,预期向每个桶分配 2 个元素</span>
|
<a id="__codelineno-10-3" name="__codelineno-10-3" href="#__codelineno-10-3"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">k</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mi">2</span><span class="p">;</span><span class="w"> </span><span class="c1">// 初始化 k = n/2 个桶</span>
|
||||||
<a id="__codelineno-10-4" name="__codelineno-10-4" href="#__codelineno-10-4"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">k</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">size</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mi">2</span><span class="p">;</span>
|
<a id="__codelineno-10-4" name="__codelineno-10-4" href="#__codelineno-10-4"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="o">*</span><span class="n">sizes</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="n">k</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="kt">int</span><span class="p">));</span><span class="w"> </span><span class="c1">// 记录每个桶的大小</span>
|
||||||
<a id="__codelineno-10-5" name="__codelineno-10-5" href="#__codelineno-10-5"></a><span class="w"> </span><span class="kt">float</span><span class="w"> </span><span class="o">**</span><span class="n">buckets</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">calloc</span><span class="p">(</span><span class="n">k</span><span class="p">,</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="kt">float</span><span class="w"> </span><span class="o">*</span><span class="p">));</span>
|
<a id="__codelineno-10-5" name="__codelineno-10-5" href="#__codelineno-10-5"></a><span class="w"> </span><span class="kt">float</span><span class="w"> </span><span class="o">**</span><span class="n">buckets</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="n">k</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="kt">float</span><span class="w"> </span><span class="o">*</span><span class="p">));</span><span class="w"> </span><span class="c1">// 动态数组的数组(桶)</span>
|
||||||
<a id="__codelineno-10-6" name="__codelineno-10-6" href="#__codelineno-10-6"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">k</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-10-6" name="__codelineno-10-6" href="#__codelineno-10-6"></a>
|
||||||
<a id="__codelineno-10-7" name="__codelineno-10-7" href="#__codelineno-10-7"></a><span class="w"> </span><span class="c1">// 每个桶最多可以分配 size 个元素</span>
|
<a id="__codelineno-10-7" name="__codelineno-10-7" href="#__codelineno-10-7"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">k</span><span class="p">;</span><span class="w"> </span><span class="o">++</span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-10-8" name="__codelineno-10-8" href="#__codelineno-10-8"></a><span class="w"> </span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">calloc</span><span class="p">(</span><span class="n">size</span><span class="p">,</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="kt">float</span><span class="p">));</span>
|
<a id="__codelineno-10-8" name="__codelineno-10-8" href="#__codelineno-10-8"></a><span class="w"> </span><span class="c1">// 为每个桶预分配足够的空间</span>
|
||||||
<a id="__codelineno-10-9" name="__codelineno-10-9" href="#__codelineno-10-9"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-10-9" name="__codelineno-10-9" href="#__codelineno-10-9"></a><span class="w"> </span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="kt">float</span><span class="w"> </span><span class="o">*</span><span class="p">)</span><span class="n">malloc</span><span class="p">(</span><span class="n">n</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="kt">float</span><span class="p">));</span>
|
||||||
<a id="__codelineno-10-10" name="__codelineno-10-10" href="#__codelineno-10-10"></a>
|
<a id="__codelineno-10-10" name="__codelineno-10-10" href="#__codelineno-10-10"></a><span class="w"> </span><span class="n">sizes</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-10-11" name="__codelineno-10-11" href="#__codelineno-10-11"></a><span class="w"> </span><span class="c1">// 1. 将数组元素分配到各个桶中</span>
|
<a id="__codelineno-10-11" name="__codelineno-10-11" href="#__codelineno-10-11"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-10-12" name="__codelineno-10-12" href="#__codelineno-10-12"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">size</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-10-12" name="__codelineno-10-12" href="#__codelineno-10-12"></a>
|
||||||
<a id="__codelineno-10-13" name="__codelineno-10-13" href="#__codelineno-10-13"></a><span class="w"> </span><span class="c1">// 输入数据范围为 [0, 1),使用 num * k 映射到索引范围 [0, k-1]</span>
|
<a id="__codelineno-10-13" name="__codelineno-10-13" href="#__codelineno-10-13"></a><span class="w"> </span><span class="c1">// 1. 将数组元素分配到各个桶中</span>
|
||||||
<a id="__codelineno-10-14" name="__codelineno-10-14" href="#__codelineno-10-14"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">bucket_idx</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">nums</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">k</span><span class="p">;</span>
|
<a id="__codelineno-10-14" name="__codelineno-10-14" href="#__codelineno-10-14"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="o">++</span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-10-15" name="__codelineno-10-15" href="#__codelineno-10-15"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
<a id="__codelineno-10-15" name="__codelineno-10-15" href="#__codelineno-10-15"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">idx</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="p">)(</span><span class="n">nums</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">k</span><span class="p">);</span>
|
||||||
<a id="__codelineno-10-16" name="__codelineno-10-16" href="#__codelineno-10-16"></a><span class="w"> </span><span class="c1">// 如果桶中有数据且数据小于当前值 nums[i], 要将其放到当前桶的后面,相当于 cpp 中的 push_back</span>
|
<a id="__codelineno-10-16" name="__codelineno-10-16" href="#__codelineno-10-16"></a><span class="w"> </span><span class="n">buckets</span><span class="p">[</span><span class="n">idx</span><span class="p">][</span><span class="n">sizes</span><span class="p">[</span><span class="n">idx</span><span class="p">]</span><span class="o">++</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">nums</span><span class="p">[</span><span class="n">i</span><span class="p">];</span>
|
||||||
<a id="__codelineno-10-17" name="__codelineno-10-17" href="#__codelineno-10-17"></a><span class="w"> </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">buckets</span><span class="p">[</span><span class="n">bucket_idx</span><span class="p">][</span><span class="n">j</span><span class="p">]</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="n">buckets</span><span class="p">[</span><span class="n">bucket_idx</span><span class="p">][</span><span class="n">j</span><span class="p">]</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">nums</span><span class="p">[</span><span class="n">i</span><span class="p">])</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-10-17" name="__codelineno-10-17" href="#__codelineno-10-17"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-10-18" name="__codelineno-10-18" href="#__codelineno-10-18"></a><span class="w"> </span><span class="n">j</span><span class="o">++</span><span class="p">;</span>
|
<a id="__codelineno-10-18" name="__codelineno-10-18" href="#__codelineno-10-18"></a>
|
||||||
<a id="__codelineno-10-19" name="__codelineno-10-19" href="#__codelineno-10-19"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-10-19" name="__codelineno-10-19" href="#__codelineno-10-19"></a><span class="w"> </span><span class="c1">// 2. 对各个桶执行排序</span>
|
||||||
<a id="__codelineno-10-20" name="__codelineno-10-20" href="#__codelineno-10-20"></a><span class="w"> </span><span class="kt">float</span><span class="w"> </span><span class="n">temp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">nums</span><span class="p">[</span><span class="n">i</span><span class="p">];</span>
|
<a id="__codelineno-10-20" name="__codelineno-10-20" href="#__codelineno-10-20"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">k</span><span class="p">;</span><span class="w"> </span><span class="o">++</span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-10-21" name="__codelineno-10-21" href="#__codelineno-10-21"></a><span class="w"> </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">j</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">size</span><span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="n">buckets</span><span class="p">[</span><span class="n">bucket_idx</span><span class="p">][</span><span class="n">j</span><span class="p">]</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="mi">0</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-10-21" name="__codelineno-10-21" href="#__codelineno-10-21"></a><span class="w"> </span><span class="n">qsort</span><span class="p">(</span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">],</span><span class="w"> </span><span class="n">sizes</span><span class="p">[</span><span class="n">i</span><span class="p">],</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="kt">float</span><span class="p">),</span><span class="w"> </span><span class="n">compare</span><span class="p">);</span>
|
||||||
<a id="__codelineno-10-22" name="__codelineno-10-22" href="#__codelineno-10-22"></a><span class="w"> </span><span class="n">swap</span><span class="p">(</span><span class="o">&</span><span class="n">temp</span><span class="p">,</span><span class="w"> </span><span class="o">&</span><span class="n">buckets</span><span class="p">[</span><span class="n">bucket_idx</span><span class="p">][</span><span class="n">j</span><span class="p">]);</span>
|
<a id="__codelineno-10-22" name="__codelineno-10-22" href="#__codelineno-10-22"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-10-23" name="__codelineno-10-23" href="#__codelineno-10-23"></a><span class="w"> </span><span class="n">j</span><span class="o">++</span><span class="p">;</span>
|
<a id="__codelineno-10-23" name="__codelineno-10-23" href="#__codelineno-10-23"></a>
|
||||||
<a id="__codelineno-10-24" name="__codelineno-10-24" href="#__codelineno-10-24"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-10-24" name="__codelineno-10-24" href="#__codelineno-10-24"></a><span class="w"> </span><span class="c1">// 3. 合并排序后的桶</span>
|
||||||
<a id="__codelineno-10-25" name="__codelineno-10-25" href="#__codelineno-10-25"></a><span class="w"> </span><span class="n">buckets</span><span class="p">[</span><span class="n">bucket_idx</span><span class="p">][</span><span class="n">j</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">temp</span><span class="p">;</span>
|
<a id="__codelineno-10-25" name="__codelineno-10-25" href="#__codelineno-10-25"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">idx</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-10-26" name="__codelineno-10-26" href="#__codelineno-10-26"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-10-26" name="__codelineno-10-26" href="#__codelineno-10-26"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">k</span><span class="p">;</span><span class="w"> </span><span class="o">++</span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-10-27" name="__codelineno-10-27" href="#__codelineno-10-27"></a>
|
<a id="__codelineno-10-27" name="__codelineno-10-27" href="#__codelineno-10-27"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">sizes</span><span class="p">[</span><span class="n">i</span><span class="p">];</span><span class="w"> </span><span class="o">++</span><span class="n">j</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-10-28" name="__codelineno-10-28" href="#__codelineno-10-28"></a><span class="w"> </span><span class="c1">// 2. 对各个桶执行排序</span>
|
<a id="__codelineno-10-28" name="__codelineno-10-28" href="#__codelineno-10-28"></a><span class="w"> </span><span class="n">nums</span><span class="p">[</span><span class="n">idx</span><span class="o">++</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">];</span>
|
||||||
<a id="__codelineno-10-29" name="__codelineno-10-29" href="#__codelineno-10-29"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">k</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-10-29" name="__codelineno-10-29" href="#__codelineno-10-29"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-10-30" name="__codelineno-10-30" href="#__codelineno-10-30"></a><span class="w"> </span><span class="n">qsort</span><span class="p">(</span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">],</span><span class="w"> </span><span class="n">size</span><span class="p">,</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="kt">float</span><span class="p">),</span><span class="w"> </span><span class="n">compare_float</span><span class="p">);</span>
|
<a id="__codelineno-10-30" name="__codelineno-10-30" href="#__codelineno-10-30"></a><span class="w"> </span><span class="c1">// 释放内存</span>
|
||||||
<a id="__codelineno-10-31" name="__codelineno-10-31" href="#__codelineno-10-31"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-10-31" name="__codelineno-10-31" href="#__codelineno-10-31"></a><span class="w"> </span><span class="n">free</span><span class="p">(</span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]);</span>
|
||||||
<a id="__codelineno-10-32" name="__codelineno-10-32" href="#__codelineno-10-32"></a>
|
<a id="__codelineno-10-32" name="__codelineno-10-32" href="#__codelineno-10-32"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-10-33" name="__codelineno-10-33" href="#__codelineno-10-33"></a><span class="w"> </span><span class="c1">// 3. 遍历桶合并结果</span>
|
<a id="__codelineno-10-33" name="__codelineno-10-33" href="#__codelineno-10-33"></a><span class="p">}</span>
|
||||||
<a id="__codelineno-10-34" name="__codelineno-10-34" href="#__codelineno-10-34"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">k</span><span class="p">;</span><span class="w"> </span><span class="n">j</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
|
||||||
<a id="__codelineno-10-35" name="__codelineno-10-35" href="#__codelineno-10-35"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">l</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">l</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">size</span><span class="p">;</span><span class="w"> </span><span class="n">l</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
|
||||||
<a id="__codelineno-10-36" name="__codelineno-10-36" href="#__codelineno-10-36"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">buckets</span><span class="p">[</span><span class="n">j</span><span class="p">][</span><span class="n">l</span><span class="p">]</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="mi">0</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
|
||||||
<a id="__codelineno-10-37" name="__codelineno-10-37" href="#__codelineno-10-37"></a><span class="w"> </span><span class="n">nums</span><span class="p">[</span><span class="n">i</span><span class="o">++</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buckets</span><span class="p">[</span><span class="n">j</span><span class="p">][</span><span class="n">l</span><span class="p">];</span>
|
|
||||||
<a id="__codelineno-10-38" name="__codelineno-10-38" href="#__codelineno-10-38"></a><span class="w"> </span><span class="p">}</span>
|
|
||||||
<a id="__codelineno-10-39" name="__codelineno-10-39" href="#__codelineno-10-39"></a><span class="w"> </span><span class="p">}</span>
|
|
||||||
<a id="__codelineno-10-40" name="__codelineno-10-40" href="#__codelineno-10-40"></a><span class="w"> </span><span class="p">}</span>
|
|
||||||
<a id="__codelineno-10-41" name="__codelineno-10-41" href="#__codelineno-10-41"></a>
|
|
||||||
<a id="__codelineno-10-42" name="__codelineno-10-42" href="#__codelineno-10-42"></a><span class="w"> </span><span class="c1">// 释放上述分配的内存</span>
|
|
||||||
<a id="__codelineno-10-43" name="__codelineno-10-43" href="#__codelineno-10-43"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">k</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
|
||||||
<a id="__codelineno-10-44" name="__codelineno-10-44" href="#__codelineno-10-44"></a><span class="w"> </span><span class="n">free</span><span class="p">(</span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]);</span>
|
|
||||||
<a id="__codelineno-10-45" name="__codelineno-10-45" href="#__codelineno-10-45"></a><span class="w"> </span><span class="p">}</span>
|
|
||||||
<a id="__codelineno-10-46" name="__codelineno-10-46" href="#__codelineno-10-46"></a><span class="w"> </span><span class="n">free</span><span class="p">(</span><span class="n">buckets</span><span class="p">);</span>
|
|
||||||
<a id="__codelineno-10-47" name="__codelineno-10-47" href="#__codelineno-10-47"></a><span class="p">}</span>
|
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
|
|
|
@ -3900,7 +3900,21 @@
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
<div class="highlight"><span class="filename">insertion_sort.rb</span><pre><span></span><code><a id="__codelineno-12-1" name="__codelineno-12-1" href="#__codelineno-12-1"></a><span class="o">[</span><span class="n">class</span><span class="o">]</span><span class="p">{}</span><span class="o">-[</span><span class="n">func</span><span class="o">]</span><span class="p">{</span><span class="n">insertion_sort</span><span class="p">}</span>
|
<div class="highlight"><span class="filename">insertion_sort.rb</span><pre><span></span><code><a id="__codelineno-12-1" name="__codelineno-12-1" href="#__codelineno-12-1"></a><span class="c1">### 插入排序 ###</span>
|
||||||
|
<a id="__codelineno-12-2" name="__codelineno-12-2" href="#__codelineno-12-2"></a><span class="k">def</span><span class="w"> </span><span class="nf">insertion_sort</span><span class="p">(</span><span class="n">nums</span><span class="p">)</span>
|
||||||
|
<a id="__codelineno-12-3" name="__codelineno-12-3" href="#__codelineno-12-3"></a><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">nums</span><span class="o">.</span><span class="n">length</span>
|
||||||
|
<a id="__codelineno-12-4" name="__codelineno-12-4" href="#__codelineno-12-4"></a><span class="w"> </span><span class="c1"># 外循环:已排序区间为 [0, i-1]</span>
|
||||||
|
<a id="__codelineno-12-5" name="__codelineno-12-5" href="#__codelineno-12-5"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="mi">1</span><span class="o">...</span><span class="n">n</span>
|
||||||
|
<a id="__codelineno-12-6" name="__codelineno-12-6" href="#__codelineno-12-6"></a><span class="w"> </span><span class="n">base</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">nums</span><span class="o">[</span><span class="n">i</span><span class="o">]</span>
|
||||||
|
<a id="__codelineno-12-7" name="__codelineno-12-7" href="#__codelineno-12-7"></a><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span>
|
||||||
|
<a id="__codelineno-12-8" name="__codelineno-12-8" href="#__codelineno-12-8"></a><span class="w"> </span><span class="c1"># 内循环:将 base 插入到已排序区间 [0, i-1] 中的正确位置</span>
|
||||||
|
<a id="__codelineno-12-9" name="__codelineno-12-9" href="#__codelineno-12-9"></a><span class="w"> </span><span class="k">while</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o">>=</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="n">nums</span><span class="o">[</span><span class="n">j</span><span class="o">]</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="n">base</span>
|
||||||
|
<a id="__codelineno-12-10" name="__codelineno-12-10" href="#__codelineno-12-10"></a><span class="w"> </span><span class="n">nums</span><span class="o">[</span><span class="n">j</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">nums</span><span class="o">[</span><span class="n">j</span><span class="o">]</span><span class="w"> </span><span class="c1"># 将 nums[j] 向右移动一位</span>
|
||||||
|
<a id="__codelineno-12-11" name="__codelineno-12-11" href="#__codelineno-12-11"></a><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o">-=</span><span class="w"> </span><span class="mi">1</span>
|
||||||
|
<a id="__codelineno-12-12" name="__codelineno-12-12" href="#__codelineno-12-12"></a><span class="w"> </span><span class="k">end</span>
|
||||||
|
<a id="__codelineno-12-13" name="__codelineno-12-13" href="#__codelineno-12-13"></a><span class="w"> </span><span class="n">nums</span><span class="o">[</span><span class="n">j</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">base</span><span class="w"> </span><span class="c1"># 将 base 赋值到正确位置</span>
|
||||||
|
<a id="__codelineno-12-14" name="__codelineno-12-14" href="#__codelineno-12-14"></a><span class="w"> </span><span class="k">end</span>
|
||||||
|
<a id="__codelineno-12-15" name="__codelineno-12-15" href="#__codelineno-12-15"></a><span class="k">end</span>
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
|
|
|
@ -3507,124 +3507,127 @@
|
||||||
<a id="__codelineno-37-6" name="__codelineno-37-6" href="#__codelineno-37-6"></a>
|
<a id="__codelineno-37-6" name="__codelineno-37-6" href="#__codelineno-37-6"></a>
|
||||||
<a id="__codelineno-37-7" name="__codelineno-37-7" href="#__codelineno-37-7"></a><span class="cm">/* 基于数组实现的哈希表 */</span>
|
<a id="__codelineno-37-7" name="__codelineno-37-7" href="#__codelineno-37-7"></a><span class="cm">/* 基于数组实现的哈希表 */</span>
|
||||||
<a id="__codelineno-37-8" name="__codelineno-37-8" href="#__codelineno-37-8"></a><span class="k">typedef</span><span class="w"> </span><span class="k">struct</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-37-8" name="__codelineno-37-8" href="#__codelineno-37-8"></a><span class="k">typedef</span><span class="w"> </span><span class="k">struct</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-37-9" name="__codelineno-37-9" href="#__codelineno-37-9"></a><span class="w"> </span><span class="n">Pair</span><span class="w"> </span><span class="o">*</span><span class="n">buckets</span><span class="p">[</span><span class="n">HASHTABLE_CAPACITY</span><span class="p">];</span>
|
<a id="__codelineno-37-9" name="__codelineno-37-9" href="#__codelineno-37-9"></a><span class="w"> </span><span class="n">Pair</span><span class="w"> </span><span class="o">*</span><span class="n">buckets</span><span class="p">[</span><span class="n">MAX_SIZE</span><span class="p">];</span>
|
||||||
<a id="__codelineno-37-10" name="__codelineno-37-10" href="#__codelineno-37-10"></a><span class="p">}</span><span class="w"> </span><span class="n">ArrayHashMap</span><span class="p">;</span>
|
<a id="__codelineno-37-10" name="__codelineno-37-10" href="#__codelineno-37-10"></a><span class="p">}</span><span class="w"> </span><span class="n">ArrayHashMap</span><span class="p">;</span>
|
||||||
<a id="__codelineno-37-11" name="__codelineno-37-11" href="#__codelineno-37-11"></a>
|
<a id="__codelineno-37-11" name="__codelineno-37-11" href="#__codelineno-37-11"></a>
|
||||||
<a id="__codelineno-37-12" name="__codelineno-37-12" href="#__codelineno-37-12"></a><span class="cm">/* 构造函数 */</span>
|
<a id="__codelineno-37-12" name="__codelineno-37-12" href="#__codelineno-37-12"></a><span class="cm">/* 构造函数 */</span>
|
||||||
<a id="__codelineno-37-13" name="__codelineno-37-13" href="#__codelineno-37-13"></a><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="nf">newArrayHashMap</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-37-13" name="__codelineno-37-13" href="#__codelineno-37-13"></a><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="nf">newArrayHashMap</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-37-14" name="__codelineno-37-14" href="#__codelineno-37-14"></a><span class="w"> </span><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">hmap</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="n">ArrayHashMap</span><span class="p">));</span>
|
<a id="__codelineno-37-14" name="__codelineno-37-14" href="#__codelineno-37-14"></a><span class="w"> </span><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">hmap</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="n">ArrayHashMap</span><span class="p">));</span>
|
||||||
<a id="__codelineno-37-15" name="__codelineno-37-15" href="#__codelineno-37-15"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">hmap</span><span class="p">;</span>
|
<a id="__codelineno-37-15" name="__codelineno-37-15" href="#__codelineno-37-15"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">MAX_SIZE</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-37-16" name="__codelineno-37-16" href="#__codelineno-37-16"></a><span class="p">}</span>
|
<a id="__codelineno-37-16" name="__codelineno-37-16" href="#__codelineno-37-16"></a><span class="w"> </span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">;</span>
|
||||||
<a id="__codelineno-37-17" name="__codelineno-37-17" href="#__codelineno-37-17"></a>
|
<a id="__codelineno-37-17" name="__codelineno-37-17" href="#__codelineno-37-17"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-37-18" name="__codelineno-37-18" href="#__codelineno-37-18"></a><span class="cm">/* 析构函数 */</span>
|
<a id="__codelineno-37-18" name="__codelineno-37-18" href="#__codelineno-37-18"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">hmap</span><span class="p">;</span>
|
||||||
<a id="__codelineno-37-19" name="__codelineno-37-19" href="#__codelineno-37-19"></a><span class="kt">void</span><span class="w"> </span><span class="nf">delArrayHashMap</span><span class="p">(</span><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">hmap</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-37-19" name="__codelineno-37-19" href="#__codelineno-37-19"></a><span class="p">}</span>
|
||||||
<a id="__codelineno-37-20" name="__codelineno-37-20" href="#__codelineno-37-20"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">HASHTABLE_CAPACITY</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-37-20" name="__codelineno-37-20" href="#__codelineno-37-20"></a>
|
||||||
<a id="__codelineno-37-21" name="__codelineno-37-21" href="#__codelineno-37-21"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-37-21" name="__codelineno-37-21" href="#__codelineno-37-21"></a><span class="cm">/* 析构函数 */</span>
|
||||||
<a id="__codelineno-37-22" name="__codelineno-37-22" href="#__codelineno-37-22"></a><span class="w"> </span><span class="n">free</span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">-></span><span class="n">val</span><span class="p">);</span>
|
<a id="__codelineno-37-22" name="__codelineno-37-22" href="#__codelineno-37-22"></a><span class="kt">void</span><span class="w"> </span><span class="nf">delArrayHashMap</span><span class="p">(</span><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">hmap</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-37-23" name="__codelineno-37-23" href="#__codelineno-37-23"></a><span class="w"> </span><span class="n">free</span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]);</span>
|
<a id="__codelineno-37-23" name="__codelineno-37-23" href="#__codelineno-37-23"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">MAX_SIZE</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-37-24" name="__codelineno-37-24" href="#__codelineno-37-24"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-37-24" name="__codelineno-37-24" href="#__codelineno-37-24"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-37-25" name="__codelineno-37-25" href="#__codelineno-37-25"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-37-25" name="__codelineno-37-25" href="#__codelineno-37-25"></a><span class="w"> </span><span class="n">free</span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">-></span><span class="n">val</span><span class="p">);</span>
|
||||||
<a id="__codelineno-37-26" name="__codelineno-37-26" href="#__codelineno-37-26"></a><span class="w"> </span><span class="n">free</span><span class="p">(</span><span class="n">hmap</span><span class="p">);</span>
|
<a id="__codelineno-37-26" name="__codelineno-37-26" href="#__codelineno-37-26"></a><span class="w"> </span><span class="n">free</span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]);</span>
|
||||||
<a id="__codelineno-37-27" name="__codelineno-37-27" href="#__codelineno-37-27"></a><span class="p">}</span>
|
<a id="__codelineno-37-27" name="__codelineno-37-27" href="#__codelineno-37-27"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-37-28" name="__codelineno-37-28" href="#__codelineno-37-28"></a>
|
<a id="__codelineno-37-28" name="__codelineno-37-28" href="#__codelineno-37-28"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-37-29" name="__codelineno-37-29" href="#__codelineno-37-29"></a><span class="cm">/* 添加操作 */</span>
|
<a id="__codelineno-37-29" name="__codelineno-37-29" href="#__codelineno-37-29"></a><span class="w"> </span><span class="n">free</span><span class="p">(</span><span class="n">hmap</span><span class="p">);</span>
|
||||||
<a id="__codelineno-37-30" name="__codelineno-37-30" href="#__codelineno-37-30"></a><span class="kt">void</span><span class="w"> </span><span class="nf">put</span><span class="p">(</span><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">hmap</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">key</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="n">val</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-37-30" name="__codelineno-37-30" href="#__codelineno-37-30"></a><span class="p">}</span>
|
||||||
<a id="__codelineno-37-31" name="__codelineno-37-31" href="#__codelineno-37-31"></a><span class="w"> </span><span class="n">Pair</span><span class="w"> </span><span class="o">*</span><span class="n">Pair</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="n">Pair</span><span class="p">));</span>
|
<a id="__codelineno-37-31" name="__codelineno-37-31" href="#__codelineno-37-31"></a>
|
||||||
<a id="__codelineno-37-32" name="__codelineno-37-32" href="#__codelineno-37-32"></a><span class="w"> </span><span class="n">Pair</span><span class="o">-></span><span class="n">key</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">key</span><span class="p">;</span>
|
<a id="__codelineno-37-32" name="__codelineno-37-32" href="#__codelineno-37-32"></a><span class="cm">/* 添加操作 */</span>
|
||||||
<a id="__codelineno-37-33" name="__codelineno-37-33" href="#__codelineno-37-33"></a><span class="w"> </span><span class="n">Pair</span><span class="o">-></span><span class="n">val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="n">strlen</span><span class="p">(</span><span class="n">val</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">);</span>
|
<a id="__codelineno-37-33" name="__codelineno-37-33" href="#__codelineno-37-33"></a><span class="kt">void</span><span class="w"> </span><span class="nf">put</span><span class="p">(</span><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">hmap</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">key</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="n">val</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-37-34" name="__codelineno-37-34" href="#__codelineno-37-34"></a><span class="w"> </span><span class="n">strcpy</span><span class="p">(</span><span class="n">Pair</span><span class="o">-></span><span class="n">val</span><span class="p">,</span><span class="w"> </span><span class="n">val</span><span class="p">);</span>
|
<a id="__codelineno-37-34" name="__codelineno-37-34" href="#__codelineno-37-34"></a><span class="w"> </span><span class="n">Pair</span><span class="w"> </span><span class="o">*</span><span class="n">Pair</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="n">Pair</span><span class="p">));</span>
|
||||||
<a id="__codelineno-37-35" name="__codelineno-37-35" href="#__codelineno-37-35"></a>
|
<a id="__codelineno-37-35" name="__codelineno-37-35" href="#__codelineno-37-35"></a><span class="w"> </span><span class="n">Pair</span><span class="o">-></span><span class="n">key</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">key</span><span class="p">;</span>
|
||||||
<a id="__codelineno-37-36" name="__codelineno-37-36" href="#__codelineno-37-36"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">hashFunc</span><span class="p">(</span><span class="n">key</span><span class="p">);</span>
|
<a id="__codelineno-37-36" name="__codelineno-37-36" href="#__codelineno-37-36"></a><span class="w"> </span><span class="n">Pair</span><span class="o">-></span><span class="n">val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="n">strlen</span><span class="p">(</span><span class="n">val</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">);</span>
|
||||||
<a id="__codelineno-37-37" name="__codelineno-37-37" href="#__codelineno-37-37"></a><span class="w"> </span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Pair</span><span class="p">;</span>
|
<a id="__codelineno-37-37" name="__codelineno-37-37" href="#__codelineno-37-37"></a><span class="w"> </span><span class="n">strcpy</span><span class="p">(</span><span class="n">Pair</span><span class="o">-></span><span class="n">val</span><span class="p">,</span><span class="w"> </span><span class="n">val</span><span class="p">);</span>
|
||||||
<a id="__codelineno-37-38" name="__codelineno-37-38" href="#__codelineno-37-38"></a><span class="p">}</span>
|
<a id="__codelineno-37-38" name="__codelineno-37-38" href="#__codelineno-37-38"></a>
|
||||||
<a id="__codelineno-37-39" name="__codelineno-37-39" href="#__codelineno-37-39"></a>
|
<a id="__codelineno-37-39" name="__codelineno-37-39" href="#__codelineno-37-39"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">hashFunc</span><span class="p">(</span><span class="n">key</span><span class="p">);</span>
|
||||||
<a id="__codelineno-37-40" name="__codelineno-37-40" href="#__codelineno-37-40"></a><span class="cm">/* 删除操作 */</span>
|
<a id="__codelineno-37-40" name="__codelineno-37-40" href="#__codelineno-37-40"></a><span class="w"> </span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Pair</span><span class="p">;</span>
|
||||||
<a id="__codelineno-37-41" name="__codelineno-37-41" href="#__codelineno-37-41"></a><span class="kt">void</span><span class="w"> </span><span class="nf">removeItem</span><span class="p">(</span><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">hmap</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">key</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-37-41" name="__codelineno-37-41" href="#__codelineno-37-41"></a><span class="p">}</span>
|
||||||
<a id="__codelineno-37-42" name="__codelineno-37-42" href="#__codelineno-37-42"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">hashFunc</span><span class="p">(</span><span class="n">key</span><span class="p">);</span>
|
<a id="__codelineno-37-42" name="__codelineno-37-42" href="#__codelineno-37-42"></a>
|
||||||
<a id="__codelineno-37-43" name="__codelineno-37-43" href="#__codelineno-37-43"></a><span class="w"> </span><span class="n">free</span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="o">-></span><span class="n">val</span><span class="p">);</span>
|
<a id="__codelineno-37-43" name="__codelineno-37-43" href="#__codelineno-37-43"></a><span class="cm">/* 删除操作 */</span>
|
||||||
<a id="__codelineno-37-44" name="__codelineno-37-44" href="#__codelineno-37-44"></a><span class="w"> </span><span class="n">free</span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">index</span><span class="p">]);</span>
|
<a id="__codelineno-37-44" name="__codelineno-37-44" href="#__codelineno-37-44"></a><span class="kt">void</span><span class="w"> </span><span class="nf">removeItem</span><span class="p">(</span><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">hmap</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">key</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-37-45" name="__codelineno-37-45" href="#__codelineno-37-45"></a><span class="w"> </span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">;</span>
|
<a id="__codelineno-37-45" name="__codelineno-37-45" href="#__codelineno-37-45"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">hashFunc</span><span class="p">(</span><span class="n">key</span><span class="p">);</span>
|
||||||
<a id="__codelineno-37-46" name="__codelineno-37-46" href="#__codelineno-37-46"></a><span class="p">}</span>
|
<a id="__codelineno-37-46" name="__codelineno-37-46" href="#__codelineno-37-46"></a><span class="w"> </span><span class="n">free</span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="o">-></span><span class="n">val</span><span class="p">);</span>
|
||||||
<a id="__codelineno-37-47" name="__codelineno-37-47" href="#__codelineno-37-47"></a>
|
<a id="__codelineno-37-47" name="__codelineno-37-47" href="#__codelineno-37-47"></a><span class="w"> </span><span class="n">free</span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">index</span><span class="p">]);</span>
|
||||||
<a id="__codelineno-37-48" name="__codelineno-37-48" href="#__codelineno-37-48"></a><span class="cm">/* 获取所有键值对 */</span>
|
<a id="__codelineno-37-48" name="__codelineno-37-48" href="#__codelineno-37-48"></a><span class="w"> </span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">;</span>
|
||||||
<a id="__codelineno-37-49" name="__codelineno-37-49" href="#__codelineno-37-49"></a><span class="kt">void</span><span class="w"> </span><span class="nf">pairSet</span><span class="p">(</span><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">hmap</span><span class="p">,</span><span class="w"> </span><span class="n">MapSet</span><span class="w"> </span><span class="o">*</span><span class="n">set</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-37-49" name="__codelineno-37-49" href="#__codelineno-37-49"></a><span class="p">}</span>
|
||||||
<a id="__codelineno-37-50" name="__codelineno-37-50" href="#__codelineno-37-50"></a><span class="w"> </span><span class="n">Pair</span><span class="w"> </span><span class="o">*</span><span class="n">entries</span><span class="p">;</span>
|
<a id="__codelineno-37-50" name="__codelineno-37-50" href="#__codelineno-37-50"></a>
|
||||||
<a id="__codelineno-37-51" name="__codelineno-37-51" href="#__codelineno-37-51"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
<a id="__codelineno-37-51" name="__codelineno-37-51" href="#__codelineno-37-51"></a><span class="cm">/* 获取所有键值对 */</span>
|
||||||
<a id="__codelineno-37-52" name="__codelineno-37-52" href="#__codelineno-37-52"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">total</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
<a id="__codelineno-37-52" name="__codelineno-37-52" href="#__codelineno-37-52"></a><span class="kt">void</span><span class="w"> </span><span class="nf">pairSet</span><span class="p">(</span><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">hmap</span><span class="p">,</span><span class="w"> </span><span class="n">MapSet</span><span class="w"> </span><span class="o">*</span><span class="n">set</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-37-53" name="__codelineno-37-53" href="#__codelineno-37-53"></a><span class="w"> </span><span class="cm">/* 统计有效键值对数量 */</span>
|
<a id="__codelineno-37-53" name="__codelineno-37-53" href="#__codelineno-37-53"></a><span class="w"> </span><span class="n">Pair</span><span class="w"> </span><span class="o">*</span><span class="n">entries</span><span class="p">;</span>
|
||||||
<a id="__codelineno-37-54" name="__codelineno-37-54" href="#__codelineno-37-54"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">HASHTABLE_CAPACITY</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-37-54" name="__codelineno-37-54" href="#__codelineno-37-54"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-37-55" name="__codelineno-37-55" href="#__codelineno-37-55"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-37-55" name="__codelineno-37-55" href="#__codelineno-37-55"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">total</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-37-56" name="__codelineno-37-56" href="#__codelineno-37-56"></a><span class="w"> </span><span class="n">total</span><span class="o">++</span><span class="p">;</span>
|
<a id="__codelineno-37-56" name="__codelineno-37-56" href="#__codelineno-37-56"></a><span class="w"> </span><span class="cm">/* 统计有效键值对数量 */</span>
|
||||||
<a id="__codelineno-37-57" name="__codelineno-37-57" href="#__codelineno-37-57"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-37-57" name="__codelineno-37-57" href="#__codelineno-37-57"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">MAX_SIZE</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-37-58" name="__codelineno-37-58" href="#__codelineno-37-58"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-37-58" name="__codelineno-37-58" href="#__codelineno-37-58"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-37-59" name="__codelineno-37-59" href="#__codelineno-37-59"></a><span class="w"> </span><span class="n">entries</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="n">Pair</span><span class="p">)</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">total</span><span class="p">);</span>
|
<a id="__codelineno-37-59" name="__codelineno-37-59" href="#__codelineno-37-59"></a><span class="w"> </span><span class="n">total</span><span class="o">++</span><span class="p">;</span>
|
||||||
<a id="__codelineno-37-60" name="__codelineno-37-60" href="#__codelineno-37-60"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">HASHTABLE_CAPACITY</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-37-60" name="__codelineno-37-60" href="#__codelineno-37-60"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-37-61" name="__codelineno-37-61" href="#__codelineno-37-61"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-37-61" name="__codelineno-37-61" href="#__codelineno-37-61"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-37-62" name="__codelineno-37-62" href="#__codelineno-37-62"></a><span class="w"> </span><span class="n">entries</span><span class="p">[</span><span class="n">index</span><span class="p">].</span><span class="n">key</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">-></span><span class="n">key</span><span class="p">;</span>
|
<a id="__codelineno-37-62" name="__codelineno-37-62" href="#__codelineno-37-62"></a><span class="w"> </span><span class="n">entries</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="n">Pair</span><span class="p">)</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">total</span><span class="p">);</span>
|
||||||
<a id="__codelineno-37-63" name="__codelineno-37-63" href="#__codelineno-37-63"></a><span class="w"> </span><span class="n">entries</span><span class="p">[</span><span class="n">index</span><span class="p">].</span><span class="n">val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="n">strlen</span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">-></span><span class="n">val</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">);</span>
|
<a id="__codelineno-37-63" name="__codelineno-37-63" href="#__codelineno-37-63"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">MAX_SIZE</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-37-64" name="__codelineno-37-64" href="#__codelineno-37-64"></a><span class="w"> </span><span class="n">strcpy</span><span class="p">(</span><span class="n">entries</span><span class="p">[</span><span class="n">index</span><span class="p">].</span><span class="n">val</span><span class="p">,</span><span class="w"> </span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">-></span><span class="n">val</span><span class="p">);</span>
|
<a id="__codelineno-37-64" name="__codelineno-37-64" href="#__codelineno-37-64"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-37-65" name="__codelineno-37-65" href="#__codelineno-37-65"></a><span class="w"> </span><span class="n">index</span><span class="o">++</span><span class="p">;</span>
|
<a id="__codelineno-37-65" name="__codelineno-37-65" href="#__codelineno-37-65"></a><span class="w"> </span><span class="n">entries</span><span class="p">[</span><span class="n">index</span><span class="p">].</span><span class="n">key</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">-></span><span class="n">key</span><span class="p">;</span>
|
||||||
<a id="__codelineno-37-66" name="__codelineno-37-66" href="#__codelineno-37-66"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-37-66" name="__codelineno-37-66" href="#__codelineno-37-66"></a><span class="w"> </span><span class="n">entries</span><span class="p">[</span><span class="n">index</span><span class="p">].</span><span class="n">val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="n">strlen</span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">-></span><span class="n">val</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">);</span>
|
||||||
<a id="__codelineno-37-67" name="__codelineno-37-67" href="#__codelineno-37-67"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-37-67" name="__codelineno-37-67" href="#__codelineno-37-67"></a><span class="w"> </span><span class="n">strcpy</span><span class="p">(</span><span class="n">entries</span><span class="p">[</span><span class="n">index</span><span class="p">].</span><span class="n">val</span><span class="p">,</span><span class="w"> </span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">-></span><span class="n">val</span><span class="p">);</span>
|
||||||
<a id="__codelineno-37-68" name="__codelineno-37-68" href="#__codelineno-37-68"></a><span class="w"> </span><span class="n">set</span><span class="o">-></span><span class="n">set</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">entries</span><span class="p">;</span>
|
<a id="__codelineno-37-68" name="__codelineno-37-68" href="#__codelineno-37-68"></a><span class="w"> </span><span class="n">index</span><span class="o">++</span><span class="p">;</span>
|
||||||
<a id="__codelineno-37-69" name="__codelineno-37-69" href="#__codelineno-37-69"></a><span class="w"> </span><span class="n">set</span><span class="o">-></span><span class="n">len</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">total</span><span class="p">;</span>
|
<a id="__codelineno-37-69" name="__codelineno-37-69" href="#__codelineno-37-69"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-37-70" name="__codelineno-37-70" href="#__codelineno-37-70"></a><span class="p">}</span>
|
<a id="__codelineno-37-70" name="__codelineno-37-70" href="#__codelineno-37-70"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-37-71" name="__codelineno-37-71" href="#__codelineno-37-71"></a>
|
<a id="__codelineno-37-71" name="__codelineno-37-71" href="#__codelineno-37-71"></a><span class="w"> </span><span class="n">set</span><span class="o">-></span><span class="n">set</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">entries</span><span class="p">;</span>
|
||||||
<a id="__codelineno-37-72" name="__codelineno-37-72" href="#__codelineno-37-72"></a><span class="cm">/* 获取所有键 */</span>
|
<a id="__codelineno-37-72" name="__codelineno-37-72" href="#__codelineno-37-72"></a><span class="w"> </span><span class="n">set</span><span class="o">-></span><span class="n">len</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">total</span><span class="p">;</span>
|
||||||
<a id="__codelineno-37-73" name="__codelineno-37-73" href="#__codelineno-37-73"></a><span class="kt">void</span><span class="w"> </span><span class="nf">keySet</span><span class="p">(</span><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">hmap</span><span class="p">,</span><span class="w"> </span><span class="n">MapSet</span><span class="w"> </span><span class="o">*</span><span class="n">set</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-37-73" name="__codelineno-37-73" href="#__codelineno-37-73"></a><span class="p">}</span>
|
||||||
<a id="__codelineno-37-74" name="__codelineno-37-74" href="#__codelineno-37-74"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="o">*</span><span class="n">keys</span><span class="p">;</span>
|
<a id="__codelineno-37-74" name="__codelineno-37-74" href="#__codelineno-37-74"></a>
|
||||||
<a id="__codelineno-37-75" name="__codelineno-37-75" href="#__codelineno-37-75"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
<a id="__codelineno-37-75" name="__codelineno-37-75" href="#__codelineno-37-75"></a><span class="cm">/* 获取所有键 */</span>
|
||||||
<a id="__codelineno-37-76" name="__codelineno-37-76" href="#__codelineno-37-76"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">total</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
<a id="__codelineno-37-76" name="__codelineno-37-76" href="#__codelineno-37-76"></a><span class="kt">void</span><span class="w"> </span><span class="nf">keySet</span><span class="p">(</span><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">hmap</span><span class="p">,</span><span class="w"> </span><span class="n">MapSet</span><span class="w"> </span><span class="o">*</span><span class="n">set</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-37-77" name="__codelineno-37-77" href="#__codelineno-37-77"></a><span class="w"> </span><span class="cm">/* 统计有效键值对数量 */</span>
|
<a id="__codelineno-37-77" name="__codelineno-37-77" href="#__codelineno-37-77"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="o">*</span><span class="n">keys</span><span class="p">;</span>
|
||||||
<a id="__codelineno-37-78" name="__codelineno-37-78" href="#__codelineno-37-78"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">HASHTABLE_CAPACITY</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-37-78" name="__codelineno-37-78" href="#__codelineno-37-78"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-37-79" name="__codelineno-37-79" href="#__codelineno-37-79"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-37-79" name="__codelineno-37-79" href="#__codelineno-37-79"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">total</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-37-80" name="__codelineno-37-80" href="#__codelineno-37-80"></a><span class="w"> </span><span class="n">total</span><span class="o">++</span><span class="p">;</span>
|
<a id="__codelineno-37-80" name="__codelineno-37-80" href="#__codelineno-37-80"></a><span class="w"> </span><span class="cm">/* 统计有效键值对数量 */</span>
|
||||||
<a id="__codelineno-37-81" name="__codelineno-37-81" href="#__codelineno-37-81"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-37-81" name="__codelineno-37-81" href="#__codelineno-37-81"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">MAX_SIZE</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-37-82" name="__codelineno-37-82" href="#__codelineno-37-82"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-37-82" name="__codelineno-37-82" href="#__codelineno-37-82"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-37-83" name="__codelineno-37-83" href="#__codelineno-37-83"></a><span class="w"> </span><span class="n">keys</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="n">total</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="kt">int</span><span class="p">));</span>
|
<a id="__codelineno-37-83" name="__codelineno-37-83" href="#__codelineno-37-83"></a><span class="w"> </span><span class="n">total</span><span class="o">++</span><span class="p">;</span>
|
||||||
<a id="__codelineno-37-84" name="__codelineno-37-84" href="#__codelineno-37-84"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">HASHTABLE_CAPACITY</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-37-84" name="__codelineno-37-84" href="#__codelineno-37-84"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-37-85" name="__codelineno-37-85" href="#__codelineno-37-85"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-37-85" name="__codelineno-37-85" href="#__codelineno-37-85"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-37-86" name="__codelineno-37-86" href="#__codelineno-37-86"></a><span class="w"> </span><span class="n">keys</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">-></span><span class="n">key</span><span class="p">;</span>
|
<a id="__codelineno-37-86" name="__codelineno-37-86" href="#__codelineno-37-86"></a><span class="w"> </span><span class="n">keys</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="n">total</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="kt">int</span><span class="p">));</span>
|
||||||
<a id="__codelineno-37-87" name="__codelineno-37-87" href="#__codelineno-37-87"></a><span class="w"> </span><span class="n">index</span><span class="o">++</span><span class="p">;</span>
|
<a id="__codelineno-37-87" name="__codelineno-37-87" href="#__codelineno-37-87"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">MAX_SIZE</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-37-88" name="__codelineno-37-88" href="#__codelineno-37-88"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-37-88" name="__codelineno-37-88" href="#__codelineno-37-88"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-37-89" name="__codelineno-37-89" href="#__codelineno-37-89"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-37-89" name="__codelineno-37-89" href="#__codelineno-37-89"></a><span class="w"> </span><span class="n">keys</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">-></span><span class="n">key</span><span class="p">;</span>
|
||||||
<a id="__codelineno-37-90" name="__codelineno-37-90" href="#__codelineno-37-90"></a><span class="w"> </span><span class="n">set</span><span class="o">-></span><span class="n">set</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">keys</span><span class="p">;</span>
|
<a id="__codelineno-37-90" name="__codelineno-37-90" href="#__codelineno-37-90"></a><span class="w"> </span><span class="n">index</span><span class="o">++</span><span class="p">;</span>
|
||||||
<a id="__codelineno-37-91" name="__codelineno-37-91" href="#__codelineno-37-91"></a><span class="w"> </span><span class="n">set</span><span class="o">-></span><span class="n">len</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">total</span><span class="p">;</span>
|
<a id="__codelineno-37-91" name="__codelineno-37-91" href="#__codelineno-37-91"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-37-92" name="__codelineno-37-92" href="#__codelineno-37-92"></a><span class="p">}</span>
|
<a id="__codelineno-37-92" name="__codelineno-37-92" href="#__codelineno-37-92"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-37-93" name="__codelineno-37-93" href="#__codelineno-37-93"></a>
|
<a id="__codelineno-37-93" name="__codelineno-37-93" href="#__codelineno-37-93"></a><span class="w"> </span><span class="n">set</span><span class="o">-></span><span class="n">set</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">keys</span><span class="p">;</span>
|
||||||
<a id="__codelineno-37-94" name="__codelineno-37-94" href="#__codelineno-37-94"></a><span class="cm">/* 获取所有值 */</span>
|
<a id="__codelineno-37-94" name="__codelineno-37-94" href="#__codelineno-37-94"></a><span class="w"> </span><span class="n">set</span><span class="o">-></span><span class="n">len</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">total</span><span class="p">;</span>
|
||||||
<a id="__codelineno-37-95" name="__codelineno-37-95" href="#__codelineno-37-95"></a><span class="kt">void</span><span class="w"> </span><span class="nf">valueSet</span><span class="p">(</span><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">hmap</span><span class="p">,</span><span class="w"> </span><span class="n">MapSet</span><span class="w"> </span><span class="o">*</span><span class="n">set</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-37-95" name="__codelineno-37-95" href="#__codelineno-37-95"></a><span class="p">}</span>
|
||||||
<a id="__codelineno-37-96" name="__codelineno-37-96" href="#__codelineno-37-96"></a><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="o">**</span><span class="n">vals</span><span class="p">;</span>
|
<a id="__codelineno-37-96" name="__codelineno-37-96" href="#__codelineno-37-96"></a>
|
||||||
<a id="__codelineno-37-97" name="__codelineno-37-97" href="#__codelineno-37-97"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
<a id="__codelineno-37-97" name="__codelineno-37-97" href="#__codelineno-37-97"></a><span class="cm">/* 获取所有值 */</span>
|
||||||
<a id="__codelineno-37-98" name="__codelineno-37-98" href="#__codelineno-37-98"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">total</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
<a id="__codelineno-37-98" name="__codelineno-37-98" href="#__codelineno-37-98"></a><span class="kt">void</span><span class="w"> </span><span class="nf">valueSet</span><span class="p">(</span><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">hmap</span><span class="p">,</span><span class="w"> </span><span class="n">MapSet</span><span class="w"> </span><span class="o">*</span><span class="n">set</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-37-99" name="__codelineno-37-99" href="#__codelineno-37-99"></a><span class="w"> </span><span class="cm">/* 统计有效键值对数量 */</span>
|
<a id="__codelineno-37-99" name="__codelineno-37-99" href="#__codelineno-37-99"></a><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="o">**</span><span class="n">vals</span><span class="p">;</span>
|
||||||
<a id="__codelineno-37-100" name="__codelineno-37-100" href="#__codelineno-37-100"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">HASHTABLE_CAPACITY</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-37-100" name="__codelineno-37-100" href="#__codelineno-37-100"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-37-101" name="__codelineno-37-101" href="#__codelineno-37-101"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-37-101" name="__codelineno-37-101" href="#__codelineno-37-101"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">total</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-37-102" name="__codelineno-37-102" href="#__codelineno-37-102"></a><span class="w"> </span><span class="n">total</span><span class="o">++</span><span class="p">;</span>
|
<a id="__codelineno-37-102" name="__codelineno-37-102" href="#__codelineno-37-102"></a><span class="w"> </span><span class="cm">/* 统计有效键值对数量 */</span>
|
||||||
<a id="__codelineno-37-103" name="__codelineno-37-103" href="#__codelineno-37-103"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-37-103" name="__codelineno-37-103" href="#__codelineno-37-103"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">MAX_SIZE</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-37-104" name="__codelineno-37-104" href="#__codelineno-37-104"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-37-104" name="__codelineno-37-104" href="#__codelineno-37-104"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-37-105" name="__codelineno-37-105" href="#__codelineno-37-105"></a><span class="w"> </span><span class="n">vals</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="n">total</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="p">));</span>
|
<a id="__codelineno-37-105" name="__codelineno-37-105" href="#__codelineno-37-105"></a><span class="w"> </span><span class="n">total</span><span class="o">++</span><span class="p">;</span>
|
||||||
<a id="__codelineno-37-106" name="__codelineno-37-106" href="#__codelineno-37-106"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">HASHTABLE_CAPACITY</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-37-106" name="__codelineno-37-106" href="#__codelineno-37-106"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-37-107" name="__codelineno-37-107" href="#__codelineno-37-107"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-37-107" name="__codelineno-37-107" href="#__codelineno-37-107"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-37-108" name="__codelineno-37-108" href="#__codelineno-37-108"></a><span class="w"> </span><span class="n">vals</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">-></span><span class="n">val</span><span class="p">;</span>
|
<a id="__codelineno-37-108" name="__codelineno-37-108" href="#__codelineno-37-108"></a><span class="w"> </span><span class="n">vals</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="n">total</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="p">));</span>
|
||||||
<a id="__codelineno-37-109" name="__codelineno-37-109" href="#__codelineno-37-109"></a><span class="w"> </span><span class="n">index</span><span class="o">++</span><span class="p">;</span>
|
<a id="__codelineno-37-109" name="__codelineno-37-109" href="#__codelineno-37-109"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">MAX_SIZE</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-37-110" name="__codelineno-37-110" href="#__codelineno-37-110"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-37-110" name="__codelineno-37-110" href="#__codelineno-37-110"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-37-111" name="__codelineno-37-111" href="#__codelineno-37-111"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-37-111" name="__codelineno-37-111" href="#__codelineno-37-111"></a><span class="w"> </span><span class="n">vals</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">-></span><span class="n">val</span><span class="p">;</span>
|
||||||
<a id="__codelineno-37-112" name="__codelineno-37-112" href="#__codelineno-37-112"></a><span class="w"> </span><span class="n">set</span><span class="o">-></span><span class="n">set</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">vals</span><span class="p">;</span>
|
<a id="__codelineno-37-112" name="__codelineno-37-112" href="#__codelineno-37-112"></a><span class="w"> </span><span class="n">index</span><span class="o">++</span><span class="p">;</span>
|
||||||
<a id="__codelineno-37-113" name="__codelineno-37-113" href="#__codelineno-37-113"></a><span class="w"> </span><span class="n">set</span><span class="o">-></span><span class="n">len</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">total</span><span class="p">;</span>
|
<a id="__codelineno-37-113" name="__codelineno-37-113" href="#__codelineno-37-113"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-37-114" name="__codelineno-37-114" href="#__codelineno-37-114"></a><span class="p">}</span>
|
<a id="__codelineno-37-114" name="__codelineno-37-114" href="#__codelineno-37-114"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-37-115" name="__codelineno-37-115" href="#__codelineno-37-115"></a>
|
<a id="__codelineno-37-115" name="__codelineno-37-115" href="#__codelineno-37-115"></a><span class="w"> </span><span class="n">set</span><span class="o">-></span><span class="n">set</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">vals</span><span class="p">;</span>
|
||||||
<a id="__codelineno-37-116" name="__codelineno-37-116" href="#__codelineno-37-116"></a><span class="cm">/* 打印哈希表 */</span>
|
<a id="__codelineno-37-116" name="__codelineno-37-116" href="#__codelineno-37-116"></a><span class="w"> </span><span class="n">set</span><span class="o">-></span><span class="n">len</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">total</span><span class="p">;</span>
|
||||||
<a id="__codelineno-37-117" name="__codelineno-37-117" href="#__codelineno-37-117"></a><span class="kt">void</span><span class="w"> </span><span class="nf">print</span><span class="p">(</span><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">hmap</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-37-117" name="__codelineno-37-117" href="#__codelineno-37-117"></a><span class="p">}</span>
|
||||||
<a id="__codelineno-37-118" name="__codelineno-37-118" href="#__codelineno-37-118"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="p">;</span>
|
<a id="__codelineno-37-118" name="__codelineno-37-118" href="#__codelineno-37-118"></a>
|
||||||
<a id="__codelineno-37-119" name="__codelineno-37-119" href="#__codelineno-37-119"></a><span class="w"> </span><span class="n">MapSet</span><span class="w"> </span><span class="n">set</span><span class="p">;</span>
|
<a id="__codelineno-37-119" name="__codelineno-37-119" href="#__codelineno-37-119"></a><span class="cm">/* 打印哈希表 */</span>
|
||||||
<a id="__codelineno-37-120" name="__codelineno-37-120" href="#__codelineno-37-120"></a><span class="w"> </span><span class="n">pairSet</span><span class="p">(</span><span class="n">hmap</span><span class="p">,</span><span class="w"> </span><span class="o">&</span><span class="n">set</span><span class="p">);</span>
|
<a id="__codelineno-37-120" name="__codelineno-37-120" href="#__codelineno-37-120"></a><span class="kt">void</span><span class="w"> </span><span class="nf">print</span><span class="p">(</span><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">hmap</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-37-121" name="__codelineno-37-121" href="#__codelineno-37-121"></a><span class="w"> </span><span class="n">Pair</span><span class="w"> </span><span class="o">*</span><span class="n">entries</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="n">Pair</span><span class="w"> </span><span class="o">*</span><span class="p">)</span><span class="n">set</span><span class="p">.</span><span class="n">set</span><span class="p">;</span>
|
<a id="__codelineno-37-121" name="__codelineno-37-121" href="#__codelineno-37-121"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="p">;</span>
|
||||||
<a id="__codelineno-37-122" name="__codelineno-37-122" href="#__codelineno-37-122"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">set</span><span class="p">.</span><span class="n">len</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-37-122" name="__codelineno-37-122" href="#__codelineno-37-122"></a><span class="w"> </span><span class="n">MapSet</span><span class="w"> </span><span class="n">set</span><span class="p">;</span>
|
||||||
<a id="__codelineno-37-123" name="__codelineno-37-123" href="#__codelineno-37-123"></a><span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="s">"%d -> %s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span><span class="w"> </span><span class="n">entries</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">key</span><span class="p">,</span><span class="w"> </span><span class="n">entries</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">val</span><span class="p">);</span>
|
<a id="__codelineno-37-123" name="__codelineno-37-123" href="#__codelineno-37-123"></a><span class="w"> </span><span class="n">pairSet</span><span class="p">(</span><span class="n">hmap</span><span class="p">,</span><span class="w"> </span><span class="o">&</span><span class="n">set</span><span class="p">);</span>
|
||||||
<a id="__codelineno-37-124" name="__codelineno-37-124" href="#__codelineno-37-124"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-37-124" name="__codelineno-37-124" href="#__codelineno-37-124"></a><span class="w"> </span><span class="n">Pair</span><span class="w"> </span><span class="o">*</span><span class="n">entries</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="n">Pair</span><span class="w"> </span><span class="o">*</span><span class="p">)</span><span class="n">set</span><span class="p">.</span><span class="n">set</span><span class="p">;</span>
|
||||||
<a id="__codelineno-37-125" name="__codelineno-37-125" href="#__codelineno-37-125"></a><span class="w"> </span><span class="n">free</span><span class="p">(</span><span class="n">set</span><span class="p">.</span><span class="n">set</span><span class="p">);</span>
|
<a id="__codelineno-37-125" name="__codelineno-37-125" href="#__codelineno-37-125"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">set</span><span class="p">.</span><span class="n">len</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-37-126" name="__codelineno-37-126" href="#__codelineno-37-126"></a><span class="p">}</span>
|
<a id="__codelineno-37-126" name="__codelineno-37-126" href="#__codelineno-37-126"></a><span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="s">"%d -> %s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span><span class="w"> </span><span class="n">entries</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">key</span><span class="p">,</span><span class="w"> </span><span class="n">entries</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">val</span><span class="p">);</span>
|
||||||
|
<a id="__codelineno-37-127" name="__codelineno-37-127" href="#__codelineno-37-127"></a><span class="w"> </span><span class="p">}</span>
|
||||||
|
<a id="__codelineno-37-128" name="__codelineno-37-128" href="#__codelineno-37-128"></a><span class="w"> </span><span class="n">free</span><span class="p">(</span><span class="n">set</span><span class="p">.</span><span class="n">set</span><span class="p">);</span>
|
||||||
|
<a id="__codelineno-37-129" name="__codelineno-37-129" href="#__codelineno-37-129"></a><span class="p">}</span>
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
|
|
File diff suppressed because one or more lines are too long
Binary file not shown.
File diff suppressed because one or more lines are too long
BIN
sitemap.xml.gz
BIN
sitemap.xml.gz
Binary file not shown.
|
@ -5009,124 +5009,127 @@
|
||||||
<a id="__codelineno-39-6" name="__codelineno-39-6" href="#__codelineno-39-6"></a>
|
<a id="__codelineno-39-6" name="__codelineno-39-6" href="#__codelineno-39-6"></a>
|
||||||
<a id="__codelineno-39-7" name="__codelineno-39-7" href="#__codelineno-39-7"></a><span class="cm">/* 基於陣列實現的雜湊表 */</span>
|
<a id="__codelineno-39-7" name="__codelineno-39-7" href="#__codelineno-39-7"></a><span class="cm">/* 基於陣列實現的雜湊表 */</span>
|
||||||
<a id="__codelineno-39-8" name="__codelineno-39-8" href="#__codelineno-39-8"></a><span class="k">typedef</span><span class="w"> </span><span class="k">struct</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-39-8" name="__codelineno-39-8" href="#__codelineno-39-8"></a><span class="k">typedef</span><span class="w"> </span><span class="k">struct</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-39-9" name="__codelineno-39-9" href="#__codelineno-39-9"></a><span class="w"> </span><span class="n">Pair</span><span class="w"> </span><span class="o">*</span><span class="n">buckets</span><span class="p">[</span><span class="n">HASHTABLE_CAPACITY</span><span class="p">];</span>
|
<a id="__codelineno-39-9" name="__codelineno-39-9" href="#__codelineno-39-9"></a><span class="w"> </span><span class="n">Pair</span><span class="w"> </span><span class="o">*</span><span class="n">buckets</span><span class="p">[</span><span class="n">MAX_SIZE</span><span class="p">];</span>
|
||||||
<a id="__codelineno-39-10" name="__codelineno-39-10" href="#__codelineno-39-10"></a><span class="p">}</span><span class="w"> </span><span class="n">ArrayHashMap</span><span class="p">;</span>
|
<a id="__codelineno-39-10" name="__codelineno-39-10" href="#__codelineno-39-10"></a><span class="p">}</span><span class="w"> </span><span class="n">ArrayHashMap</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-11" name="__codelineno-39-11" href="#__codelineno-39-11"></a>
|
<a id="__codelineno-39-11" name="__codelineno-39-11" href="#__codelineno-39-11"></a>
|
||||||
<a id="__codelineno-39-12" name="__codelineno-39-12" href="#__codelineno-39-12"></a><span class="cm">/* 建構子 */</span>
|
<a id="__codelineno-39-12" name="__codelineno-39-12" href="#__codelineno-39-12"></a><span class="cm">/* 建構子 */</span>
|
||||||
<a id="__codelineno-39-13" name="__codelineno-39-13" href="#__codelineno-39-13"></a><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="nf">newArrayHashMap</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-39-13" name="__codelineno-39-13" href="#__codelineno-39-13"></a><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="nf">newArrayHashMap</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-39-14" name="__codelineno-39-14" href="#__codelineno-39-14"></a><span class="w"> </span><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">hmap</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="n">ArrayHashMap</span><span class="p">));</span>
|
<a id="__codelineno-39-14" name="__codelineno-39-14" href="#__codelineno-39-14"></a><span class="w"> </span><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">hmap</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="n">ArrayHashMap</span><span class="p">));</span>
|
||||||
<a id="__codelineno-39-15" name="__codelineno-39-15" href="#__codelineno-39-15"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">hmap</span><span class="p">;</span>
|
<a id="__codelineno-39-15" name="__codelineno-39-15" href="#__codelineno-39-15"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">MAX_SIZE</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-39-16" name="__codelineno-39-16" href="#__codelineno-39-16"></a><span class="p">}</span>
|
<a id="__codelineno-39-16" name="__codelineno-39-16" href="#__codelineno-39-16"></a><span class="w"> </span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-17" name="__codelineno-39-17" href="#__codelineno-39-17"></a>
|
<a id="__codelineno-39-17" name="__codelineno-39-17" href="#__codelineno-39-17"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-39-18" name="__codelineno-39-18" href="#__codelineno-39-18"></a><span class="cm">/* 析構函式 */</span>
|
<a id="__codelineno-39-18" name="__codelineno-39-18" href="#__codelineno-39-18"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">hmap</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-19" name="__codelineno-39-19" href="#__codelineno-39-19"></a><span class="kt">void</span><span class="w"> </span><span class="nf">delArrayHashMap</span><span class="p">(</span><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">hmap</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-39-19" name="__codelineno-39-19" href="#__codelineno-39-19"></a><span class="p">}</span>
|
||||||
<a id="__codelineno-39-20" name="__codelineno-39-20" href="#__codelineno-39-20"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">HASHTABLE_CAPACITY</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-39-20" name="__codelineno-39-20" href="#__codelineno-39-20"></a>
|
||||||
<a id="__codelineno-39-21" name="__codelineno-39-21" href="#__codelineno-39-21"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-39-21" name="__codelineno-39-21" href="#__codelineno-39-21"></a><span class="cm">/* 析構函式 */</span>
|
||||||
<a id="__codelineno-39-22" name="__codelineno-39-22" href="#__codelineno-39-22"></a><span class="w"> </span><span class="n">free</span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">-></span><span class="n">val</span><span class="p">);</span>
|
<a id="__codelineno-39-22" name="__codelineno-39-22" href="#__codelineno-39-22"></a><span class="kt">void</span><span class="w"> </span><span class="nf">delArrayHashMap</span><span class="p">(</span><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">hmap</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-39-23" name="__codelineno-39-23" href="#__codelineno-39-23"></a><span class="w"> </span><span class="n">free</span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]);</span>
|
<a id="__codelineno-39-23" name="__codelineno-39-23" href="#__codelineno-39-23"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">MAX_SIZE</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-39-24" name="__codelineno-39-24" href="#__codelineno-39-24"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-39-24" name="__codelineno-39-24" href="#__codelineno-39-24"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-39-25" name="__codelineno-39-25" href="#__codelineno-39-25"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-39-25" name="__codelineno-39-25" href="#__codelineno-39-25"></a><span class="w"> </span><span class="n">free</span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">-></span><span class="n">val</span><span class="p">);</span>
|
||||||
<a id="__codelineno-39-26" name="__codelineno-39-26" href="#__codelineno-39-26"></a><span class="w"> </span><span class="n">free</span><span class="p">(</span><span class="n">hmap</span><span class="p">);</span>
|
<a id="__codelineno-39-26" name="__codelineno-39-26" href="#__codelineno-39-26"></a><span class="w"> </span><span class="n">free</span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]);</span>
|
||||||
<a id="__codelineno-39-27" name="__codelineno-39-27" href="#__codelineno-39-27"></a><span class="p">}</span>
|
<a id="__codelineno-39-27" name="__codelineno-39-27" href="#__codelineno-39-27"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-39-28" name="__codelineno-39-28" href="#__codelineno-39-28"></a>
|
<a id="__codelineno-39-28" name="__codelineno-39-28" href="#__codelineno-39-28"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-39-29" name="__codelineno-39-29" href="#__codelineno-39-29"></a><span class="cm">/* 新增操作 */</span>
|
<a id="__codelineno-39-29" name="__codelineno-39-29" href="#__codelineno-39-29"></a><span class="w"> </span><span class="n">free</span><span class="p">(</span><span class="n">hmap</span><span class="p">);</span>
|
||||||
<a id="__codelineno-39-30" name="__codelineno-39-30" href="#__codelineno-39-30"></a><span class="kt">void</span><span class="w"> </span><span class="nf">put</span><span class="p">(</span><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">hmap</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">key</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="n">val</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-39-30" name="__codelineno-39-30" href="#__codelineno-39-30"></a><span class="p">}</span>
|
||||||
<a id="__codelineno-39-31" name="__codelineno-39-31" href="#__codelineno-39-31"></a><span class="w"> </span><span class="n">Pair</span><span class="w"> </span><span class="o">*</span><span class="n">Pair</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="n">Pair</span><span class="p">));</span>
|
<a id="__codelineno-39-31" name="__codelineno-39-31" href="#__codelineno-39-31"></a>
|
||||||
<a id="__codelineno-39-32" name="__codelineno-39-32" href="#__codelineno-39-32"></a><span class="w"> </span><span class="n">Pair</span><span class="o">-></span><span class="n">key</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">key</span><span class="p">;</span>
|
<a id="__codelineno-39-32" name="__codelineno-39-32" href="#__codelineno-39-32"></a><span class="cm">/* 新增操作 */</span>
|
||||||
<a id="__codelineno-39-33" name="__codelineno-39-33" href="#__codelineno-39-33"></a><span class="w"> </span><span class="n">Pair</span><span class="o">-></span><span class="n">val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="n">strlen</span><span class="p">(</span><span class="n">val</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">);</span>
|
<a id="__codelineno-39-33" name="__codelineno-39-33" href="#__codelineno-39-33"></a><span class="kt">void</span><span class="w"> </span><span class="nf">put</span><span class="p">(</span><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">hmap</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">key</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="n">val</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-39-34" name="__codelineno-39-34" href="#__codelineno-39-34"></a><span class="w"> </span><span class="n">strcpy</span><span class="p">(</span><span class="n">Pair</span><span class="o">-></span><span class="n">val</span><span class="p">,</span><span class="w"> </span><span class="n">val</span><span class="p">);</span>
|
<a id="__codelineno-39-34" name="__codelineno-39-34" href="#__codelineno-39-34"></a><span class="w"> </span><span class="n">Pair</span><span class="w"> </span><span class="o">*</span><span class="n">Pair</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="n">Pair</span><span class="p">));</span>
|
||||||
<a id="__codelineno-39-35" name="__codelineno-39-35" href="#__codelineno-39-35"></a>
|
<a id="__codelineno-39-35" name="__codelineno-39-35" href="#__codelineno-39-35"></a><span class="w"> </span><span class="n">Pair</span><span class="o">-></span><span class="n">key</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">key</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-36" name="__codelineno-39-36" href="#__codelineno-39-36"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">hashFunc</span><span class="p">(</span><span class="n">key</span><span class="p">);</span>
|
<a id="__codelineno-39-36" name="__codelineno-39-36" href="#__codelineno-39-36"></a><span class="w"> </span><span class="n">Pair</span><span class="o">-></span><span class="n">val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="n">strlen</span><span class="p">(</span><span class="n">val</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">);</span>
|
||||||
<a id="__codelineno-39-37" name="__codelineno-39-37" href="#__codelineno-39-37"></a><span class="w"> </span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Pair</span><span class="p">;</span>
|
<a id="__codelineno-39-37" name="__codelineno-39-37" href="#__codelineno-39-37"></a><span class="w"> </span><span class="n">strcpy</span><span class="p">(</span><span class="n">Pair</span><span class="o">-></span><span class="n">val</span><span class="p">,</span><span class="w"> </span><span class="n">val</span><span class="p">);</span>
|
||||||
<a id="__codelineno-39-38" name="__codelineno-39-38" href="#__codelineno-39-38"></a><span class="p">}</span>
|
<a id="__codelineno-39-38" name="__codelineno-39-38" href="#__codelineno-39-38"></a>
|
||||||
<a id="__codelineno-39-39" name="__codelineno-39-39" href="#__codelineno-39-39"></a>
|
<a id="__codelineno-39-39" name="__codelineno-39-39" href="#__codelineno-39-39"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">hashFunc</span><span class="p">(</span><span class="n">key</span><span class="p">);</span>
|
||||||
<a id="__codelineno-39-40" name="__codelineno-39-40" href="#__codelineno-39-40"></a><span class="cm">/* 刪除操作 */</span>
|
<a id="__codelineno-39-40" name="__codelineno-39-40" href="#__codelineno-39-40"></a><span class="w"> </span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Pair</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-41" name="__codelineno-39-41" href="#__codelineno-39-41"></a><span class="kt">void</span><span class="w"> </span><span class="nf">removeItem</span><span class="p">(</span><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">hmap</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">key</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-39-41" name="__codelineno-39-41" href="#__codelineno-39-41"></a><span class="p">}</span>
|
||||||
<a id="__codelineno-39-42" name="__codelineno-39-42" href="#__codelineno-39-42"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">hashFunc</span><span class="p">(</span><span class="n">key</span><span class="p">);</span>
|
<a id="__codelineno-39-42" name="__codelineno-39-42" href="#__codelineno-39-42"></a>
|
||||||
<a id="__codelineno-39-43" name="__codelineno-39-43" href="#__codelineno-39-43"></a><span class="w"> </span><span class="n">free</span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="o">-></span><span class="n">val</span><span class="p">);</span>
|
<a id="__codelineno-39-43" name="__codelineno-39-43" href="#__codelineno-39-43"></a><span class="cm">/* 刪除操作 */</span>
|
||||||
<a id="__codelineno-39-44" name="__codelineno-39-44" href="#__codelineno-39-44"></a><span class="w"> </span><span class="n">free</span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">index</span><span class="p">]);</span>
|
<a id="__codelineno-39-44" name="__codelineno-39-44" href="#__codelineno-39-44"></a><span class="kt">void</span><span class="w"> </span><span class="nf">removeItem</span><span class="p">(</span><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">hmap</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">key</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-39-45" name="__codelineno-39-45" href="#__codelineno-39-45"></a><span class="w"> </span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">;</span>
|
<a id="__codelineno-39-45" name="__codelineno-39-45" href="#__codelineno-39-45"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">hashFunc</span><span class="p">(</span><span class="n">key</span><span class="p">);</span>
|
||||||
<a id="__codelineno-39-46" name="__codelineno-39-46" href="#__codelineno-39-46"></a><span class="p">}</span>
|
<a id="__codelineno-39-46" name="__codelineno-39-46" href="#__codelineno-39-46"></a><span class="w"> </span><span class="n">free</span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="o">-></span><span class="n">val</span><span class="p">);</span>
|
||||||
<a id="__codelineno-39-47" name="__codelineno-39-47" href="#__codelineno-39-47"></a>
|
<a id="__codelineno-39-47" name="__codelineno-39-47" href="#__codelineno-39-47"></a><span class="w"> </span><span class="n">free</span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">index</span><span class="p">]);</span>
|
||||||
<a id="__codelineno-39-48" name="__codelineno-39-48" href="#__codelineno-39-48"></a><span class="cm">/* 獲取所有鍵值對 */</span>
|
<a id="__codelineno-39-48" name="__codelineno-39-48" href="#__codelineno-39-48"></a><span class="w"> </span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-49" name="__codelineno-39-49" href="#__codelineno-39-49"></a><span class="kt">void</span><span class="w"> </span><span class="nf">pairSet</span><span class="p">(</span><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">hmap</span><span class="p">,</span><span class="w"> </span><span class="n">MapSet</span><span class="w"> </span><span class="o">*</span><span class="n">set</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-39-49" name="__codelineno-39-49" href="#__codelineno-39-49"></a><span class="p">}</span>
|
||||||
<a id="__codelineno-39-50" name="__codelineno-39-50" href="#__codelineno-39-50"></a><span class="w"> </span><span class="n">Pair</span><span class="w"> </span><span class="o">*</span><span class="n">entries</span><span class="p">;</span>
|
<a id="__codelineno-39-50" name="__codelineno-39-50" href="#__codelineno-39-50"></a>
|
||||||
<a id="__codelineno-39-51" name="__codelineno-39-51" href="#__codelineno-39-51"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
<a id="__codelineno-39-51" name="__codelineno-39-51" href="#__codelineno-39-51"></a><span class="cm">/* 獲取所有鍵值對 */</span>
|
||||||
<a id="__codelineno-39-52" name="__codelineno-39-52" href="#__codelineno-39-52"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">total</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
<a id="__codelineno-39-52" name="__codelineno-39-52" href="#__codelineno-39-52"></a><span class="kt">void</span><span class="w"> </span><span class="nf">pairSet</span><span class="p">(</span><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">hmap</span><span class="p">,</span><span class="w"> </span><span class="n">MapSet</span><span class="w"> </span><span class="o">*</span><span class="n">set</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-39-53" name="__codelineno-39-53" href="#__codelineno-39-53"></a><span class="w"> </span><span class="cm">/* 統計有效鍵值對數量 */</span>
|
<a id="__codelineno-39-53" name="__codelineno-39-53" href="#__codelineno-39-53"></a><span class="w"> </span><span class="n">Pair</span><span class="w"> </span><span class="o">*</span><span class="n">entries</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-54" name="__codelineno-39-54" href="#__codelineno-39-54"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">HASHTABLE_CAPACITY</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-39-54" name="__codelineno-39-54" href="#__codelineno-39-54"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-55" name="__codelineno-39-55" href="#__codelineno-39-55"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-39-55" name="__codelineno-39-55" href="#__codelineno-39-55"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">total</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-56" name="__codelineno-39-56" href="#__codelineno-39-56"></a><span class="w"> </span><span class="n">total</span><span class="o">++</span><span class="p">;</span>
|
<a id="__codelineno-39-56" name="__codelineno-39-56" href="#__codelineno-39-56"></a><span class="w"> </span><span class="cm">/* 統計有效鍵值對數量 */</span>
|
||||||
<a id="__codelineno-39-57" name="__codelineno-39-57" href="#__codelineno-39-57"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-39-57" name="__codelineno-39-57" href="#__codelineno-39-57"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">MAX_SIZE</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-39-58" name="__codelineno-39-58" href="#__codelineno-39-58"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-39-58" name="__codelineno-39-58" href="#__codelineno-39-58"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-39-59" name="__codelineno-39-59" href="#__codelineno-39-59"></a><span class="w"> </span><span class="n">entries</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="n">Pair</span><span class="p">)</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">total</span><span class="p">);</span>
|
<a id="__codelineno-39-59" name="__codelineno-39-59" href="#__codelineno-39-59"></a><span class="w"> </span><span class="n">total</span><span class="o">++</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-60" name="__codelineno-39-60" href="#__codelineno-39-60"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">HASHTABLE_CAPACITY</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-39-60" name="__codelineno-39-60" href="#__codelineno-39-60"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-39-61" name="__codelineno-39-61" href="#__codelineno-39-61"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-39-61" name="__codelineno-39-61" href="#__codelineno-39-61"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-39-62" name="__codelineno-39-62" href="#__codelineno-39-62"></a><span class="w"> </span><span class="n">entries</span><span class="p">[</span><span class="n">index</span><span class="p">].</span><span class="n">key</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">-></span><span class="n">key</span><span class="p">;</span>
|
<a id="__codelineno-39-62" name="__codelineno-39-62" href="#__codelineno-39-62"></a><span class="w"> </span><span class="n">entries</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="n">Pair</span><span class="p">)</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">total</span><span class="p">);</span>
|
||||||
<a id="__codelineno-39-63" name="__codelineno-39-63" href="#__codelineno-39-63"></a><span class="w"> </span><span class="n">entries</span><span class="p">[</span><span class="n">index</span><span class="p">].</span><span class="n">val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="n">strlen</span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">-></span><span class="n">val</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">);</span>
|
<a id="__codelineno-39-63" name="__codelineno-39-63" href="#__codelineno-39-63"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">MAX_SIZE</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-39-64" name="__codelineno-39-64" href="#__codelineno-39-64"></a><span class="w"> </span><span class="n">strcpy</span><span class="p">(</span><span class="n">entries</span><span class="p">[</span><span class="n">index</span><span class="p">].</span><span class="n">val</span><span class="p">,</span><span class="w"> </span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">-></span><span class="n">val</span><span class="p">);</span>
|
<a id="__codelineno-39-64" name="__codelineno-39-64" href="#__codelineno-39-64"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-39-65" name="__codelineno-39-65" href="#__codelineno-39-65"></a><span class="w"> </span><span class="n">index</span><span class="o">++</span><span class="p">;</span>
|
<a id="__codelineno-39-65" name="__codelineno-39-65" href="#__codelineno-39-65"></a><span class="w"> </span><span class="n">entries</span><span class="p">[</span><span class="n">index</span><span class="p">].</span><span class="n">key</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">-></span><span class="n">key</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-66" name="__codelineno-39-66" href="#__codelineno-39-66"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-39-66" name="__codelineno-39-66" href="#__codelineno-39-66"></a><span class="w"> </span><span class="n">entries</span><span class="p">[</span><span class="n">index</span><span class="p">].</span><span class="n">val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="n">strlen</span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">-></span><span class="n">val</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">);</span>
|
||||||
<a id="__codelineno-39-67" name="__codelineno-39-67" href="#__codelineno-39-67"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-39-67" name="__codelineno-39-67" href="#__codelineno-39-67"></a><span class="w"> </span><span class="n">strcpy</span><span class="p">(</span><span class="n">entries</span><span class="p">[</span><span class="n">index</span><span class="p">].</span><span class="n">val</span><span class="p">,</span><span class="w"> </span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">-></span><span class="n">val</span><span class="p">);</span>
|
||||||
<a id="__codelineno-39-68" name="__codelineno-39-68" href="#__codelineno-39-68"></a><span class="w"> </span><span class="n">set</span><span class="o">-></span><span class="n">set</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">entries</span><span class="p">;</span>
|
<a id="__codelineno-39-68" name="__codelineno-39-68" href="#__codelineno-39-68"></a><span class="w"> </span><span class="n">index</span><span class="o">++</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-69" name="__codelineno-39-69" href="#__codelineno-39-69"></a><span class="w"> </span><span class="n">set</span><span class="o">-></span><span class="n">len</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">total</span><span class="p">;</span>
|
<a id="__codelineno-39-69" name="__codelineno-39-69" href="#__codelineno-39-69"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-39-70" name="__codelineno-39-70" href="#__codelineno-39-70"></a><span class="p">}</span>
|
<a id="__codelineno-39-70" name="__codelineno-39-70" href="#__codelineno-39-70"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-39-71" name="__codelineno-39-71" href="#__codelineno-39-71"></a>
|
<a id="__codelineno-39-71" name="__codelineno-39-71" href="#__codelineno-39-71"></a><span class="w"> </span><span class="n">set</span><span class="o">-></span><span class="n">set</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">entries</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-72" name="__codelineno-39-72" href="#__codelineno-39-72"></a><span class="cm">/* 獲取所有鍵 */</span>
|
<a id="__codelineno-39-72" name="__codelineno-39-72" href="#__codelineno-39-72"></a><span class="w"> </span><span class="n">set</span><span class="o">-></span><span class="n">len</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">total</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-73" name="__codelineno-39-73" href="#__codelineno-39-73"></a><span class="kt">void</span><span class="w"> </span><span class="nf">keySet</span><span class="p">(</span><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">hmap</span><span class="p">,</span><span class="w"> </span><span class="n">MapSet</span><span class="w"> </span><span class="o">*</span><span class="n">set</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-39-73" name="__codelineno-39-73" href="#__codelineno-39-73"></a><span class="p">}</span>
|
||||||
<a id="__codelineno-39-74" name="__codelineno-39-74" href="#__codelineno-39-74"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="o">*</span><span class="n">keys</span><span class="p">;</span>
|
<a id="__codelineno-39-74" name="__codelineno-39-74" href="#__codelineno-39-74"></a>
|
||||||
<a id="__codelineno-39-75" name="__codelineno-39-75" href="#__codelineno-39-75"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
<a id="__codelineno-39-75" name="__codelineno-39-75" href="#__codelineno-39-75"></a><span class="cm">/* 獲取所有鍵 */</span>
|
||||||
<a id="__codelineno-39-76" name="__codelineno-39-76" href="#__codelineno-39-76"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">total</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
<a id="__codelineno-39-76" name="__codelineno-39-76" href="#__codelineno-39-76"></a><span class="kt">void</span><span class="w"> </span><span class="nf">keySet</span><span class="p">(</span><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">hmap</span><span class="p">,</span><span class="w"> </span><span class="n">MapSet</span><span class="w"> </span><span class="o">*</span><span class="n">set</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-39-77" name="__codelineno-39-77" href="#__codelineno-39-77"></a><span class="w"> </span><span class="cm">/* 統計有效鍵值對數量 */</span>
|
<a id="__codelineno-39-77" name="__codelineno-39-77" href="#__codelineno-39-77"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="o">*</span><span class="n">keys</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-78" name="__codelineno-39-78" href="#__codelineno-39-78"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">HASHTABLE_CAPACITY</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-39-78" name="__codelineno-39-78" href="#__codelineno-39-78"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-79" name="__codelineno-39-79" href="#__codelineno-39-79"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-39-79" name="__codelineno-39-79" href="#__codelineno-39-79"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">total</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-80" name="__codelineno-39-80" href="#__codelineno-39-80"></a><span class="w"> </span><span class="n">total</span><span class="o">++</span><span class="p">;</span>
|
<a id="__codelineno-39-80" name="__codelineno-39-80" href="#__codelineno-39-80"></a><span class="w"> </span><span class="cm">/* 統計有效鍵值對數量 */</span>
|
||||||
<a id="__codelineno-39-81" name="__codelineno-39-81" href="#__codelineno-39-81"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-39-81" name="__codelineno-39-81" href="#__codelineno-39-81"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">MAX_SIZE</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-39-82" name="__codelineno-39-82" href="#__codelineno-39-82"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-39-82" name="__codelineno-39-82" href="#__codelineno-39-82"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-39-83" name="__codelineno-39-83" href="#__codelineno-39-83"></a><span class="w"> </span><span class="n">keys</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="n">total</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="kt">int</span><span class="p">));</span>
|
<a id="__codelineno-39-83" name="__codelineno-39-83" href="#__codelineno-39-83"></a><span class="w"> </span><span class="n">total</span><span class="o">++</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-84" name="__codelineno-39-84" href="#__codelineno-39-84"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">HASHTABLE_CAPACITY</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-39-84" name="__codelineno-39-84" href="#__codelineno-39-84"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-39-85" name="__codelineno-39-85" href="#__codelineno-39-85"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-39-85" name="__codelineno-39-85" href="#__codelineno-39-85"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-39-86" name="__codelineno-39-86" href="#__codelineno-39-86"></a><span class="w"> </span><span class="n">keys</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">-></span><span class="n">key</span><span class="p">;</span>
|
<a id="__codelineno-39-86" name="__codelineno-39-86" href="#__codelineno-39-86"></a><span class="w"> </span><span class="n">keys</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="n">total</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="kt">int</span><span class="p">));</span>
|
||||||
<a id="__codelineno-39-87" name="__codelineno-39-87" href="#__codelineno-39-87"></a><span class="w"> </span><span class="n">index</span><span class="o">++</span><span class="p">;</span>
|
<a id="__codelineno-39-87" name="__codelineno-39-87" href="#__codelineno-39-87"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">MAX_SIZE</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-39-88" name="__codelineno-39-88" href="#__codelineno-39-88"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-39-88" name="__codelineno-39-88" href="#__codelineno-39-88"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-39-89" name="__codelineno-39-89" href="#__codelineno-39-89"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-39-89" name="__codelineno-39-89" href="#__codelineno-39-89"></a><span class="w"> </span><span class="n">keys</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">-></span><span class="n">key</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-90" name="__codelineno-39-90" href="#__codelineno-39-90"></a><span class="w"> </span><span class="n">set</span><span class="o">-></span><span class="n">set</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">keys</span><span class="p">;</span>
|
<a id="__codelineno-39-90" name="__codelineno-39-90" href="#__codelineno-39-90"></a><span class="w"> </span><span class="n">index</span><span class="o">++</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-91" name="__codelineno-39-91" href="#__codelineno-39-91"></a><span class="w"> </span><span class="n">set</span><span class="o">-></span><span class="n">len</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">total</span><span class="p">;</span>
|
<a id="__codelineno-39-91" name="__codelineno-39-91" href="#__codelineno-39-91"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-39-92" name="__codelineno-39-92" href="#__codelineno-39-92"></a><span class="p">}</span>
|
<a id="__codelineno-39-92" name="__codelineno-39-92" href="#__codelineno-39-92"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-39-93" name="__codelineno-39-93" href="#__codelineno-39-93"></a>
|
<a id="__codelineno-39-93" name="__codelineno-39-93" href="#__codelineno-39-93"></a><span class="w"> </span><span class="n">set</span><span class="o">-></span><span class="n">set</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">keys</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-94" name="__codelineno-39-94" href="#__codelineno-39-94"></a><span class="cm">/* 獲取所有值 */</span>
|
<a id="__codelineno-39-94" name="__codelineno-39-94" href="#__codelineno-39-94"></a><span class="w"> </span><span class="n">set</span><span class="o">-></span><span class="n">len</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">total</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-95" name="__codelineno-39-95" href="#__codelineno-39-95"></a><span class="kt">void</span><span class="w"> </span><span class="nf">valueSet</span><span class="p">(</span><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">hmap</span><span class="p">,</span><span class="w"> </span><span class="n">MapSet</span><span class="w"> </span><span class="o">*</span><span class="n">set</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-39-95" name="__codelineno-39-95" href="#__codelineno-39-95"></a><span class="p">}</span>
|
||||||
<a id="__codelineno-39-96" name="__codelineno-39-96" href="#__codelineno-39-96"></a><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="o">**</span><span class="n">vals</span><span class="p">;</span>
|
<a id="__codelineno-39-96" name="__codelineno-39-96" href="#__codelineno-39-96"></a>
|
||||||
<a id="__codelineno-39-97" name="__codelineno-39-97" href="#__codelineno-39-97"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
<a id="__codelineno-39-97" name="__codelineno-39-97" href="#__codelineno-39-97"></a><span class="cm">/* 獲取所有值 */</span>
|
||||||
<a id="__codelineno-39-98" name="__codelineno-39-98" href="#__codelineno-39-98"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">total</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
<a id="__codelineno-39-98" name="__codelineno-39-98" href="#__codelineno-39-98"></a><span class="kt">void</span><span class="w"> </span><span class="nf">valueSet</span><span class="p">(</span><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">hmap</span><span class="p">,</span><span class="w"> </span><span class="n">MapSet</span><span class="w"> </span><span class="o">*</span><span class="n">set</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-39-99" name="__codelineno-39-99" href="#__codelineno-39-99"></a><span class="w"> </span><span class="cm">/* 統計有效鍵值對數量 */</span>
|
<a id="__codelineno-39-99" name="__codelineno-39-99" href="#__codelineno-39-99"></a><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="o">**</span><span class="n">vals</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-100" name="__codelineno-39-100" href="#__codelineno-39-100"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">HASHTABLE_CAPACITY</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-39-100" name="__codelineno-39-100" href="#__codelineno-39-100"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-101" name="__codelineno-39-101" href="#__codelineno-39-101"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-39-101" name="__codelineno-39-101" href="#__codelineno-39-101"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">total</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-102" name="__codelineno-39-102" href="#__codelineno-39-102"></a><span class="w"> </span><span class="n">total</span><span class="o">++</span><span class="p">;</span>
|
<a id="__codelineno-39-102" name="__codelineno-39-102" href="#__codelineno-39-102"></a><span class="w"> </span><span class="cm">/* 統計有效鍵值對數量 */</span>
|
||||||
<a id="__codelineno-39-103" name="__codelineno-39-103" href="#__codelineno-39-103"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-39-103" name="__codelineno-39-103" href="#__codelineno-39-103"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">MAX_SIZE</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-39-104" name="__codelineno-39-104" href="#__codelineno-39-104"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-39-104" name="__codelineno-39-104" href="#__codelineno-39-104"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-39-105" name="__codelineno-39-105" href="#__codelineno-39-105"></a><span class="w"> </span><span class="n">vals</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="n">total</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="p">));</span>
|
<a id="__codelineno-39-105" name="__codelineno-39-105" href="#__codelineno-39-105"></a><span class="w"> </span><span class="n">total</span><span class="o">++</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-106" name="__codelineno-39-106" href="#__codelineno-39-106"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">HASHTABLE_CAPACITY</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-39-106" name="__codelineno-39-106" href="#__codelineno-39-106"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-39-107" name="__codelineno-39-107" href="#__codelineno-39-107"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-39-107" name="__codelineno-39-107" href="#__codelineno-39-107"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-39-108" name="__codelineno-39-108" href="#__codelineno-39-108"></a><span class="w"> </span><span class="n">vals</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">-></span><span class="n">val</span><span class="p">;</span>
|
<a id="__codelineno-39-108" name="__codelineno-39-108" href="#__codelineno-39-108"></a><span class="w"> </span><span class="n">vals</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="n">total</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="p">));</span>
|
||||||
<a id="__codelineno-39-109" name="__codelineno-39-109" href="#__codelineno-39-109"></a><span class="w"> </span><span class="n">index</span><span class="o">++</span><span class="p">;</span>
|
<a id="__codelineno-39-109" name="__codelineno-39-109" href="#__codelineno-39-109"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">MAX_SIZE</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-39-110" name="__codelineno-39-110" href="#__codelineno-39-110"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-39-110" name="__codelineno-39-110" href="#__codelineno-39-110"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-39-111" name="__codelineno-39-111" href="#__codelineno-39-111"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-39-111" name="__codelineno-39-111" href="#__codelineno-39-111"></a><span class="w"> </span><span class="n">vals</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">hmap</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">-></span><span class="n">val</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-112" name="__codelineno-39-112" href="#__codelineno-39-112"></a><span class="w"> </span><span class="n">set</span><span class="o">-></span><span class="n">set</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">vals</span><span class="p">;</span>
|
<a id="__codelineno-39-112" name="__codelineno-39-112" href="#__codelineno-39-112"></a><span class="w"> </span><span class="n">index</span><span class="o">++</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-113" name="__codelineno-39-113" href="#__codelineno-39-113"></a><span class="w"> </span><span class="n">set</span><span class="o">-></span><span class="n">len</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">total</span><span class="p">;</span>
|
<a id="__codelineno-39-113" name="__codelineno-39-113" href="#__codelineno-39-113"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-39-114" name="__codelineno-39-114" href="#__codelineno-39-114"></a><span class="p">}</span>
|
<a id="__codelineno-39-114" name="__codelineno-39-114" href="#__codelineno-39-114"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-39-115" name="__codelineno-39-115" href="#__codelineno-39-115"></a>
|
<a id="__codelineno-39-115" name="__codelineno-39-115" href="#__codelineno-39-115"></a><span class="w"> </span><span class="n">set</span><span class="o">-></span><span class="n">set</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">vals</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-116" name="__codelineno-39-116" href="#__codelineno-39-116"></a><span class="cm">/* 列印雜湊表 */</span>
|
<a id="__codelineno-39-116" name="__codelineno-39-116" href="#__codelineno-39-116"></a><span class="w"> </span><span class="n">set</span><span class="o">-></span><span class="n">len</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">total</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-117" name="__codelineno-39-117" href="#__codelineno-39-117"></a><span class="kt">void</span><span class="w"> </span><span class="nf">print</span><span class="p">(</span><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">hmap</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-39-117" name="__codelineno-39-117" href="#__codelineno-39-117"></a><span class="p">}</span>
|
||||||
<a id="__codelineno-39-118" name="__codelineno-39-118" href="#__codelineno-39-118"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="p">;</span>
|
<a id="__codelineno-39-118" name="__codelineno-39-118" href="#__codelineno-39-118"></a>
|
||||||
<a id="__codelineno-39-119" name="__codelineno-39-119" href="#__codelineno-39-119"></a><span class="w"> </span><span class="n">MapSet</span><span class="w"> </span><span class="n">set</span><span class="p">;</span>
|
<a id="__codelineno-39-119" name="__codelineno-39-119" href="#__codelineno-39-119"></a><span class="cm">/* 列印雜湊表 */</span>
|
||||||
<a id="__codelineno-39-120" name="__codelineno-39-120" href="#__codelineno-39-120"></a><span class="w"> </span><span class="n">pairSet</span><span class="p">(</span><span class="n">hmap</span><span class="p">,</span><span class="w"> </span><span class="o">&</span><span class="n">set</span><span class="p">);</span>
|
<a id="__codelineno-39-120" name="__codelineno-39-120" href="#__codelineno-39-120"></a><span class="kt">void</span><span class="w"> </span><span class="nf">print</span><span class="p">(</span><span class="n">ArrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">hmap</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-39-121" name="__codelineno-39-121" href="#__codelineno-39-121"></a><span class="w"> </span><span class="n">Pair</span><span class="w"> </span><span class="o">*</span><span class="n">entries</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="n">Pair</span><span class="w"> </span><span class="o">*</span><span class="p">)</span><span class="n">set</span><span class="p">.</span><span class="n">set</span><span class="p">;</span>
|
<a id="__codelineno-39-121" name="__codelineno-39-121" href="#__codelineno-39-121"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-122" name="__codelineno-39-122" href="#__codelineno-39-122"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">set</span><span class="p">.</span><span class="n">len</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-39-122" name="__codelineno-39-122" href="#__codelineno-39-122"></a><span class="w"> </span><span class="n">MapSet</span><span class="w"> </span><span class="n">set</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-123" name="__codelineno-39-123" href="#__codelineno-39-123"></a><span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="s">"%d -> %s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span><span class="w"> </span><span class="n">entries</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">key</span><span class="p">,</span><span class="w"> </span><span class="n">entries</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">val</span><span class="p">);</span>
|
<a id="__codelineno-39-123" name="__codelineno-39-123" href="#__codelineno-39-123"></a><span class="w"> </span><span class="n">pairSet</span><span class="p">(</span><span class="n">hmap</span><span class="p">,</span><span class="w"> </span><span class="o">&</span><span class="n">set</span><span class="p">);</span>
|
||||||
<a id="__codelineno-39-124" name="__codelineno-39-124" href="#__codelineno-39-124"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-39-124" name="__codelineno-39-124" href="#__codelineno-39-124"></a><span class="w"> </span><span class="n">Pair</span><span class="w"> </span><span class="o">*</span><span class="n">entries</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="n">Pair</span><span class="w"> </span><span class="o">*</span><span class="p">)</span><span class="n">set</span><span class="p">.</span><span class="n">set</span><span class="p">;</span>
|
||||||
<a id="__codelineno-39-125" name="__codelineno-39-125" href="#__codelineno-39-125"></a><span class="w"> </span><span class="n">free</span><span class="p">(</span><span class="n">set</span><span class="p">.</span><span class="n">set</span><span class="p">);</span>
|
<a id="__codelineno-39-125" name="__codelineno-39-125" href="#__codelineno-39-125"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">set</span><span class="p">.</span><span class="n">len</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-39-126" name="__codelineno-39-126" href="#__codelineno-39-126"></a><span class="p">}</span>
|
<a id="__codelineno-39-126" name="__codelineno-39-126" href="#__codelineno-39-126"></a><span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="s">"%d -> %s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span><span class="w"> </span><span class="n">entries</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">key</span><span class="p">,</span><span class="w"> </span><span class="n">entries</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">val</span><span class="p">);</span>
|
||||||
|
<a id="__codelineno-39-127" name="__codelineno-39-127" href="#__codelineno-39-127"></a><span class="w"> </span><span class="p">}</span>
|
||||||
|
<a id="__codelineno-39-128" name="__codelineno-39-128" href="#__codelineno-39-128"></a><span class="w"> </span><span class="n">free</span><span class="p">(</span><span class="n">set</span><span class="p">.</span><span class="n">set</span><span class="p">);</span>
|
||||||
|
<a id="__codelineno-39-129" name="__codelineno-39-129" href="#__codelineno-39-129"></a><span class="p">}</span>
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
|
|
|
@ -3928,52 +3928,38 @@
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
<div class="highlight"><span class="filename">bucket_sort.c</span><pre><span></span><code><a id="__codelineno-10-1" name="__codelineno-10-1" href="#__codelineno-10-1"></a><span class="cm">/* 桶排序 */</span>
|
<div class="highlight"><span class="filename">bucket_sort.c</span><pre><span></span><code><a id="__codelineno-10-1" name="__codelineno-10-1" href="#__codelineno-10-1"></a><span class="cm">/* 桶排序 */</span>
|
||||||
<a id="__codelineno-10-2" name="__codelineno-10-2" href="#__codelineno-10-2"></a><span class="kt">void</span><span class="w"> </span><span class="nf">bucketSort</span><span class="p">(</span><span class="kt">float</span><span class="w"> </span><span class="n">nums</span><span class="p">[],</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">size</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-10-2" name="__codelineno-10-2" href="#__codelineno-10-2"></a><span class="kt">void</span><span class="w"> </span><span class="nf">bucketSort</span><span class="p">(</span><span class="kt">float</span><span class="w"> </span><span class="n">nums</span><span class="p">[],</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-10-3" name="__codelineno-10-3" href="#__codelineno-10-3"></a><span class="w"> </span><span class="c1">// 初始化 k = n/2 個桶,預期向每個桶分配 2 個元素</span>
|
<a id="__codelineno-10-3" name="__codelineno-10-3" href="#__codelineno-10-3"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">k</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mi">2</span><span class="p">;</span><span class="w"> </span><span class="c1">// 初始化 k = n/2 個桶</span>
|
||||||
<a id="__codelineno-10-4" name="__codelineno-10-4" href="#__codelineno-10-4"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">k</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">size</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mi">2</span><span class="p">;</span>
|
<a id="__codelineno-10-4" name="__codelineno-10-4" href="#__codelineno-10-4"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="o">*</span><span class="n">sizes</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="n">k</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="kt">int</span><span class="p">));</span><span class="w"> </span><span class="c1">// 記錄每個桶的大小</span>
|
||||||
<a id="__codelineno-10-5" name="__codelineno-10-5" href="#__codelineno-10-5"></a><span class="w"> </span><span class="kt">float</span><span class="w"> </span><span class="o">**</span><span class="n">buckets</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">calloc</span><span class="p">(</span><span class="n">k</span><span class="p">,</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="kt">float</span><span class="w"> </span><span class="o">*</span><span class="p">));</span>
|
<a id="__codelineno-10-5" name="__codelineno-10-5" href="#__codelineno-10-5"></a><span class="w"> </span><span class="kt">float</span><span class="w"> </span><span class="o">**</span><span class="n">buckets</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="n">k</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="kt">float</span><span class="w"> </span><span class="o">*</span><span class="p">));</span><span class="w"> </span><span class="c1">// 動態陣列的陣列(桶)</span>
|
||||||
<a id="__codelineno-10-6" name="__codelineno-10-6" href="#__codelineno-10-6"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">k</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-10-6" name="__codelineno-10-6" href="#__codelineno-10-6"></a>
|
||||||
<a id="__codelineno-10-7" name="__codelineno-10-7" href="#__codelineno-10-7"></a><span class="w"> </span><span class="c1">// 每個桶最多可以分配 size 個元素</span>
|
<a id="__codelineno-10-7" name="__codelineno-10-7" href="#__codelineno-10-7"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">k</span><span class="p">;</span><span class="w"> </span><span class="o">++</span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-10-8" name="__codelineno-10-8" href="#__codelineno-10-8"></a><span class="w"> </span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">calloc</span><span class="p">(</span><span class="n">size</span><span class="p">,</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="kt">float</span><span class="p">));</span>
|
<a id="__codelineno-10-8" name="__codelineno-10-8" href="#__codelineno-10-8"></a><span class="w"> </span><span class="c1">// 為每個桶預分配足夠的空間</span>
|
||||||
<a id="__codelineno-10-9" name="__codelineno-10-9" href="#__codelineno-10-9"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-10-9" name="__codelineno-10-9" href="#__codelineno-10-9"></a><span class="w"> </span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="kt">float</span><span class="w"> </span><span class="o">*</span><span class="p">)</span><span class="n">malloc</span><span class="p">(</span><span class="n">n</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="kt">float</span><span class="p">));</span>
|
||||||
<a id="__codelineno-10-10" name="__codelineno-10-10" href="#__codelineno-10-10"></a>
|
<a id="__codelineno-10-10" name="__codelineno-10-10" href="#__codelineno-10-10"></a><span class="w"> </span><span class="n">sizes</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-10-11" name="__codelineno-10-11" href="#__codelineno-10-11"></a><span class="w"> </span><span class="c1">// 1. 將陣列元素分配到各個桶中</span>
|
<a id="__codelineno-10-11" name="__codelineno-10-11" href="#__codelineno-10-11"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-10-12" name="__codelineno-10-12" href="#__codelineno-10-12"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">size</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-10-12" name="__codelineno-10-12" href="#__codelineno-10-12"></a>
|
||||||
<a id="__codelineno-10-13" name="__codelineno-10-13" href="#__codelineno-10-13"></a><span class="w"> </span><span class="c1">// 輸入資料範圍為 [0, 1),使用 num * k 對映到索引範圍 [0, k-1]</span>
|
<a id="__codelineno-10-13" name="__codelineno-10-13" href="#__codelineno-10-13"></a><span class="w"> </span><span class="c1">// 1. 將陣列元素分配到各個桶中</span>
|
||||||
<a id="__codelineno-10-14" name="__codelineno-10-14" href="#__codelineno-10-14"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">bucket_idx</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">nums</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">k</span><span class="p">;</span>
|
<a id="__codelineno-10-14" name="__codelineno-10-14" href="#__codelineno-10-14"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="o">++</span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-10-15" name="__codelineno-10-15" href="#__codelineno-10-15"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
<a id="__codelineno-10-15" name="__codelineno-10-15" href="#__codelineno-10-15"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">idx</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="p">)(</span><span class="n">nums</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">k</span><span class="p">);</span>
|
||||||
<a id="__codelineno-10-16" name="__codelineno-10-16" href="#__codelineno-10-16"></a><span class="w"> </span><span class="c1">// 如果桶中有資料且資料小於當前值 nums[i], 要將其放到當前桶的後面,相當於 cpp 中的 push_back</span>
|
<a id="__codelineno-10-16" name="__codelineno-10-16" href="#__codelineno-10-16"></a><span class="w"> </span><span class="n">buckets</span><span class="p">[</span><span class="n">idx</span><span class="p">][</span><span class="n">sizes</span><span class="p">[</span><span class="n">idx</span><span class="p">]</span><span class="o">++</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">nums</span><span class="p">[</span><span class="n">i</span><span class="p">];</span>
|
||||||
<a id="__codelineno-10-17" name="__codelineno-10-17" href="#__codelineno-10-17"></a><span class="w"> </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">buckets</span><span class="p">[</span><span class="n">bucket_idx</span><span class="p">][</span><span class="n">j</span><span class="p">]</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="n">buckets</span><span class="p">[</span><span class="n">bucket_idx</span><span class="p">][</span><span class="n">j</span><span class="p">]</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">nums</span><span class="p">[</span><span class="n">i</span><span class="p">])</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-10-17" name="__codelineno-10-17" href="#__codelineno-10-17"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-10-18" name="__codelineno-10-18" href="#__codelineno-10-18"></a><span class="w"> </span><span class="n">j</span><span class="o">++</span><span class="p">;</span>
|
<a id="__codelineno-10-18" name="__codelineno-10-18" href="#__codelineno-10-18"></a>
|
||||||
<a id="__codelineno-10-19" name="__codelineno-10-19" href="#__codelineno-10-19"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-10-19" name="__codelineno-10-19" href="#__codelineno-10-19"></a><span class="w"> </span><span class="c1">// 2. 對各個桶執行排序</span>
|
||||||
<a id="__codelineno-10-20" name="__codelineno-10-20" href="#__codelineno-10-20"></a><span class="w"> </span><span class="kt">float</span><span class="w"> </span><span class="n">temp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">nums</span><span class="p">[</span><span class="n">i</span><span class="p">];</span>
|
<a id="__codelineno-10-20" name="__codelineno-10-20" href="#__codelineno-10-20"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">k</span><span class="p">;</span><span class="w"> </span><span class="o">++</span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-10-21" name="__codelineno-10-21" href="#__codelineno-10-21"></a><span class="w"> </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">j</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">size</span><span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="n">buckets</span><span class="p">[</span><span class="n">bucket_idx</span><span class="p">][</span><span class="n">j</span><span class="p">]</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="mi">0</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-10-21" name="__codelineno-10-21" href="#__codelineno-10-21"></a><span class="w"> </span><span class="n">qsort</span><span class="p">(</span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">],</span><span class="w"> </span><span class="n">sizes</span><span class="p">[</span><span class="n">i</span><span class="p">],</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="kt">float</span><span class="p">),</span><span class="w"> </span><span class="n">compare</span><span class="p">);</span>
|
||||||
<a id="__codelineno-10-22" name="__codelineno-10-22" href="#__codelineno-10-22"></a><span class="w"> </span><span class="n">swap</span><span class="p">(</span><span class="o">&</span><span class="n">temp</span><span class="p">,</span><span class="w"> </span><span class="o">&</span><span class="n">buckets</span><span class="p">[</span><span class="n">bucket_idx</span><span class="p">][</span><span class="n">j</span><span class="p">]);</span>
|
<a id="__codelineno-10-22" name="__codelineno-10-22" href="#__codelineno-10-22"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-10-23" name="__codelineno-10-23" href="#__codelineno-10-23"></a><span class="w"> </span><span class="n">j</span><span class="o">++</span><span class="p">;</span>
|
<a id="__codelineno-10-23" name="__codelineno-10-23" href="#__codelineno-10-23"></a>
|
||||||
<a id="__codelineno-10-24" name="__codelineno-10-24" href="#__codelineno-10-24"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-10-24" name="__codelineno-10-24" href="#__codelineno-10-24"></a><span class="w"> </span><span class="c1">// 3. 合併排序後的桶</span>
|
||||||
<a id="__codelineno-10-25" name="__codelineno-10-25" href="#__codelineno-10-25"></a><span class="w"> </span><span class="n">buckets</span><span class="p">[</span><span class="n">bucket_idx</span><span class="p">][</span><span class="n">j</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">temp</span><span class="p">;</span>
|
<a id="__codelineno-10-25" name="__codelineno-10-25" href="#__codelineno-10-25"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">idx</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-10-26" name="__codelineno-10-26" href="#__codelineno-10-26"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-10-26" name="__codelineno-10-26" href="#__codelineno-10-26"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">k</span><span class="p">;</span><span class="w"> </span><span class="o">++</span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-10-27" name="__codelineno-10-27" href="#__codelineno-10-27"></a>
|
<a id="__codelineno-10-27" name="__codelineno-10-27" href="#__codelineno-10-27"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">sizes</span><span class="p">[</span><span class="n">i</span><span class="p">];</span><span class="w"> </span><span class="o">++</span><span class="n">j</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-10-28" name="__codelineno-10-28" href="#__codelineno-10-28"></a><span class="w"> </span><span class="c1">// 2. 對各個桶執行排序</span>
|
<a id="__codelineno-10-28" name="__codelineno-10-28" href="#__codelineno-10-28"></a><span class="w"> </span><span class="n">nums</span><span class="p">[</span><span class="n">idx</span><span class="o">++</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">];</span>
|
||||||
<a id="__codelineno-10-29" name="__codelineno-10-29" href="#__codelineno-10-29"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">k</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-10-29" name="__codelineno-10-29" href="#__codelineno-10-29"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-10-30" name="__codelineno-10-30" href="#__codelineno-10-30"></a><span class="w"> </span><span class="n">qsort</span><span class="p">(</span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">],</span><span class="w"> </span><span class="n">size</span><span class="p">,</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="kt">float</span><span class="p">),</span><span class="w"> </span><span class="n">compare_float</span><span class="p">);</span>
|
<a id="__codelineno-10-30" name="__codelineno-10-30" href="#__codelineno-10-30"></a><span class="w"> </span><span class="c1">// 釋放記憶體</span>
|
||||||
<a id="__codelineno-10-31" name="__codelineno-10-31" href="#__codelineno-10-31"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-10-31" name="__codelineno-10-31" href="#__codelineno-10-31"></a><span class="w"> </span><span class="n">free</span><span class="p">(</span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]);</span>
|
||||||
<a id="__codelineno-10-32" name="__codelineno-10-32" href="#__codelineno-10-32"></a>
|
<a id="__codelineno-10-32" name="__codelineno-10-32" href="#__codelineno-10-32"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-10-33" name="__codelineno-10-33" href="#__codelineno-10-33"></a><span class="w"> </span><span class="c1">// 3. 走訪桶合併結果</span>
|
<a id="__codelineno-10-33" name="__codelineno-10-33" href="#__codelineno-10-33"></a><span class="p">}</span>
|
||||||
<a id="__codelineno-10-34" name="__codelineno-10-34" href="#__codelineno-10-34"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">k</span><span class="p">;</span><span class="w"> </span><span class="n">j</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
|
||||||
<a id="__codelineno-10-35" name="__codelineno-10-35" href="#__codelineno-10-35"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">l</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">l</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">size</span><span class="p">;</span><span class="w"> </span><span class="n">l</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
|
||||||
<a id="__codelineno-10-36" name="__codelineno-10-36" href="#__codelineno-10-36"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">buckets</span><span class="p">[</span><span class="n">j</span><span class="p">][</span><span class="n">l</span><span class="p">]</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="mi">0</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
|
||||||
<a id="__codelineno-10-37" name="__codelineno-10-37" href="#__codelineno-10-37"></a><span class="w"> </span><span class="n">nums</span><span class="p">[</span><span class="n">i</span><span class="o">++</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buckets</span><span class="p">[</span><span class="n">j</span><span class="p">][</span><span class="n">l</span><span class="p">];</span>
|
|
||||||
<a id="__codelineno-10-38" name="__codelineno-10-38" href="#__codelineno-10-38"></a><span class="w"> </span><span class="p">}</span>
|
|
||||||
<a id="__codelineno-10-39" name="__codelineno-10-39" href="#__codelineno-10-39"></a><span class="w"> </span><span class="p">}</span>
|
|
||||||
<a id="__codelineno-10-40" name="__codelineno-10-40" href="#__codelineno-10-40"></a><span class="w"> </span><span class="p">}</span>
|
|
||||||
<a id="__codelineno-10-41" name="__codelineno-10-41" href="#__codelineno-10-41"></a>
|
|
||||||
<a id="__codelineno-10-42" name="__codelineno-10-42" href="#__codelineno-10-42"></a><span class="w"> </span><span class="c1">// 釋放上述分配的記憶體</span>
|
|
||||||
<a id="__codelineno-10-43" name="__codelineno-10-43" href="#__codelineno-10-43"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">k</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
|
||||||
<a id="__codelineno-10-44" name="__codelineno-10-44" href="#__codelineno-10-44"></a><span class="w"> </span><span class="n">free</span><span class="p">(</span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]);</span>
|
|
||||||
<a id="__codelineno-10-45" name="__codelineno-10-45" href="#__codelineno-10-45"></a><span class="w"> </span><span class="p">}</span>
|
|
||||||
<a id="__codelineno-10-46" name="__codelineno-10-46" href="#__codelineno-10-46"></a><span class="w"> </span><span class="n">free</span><span class="p">(</span><span class="n">buckets</span><span class="p">);</span>
|
|
||||||
<a id="__codelineno-10-47" name="__codelineno-10-47" href="#__codelineno-10-47"></a><span class="p">}</span>
|
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
|
|
File diff suppressed because one or more lines are too long
Binary file not shown.
Loading…
Reference in a new issue