buckets.qbk 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. [/ Copyright 2006-2008 Daniel James.
  2. / Distributed under the Boost Software License, Version 1.0. (See accompanying
  3. / file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) ]
  4. [section:buckets The Data Structure]
  5. The containers are made up of a number of 'buckets', each of which can contain
  6. any number of elements. For example, the following diagram shows an [classref
  7. boost::unordered_set unordered_set] with 7 buckets containing 5 elements, `A`,
  8. `B`, `C`, `D` and `E` (this is just for illustration, containers will typically
  9. have more buckets).
  10. [diagram buckets]
  11. In order to decide which bucket to place an element in, the container applies
  12. the hash function, `Hash`, to the element's key (for `unordered_set` and
  13. `unordered_multiset` the key is the whole element, but is referred to as the key
  14. so that the same terminology can be used for sets and maps). This returns a
  15. value of type `std::size_t`. `std::size_t` has a much greater range of values
  16. then the number of buckets, so the container applies another transformation to
  17. that value to choose a bucket to place the element in.
  18. Retrieving the elements for a given key is simple. The same process is applied
  19. to the key to find the correct bucket. Then the key is compared with the
  20. elements in the bucket to find any elements that match (using the equality
  21. predicate `Pred`). If the hash function has worked well the elements will be
  22. evenly distributed amongst the buckets so only a small number of elements will
  23. need to be examined.
  24. There is [link unordered.hash_equality more information on hash functions and
  25. equality predicates in the next section].
  26. You can see in the diagram that `A` & `D` have been placed in the same bucket.
  27. When looking for elements in this bucket up to 2 comparisons are made, making
  28. the search slower. This is known as a collision. To keep things fast we try to
  29. keep collisions to a minimum.
  30. '''
  31. <table frame="all"><title>Methods for Accessing Buckets</title>
  32. <tgroup cols="2">
  33. <thead><row>
  34. <entry><para>Method</para></entry>
  35. <entry><para>Description</para></entry>
  36. </row></thead>
  37. <tbody>
  38. <row>
  39. <entry>'''`size_type bucket_count() const`'''</entry>
  40. <entry>'''The number of buckets.'''</entry>
  41. </row>
  42. <row>
  43. <entry>'''`size_type max_bucket_count() const`'''</entry>
  44. <entry>'''An upper bound on the number of buckets.'''</entry>
  45. </row>
  46. <row>
  47. <entry>'''`size_type bucket_size(size_type n) const`'''</entry>
  48. <entry>'''The number of elements in bucket `n`.'''</entry>
  49. </row>
  50. <row>
  51. <entry>'''`size_type bucket(key_type const& k) const`'''</entry>
  52. <entry>'''Returns the index of the bucket which would contain `k`.'''</entry>
  53. </row>
  54. <row>
  55. <entry>'''`local_iterator begin(size_type n);`'''</entry>
  56. <entry morerows='5'>'''Return begin and end iterators for bucket `n`.'''</entry>
  57. </row>
  58. <row>
  59. <entry>'''`local_iterator end(size_type n);`'''</entry>
  60. </row>
  61. <row>
  62. <entry>'''`const_local_iterator begin(size_type n) const;`'''</entry>
  63. </row>
  64. <row>
  65. <entry>'''`const_local_iterator end(size_type n) const;`'''</entry>
  66. </row>
  67. <row>
  68. <entry>'''`const_local_iterator cbegin(size_type n) const;`'''</entry>
  69. </row>
  70. <row>
  71. <entry>'''`const_local_iterator cend(size_type n) const;`'''</entry>
  72. </row>
  73. </tbody>
  74. </tgroup>
  75. </table>
  76. '''
  77. [h2 Controlling the number of buckets]
  78. As more elements are added to an unordered associative container, the number
  79. of elements in the buckets will increase causing performance to degrade.
  80. To combat this the containers increase the bucket count as elements are inserted.
  81. You can also tell the container to change the bucket count (if required) by
  82. calling `rehash`.
  83. The standard leaves a lot of freedom to the implementer to decide how the
  84. number of buckets is chosen, but it does make some requirements based on the
  85. container's 'load factor', the average number of elements per bucket.
  86. Containers also have a 'maximum load factor' which they should try to keep the
  87. load factor below.
  88. You can't control the bucket count directly but there are two ways to
  89. influence it:
  90. * Specify the minimum number of buckets when constructing a container or
  91. when calling `rehash`.
  92. * Suggest a maximum load factor by calling `max_load_factor`.
  93. `max_load_factor` doesn't let you set the maximum load factor yourself, it just
  94. lets you give a /hint/. And even then, the draft standard doesn't actually
  95. require the container to pay much attention to this value. The only time the
  96. load factor is /required/ to be less than the maximum is following a call to
  97. `rehash`. But most implementations will try to keep the number of elements
  98. below the max load factor, and set the maximum load factor to be the same as
  99. or close to the hint - unless your hint is unreasonably small or large.
  100. [table:bucket_size Methods for Controlling Bucket Size
  101. [[Method] [Description]]
  102. [
  103. [`X(size_type n)`]
  104. [Construct an empty container with at least `n` buckets (`X` is the container type).]
  105. ]
  106. [
  107. [`X(InputIterator i, InputIterator j, size_type n)`]
  108. [Construct an empty container with at least `n` buckets and insert elements
  109. from the range \[`i`, `j`) (`X` is the container type).]
  110. ]
  111. [
  112. [`float load_factor() const`]
  113. [The average number of elements per bucket.]
  114. ]
  115. [
  116. [`float max_load_factor() const`]
  117. [Returns the current maximum load factor.]
  118. ]
  119. [
  120. [`float max_load_factor(float z)`]
  121. [Changes the container's maximum load factor, using `z` as a hint.]
  122. ]
  123. [
  124. [`void rehash(size_type n)`]
  125. [Changes the number of buckets so that there at least `n` buckets, and
  126. so that the load factor is less than the maximum load factor.]
  127. ]
  128. ]
  129. [h2 Iterator Invalidation]
  130. It is not specified how member functions other than `rehash` affect
  131. the bucket count, although `insert` is only allowed to invalidate iterators
  132. when the insertion causes the load factor to be greater than or equal to the
  133. maximum load factor. For most implementations this means that `insert` will only
  134. change the number of buckets when this happens. While iterators can be
  135. invalidated by calls to `insert` and `rehash`, pointers and references to the
  136. container's elements are never invalidated.
  137. In a similar manner to using `reserve` for `vector`s, it can be a good idea
  138. to call `rehash` before inserting a large number of elements. This will get
  139. the expensive rehashing out of the way and let you store iterators, safe in
  140. the knowledge that they won't be invalidated. If you are inserting `n`
  141. elements into container `x`, you could first call:
  142. x.rehash((x.size() + n) / x.max_load_factor());
  143. [blurb Note: `rehash`'s argument is the minimum number of buckets, not the
  144. number of elements, which is why the new size is divided by the maximum load factor.]
  145. [endsect]