<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 384: equal_range has unimplementable runtime complexity</title>
<meta property="og:title" content="Issue 384: equal_range has unimplementable runtime complexity">
<meta property="og:description" content="C++ library issue. Status: CD1">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue384.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#CD1">CD1</a> status.</em></p>
<h3 id="384"><a href="lwg-defects.html#384">384</a>. equal_range has unimplementable runtime complexity</h3>
<p><b>Section:</b> 26.8.4.4 <a href="https://wg21.link/equal.range">[equal.range]</a> <b>Status:</b> <a href="lwg-active.html#CD1">CD1</a>
 <b>Submitter:</b> Hans Bos <b>Opened:</b> 2002-10-18 <b>Last modified:</b> 2016-01-28</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all other</b> <a href="lwg-index.html#equal.range">issues</a> in [equal.range].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#CD1">CD1</a> status.</p>
<p><b>Discussion:</b></p>
<p>
Section 26.8.4.4 <a href="https://wg21.link/equal.range">[equal.range]</a>
states that at most 2 * log(last - first) + 1
comparisons are allowed for equal_range.
</p>

<p>It is not possible to implement equal_range with these constraints.</p>

<p>In a range of one element as in:</p>
<pre>
    int x = 1;
    equal_range(&amp;x, &amp;x + 1, 1)
</pre>

<p>it is easy to see that at least 2 comparison operations are needed.</p>

<p>For this case at most 2 * log(1) + 1 = 1 comparison is allowed.</p>

<p>I have checked a few libraries and they all use the same (nonconforming)
algorithm for equal_range that has a complexity of</p>
<pre>
     2* log(distance(first, last)) + 2.
</pre>
<p>I guess this is the algorithm that the standard assumes for equal_range.</p>

<p>
It is easy to see that 2 * log(distance) + 2 comparisons are enough
since equal range can be implemented with lower_bound and upper_bound
(both log(distance) + 1).
</p>

<p>
I think it is better to require something like 2log(distance) + O(1)  (or
even logarithmic as multiset::equal_range).
Then an implementation has more room to optimize for certain cases (e.g.
have log(distance) characteristics when at most match is found in the range
but 2log(distance) + 4 for the worst case).
</p>



<p id="res-384"><b>Proposed resolution:</b></p>
<p>In 26.8.4.2 <a href="https://wg21.link/lower.bound">[lower.bound]</a>/4, change <code>log(last - first) + 1</code>
to <code>log<sub>2</sub>(last - first) + <i>O</i>(1)</code>.</p>

<p>In 26.8.4.3 <a href="https://wg21.link/upper.bound">[upper.bound]</a>/4, change <code>log(last - first) + 1</code>
to <code>log<sub>2</sub>(last - first) + <i>O</i>(1)</code>.</p>

<p>In 26.8.4.4 <a href="https://wg21.link/equal.range">[equal.range]</a>/4, change <code>2*log(last - first) + 1</code>
to <code>2*log<sub>2</sub>(last - first) + <i>O</i>(1)</code>.</p>

<p><i>[Matt provided wording]</i></p>



<p><b>Rationale:</b></p>
<p>The LWG considered just saying <i>O</i>(log n) for all three, but
  decided that threw away too much valuable information.  The fact
  that lower_bound is twice as fast as equal_range is important.
  However, it's better to allow an arbitrary additive constant than to
  specify an exact count.  An exact count would have to
  involve <code>floor</code> or <code>ceil</code>.  It would be too easy to
  get this wrong, and don't provide any substantial value for users.</p>




</body>
</html>
