heapqueue.html 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <!-- This file is generated by Nim. -->
  4. <html xmlns="https://www.w3.org/1999/xhtml" xml:lang="en" lang="en" data-theme="auto">
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  7. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  8. <title>std/heapqueue</title>
  9. <!-- Google fonts -->
  10. <link href='https://fonts.googleapis.com/css?family=Lato:400,600,900' rel='stylesheet' type='text/css'/>
  11. <link href='https://fonts.googleapis.com/css?family=Source+Code+Pro:400,500,600' rel='stylesheet' type='text/css'/>
  12. <!-- Favicon -->
  13. <link rel="shortcut icon" href="data:image/x-icon;base64,AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AAAAAAUAAAAF////AP///wD///8A////AP///wD///8A////AP///wD///8A////AAAAAAIAAABbAAAAlQAAAKIAAACbAAAAmwAAAKIAAACVAAAAWwAAAAL///8A////AP///wD///8A////AAAAABQAAADAAAAAYwAAAA3///8A////AP///wD///8AAAAADQAAAGMAAADAAAAAFP///wD///8A////AP///wAAAACdAAAAOv///wD///8A////AP///wD///8A////AP///wD///8AAAAAOgAAAJ3///8A////AP///wAAAAAnAAAAcP///wAAAAAoAAAASv///wD///8A////AP///wAAAABKAAAAKP///wAAAABwAAAAJ////wD///8AAAAAgQAAABwAAACIAAAAkAAAAJMAAACtAAAAFQAAABUAAACtAAAAkwAAAJAAAACIAAAAHAAAAIH///8A////AAAAAKQAAACrAAAAaP///wD///8AAAAARQAAANIAAADSAAAARf///wD///8AAAAAaAAAAKsAAACk////AAAAADMAAACcAAAAnQAAABj///8A////AP///wAAAAAYAAAAGP///wD///8A////AAAAABgAAACdAAAAnAAAADMAAAB1AAAAwwAAAP8AAADpAAAAsQAAAE4AAAAb////AP///wAAAAAbAAAATgAAALEAAADpAAAA/wAAAMMAAAB1AAAAtwAAAOkAAAD/AAAA/wAAAP8AAADvAAAA3gAAAN4AAADeAAAA3gAAAO8AAAD/AAAA/wAAAP8AAADpAAAAtwAAAGUAAAA/AAAA3wAAAP8AAAD/AAAA/wAAAP8AAAD/AAAA/wAAAP8AAAD/AAAA/wAAAP8AAADfAAAAPwAAAGX///8A////AAAAAEgAAADtAAAAvwAAAL0AAADGAAAA7wAAAO8AAADGAAAAvQAAAL8AAADtAAAASP///wD///8A////AP///wD///8AAAAAO////wD///8A////AAAAAIcAAACH////AP///wD///8AAAAAO////wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A//8AAP//AAD4HwAA7/cAAN/7AAD//wAAoYUAAJ55AACf+QAAh+EAAAAAAADAAwAA4AcAAP5/AAD//wAA//8AAA=="/>
  14. <link rel="icon" type="image/png" sizes="32x32" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAA3XAAAN1wFCKJt4AAAAB3RJTUUH4QQQEwksSS9ZWwAAAk1JREFUWMPtll2ITVEUx39nn/O7Y5qR8f05wtCUUr6ZIS++8pEnkZInPImneaCQ5METNdOkeFBKUhMPRIkHKfEuUZSUlGlKPN2TrgfncpvmnntnmlEyq1Z7t89/rf9a6+y99oZxGZf/XeIq61EdtgKXgdXA0xrYAvBjOIF1AI9zvjcC74BSpndrJPkBWDScTF8Aa4E3wDlgHbASaANmVqlcCnwHvgDvgVfAJ+AikAAvgfVZwLnSVZHZaOuKoQi3ZOMi4NkYkpe1p4J7A8BpYAD49hfIy/oqG0+hLomiKP2L5L+1ubn5115S+3OAn4EnwBlgMzCjyt6ZAnQCJ4A7wOs88iRJHvw50HoujuPBoCKwHWiosy8MdfZnAdcHk8dxXFJ3VQbQlCTJvRBCGdRbD4M6uc5glpY3eAihpN5S5w12diSEcCCEcKUO4ljdr15T76ur1FDDLIQQ3qv71EdDOe3Kxj3leRXyk+pxdWnFWod6Wt2bY3de3aSuUHcPBVimHs7mK9WrmeOF6lR1o9qnzskh2ar2qm1qizpfXaPeVGdlmGN5pb09qMxz1Xb1kLqgzn1RyH7JUXW52lr5e/Kqi9qpto7V1atuUzfnARrV7jEib1T76gG2qxdGmXyiekkt1GswPTtek0aBfJp6YySGBfWg2tPQ0FAYgf1stUfdmdcjarbYJEniKIq6gY/Aw+zWHAC+p2labGpqiorFYgGYCEzN7oQdQClN07O1/EfDyGgC0ALMBdYAi4FyK+4H3gLPsxfR1zRNi+NP7nH5J+QntnXe5B5mpfQAAAAASUVORK5CYII=">
  15. <!-- CSS -->
  16. <link rel="stylesheet" type="text/css" href="nimdoc.out.css?v=2.3.1">
  17. <!-- JS -->
  18. <script type="text/javascript" src="dochack.js?v=2.3.1"></script>
  19. </head>
  20. <body>
  21. <div class="document" id="documentId">
  22. <div class="container">
  23. <h1 class="title">std/heapqueue</h1>
  24. <div class="row">
  25. <div class="three columns">
  26. <div class="theme-select-wrapper">
  27. <label for="theme-select">Theme:&nbsp;</label>
  28. <select id="theme-select" onchange="setTheme(this.value)">
  29. <option value="auto">🌗 Match OS</option>
  30. <option value="dark">🌑 Dark</option>
  31. <option value="light">🌕 Light</option>
  32. </select>
  33. </div>
  34. <div id="global-links">
  35. <ul class="simple-boot">
  36. <li><a href="manual.html">Manual</a></li>
  37. <li><a href="lib.html">Standard library</a></li>
  38. <li> <a id="indexLink" href="theindex.html">Index</a></li>
  39. <li><a href="compiler/theindex.html">Compiler docs</a></li>
  40. <li><a href="https://nim-lang.github.io/fusion/theindex.html">Fusion docs</a></li>
  41. <li><a href="https://nim-lang.github.io/Nim/">devel</a>, <a href="https://nim-lang.org/documentation.html">stable</a></li>
  42. </ul>
  43. </div>
  44. <div id="searchInputDiv">
  45. Search: <input type="search" id="searchInput"
  46. oninput="search()" />
  47. </div>
  48. <ul class="simple simple-toc" id="toc-list">
  49. <li><a class="reference" id="basic-usage_toc" href="#basic-usage">Basic usage</a></li>
  50. <li><a class="reference" id="usage-with-custom-objects_toc" href="#usage-with-custom-objects">Usage with custom objects</a></li>
  51. <li>
  52. <a class="reference reference-toplevel" href="#6" id="56">Imports</a>
  53. </li>
  54. <li>
  55. <details open>
  56. <summary><a class="reference reference-toplevel" href="#7" id="57">Types</a></summary>
  57. <ul class="simple simple-toc-section">
  58. <li><a class="reference" href="#HeapQueue" title="HeapQueue[T] = object">HeapQueue</a></li>
  59. </ul>
  60. </details>
  61. </li>
  62. <li>
  63. <details open>
  64. <summary><a class="reference reference-toplevel" href="#12" id="62">Procs</a></summary>
  65. <ul class="simple simple-toc-section">
  66. <ul class="simple nested-toc-section">$
  67. <li><a class="reference" href="#%24%2CHeapQueue%5BT%5D" title="`$`[T](heap: HeapQueue[T]): string">`$`[T](heap: HeapQueue[T]): string</a></li>
  68. </ul>
  69. <ul class="simple nested-toc-section">[]
  70. <li><a class="reference" href="#%5B%5D%2CHeapQueue%5BT%5D%2CNatural" title="`[]`[T](heap: HeapQueue[T]; i: Natural): lent T">`[]`[T](heap: HeapQueue[T]; i: Natural): lent T</a></li>
  71. </ul>
  72. <ul class="simple nested-toc-section">clear
  73. <li><a class="reference" href="#clear%2CHeapQueue%5BT%5D" title="clear[T](heap: var HeapQueue[T])">clear[T](heap: var HeapQueue[T])</a></li>
  74. </ul>
  75. <ul class="simple nested-toc-section">contains
  76. <li><a class="reference" href="#contains%2CHeapQueue%5BT%5D%2CT" title="contains[T](heap: HeapQueue[T]; x: T): bool">contains[T](heap: HeapQueue[T]; x: T): bool</a></li>
  77. </ul>
  78. <ul class="simple nested-toc-section">del
  79. <li><a class="reference" href="#del%2CHeapQueue%5BT%5D%2CNatural" title="del[T](heap: var HeapQueue[T]; index: Natural)">del[T](heap: var HeapQueue[T]; index: Natural)</a></li>
  80. </ul>
  81. <ul class="simple nested-toc-section">find
  82. <li><a class="reference" href="#find%2CHeapQueue%5BT%5D%2CT" title="find[T](heap: HeapQueue[T]; x: T): int">find[T](heap: HeapQueue[T]; x: T): int</a></li>
  83. </ul>
  84. <ul class="simple nested-toc-section">initHeapQueue
  85. <li><a class="reference" href="#initHeapQueue" title="initHeapQueue[T](): HeapQueue[T]">initHeapQueue[T](): HeapQueue[T]</a></li>
  86. </ul>
  87. <ul class="simple nested-toc-section">len
  88. <li><a class="reference" href="#len%2CHeapQueue%5BT%5D" title="len[T](heap: HeapQueue[T]): int">len[T](heap: HeapQueue[T]): int</a></li>
  89. </ul>
  90. <ul class="simple nested-toc-section">pop
  91. <li><a class="reference" href="#pop%2CHeapQueue%5BT%5D" title="pop[T](heap: var HeapQueue[T]): T">pop[T](heap: var HeapQueue[T]): T</a></li>
  92. </ul>
  93. <ul class="simple nested-toc-section">push
  94. <li><a class="reference" href="#push%2CHeapQueue%5BT%5D%2CsinkT" title="push[T](heap: var HeapQueue[T]; item: sink T)">push[T](heap: var HeapQueue[T]; item: sink T)</a></li>
  95. </ul>
  96. <ul class="simple nested-toc-section">pushpop
  97. <li><a class="reference" href="#pushpop%2CHeapQueue%5BT%5D%2CsinkT" title="pushpop[T](heap: var HeapQueue[T]; item: sink T): T">pushpop[T](heap: var HeapQueue[T]; item: sink T): T</a></li>
  98. </ul>
  99. <ul class="simple nested-toc-section">replace
  100. <li><a class="reference" href="#replace%2CHeapQueue%5BT%5D%2CsinkT" title="replace[T](heap: var HeapQueue[T]; item: sink T): T">replace[T](heap: var HeapQueue[T]; item: sink T): T</a></li>
  101. </ul>
  102. <ul class="simple nested-toc-section">toHeapQueue
  103. <li><a class="reference" href="#toHeapQueue%2CopenArray%5BT%5D" title="toHeapQueue[T](x: openArray[T]): HeapQueue[T]">toHeapQueue[T](x: openArray[T]): HeapQueue[T]</a></li>
  104. </ul>
  105. </ul>
  106. </details>
  107. </li>
  108. <li>
  109. <details open>
  110. <summary><a class="reference reference-toplevel" href="#15" id="65">Iterators</a></summary>
  111. <ul class="simple simple-toc-section">
  112. <ul class="simple nested-toc-section">items
  113. <li><a class="reference" href="#items.i%2CHeapQueue%5BT%5D" title="items[T](heap: HeapQueue[T]): lent T">items[T](heap: HeapQueue[T]): lent T</a></li>
  114. </ul>
  115. </ul>
  116. </details>
  117. </li>
  118. </ul>
  119. </div>
  120. <div class="nine columns" id="content">
  121. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/heapqueue.nim#L1" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  122. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/heapqueue.nim#L1" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  123. <div id="tocRoot"></div>
  124. <p class="module-desc">The <tt class="docutils literal"><span class="pre"><span class="Identifier">heapqueue</span></span></tt> module implements a <a class="reference external" href="https://en.wikipedia.org/wiki/Binary_heap">binary heap data structure</a> that can be used as a <a class="reference external" href="https://en.wikipedia.org/wiki/Priority_queue">priority queue</a>. They are represented as arrays for which <tt class="docutils literal"><span class="pre"><span class="Identifier">a</span><span class="Punctuation">[</span><span class="Identifier">k</span><span class="Punctuation">]</span> <span class="Operator">&lt;=</span> <span class="Identifier">a</span><span class="Punctuation">[</span><span class="DecNumber">2</span><span class="Operator">*</span><span class="Identifier">k</span><span class="Operator">+</span><span class="DecNumber">1</span><span class="Punctuation">]</span></span></tt> and <tt class="docutils literal"><span class="pre"><span class="Identifier">a</span><span class="Punctuation">[</span><span class="Identifier">k</span><span class="Punctuation">]</span> <span class="Operator">&lt;=</span> <span class="Identifier">a</span><span class="Punctuation">[</span><span class="DecNumber">2</span><span class="Operator">*</span><span class="Identifier">k</span><span class="Operator">+</span><span class="DecNumber">2</span><span class="Punctuation">]</span></span></tt> for all indices <tt class="docutils literal"><span class="pre"><span class="Identifier">k</span></span></tt> (counting elements from 0). The interesting property of a heap is that <tt class="docutils literal"><span class="pre"><span class="Identifier">a</span><span class="Punctuation">[</span><span class="DecNumber">0</span><span class="Punctuation">]</span></span></tt> is always its smallest element.
  125. <h1><a class="toc-backref" id="basic-usage" href="#basic-usage">Basic usage</a></h1>
  126. <p><strong class="examples_text">Example:</strong></p>
  127. <pre class="listing"><span class="Keyword">import</span> <span class="Identifier">std</span><span class="Operator">/</span><span class="Identifier">heapqueue</span>
  128. <span class="Keyword">var</span> <span class="Identifier">heap</span> <span class="Operator">=</span> <span class="Punctuation">[</span><span class="DecNumber">8</span><span class="Punctuation">,</span> <span class="DecNumber">2</span><span class="Punctuation">]</span><span class="Operator">.</span><span class="Identifier">toHeapQueue</span>
  129. <span class="Identifier">heap</span><span class="Operator">.</span><span class="Identifier">push</span><span class="Punctuation">(</span><span class="DecNumber">5</span><span class="Punctuation">)</span>
  130. <span class="Comment"># the first element is the lowest element</span>
  131. <span class="Identifier">assert</span> <span class="Identifier">heap</span><span class="Punctuation">[</span><span class="DecNumber">0</span><span class="Punctuation">]</span> <span class="Operator">==</span> <span class="DecNumber">2</span>
  132. <span class="Comment"># remove and return the lowest element</span>
  133. <span class="Identifier">assert</span> <span class="Identifier">heap</span><span class="Operator">.</span><span class="Identifier">pop</span><span class="Punctuation">(</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">2</span>
  134. <span class="Comment"># the lowest element remaining is 5</span>
  135. <span class="Identifier">assert</span> <span class="Identifier">heap</span><span class="Punctuation">[</span><span class="DecNumber">0</span><span class="Punctuation">]</span> <span class="Operator">==</span> <span class="DecNumber">5</span></pre>
  136. <h1><a class="toc-backref" id="usage-with-custom-objects" href="#usage-with-custom-objects">Usage with custom objects</a></h1><p>To use a <tt class="docutils literal"><span class="pre"><span class="Identifier">HeapQueue</span></span></tt> with a custom object, the <tt class="docutils literal"><span class="pre"><span class="Operator">&lt;</span></span></tt> operator must be implemented.</p>
  137. <p><strong class="examples_text">Example:</strong></p>
  138. <pre class="listing"><span class="Keyword">import</span> <span class="Identifier">std</span><span class="Operator">/</span><span class="Identifier">heapqueue</span>
  139. <span class="Keyword">type</span> <span class="Identifier">Job</span> <span class="Operator">=</span> <span class="Keyword">object</span>
  140. <span class="Identifier">priority</span><span class="Punctuation">:</span> <span class="Identifier">int</span>
  141. <span class="Keyword">proc</span> <span class="Punctuation">`</span><span class="Operator">&lt;</span><span class="Punctuation">`</span><span class="Punctuation">(</span><span class="Identifier">a</span><span class="Punctuation">,</span> <span class="Identifier">b</span><span class="Punctuation">:</span> <span class="Identifier">Job</span><span class="Punctuation">)</span><span class="Punctuation">:</span> <span class="Identifier">bool</span> <span class="Operator">=</span> <span class="Identifier">a</span><span class="Operator">.</span><span class="Identifier">priority</span> <span class="Operator">&lt;</span> <span class="Identifier">b</span><span class="Operator">.</span><span class="Identifier">priority</span>
  142. <span class="Keyword">var</span> <span class="Identifier">jobs</span> <span class="Operator">=</span> <span class="Identifier">initHeapQueue</span><span class="Punctuation">[</span><span class="Identifier">Job</span><span class="Punctuation">]</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
  143. <span class="Identifier">jobs</span><span class="Operator">.</span><span class="Identifier">push</span><span class="Punctuation">(</span><span class="Identifier">Job</span><span class="Punctuation">(</span><span class="Identifier">priority</span><span class="Punctuation">:</span> <span class="DecNumber">1</span><span class="Punctuation">)</span><span class="Punctuation">)</span>
  144. <span class="Identifier">jobs</span><span class="Operator">.</span><span class="Identifier">push</span><span class="Punctuation">(</span><span class="Identifier">Job</span><span class="Punctuation">(</span><span class="Identifier">priority</span><span class="Punctuation">:</span> <span class="DecNumber">2</span><span class="Punctuation">)</span><span class="Punctuation">)</span>
  145. <span class="Identifier">assert</span> <span class="Identifier">jobs</span><span class="Punctuation">[</span><span class="DecNumber">0</span><span class="Punctuation">]</span><span class="Operator">.</span><span class="Identifier">priority</span> <span class="Operator">==</span> <span class="DecNumber">1</span></pre></p>
  146. <div class="section" id="6">
  147. <h1><a class="toc-backref" href="#6">Imports</a></h1>
  148. <dl class="item">
  149. <a class="reference external" href="since.html">since</a>, <a class="reference external" href="assertions.html">assertions</a>
  150. </dl>
  151. </div>
  152. <div class="section" id="7">
  153. <h1><a class="toc-backref" href="#7">Types</a></h1>
  154. <dl class="item">
  155. <div id="HeapQueue">
  156. <dt><pre><a href="heapqueue.html#HeapQueue"><span class="Identifier">HeapQueue</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span> <span class="Other">=</span> <span class="Keyword">object</span></pre></dt>
  157. <dd>
  158. A heap queue, commonly known as a priority queue.
  159. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/heapqueue.nim#L53" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  160. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/heapqueue.nim#L53" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  161. </dd>
  162. </div>
  163. </dl>
  164. </div>
  165. <div class="section" id="12">
  166. <h1><a class="toc-backref" href="#12">Procs</a></h1>
  167. <dl class="item">
  168. <div id="$-procs-all">
  169. <div id="$,HeapQueue[T]">
  170. <dt><pre><span class="Keyword">proc</span> <a href="#%24%2CHeapQueue%5BT%5D"><span class="Identifier">`$`</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">heap</span><span class="Other">:</span> <a href="heapqueue.html#HeapQueue"><span class="Identifier">HeapQueue</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <a href="system.html#string"><span class="Identifier">string</span></a></pre></dt>
  171. <dd>
  172. Turns a heap into its string representation.
  173. <p><strong class="examples_text">Example:</strong></p>
  174. <pre class="listing"><span class="Keyword">let</span> <span class="Identifier">heap</span> <span class="Operator">=</span> <span class="Punctuation">[</span><span class="DecNumber">1</span><span class="Punctuation">,</span> <span class="DecNumber">2</span><span class="Punctuation">]</span><span class="Operator">.</span><span class="Identifier">toHeapQueue</span>
  175. <span class="Identifier">assert</span> <span class="Operator">$</span><span class="Identifier">heap</span> <span class="Operator">==</span> <span class="StringLit">&quot;[1, 2]&quot;</span></pre>
  176. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/heapqueue.nim#L256" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  177. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/heapqueue.nim#L256" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  178. </dd>
  179. </div>
  180. </div>
  181. <div id="[]-procs-all">
  182. <div id="[],HeapQueue[T],Natural">
  183. <dt><pre><span class="Keyword">proc</span> <a href="#%5B%5D%2CHeapQueue%5BT%5D%2CNatural"><span class="Identifier">`[]`</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">heap</span><span class="Other">:</span> <a href="heapqueue.html#HeapQueue"><span class="Identifier">HeapQueue</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">i</span><span class="Other">:</span> <a href="system.html#Natural"><span class="Identifier">Natural</span></a><span class="Other">)</span><span class="Other">:</span> <a href="system.html#lent"><span class="Identifier">lent</span></a> <span class="Identifier">T</span> {.<span class="Identifier">inline</span>.}</pre></dt>
  184. <dd>
  185. Accesses the i-th element of <tt class="docutils literal"><span class="pre"><span class="Identifier">heap</span></span></tt>.
  186. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/heapqueue.nim#L75" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  187. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/heapqueue.nim#L75" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  188. </dd>
  189. </div>
  190. </div>
  191. <div id="clear-procs-all">
  192. <div id="clear,HeapQueue[T]">
  193. <dt><pre><span class="Keyword">proc</span> <a href="#clear%2CHeapQueue%5BT%5D"><span class="Identifier">clear</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">heap</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="heapqueue.html#HeapQueue"><span class="Identifier">HeapQueue</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span></pre></dt>
  194. <dd>
  195. Removes all elements from <tt class="docutils literal"><span class="pre"><span class="Identifier">heap</span></span></tt>, making it empty.
  196. <p><strong class="examples_text">Example:</strong></p>
  197. <pre class="listing"><span class="Keyword">var</span> <span class="Identifier">heap</span> <span class="Operator">=</span> <span class="Punctuation">[</span><span class="DecNumber">9</span><span class="Punctuation">,</span> <span class="DecNumber">5</span><span class="Punctuation">,</span> <span class="DecNumber">8</span><span class="Punctuation">]</span><span class="Operator">.</span><span class="Identifier">toHeapQueue</span>
  198. <span class="Identifier">heap</span><span class="Operator">.</span><span class="Identifier">clear</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
  199. <span class="Identifier">assert</span> <span class="Identifier">heap</span><span class="Operator">.</span><span class="Identifier">len</span> <span class="Operator">==</span> <span class="DecNumber">0</span></pre>
  200. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/heapqueue.nim#L247" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  201. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/heapqueue.nim#L247" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  202. </dd>
  203. </div>
  204. </div>
  205. <div id="contains-procs-all">
  206. <div id="contains,HeapQueue[T],T">
  207. <dt><pre><span class="Keyword">proc</span> <a href="#contains%2CHeapQueue%5BT%5D%2CT"><span class="Identifier">contains</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">heap</span><span class="Other">:</span> <a href="heapqueue.html#HeapQueue"><span class="Identifier">HeapQueue</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">x</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">)</span><span class="Other">:</span> <a href="system.html#bool"><span class="Identifier">bool</span></a></pre></dt>
  208. <dd>
  209. Returns true if <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span></span></tt> is in <tt class="docutils literal"><span class="pre"><span class="Identifier">heap</span></span></tt> or false if not found. This is a shortcut for <tt class="docutils literal"><span class="pre"><span class="Identifier">find</span><span class="Punctuation">(</span><span class="Identifier">heap</span><span class="Punctuation">,</span> <span class="Identifier">x</span><span class="Punctuation">)</span> <span class="Operator">&gt;=</span> <span class="DecNumber">0</span></span></tt>.
  210. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/heapqueue.nim#L191" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  211. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/heapqueue.nim#L191" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  212. </dd>
  213. </div>
  214. </div>
  215. <div id="del-procs-all">
  216. <div id="del,HeapQueue[T],Natural">
  217. <dt><pre><span class="Keyword">proc</span> <a href="#del%2CHeapQueue%5BT%5D%2CNatural"><span class="Identifier">del</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">heap</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="heapqueue.html#HeapQueue"><span class="Identifier">HeapQueue</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">index</span><span class="Other">:</span> <a href="system.html#Natural"><span class="Identifier">Natural</span></a><span class="Other">)</span></pre></dt>
  218. <dd>
  219. Removes the element at <tt class="docutils literal"><span class="pre"><span class="Identifier">index</span></span></tt> from <tt class="docutils literal"><span class="pre"><span class="Identifier">heap</span></span></tt>, maintaining the heap invariant.
  220. <p><strong class="examples_text">Example:</strong></p>
  221. <pre class="listing"><span class="Keyword">var</span> <span class="Identifier">heap</span> <span class="Operator">=</span> <span class="Punctuation">[</span><span class="DecNumber">9</span><span class="Punctuation">,</span> <span class="DecNumber">5</span><span class="Punctuation">,</span> <span class="DecNumber">8</span><span class="Punctuation">]</span><span class="Operator">.</span><span class="Identifier">toHeapQueue</span>
  222. <span class="Identifier">heap</span><span class="Operator">.</span><span class="Identifier">del</span><span class="Punctuation">(</span><span class="DecNumber">1</span><span class="Punctuation">)</span>
  223. <span class="Identifier">assert</span> <span class="Identifier">heap</span><span class="Punctuation">[</span><span class="DecNumber">0</span><span class="Punctuation">]</span> <span class="Operator">==</span> <span class="DecNumber">5</span>
  224. <span class="Identifier">assert</span> <span class="Identifier">heap</span><span class="Punctuation">[</span><span class="DecNumber">1</span><span class="Punctuation">]</span> <span class="Operator">==</span> <span class="DecNumber">8</span></pre>
  225. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/heapqueue.nim#L196" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  226. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/heapqueue.nim#L196" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  227. </dd>
  228. </div>
  229. </div>
  230. <div id="find-procs-all">
  231. <div id="find,HeapQueue[T],T">
  232. <dt><pre><span class="Keyword">proc</span> <a href="#find%2CHeapQueue%5BT%5D%2CT"><span class="Identifier">find</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">heap</span><span class="Other">:</span> <a href="heapqueue.html#HeapQueue"><span class="Identifier">HeapQueue</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">x</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">)</span><span class="Other">:</span> <a href="system.html#int"><span class="Identifier">int</span></a></pre></dt>
  233. <dd>
  234. Linear scan to find the index of the item <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span></span></tt> or -1 if not found.
  235. <p><strong class="examples_text">Example:</strong></p>
  236. <pre class="listing"><span class="Keyword">let</span> <span class="Identifier">heap</span> <span class="Operator">=</span> <span class="Punctuation">[</span><span class="DecNumber">9</span><span class="Punctuation">,</span> <span class="DecNumber">5</span><span class="Punctuation">,</span> <span class="DecNumber">8</span><span class="Punctuation">]</span><span class="Operator">.</span><span class="Identifier">toHeapQueue</span>
  237. <span class="Identifier">assert</span> <span class="Identifier">heap</span><span class="Operator">.</span><span class="Identifier">find</span><span class="Punctuation">(</span><span class="DecNumber">5</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">0</span>
  238. <span class="Identifier">assert</span> <span class="Identifier">heap</span><span class="Operator">.</span><span class="Identifier">find</span><span class="Punctuation">(</span><span class="DecNumber">9</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">1</span>
  239. <span class="Identifier">assert</span> <span class="Identifier">heap</span><span class="Operator">.</span><span class="Identifier">find</span><span class="Punctuation">(</span><span class="DecNumber">777</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="Operator">-</span><span class="DecNumber">1</span></pre>
  240. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/heapqueue.nim#L179" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  241. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/heapqueue.nim#L179" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  242. </dd>
  243. </div>
  244. </div>
  245. <div id="initHeapQueue-procs-all">
  246. <div id="initHeapQueue">
  247. <dt><pre><span class="Keyword">proc</span> <a href="#initHeapQueue"><span class="Identifier">initHeapQueue</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Other">)</span><span class="Other">:</span> <a href="heapqueue.html#HeapQueue"><span class="Identifier">HeapQueue</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span></pre></dt>
  248. <dd>
  249. <p>Creates a new empty heap.</p>
  250. <p>Heaps are initialized by default, so it is not necessary to call this function explicitly.</p>
  251. <p><strong>See also:</strong></p>
  252. <ul class="simple"><li><a class="reference external" href="#toHeapQueue,openArray[T]">toHeapQueue proc</a></li>
  253. </ul>
  254. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/heapqueue.nim#L57" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  255. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/heapqueue.nim#L57" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  256. </dd>
  257. </div>
  258. </div>
  259. <div id="len-procs-all">
  260. <div id="len,HeapQueue[T]">
  261. <dt><pre><span class="Keyword">proc</span> <a href="#len%2CHeapQueue%5BT%5D"><span class="Identifier">len</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">heap</span><span class="Other">:</span> <a href="heapqueue.html#HeapQueue"><span class="Identifier">HeapQueue</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <a href="system.html#int"><span class="Identifier">int</span></a> {.<span class="Identifier">inline</span>.}</pre></dt>
  262. <dd>
  263. Returns the number of elements of <tt class="docutils literal"><span class="pre"><span class="Identifier">heap</span></span></tt>.
  264. <p><strong class="examples_text">Example:</strong></p>
  265. <pre class="listing"><span class="Keyword">let</span> <span class="Identifier">heap</span> <span class="Operator">=</span> <span class="Punctuation">[</span><span class="DecNumber">9</span><span class="Punctuation">,</span> <span class="DecNumber">5</span><span class="Punctuation">,</span> <span class="DecNumber">8</span><span class="Punctuation">]</span><span class="Operator">.</span><span class="Identifier">toHeapQueue</span>
  266. <span class="Identifier">assert</span> <span class="Identifier">heap</span><span class="Operator">.</span><span class="Identifier">len</span> <span class="Operator">==</span> <span class="DecNumber">3</span></pre>
  267. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/heapqueue.nim#L67" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  268. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/heapqueue.nim#L67" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  269. </dd>
  270. </div>
  271. </div>
  272. <div id="pop-procs-all">
  273. <div id="pop,HeapQueue[T]">
  274. <dt><pre><span class="Keyword">proc</span> <a href="#pop%2CHeapQueue%5BT%5D"><span class="Identifier">pop</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">heap</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="heapqueue.html#HeapQueue"><span class="Identifier">HeapQueue</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">T</span></pre></dt>
  275. <dd>
  276. Pops and returns the smallest item from <tt class="docutils literal"><span class="pre"><span class="Identifier">heap</span></span></tt>, maintaining the heap invariant.
  277. <p><strong class="examples_text">Example:</strong></p>
  278. <pre class="listing"><span class="Keyword">var</span> <span class="Identifier">heap</span> <span class="Operator">=</span> <span class="Punctuation">[</span><span class="DecNumber">9</span><span class="Punctuation">,</span> <span class="DecNumber">5</span><span class="Punctuation">,</span> <span class="DecNumber">8</span><span class="Punctuation">]</span><span class="Operator">.</span><span class="Identifier">toHeapQueue</span>
  279. <span class="Identifier">assert</span> <span class="Identifier">heap</span><span class="Operator">.</span><span class="Identifier">pop</span><span class="Punctuation">(</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">5</span></pre>
  280. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/heapqueue.nim#L164" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  281. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/heapqueue.nim#L164" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  282. </dd>
  283. </div>
  284. </div>
  285. <div id="push-procs-all">
  286. <div id="push,HeapQueue[T],sinkT">
  287. <dt><pre><span class="Keyword">proc</span> <a href="#push%2CHeapQueue%5BT%5D%2CsinkT"><span class="Identifier">push</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">heap</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="heapqueue.html#HeapQueue"><span class="Identifier">HeapQueue</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">item</span><span class="Other">:</span> <a href="system.html#sink"><span class="Identifier">sink</span></a> <span class="Identifier">T</span><span class="Other">)</span></pre></dt>
  288. <dd>
  289. Pushes <tt class="docutils literal"><span class="pre"><span class="Identifier">item</span></span></tt> onto <tt class="docutils literal"><span class="pre"><span class="Identifier">heap</span></span></tt>, maintaining the heap invariant.
  290. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/heapqueue.nim#L144" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  291. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/heapqueue.nim#L144" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  292. </dd>
  293. </div>
  294. </div>
  295. <div id="pushpop-procs-all">
  296. <div id="pushpop,HeapQueue[T],sinkT">
  297. <dt><pre><span class="Keyword">proc</span> <a href="#pushpop%2CHeapQueue%5BT%5D%2CsinkT"><span class="Identifier">pushpop</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">heap</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="heapqueue.html#HeapQueue"><span class="Identifier">HeapQueue</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">item</span><span class="Other">:</span> <a href="system.html#sink"><span class="Identifier">sink</span></a> <span class="Identifier">T</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">T</span></pre></dt>
  298. <dd>
  299. <p>Fast version of a <tt class="docutils literal"><span class="pre"><span class="Identifier">push</span><span class="Punctuation">(</span><span class="Punctuation">)</span></span></tt> followed by a <tt class="docutils literal"><span class="pre"><span class="Identifier">pop</span><span class="Punctuation">(</span><span class="Punctuation">)</span></span></tt>.</p>
  300. <p><strong>See also:</strong></p>
  301. <ul class="simple"><li><a class="reference external" href="#replace,HeapQueue[T],sinkT">replace proc</a></li>
  302. </ul>
  303. <p><strong class="examples_text">Example:</strong></p>
  304. <pre class="listing"><span class="Keyword">var</span> <span class="Identifier">heap</span> <span class="Operator">=</span> <span class="Punctuation">[</span><span class="DecNumber">5</span><span class="Punctuation">,</span> <span class="DecNumber">12</span><span class="Punctuation">]</span><span class="Operator">.</span><span class="Identifier">toHeapQueue</span>
  305. <span class="Identifier">assert</span> <span class="Identifier">heap</span><span class="Operator">.</span><span class="Identifier">pushpop</span><span class="Punctuation">(</span><span class="DecNumber">6</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">5</span>
  306. <span class="Identifier">assert</span> <span class="Identifier">heap</span><span class="Operator">.</span><span class="Identifier">len</span> <span class="Operator">==</span> <span class="DecNumber">2</span>
  307. <span class="Identifier">assert</span> <span class="Identifier">heap</span><span class="Punctuation">[</span><span class="DecNumber">0</span><span class="Punctuation">]</span> <span class="Operator">==</span> <span class="DecNumber">6</span>
  308. <span class="Identifier">assert</span> <span class="Identifier">heap</span><span class="Operator">.</span><span class="Identifier">pushpop</span><span class="Punctuation">(</span><span class="DecNumber">4</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">4</span></pre>
  309. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/heapqueue.nim#L230" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  310. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/heapqueue.nim#L230" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  311. </dd>
  312. </div>
  313. </div>
  314. <div id="replace-procs-all">
  315. <div id="replace,HeapQueue[T],sinkT">
  316. <dt><pre><span class="Keyword">proc</span> <a href="#replace%2CHeapQueue%5BT%5D%2CsinkT"><span class="Identifier">replace</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">heap</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="heapqueue.html#HeapQueue"><span class="Identifier">HeapQueue</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">item</span><span class="Other">:</span> <a href="system.html#sink"><span class="Identifier">sink</span></a> <span class="Identifier">T</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">T</span></pre></dt>
  317. <dd>
  318. <p>Pops and returns the current smallest value, and add the new item. This is more efficient than <tt class="docutils literal"><span class="pre"><span class="Identifier">pop</span><span class="Punctuation">(</span><span class="Punctuation">)</span></span></tt> followed by <tt class="docutils literal"><span class="pre"><span class="Identifier">push</span><span class="Punctuation">(</span><span class="Punctuation">)</span></span></tt>, and can be more appropriate when using a fixed-size heap. Note that the value returned may be larger than <tt class="docutils literal"><span class="pre"><span class="Identifier">item</span></span></tt>! That constrains reasonable uses of this routine unless written as part of a conditional replacement.</p>
  319. <p><strong>See also:</strong></p>
  320. <ul class="simple"><li><a class="reference external" href="#pushpop,HeapQueue[T],sinkT">pushpop proc</a></li>
  321. </ul>
  322. <p><strong class="examples_text">Example:</strong></p>
  323. <pre class="listing"><span class="Keyword">var</span> <span class="Identifier">heap</span> <span class="Operator">=</span> <span class="Punctuation">[</span><span class="DecNumber">5</span><span class="Punctuation">,</span> <span class="DecNumber">12</span><span class="Punctuation">]</span><span class="Operator">.</span><span class="Identifier">toHeapQueue</span>
  324. <span class="Identifier">assert</span> <span class="Identifier">heap</span><span class="Operator">.</span><span class="Identifier">replace</span><span class="Punctuation">(</span><span class="DecNumber">6</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">5</span>
  325. <span class="Identifier">assert</span> <span class="Identifier">heap</span><span class="Operator">.</span><span class="Identifier">len</span> <span class="Operator">==</span> <span class="DecNumber">2</span>
  326. <span class="Identifier">assert</span> <span class="Identifier">heap</span><span class="Punctuation">[</span><span class="DecNumber">0</span><span class="Punctuation">]</span> <span class="Operator">==</span> <span class="DecNumber">6</span>
  327. <span class="Identifier">assert</span> <span class="Identifier">heap</span><span class="Operator">.</span><span class="Identifier">replace</span><span class="Punctuation">(</span><span class="DecNumber">4</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">6</span></pre>
  328. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/heapqueue.nim#L210" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  329. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/heapqueue.nim#L210" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  330. </dd>
  331. </div>
  332. </div>
  333. <div id="toHeapQueue-procs-all">
  334. <div id="toHeapQueue,openArray[T]">
  335. <dt><pre><span class="Keyword">proc</span> <a href="#toHeapQueue%2CopenArray%5BT%5D"><span class="Identifier">toHeapQueue</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <a href="system.html#openArray"><span class="Identifier">openArray</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <a href="heapqueue.html#HeapQueue"><span class="Identifier">HeapQueue</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span></pre></dt>
  336. <dd>
  337. <p>Creates a new HeapQueue that contains the elements of <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span></span></tt>.</p>
  338. <p><strong>See also:</strong></p>
  339. <ul class="simple"><li><a class="reference external" href="#initHeapQueue">initHeapQueue proc</a></li>
  340. </ul>
  341. <p><strong class="examples_text">Example:</strong></p>
  342. <pre class="listing"><span class="Keyword">var</span> <span class="Identifier">heap</span> <span class="Operator">=</span> <span class="Punctuation">[</span><span class="DecNumber">9</span><span class="Punctuation">,</span> <span class="DecNumber">5</span><span class="Punctuation">,</span> <span class="DecNumber">8</span><span class="Punctuation">]</span><span class="Operator">.</span><span class="Identifier">toHeapQueue</span>
  343. <span class="Identifier">assert</span> <span class="Identifier">heap</span><span class="Operator">.</span><span class="Identifier">pop</span><span class="Punctuation">(</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">5</span>
  344. <span class="Identifier">assert</span> <span class="Identifier">heap</span><span class="Punctuation">[</span><span class="DecNumber">0</span><span class="Punctuation">]</span> <span class="Operator">==</span> <span class="DecNumber">8</span></pre>
  345. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/heapqueue.nim#L149" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  346. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/heapqueue.nim#L149" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  347. </dd>
  348. </div>
  349. </div>
  350. </dl>
  351. </div>
  352. <div class="section" id="15">
  353. <h1><a class="toc-backref" href="#15">Iterators</a></h1>
  354. <dl class="item">
  355. <div id="items-iterators-all">
  356. <div id="items.i,HeapQueue[T]">
  357. <dt><pre><span class="Keyword">iterator</span> <a href="#items.i%2CHeapQueue%5BT%5D"><span class="Identifier">items</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">heap</span><span class="Other">:</span> <a href="heapqueue.html#HeapQueue"><span class="Identifier">HeapQueue</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <a href="system.html#lent"><span class="Identifier">lent</span></a> <span class="Identifier">T</span> {.<span class="Identifier">inline</span>.}</pre></dt>
  358. <dd>
  359. Iterates over each item of <tt class="docutils literal"><span class="pre"><span class="Identifier">heap</span></span></tt>.
  360. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/heapqueue.nim#L79" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  361. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/heapqueue.nim#L79" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  362. </dd>
  363. </div>
  364. </div>
  365. </dl>
  366. </div>
  367. </div>
  368. </div>
  369. <div class="twelve-columns footer">
  370. <span class="nim-sprite"></span>
  371. <br>
  372. <small style="color: var(--hint);">Made with Nim. Generated: 2025-02-06 22:28:16 UTC</small>
  373. </div>
  374. </div>
  375. </div>
  376. <script defer data-domain="nim-lang.org" src="https://plausible.io/js/plausible.js"></script>
  377. </body>
  378. </html>