1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 2<html> 3<head> 4<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 5<title>The Data Structure</title> 6<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css"> 7<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 8<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset"> 9<link rel="up" href="../unordered.html" title="Chapter 44. Boost.Unordered"> 10<link rel="prev" href="../unordered.html" title="Chapter 44. Boost.Unordered"> 11<link rel="next" href="hash_equality.html" title="Equality Predicates and Hash Functions"> 12</head> 13<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 14<table cellpadding="2" width="100%"><tr> 15<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td> 16<td align="center"><a href="../../../index.html">Home</a></td> 17<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td> 18<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 19<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 20<td align="center"><a href="../../../more/index.htm">More</a></td> 21</tr></table> 22<hr> 23<div class="spirit-nav"> 24<a accesskey="p" href="../unordered.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../unordered.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="hash_equality.html"><img src="../../../doc/src/images/next.png" alt="Next"></a> 25</div> 26<div class="section"> 27<div class="titlepage"><div><div><h2 class="title" style="clear: both"> 28<a name="unordered.buckets"></a><a class="link" href="buckets.html" title="The Data Structure">The Data Structure</a> 29</h2></div></div></div> 30<p> 31 The containers are made up of a number of 'buckets', each of which can contain 32 any number of elements. For example, the following diagram shows an <code class="computeroutput"><a class="link" href="../boost/unordered_set.html" title="Class template unordered_set">unordered_set</a></code> with 7 buckets containing 33 5 elements, <code class="computeroutput"><span class="identifier">A</span></code>, <code class="computeroutput"><span class="identifier">B</span></code>, <code class="computeroutput"><span class="identifier">C</span></code>, 34 <code class="computeroutput"><span class="identifier">D</span></code> and <code class="computeroutput"><span class="identifier">E</span></code> 35 (this is just for illustration, containers will typically have more buckets). 36 </p> 37<p> 38 <span class="inlinemediaobject"><img src="../../../libs/unordered/doc/diagrams/buckets.png" align="middle"></span> 39 </p> 40<p> 41 In order to decide which bucket to place an element in, the container applies 42 the hash function, <code class="computeroutput"><span class="identifier">Hash</span></code>, to 43 the element's key (for <code class="computeroutput"><span class="identifier">unordered_set</span></code> 44 and <code class="computeroutput"><span class="identifier">unordered_multiset</span></code> the 45 key is the whole element, but is referred to as the key so that the same terminology 46 can be used for sets and maps). This returns a value of type <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span></code>. 47 <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span></code> has a much greater range of values 48 then the number of buckets, so the container applies another transformation 49 to that value to choose a bucket to place the element in. 50 </p> 51<p> 52 Retrieving the elements for a given key is simple. The same process is applied 53 to the key to find the correct bucket. Then the key is compared with the elements 54 in the bucket to find any elements that match (using the equality predicate 55 <code class="computeroutput"><span class="identifier">Pred</span></code>). If the hash function 56 has worked well the elements will be evenly distributed amongst the buckets 57 so only a small number of elements will need to be examined. 58 </p> 59<p> 60 There is <a class="link" href="hash_equality.html" title="Equality Predicates and Hash Functions">more information on hash functions 61 and equality predicates in the next section</a>. 62 </p> 63<p> 64 You can see in the diagram that <code class="computeroutput"><span class="identifier">A</span></code> 65 & <code class="computeroutput"><span class="identifier">D</span></code> have been placed in 66 the same bucket. When looking for elements in this bucket up to 2 comparisons 67 are made, making the search slower. This is known as a collision. To keep things 68 fast we try to keep collisions to a minimum. 69 </p> 70<p> 71 </p> 72<div class="table"> 73<a name="id-1.3.45.4.8.1"></a><p class="title"><b>Table 44.1. Methods for Accessing Buckets</b></p> 74<div class="table-contents"><table class="table" summary="Methods for Accessing Buckets"> 75<colgroup> 76<col> 77<col> 78</colgroup> 79<thead><tr> 80<th><p>Method</p></th> 81<th><p>Description</p></th> 82</tr></thead> 83<tbody> 84<tr> 85<td><code class="computeroutput"><span class="identifier">size_type</span> <span class="identifier">bucket_count</span><span class="special">()</span> <span class="keyword">const</span></code></td> 86<td>The 87 number of buckets.</td> 88</tr> 89<tr> 90<td><code class="computeroutput"><span class="identifier">size_type</span> <span class="identifier">max_bucket_count</span><span class="special">()</span> 91 <span class="keyword">const</span></code></td> 92<td>An upper bound on the number of 93 buckets.</td> 94</tr> 95<tr> 96<td><code class="computeroutput"><span class="identifier">size_type</span> <span class="identifier">bucket_size</span><span class="special">(</span><span class="identifier">size_type</span> <span class="identifier">n</span><span class="special">)</span> <span class="keyword">const</span></code></td> 97<td>The 98 number of elements in bucket <code class="computeroutput"><span class="identifier">n</span></code>.</td> 99</tr> 100<tr> 101<td><code class="computeroutput"><span class="identifier">size_type</span> <span class="identifier">bucket</span><span class="special">(</span><span class="identifier">key_type</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">k</span><span class="special">)</span> <span class="keyword">const</span></code></td> 102<td>Returns 103 the index of the bucket which would contain <code class="computeroutput"><span class="identifier">k</span></code>.</td> 104</tr> 105<tr> 106<td><code class="computeroutput"><span class="identifier">local_iterator</span> <span class="identifier">begin</span><span class="special">(</span><span class="identifier">size_type</span> <span class="identifier">n</span><span class="special">);</span></code></td> 107<td rowspan="6">Return 108 begin and end iterators for bucket <code class="computeroutput"><span class="identifier">n</span></code>.</td> 109</tr> 110<tr><td><code class="computeroutput"><span class="identifier">local_iterator</span> <span class="identifier">end</span><span class="special">(</span><span class="identifier">size_type</span> <span class="identifier">n</span><span class="special">);</span></code></td></tr> 111<tr><td><code class="computeroutput"><span class="identifier">const_local_iterator</span> <span class="identifier">begin</span><span class="special">(</span><span class="identifier">size_type</span> <span class="identifier">n</span><span class="special">)</span> <span class="keyword">const</span><span class="special">;</span></code></td></tr> 112<tr><td><code class="computeroutput"><span class="identifier">const_local_iterator</span> 113 <span class="identifier">end</span><span class="special">(</span><span class="identifier">size_type</span> <span class="identifier">n</span><span class="special">)</span> <span class="keyword">const</span><span class="special">;</span></code></td></tr> 114<tr><td><code class="computeroutput"><span class="identifier">const_local_iterator</span> <span class="identifier">cbegin</span><span class="special">(</span><span class="identifier">size_type</span> <span class="identifier">n</span><span class="special">)</span> <span class="keyword">const</span><span class="special">;</span></code></td></tr> 115<tr><td><code class="computeroutput"><span class="identifier">const_local_iterator</span> 116 <span class="identifier">cend</span><span class="special">(</span><span class="identifier">size_type</span> <span class="identifier">n</span><span class="special">)</span> <span class="keyword">const</span><span class="special">;</span></code></td></tr> 117</tbody> 118</table></div> 119</div> 120<p><br class="table-break"> 121 </p> 122<h3> 123<a name="unordered.buckets.h0"></a> 124 <span class="phrase"><a name="unordered.buckets.controlling_the_number_of_buckets"></a></span><a class="link" href="buckets.html#unordered.buckets.controlling_the_number_of_buckets">Controlling 125 the number of buckets</a> 126 </h3> 127<p> 128 As more elements are added to an unordered associative container, the number 129 of elements in the buckets will increase causing performance to degrade. To 130 combat this the containers increase the bucket count as elements are inserted. 131 You can also tell the container to change the bucket count (if required) by 132 calling <code class="computeroutput"><span class="identifier">rehash</span></code>. 133 </p> 134<p> 135 The standard leaves a lot of freedom to the implementer to decide how the number 136 of buckets is chosen, but it does make some requirements based on the container's 137 'load factor', the average number of elements per bucket. Containers also have 138 a 'maximum load factor' which they should try to keep the load factor below. 139 </p> 140<p> 141 You can't control the bucket count directly but there are two ways to influence 142 it: 143 </p> 144<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 145<li class="listitem"> 146 Specify the minimum number of buckets when constructing a container or 147 when calling <code class="computeroutput"><span class="identifier">rehash</span></code>. 148 </li> 149<li class="listitem"> 150 Suggest a maximum load factor by calling <code class="computeroutput"><span class="identifier">max_load_factor</span></code>. 151 </li> 152</ul></div> 153<p> 154 <code class="computeroutput"><span class="identifier">max_load_factor</span></code> doesn't let 155 you set the maximum load factor yourself, it just lets you give a <span class="emphasis"><em>hint</em></span>. 156 And even then, the draft standard doesn't actually require the container to 157 pay much attention to this value. The only time the load factor is <span class="emphasis"><em>required</em></span> 158 to be less than the maximum is following a call to <code class="computeroutput"><span class="identifier">rehash</span></code>. 159 But most implementations will try to keep the number of elements below the 160 max load factor, and set the maximum load factor to be the same as or close 161 to the hint - unless your hint is unreasonably small or large. 162 </p> 163<div class="table"> 164<a name="unordered.buckets.bucket_size"></a><p class="title"><b>Table 44.2. Methods for Controlling Bucket Size</b></p> 165<div class="table-contents"><table class="table" summary="Methods for Controlling Bucket Size"> 166<colgroup> 167<col> 168<col> 169</colgroup> 170<thead><tr> 171<th> 172 <p> 173 Method 174 </p> 175 </th> 176<th> 177 <p> 178 Description 179 </p> 180 </th> 181</tr></thead> 182<tbody> 183<tr> 184<td> 185 <p> 186 <code class="computeroutput"><span class="identifier">X</span><span class="special">(</span><span class="identifier">size_type</span> <span class="identifier">n</span><span class="special">)</span></code> 187 </p> 188 </td> 189<td> 190 <p> 191 Construct an empty container with at least <code class="computeroutput"><span class="identifier">n</span></code> 192 buckets (<code class="computeroutput"><span class="identifier">X</span></code> is the 193 container type). 194 </p> 195 </td> 196</tr> 197<tr> 198<td> 199 <p> 200 <code class="computeroutput"><span class="identifier">X</span><span class="special">(</span><span class="identifier">InputIterator</span> <span class="identifier">i</span><span class="special">,</span> <span class="identifier">InputIterator</span> 201 <span class="identifier">j</span><span class="special">,</span> 202 <span class="identifier">size_type</span> <span class="identifier">n</span><span class="special">)</span></code> 203 </p> 204 </td> 205<td> 206 <p> 207 Construct an empty container with at least <code class="computeroutput"><span class="identifier">n</span></code> 208 buckets and insert elements from the range [<code class="computeroutput"><span class="identifier">i</span></code>, 209 <code class="computeroutput"><span class="identifier">j</span></code>) (<code class="computeroutput"><span class="identifier">X</span></code> is the container type). 210 </p> 211 </td> 212</tr> 213<tr> 214<td> 215 <p> 216 <code class="computeroutput"><span class="keyword">float</span> <span class="identifier">load_factor</span><span class="special">()</span> <span class="keyword">const</span></code> 217 </p> 218 </td> 219<td> 220 <p> 221 The average number of elements per bucket. 222 </p> 223 </td> 224</tr> 225<tr> 226<td> 227 <p> 228 <code class="computeroutput"><span class="keyword">float</span> <span class="identifier">max_load_factor</span><span class="special">()</span> <span class="keyword">const</span></code> 229 </p> 230 </td> 231<td> 232 <p> 233 Returns the current maximum load factor. 234 </p> 235 </td> 236</tr> 237<tr> 238<td> 239 <p> 240 <code class="computeroutput"><span class="keyword">float</span> <span class="identifier">max_load_factor</span><span class="special">(</span><span class="keyword">float</span> <span class="identifier">z</span><span class="special">)</span></code> 241 </p> 242 </td> 243<td> 244 <p> 245 Changes the container's maximum load factor, using <code class="computeroutput"><span class="identifier">z</span></code> as a hint. 246 </p> 247 </td> 248</tr> 249<tr> 250<td> 251 <p> 252 <code class="computeroutput"><span class="keyword">void</span> <span class="identifier">rehash</span><span class="special">(</span><span class="identifier">size_type</span> 253 <span class="identifier">n</span><span class="special">)</span></code> 254 </p> 255 </td> 256<td> 257 <p> 258 Changes the number of buckets so that there at least <code class="computeroutput"><span class="identifier">n</span></code> buckets, and so that the load 259 factor is less than the maximum load factor. 260 </p> 261 </td> 262</tr> 263</tbody> 264</table></div> 265</div> 266<br class="table-break"><h3> 267<a name="unordered.buckets.h1"></a> 268 <span class="phrase"><a name="unordered.buckets.iterator_invalidation"></a></span><a class="link" href="buckets.html#unordered.buckets.iterator_invalidation">Iterator 269 Invalidation</a> 270 </h3> 271<p> 272 It is not specified how member functions other than <code class="computeroutput"><span class="identifier">rehash</span></code> 273 affect the bucket count, although <code class="computeroutput"><span class="identifier">insert</span></code> 274 is only allowed to invalidate iterators when the insertion causes the load 275 factor to be greater than or equal to the maximum load factor. For most implementations 276 this means that <code class="computeroutput"><span class="identifier">insert</span></code> will 277 only change the number of buckets when this happens. While iterators can be 278 invalidated by calls to <code class="computeroutput"><span class="identifier">insert</span></code> 279 and <code class="computeroutput"><span class="identifier">rehash</span></code>, pointers and references 280 to the container's elements are never invalidated. 281 </p> 282<p> 283 In a similar manner to using <code class="computeroutput"><span class="identifier">reserve</span></code> 284 for <code class="computeroutput"><span class="identifier">vector</span></code>s, it can be a good 285 idea to call <code class="computeroutput"><span class="identifier">rehash</span></code> before 286 inserting a large number of elements. This will get the expensive rehashing 287 out of the way and let you store iterators, safe in the knowledge that they 288 won't be invalidated. If you are inserting <code class="computeroutput"><span class="identifier">n</span></code> 289 elements into container <code class="computeroutput"><span class="identifier">x</span></code>, 290 you could first call: 291 </p> 292<pre class="programlisting"><span class="identifier">x</span><span class="special">.</span><span class="identifier">rehash</span><span class="special">((</span><span class="identifier">x</span><span class="special">.</span><span class="identifier">size</span><span class="special">()</span> <span class="special">+</span> <span class="identifier">n</span><span class="special">)</span> <span class="special">/</span> <span class="identifier">x</span><span class="special">.</span><span class="identifier">max_load_factor</span><span class="special">());</span> 293</pre> 294<div class="blurb"> 295<div class="titlepage"><div><div><p class="title"><b></b></p></div></div></div> 296<p> 297 Note: <code class="computeroutput"><span class="identifier">rehash</span></code>'s argument is 298 the minimum number of buckets, not the number of elements, which is why the 299 new size is divided by the maximum load factor. 300 </p> 301</div> 302</div> 303<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 304<td align="left"></td> 305<td align="right"><div class="copyright-footer">Copyright © 2003, 2004 Jeremy B. Maitin-Shepard<br>Copyright © 2005-2008 Daniel 306 James<p> 307 Distributed under the Boost Software License, Version 1.0. (See accompanying 308 file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) 309 </p> 310</div></td> 311</tr></table> 312<hr> 313<div class="spirit-nav"> 314<a accesskey="p" href="../unordered.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../unordered.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="hash_equality.html"><img src="../../../doc/src/images/next.png" alt="Next"></a> 315</div> 316</body> 317</html> 318