Imported Upstream version 1.57.0
[platform/upstream/boost.git] / doc / html / unordered / rationale.html
1 <html>
2 <head>
3 <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
4 <title>Implementation Rationale</title>
5 <link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
6 <meta name="generator" content="DocBook XSL Stylesheets V1.78.1">
7 <link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
8 <link rel="up" href="../unordered.html" title="Chapter&#160;37.&#160;Boost.Unordered">
9 <link rel="prev" href="compliance.html" title="C++11 Compliance">
10 <link rel="next" href="changes.html" title="Change Log">
11 </head>
12 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
13 <table cellpadding="2" width="100%"><tr>
14 <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
15 <td align="center"><a href="../../../index.html">Home</a></td>
16 <td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
17 <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
18 <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
19 <td align="center"><a href="../../../more/index.htm">More</a></td>
20 </tr></table>
21 <hr>
22 <div class="spirit-nav">
23 <a accesskey="p" href="compliance.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="changes.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
24 </div>
25 <div class="section">
26 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
27 <a name="unordered.rationale"></a><a class="link" href="rationale.html" title="Implementation Rationale">Implementation Rationale</a>
28 </h2></div></div></div>
29 <p>
30       The intent of this library is to implement the unordered containers in the
31       draft standard, so the interface was fixed. But there are still some implementation
32       decisions to make. The priorities are conformance to the standard and portability.
33     </p>
34 <p>
35       The <a href="http://en.wikipedia.org/wiki/Hash_table" target="_top">wikipedia article
36       on hash tables</a> has a good summary of the implementation issues for
37       hash tables in general.
38     </p>
39 <h3>
40 <a name="unordered.rationale.h0"></a>
41       <span class="phrase"><a name="unordered.rationale.data_structure"></a></span><a class="link" href="rationale.html#unordered.rationale.data_structure">Data
42       Structure</a>
43     </h3>
44 <p>
45       By specifying an interface for accessing the buckets of the container the standard
46       pretty much requires that the hash table uses chained addressing.
47     </p>
48 <p>
49       It would be conceivable to write a hash table that uses another method. For
50       example, it could use open addressing, and use the lookup chain to act as a
51       bucket but there are a some serious problems with this:
52     </p>
53 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
54 <li class="listitem">
55           The draft standard requires that pointers to elements aren't invalidated,
56           so the elements can't be stored in one array, but will need a layer of
57           indirection instead - losing the efficiency and most of the memory gain,
58           the main advantages of open addressing.
59         </li>
60 <li class="listitem">
61           Local iterators would be very inefficient and may not be able to meet the
62           complexity requirements.
63         </li>
64 <li class="listitem">
65           There are also the restrictions on when iterators can be invalidated. Since
66           open addressing degrades badly when there are a high number of collisions
67           the restrictions could prevent a rehash when it's really needed. The maximum
68           load factor could be set to a fairly low value to work around this - but
69           the standard requires that it is initially set to 1.0.
70         </li>
71 <li class="listitem">
72           And since the standard is written with a eye towards chained addressing,
73           users will be surprised if the performance doesn't reflect that.
74         </li>
75 </ul></div>
76 <p>
77       So chained addressing is used.
78     </p>
79 <h3>
80 <a name="unordered.rationale.h1"></a>
81       <span class="phrase"><a name="unordered.rationale.number_of_buckets"></a></span><a class="link" href="rationale.html#unordered.rationale.number_of_buckets">Number
82       of Buckets</a>
83     </h3>
84 <p>
85       There are two popular methods for choosing the number of buckets in a hash
86       table. One is to have a prime number of buckets, another is to use a power
87       of 2.
88     </p>
89 <p>
90       Using a prime number of buckets, and choosing a bucket by using the modulus
91       of the hash function's result will usually give a good result. The downside
92       is that the required modulus operation is fairly expensive. This is what the
93       containers do in most cases.
94     </p>
95 <p>
96       Using a power of 2 allows for much quicker selection of the bucket to use,
97       but at the expense of loosing the upper bits of the hash value. For some specially
98       designed hash functions it is possible to do this and still get a good result
99       but as the containers can take arbitrary hash functions this can't be relied
100       on.
101     </p>
102 <p>
103       To avoid this a transformation could be applied to the hash function, for an
104       example see <a href="http://web.archive.org/web/20121102023700/http://www.concentric.net/~Ttwang/tech/inthash.htm" target="_top">Thomas
105       Wang's article on integer hash functions</a>. Unfortunately, a transformation
106       like Wang's requires knowledge of the number of bits in the hash value, so
107       it isn't portable enough to use as a default. It can applicable in certain
108       cases so the containers have a policy based implementation that can use this
109       alternative technique.
110     </p>
111 <p>
112       Currently this is only done on 64 bit architecures, where prime number modulus
113       can be expensive. Although this varies depending on the architecture, so I
114       probably should revisit it.
115     </p>
116 <p>
117       I'm also thinking of introducing a mechanism whereby a hash function can indicate
118       that it's safe to be used directly with power of 2 buckets, in which case a
119       faster plain power of 2 implementation can be used.
120     </p>
121 </div>
122 <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
123 <td align="left"></td>
124 <td align="right"><div class="copyright-footer">Copyright &#169; 2003, 2004 Jeremy B. Maitin-Shepard<br>Copyright &#169; 2005-2008 Daniel
125       James<p>
126         Distributed under the Boost Software License, Version 1.0. (See accompanying
127         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>)
128       </p>
129 </div></td>
130 </tr></table>
131 <hr>
132 <div class="spirit-nav">
133 <a accesskey="p" href="compliance.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="changes.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
134 </div>
135 </body>
136 </html>