<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2166: Heap property underspecified?</title>
<meta property="og:title" content="Issue 2166: Heap property underspecified?">
<meta property="og:description" content="C++ library issue. Status: C++17">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2166.html">
<meta property="og:type" content="website">
<meta property="og:image" content="http://cplusplus.github.io/LWG/images/cpp_logo.png">
<meta property="og:image:alt" content="C++ logo">
<style>
  p {text-align:justify}
  li {text-align:justify}
  pre code.backtick::before { content: "`" }
  pre code.backtick::after { content: "`" }
  blockquote.note
  {
    background-color:#E0E0E0;
    padding-left: 15px;
    padding-right: 15px;
    padding-top: 1px;
    padding-bottom: 1px;
  }
  ins {background-color:#A0FFA0}
  del {background-color:#FFA0A0}
  table.issues-index { border: 1px solid; border-collapse: collapse; }
  table.issues-index th { text-align: center; padding: 4px; border: 1px solid; }
  table.issues-index td { padding: 4px; border: 1px solid; }
  table.issues-index td:nth-child(1) { text-align: right; }
  table.issues-index td:nth-child(2) { text-align: left; }
  table.issues-index td:nth-child(3) { text-align: left; }
  table.issues-index td:nth-child(4) { text-align: left; }
  table.issues-index td:nth-child(5) { text-align: center; }
  table.issues-index td:nth-child(6) { text-align: center; }
  table.issues-index td:nth-child(7) { text-align: left; }
  table.issues-index td:nth-child(5) span.no-pr { color: red; }
  @media (prefers-color-scheme: dark) {
     html {
        color: #ddd;
        background-color: black;
     }
     ins {
        background-color: #225522
     }
     del {
        background-color: #662222
     }
     a {
        color: #6af
     }
     a:visited {
        color: #6af
     }
     blockquote.note
     {
        background-color: rgba(255, 255, 255, .10)
     }
  }
</style>
</head>
<body>
<hr>
<p><em>This page is a snapshot from the LWG issues list, see the <a href="lwg-active.html">Library Active Issues List</a> for more information and the meaning of <a href="lwg-active.html#C++17">C++17</a> status.</em></p>
<h3 id="2166"><a href="lwg-defects.html#2166">2166</a>. Heap property underspecified?</h3>
<p><b>Section:</b> 26.8.8 <a href="https://wg21.link/alg.heap.operations">[alg.heap.operations]</a> <b>Status:</b> <a href="lwg-active.html#C++17">C++17</a>
 <b>Submitter:</b> Peter Sommerlad <b>Opened:</b> 2012-07-09 <b>Last modified:</b> 2017-07-30</p>
<p><b>Priority: </b>3
</p>
<p><b>View all other</b> <a href="lwg-index.html#alg.heap.operations">issues</a> in [alg.heap.operations].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++17">C++17</a> status.</p>
<p><b>Discussion:</b></p>

<p>
Another similar issue to the <code>operator&lt;</code> vs greater in <code>nth_element</code> but not as direct occurs 
in 26.8.8 <a href="https://wg21.link/alg.heap.operations">[alg.heap.operations]</a>:
</p>
<blockquote><p>
-1- A <em>heap</em> is a particular organization of elements in a range between two random access iterators 
<code>[a,b)</code>. Its two key properties are:
</p>
<ol>
<li>There is no element greater than <code>*a</code> in the range and
</li>
<li><code>*a</code> may be removed by <code>pop_heap()</code>, or a new element added by <code>push_heap()</code>, in 
O(log(<code>N</code>)) time.
</li>
</ol>
</blockquote>
<p>
As noted by Richard Smith, it seems that the first bullet should read:
</p>
<blockquote><p>
<code>*a</code> is not less than any element in the range
</p></blockquote>
<p>
Even better the heap condition could be stated here directly, instead of leaving it unspecified, i.e.,
</p>
<blockquote><p>
Each element at <code>(a+2*i+1)</code> and <code>(a+2*i+2)</code> is less than the element at <code>(a+i)</code>, 
if those elements exist, for <code>i&gt;=0</code>.
</p></blockquote>
<p>
But may be that was may be intentional to allow other heap organizations?
<p/>
See also follow-up discussion of c++std-lib-32780.
<p/>

</p>
<p><i>[2016-08 Chicago]</i></p>

<p>Walter provided wording</p>
<p>Tues PM: Alisdair &amp; Billy(MS) to improve the wording.</p>

<p><i>[2016-08-02 Chicago LWG]</i></p>

<p>
Walter provides initial Proposed Resolution. Alisdair objects to perceived
complexity of the mathematical phrasing.
</p>

<p><strong>Previous resolution [SUPERSEDED]:</strong></p>
<blockquote class="note">
<p><i>[Note to editor: As a drive-by editorial adjustment, please replace the current
enumerated list format by the numbered bullet items shown below.]</i></p>


<p>Change [alg.heap.operations]:</p>

<p>1 A heap is a particular organization of elements in a range between two
random access iterators [a, b)<del>. Its two key properties are</del><ins>such
that</ins>:</p>

<p>(1.1) -- <del>There is no element greater than <code>*a</code> in the range and</del><br/>
         <ins>For all <code>i >= 0</code>,<br/>
         <code>comp(a[i], a[L])</code> is false whenever L = 2*i+1 &lt; b-a,<br/>
         and<br/>
         <code>comp(a[i], a[R])</code> is false whenever R = 2*i+2 &lt; b-a.</ins><br/>
</p>
<p>(1.2) -- <code>*a</code> may be removed by <code>pop_heap()</code>, or a new element added by <code>push_heap()</code>, in O(log(N)) time.</p>
</blockquote>

<p><i>[2016-08-03 Chicago LWG]</i></p>

<p>Walter and Billy O'Neal provide revised Proposed Resolution, superseding yesterday's.</p>
<p>Thurs PM: Moved to Tentatively Ready</p>


<p id="res-2166"><b>Proposed resolution:</b></p>
<p>
This wording is relative to N4606.
</p>

<ol>
<li><p>Change 26.8.8 <a href="https://wg21.link/alg.heap.operations">[alg.heap.operations]</a> as indicated:</p>

<blockquote class="note">
<p>
Note to project editor: As a drive-by editorial adjustment, please replace the
current enumerated list format by numbered bullet items.
</p>
</blockquote>

<blockquote>
<p>
-1- A <em>heap</em> is a particular organization of elements in a range between two random access iterators 
<code>[a, b)</code><del>. Its two key properties are</del><ins>such that</ins>:
</p>
<ol style="list-style-type: none">
<li><p>(1.1) &mdash; <del>There is no element greater than <code>*a</code> in the range and</del>
<ins>With <math xmlns="http://www.w3.org/1998/Math/MathML">
<mi>N</mi><mo>=</mo><mi mathvariant="monospace" mathsize="small">b</mi><mo>-
</mo><mi mathvariant="monospace" mathsize="small">a</mi></math>, 
for all <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>i</mi></math>,
<math xmlns="http://www.w3.org/1998/Math/MathML"><mn>0</mn><mo>&lt;</mo><mi>i</mi><mo>&lt;</mo><mi>N</mi></math>,
<code>comp(a[</code><math xmlns="http://www.w3.org/1998/Math/MathML">
  <mo rspace="0.3em" lspace="0em" stretchy="true" fence="true" form="prefix">&lfloor;</mo>
  <mrow>
	<mfrac linethickness="1">
		<mrow>
			<mi>i</mi>
			<mo>-</mo>
			<mn>1</mn>
		</mrow>
		<mn>2</mn>
	</mfrac>
  </mrow>
  <mo rspace="0em" lspace="0.3em" stretchy="true" fence="true" form="postfix">&rfloor;</mo>
</math><code>], a[<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>i</mi></math>])</code> is <code>false</code>.</ins>
</p>
<blockquote class="note">
<p>
[Note to the project editor: In LaTeX the above insertion should be expressed as follows:
<p/>
With $N = <code>b</code>-<code>a</code>$, for all $i$, $0 &lt; i &lt; N$,
<code>comp(a[$\left \lfloor{\frac{i-1}{2}}\right \rfloor$], a[$i$])</code> is <code>false</code>.]
</p>
</blockquote>
</li>
<li><p>(1.2) &mdash; <code>*a</code> may be removed by <code>pop_heap()</code>, or a new element added by <code>push_heap()</code>, in 
&#x1d4aa;(log(<i>N</i>)) time.
</p>
</li>
</ol>
</blockquote>
</li>
</ol>





</body>
</html>
