<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 764: equal_range on unordered containers should return a pair of local_iterators</title>
<meta property="og:title" content="Issue 764: equal_range on unordered containers should return a pair of local_iterators">
<meta property="og:description" content="C++ library issue. Status: NAD">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue764.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#NAD">NAD</a> status.</em></p>
<h3 id="764"><a href="lwg-closed.html#764">764</a>. <code>equal_range</code> on unordered containers should return a <code>pair</code> of <code>local_iterators</code></h3>
<p><b>Section:</b> 23.2.8 <a href="https://wg21.link/unord.req">[unord.req]</a> <b>Status:</b> <a href="lwg-active.html#NAD">NAD</a>
 <b>Submitter:</b> Joe Gottman <b>Opened:</b> 2007-11-29 <b>Last modified:</b> 2016-01-28</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View other</b> <a href="lwg-index-open.html#unord.req">active issues</a> in [unord.req].</p>
<p><b>View all other</b> <a href="lwg-index.html#unord.req">issues</a> in [unord.req].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#NAD">NAD</a> status.</p>
<p><b>Discussion:</b></p>
<p>
    A major attribute of the unordered containers is that iterating 
though them inside a bucket is very fast while iterating between buckets 
can be much slower.  If an unordered container has a low load factor, 
iterating between the last iterator in one bucket and the next iterator, 
which is in another bucket, is <code>O(bucket_count())</code> which may be much 
larger than <code>O(size())</code>.
</p>
<p>
    If <code>b</code> is an non-const unordered container of type <code>B</code> and <code>k</code> is an 
object of it's <code>key_type</code>, then <code>b.equal_range(k)</code> currently returns 
<code>pair&lt;B::iterator, B::iterator&gt;</code>. Consider the following code:
</p>

<blockquote><pre>
B::iterator lb, ub;
tie(lb, ub) = b.equal_range(k);
for (B::iterator it = lb; it != ub; ++it) {
        // Do something with *it
}
</pre></blockquote>

<p>
If <code>b.equal_range(k)</code> returns a non-empty range (i.e. <code>b</code> contains at least 
on element whose key is equivalent to <code>k</code>), then every iterator in the 
half-open range <code>[lb, ub)</code> will be in the same bucket, but <code>ub</code> will likely 
either be in a different bucket or be equal to <code>b.end()</code>.  In either case, 
iterating between <code>ub - 1</code> and <code>ub</code> could take a much longer time than 
iterating through the rest of the range.
</p>
<p>
If instead of returning <code>pair&lt;iterator, iterator&gt;</code>, <code>equal_range</code> were to 
return <code>pair&lt;local_iterator, local_iterator&gt;</code>, then <code>ub</code> (which, like <code>lb</code>, 
would now be a <code>local_iterator</code>) could be guaranteed to always be in the 
same bucket as <code>lb</code>. In the cases where currently <code>ub</code> is equal to <code>b.end()</code>
or is in a different bucket, <code>ub</code> would be equal to <code>b.end(b.bucket(key))</code>. 
  This would make iterating between <code>lb</code> and <code>ub</code> much faster, as every 
iteration would be constant time.
</p>

<p><i>[
Bellevue:
]</i></p>


<blockquote><p>
The proposed resolution breaks consistency with other container types
for dubious benefit, and iterators are already constant time.
</p></blockquote>



<p id="res-764"><b>Proposed resolution:</b></p>
<p>
Change the entry for <code>equal_range</code> in Table 93 (23.2.8 <a href="https://wg21.link/unord.req">[unord.req]</a>) as follows:
</p>
<table border="1">
<tr>
<th>expression</th> <th>return type</th> <th>assertion/note pre/post-condition</th> <th>complexity</th>
</tr>

<tr>
<td><code>b.equal_range(k)</code></td>
<td><code>pair&lt;<ins>local_</ins>iterator,<ins>local_</ins>iterator&gt;; pair&lt;const_<ins>local_</ins>iterator,const_<ins>local_</ins>iterator&gt;</code> for <code>const b</code>.</td>
<td>Returns a range containing all elements with keys equivalent to <code>k</code>. Returns <code>make_pair(b.end(<ins>b.bucket(key)</ins>),b.end(<ins>b.bucket(key)</ins>))</code> if no such elements exist.</td>
<td>Average case &Theta;<code>(b.count(k))</code>. Worst case &Theta;<code>(b.size())</code>. </td>
</tr>
</table>





</body>
</html>
