2008-03-27 23:38:01 +00:00
|
|
|
[/ Copyright 2006-2008 Daniel James.
|
2007-05-20 16:48:52 +00:00
|
|
|
/ Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
/ file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) ]
|
|
|
|
|
|
|
|
[def __wang__
|
2013-11-10 23:25:54 +00:00
|
|
|
[@http://web.archive.org/web/20121102023700/http://www.concentric.net/~Ttwang/tech/inthash.htm
|
2007-05-20 16:48:52 +00:00
|
|
|
Thomas Wang's article on integer hash functions]]
|
|
|
|
|
|
|
|
[section:rationale Implementation Rationale]
|
|
|
|
|
|
|
|
The intent of this library is to implement the unordered
|
|
|
|
containers in the draft standard, so the interface was fixed. But there are
|
2007-11-15 23:36:33 +00:00
|
|
|
still some implementation decisions to make. The priorities are
|
2007-05-20 16:48:52 +00:00
|
|
|
conformance to the standard and portability.
|
|
|
|
|
|
|
|
The [@http://en.wikipedia.org/wiki/Hash_table wikipedia article on hash tables]
|
|
|
|
has a good summary of the implementation issues for hash tables in general.
|
|
|
|
|
|
|
|
[h2 Data Structure]
|
|
|
|
|
|
|
|
By specifying an interface for accessing the buckets of the container the
|
|
|
|
standard pretty much requires that the hash table uses chained addressing.
|
|
|
|
|
|
|
|
It would be conceivable to write a hash table that uses another method. For
|
Merged revisions 41822-41992,41994-42101 via svnmerge from
https://svn.boost.org/svn/boost/branches/unordered/dev
........
r41822 | danieljames | 2007-12-07 12:51:54 +0000 (Fri, 07 Dec 2007) | 5 lines
Change the macros to meet boost guidelines.
I should really have done this before the review. At least it'll give them
something to say.
........
r41928 | danieljames | 2007-12-09 19:23:27 +0000 (Sun, 09 Dec 2007) | 1 line
Add some parameters to standalone documentation build.
........
r41929 | danieljames | 2007-12-09 19:24:07 +0000 (Sun, 09 Dec 2007) | 1 line
An extra rehash test for inserting a range.
........
r41930 | danieljames | 2007-12-09 19:24:52 +0000 (Sun, 09 Dec 2007) | 1 line
get_for_erase can be static because all the required information is in the iterator.
........
r41931 | danieljames | 2007-12-09 19:31:00 +0000 (Sun, 09 Dec 2007) | 1 line
ADL doesn't seem to be working properly on Visual C++ 7.1 when calling swap, so workaround this in the compile tests.
........
r41932 | danieljames | 2007-12-09 19:44:46 +0000 (Sun, 09 Dec 2007) | 1 line
Try to make the erase exception requirements a little clearer.
........
r41933 | danieljames | 2007-12-09 19:52:50 +0000 (Sun, 09 Dec 2007) | 1 line
Hopefully clearer comparison of accessors for comparison/hash function objects.
........
r41943 | danieljames | 2007-12-10 00:03:53 +0000 (Mon, 10 Dec 2007) | 1 line
Fix a typo.
........
r41951 | danieljames | 2007-12-10 11:08:02 +0000 (Mon, 10 Dec 2007) | 1 line
Use the locale in the case insensitive comparison, I really should add a test for this.
........
r41994 | danieljames | 2007-12-13 00:26:05 +0000 (Thu, 13 Dec 2007) | 3 lines
Hervé Brönnimann's improved explanation of the formula for avoiding
invalidating iterators.
........
r41995 | danieljames | 2007-12-13 00:30:46 +0000 (Thu, 13 Dec 2007) | 4 lines
Explicity use the classic locale in the case insensitive example. I could make
the locale a member, but that would make the example longer. Also, this would be
a good place to put a note about the need for constant function objects.
........
r41996 | danieljames | 2007-12-13 00:31:55 +0000 (Thu, 13 Dec 2007) | 1 line
Pull the point examples out into test files - fixing a few bugs in the process.
........
r41997 | danieljames | 2007-12-13 00:41:30 +0000 (Thu, 13 Dec 2007) | 3 lines
A few reference links for boost::hash, it might be better to link to the
first page of the Boost.Hash documentation though.
........
r42092 | danieljames | 2007-12-16 10:07:27 +0000 (Sun, 16 Dec 2007) | 2 lines
Fix some typos, and use American spelling.
........
r42093 | danieljames | 2007-12-16 10:11:00 +0000 (Sun, 16 Dec 2007) | 1 line
Small documentation tweak.
........
r42096 | danieljames | 2007-12-16 10:17:03 +0000 (Sun, 16 Dec 2007) | 1 line
Fix some reference documentation errors.
........
r42097 | danieljames | 2007-12-16 10:28:08 +0000 (Sun, 16 Dec 2007) | 1 line
Document the explicit constructors.
........
r42098 | danieljames | 2007-12-16 10:47:13 +0000 (Sun, 16 Dec 2007) | 1 line
Try to make the active issues and proposals a little clearer - including more obvious links to the relevant papers.
........
r42099 | danieljames | 2007-12-16 10:52:30 +0000 (Sun, 16 Dec 2007) | 1 line
Fix some complexity errors in the comparison table.
........
r42100 | danieljames | 2007-12-16 10:59:45 +0000 (Sun, 16 Dec 2007) | 1 line
Use Mapped instead of T in the documentation.
........
r42101 | danieljames | 2007-12-16 11:06:16 +0000 (Sun, 16 Dec 2007) | 1 line
Remove hard-coded length of prime numbers.
........
[SVN r42187]
2007-12-19 23:09:09 +00:00
|
|
|
example, it could use open addressing, and use the lookup chain to act as a
|
2007-05-20 16:48:52 +00:00
|
|
|
bucket but there are a some serious problems with this:
|
|
|
|
|
|
|
|
* The draft standard requires that pointers to elements aren't invalidated, so
|
|
|
|
the elements can't be stored in one array, but will need a layer of
|
Merged revisions 41822-41992,41994-42101 via svnmerge from
https://svn.boost.org/svn/boost/branches/unordered/dev
........
r41822 | danieljames | 2007-12-07 12:51:54 +0000 (Fri, 07 Dec 2007) | 5 lines
Change the macros to meet boost guidelines.
I should really have done this before the review. At least it'll give them
something to say.
........
r41928 | danieljames | 2007-12-09 19:23:27 +0000 (Sun, 09 Dec 2007) | 1 line
Add some parameters to standalone documentation build.
........
r41929 | danieljames | 2007-12-09 19:24:07 +0000 (Sun, 09 Dec 2007) | 1 line
An extra rehash test for inserting a range.
........
r41930 | danieljames | 2007-12-09 19:24:52 +0000 (Sun, 09 Dec 2007) | 1 line
get_for_erase can be static because all the required information is in the iterator.
........
r41931 | danieljames | 2007-12-09 19:31:00 +0000 (Sun, 09 Dec 2007) | 1 line
ADL doesn't seem to be working properly on Visual C++ 7.1 when calling swap, so workaround this in the compile tests.
........
r41932 | danieljames | 2007-12-09 19:44:46 +0000 (Sun, 09 Dec 2007) | 1 line
Try to make the erase exception requirements a little clearer.
........
r41933 | danieljames | 2007-12-09 19:52:50 +0000 (Sun, 09 Dec 2007) | 1 line
Hopefully clearer comparison of accessors for comparison/hash function objects.
........
r41943 | danieljames | 2007-12-10 00:03:53 +0000 (Mon, 10 Dec 2007) | 1 line
Fix a typo.
........
r41951 | danieljames | 2007-12-10 11:08:02 +0000 (Mon, 10 Dec 2007) | 1 line
Use the locale in the case insensitive comparison, I really should add a test for this.
........
r41994 | danieljames | 2007-12-13 00:26:05 +0000 (Thu, 13 Dec 2007) | 3 lines
Hervé Brönnimann's improved explanation of the formula for avoiding
invalidating iterators.
........
r41995 | danieljames | 2007-12-13 00:30:46 +0000 (Thu, 13 Dec 2007) | 4 lines
Explicity use the classic locale in the case insensitive example. I could make
the locale a member, but that would make the example longer. Also, this would be
a good place to put a note about the need for constant function objects.
........
r41996 | danieljames | 2007-12-13 00:31:55 +0000 (Thu, 13 Dec 2007) | 1 line
Pull the point examples out into test files - fixing a few bugs in the process.
........
r41997 | danieljames | 2007-12-13 00:41:30 +0000 (Thu, 13 Dec 2007) | 3 lines
A few reference links for boost::hash, it might be better to link to the
first page of the Boost.Hash documentation though.
........
r42092 | danieljames | 2007-12-16 10:07:27 +0000 (Sun, 16 Dec 2007) | 2 lines
Fix some typos, and use American spelling.
........
r42093 | danieljames | 2007-12-16 10:11:00 +0000 (Sun, 16 Dec 2007) | 1 line
Small documentation tweak.
........
r42096 | danieljames | 2007-12-16 10:17:03 +0000 (Sun, 16 Dec 2007) | 1 line
Fix some reference documentation errors.
........
r42097 | danieljames | 2007-12-16 10:28:08 +0000 (Sun, 16 Dec 2007) | 1 line
Document the explicit constructors.
........
r42098 | danieljames | 2007-12-16 10:47:13 +0000 (Sun, 16 Dec 2007) | 1 line
Try to make the active issues and proposals a little clearer - including more obvious links to the relevant papers.
........
r42099 | danieljames | 2007-12-16 10:52:30 +0000 (Sun, 16 Dec 2007) | 1 line
Fix some complexity errors in the comparison table.
........
r42100 | danieljames | 2007-12-16 10:59:45 +0000 (Sun, 16 Dec 2007) | 1 line
Use Mapped instead of T in the documentation.
........
r42101 | danieljames | 2007-12-16 11:06:16 +0000 (Sun, 16 Dec 2007) | 1 line
Remove hard-coded length of prime numbers.
........
[SVN r42187]
2007-12-19 23:09:09 +00:00
|
|
|
indirection instead - losing the efficiency and most of the memory gain,
|
2007-05-20 16:48:52 +00:00
|
|
|
the main advantages of open addressing.
|
|
|
|
|
|
|
|
* Local iterators would be very inefficient and may not be able to
|
|
|
|
meet the complexity requirements.
|
|
|
|
|
|
|
|
* There are also the restrictions on when iterators can be invalidated. Since
|
|
|
|
open addressing degrades badly when there are a high number of collisions the
|
|
|
|
restrictions could prevent a rehash when it's really needed. The maximum load
|
|
|
|
factor could be set to a fairly low value to work around this - but the
|
|
|
|
standard requires that it is initially set to 1.0.
|
|
|
|
|
|
|
|
* And since the standard is written with a eye towards chained
|
2007-11-15 23:36:33 +00:00
|
|
|
addressing, users will be surprised if the performance doesn't reflect that.
|
2007-05-20 16:48:52 +00:00
|
|
|
|
|
|
|
So chained addressing is used.
|
|
|
|
|
2011-09-14 21:05:53 +00:00
|
|
|
[/ (Removing for now as this is out of date)
|
|
|
|
|
2007-05-20 16:48:52 +00:00
|
|
|
For containers with unique keys I store the buckets in a single-linked list.
|
|
|
|
There are other possible data structures (such as a double-linked list)
|
|
|
|
that allow for some operations to be faster (such as erasing and iteration)
|
|
|
|
but the possible gain seems small compared to the extra memory needed.
|
|
|
|
The most commonly used operations (insertion and lookup) would not be improved
|
|
|
|
at all.
|
|
|
|
|
|
|
|
But for containers with equivalent keys a single-linked list can degrade badly
|
|
|
|
when a large number of elements with equivalent keys are inserted. I think it's
|
|
|
|
reasonable to assume that users who choose to use `unordered_multiset` or
|
|
|
|
`unordered_multimap` do so because they are likely to insert elements with
|
|
|
|
equivalent keys. So I have used an alternative data structure that doesn't
|
|
|
|
degrade, at the expense of an extra pointer per node.
|
|
|
|
|
|
|
|
This works by adding storing a circular linked list for each group of equivalent
|
|
|
|
nodes in reverse order. This allows quick navigation to the end of a group (since
|
|
|
|
the first element points to the last) and can be quickly updated when elements
|
|
|
|
are inserted or erased. The main disadvantage of this approach is some hairy code
|
|
|
|
for erasing elements.
|
2011-09-14 21:05:53 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
[/ (Starting to write up new structure, might not be ready in time)
|
|
|
|
The node used to be stored in a linked list for each bucket but that
|
|
|
|
didn't meet the complexity requirements for C++11, so now the nodes
|
|
|
|
are stored in one long single linked list. But there needs a way to get
|
|
|
|
the bucket from the node, to do that a copy of the key's hash value is
|
|
|
|
stored in the node. Another possibility would be to store a pointer to
|
|
|
|
the bucket, or the bucket's index, but storing the hash value allows
|
|
|
|
some operations to be faster.
|
|
|
|
]
|
2007-05-20 16:48:52 +00:00
|
|
|
|
|
|
|
[h2 Number of Buckets]
|
|
|
|
|
|
|
|
There are two popular methods for choosing the number of buckets in a hash
|
|
|
|
table. One is to have a prime number of buckets, another is to use a power
|
|
|
|
of 2.
|
|
|
|
|
2007-11-15 23:36:33 +00:00
|
|
|
Using a prime number of buckets, and choosing a bucket by using the modulus
|
|
|
|
of the hash function's result will usually give a good result. The downside
|
|
|
|
is that the required modulus operation is fairly expensive.
|
2007-05-20 16:48:52 +00:00
|
|
|
|
|
|
|
Using a power of 2 allows for much quicker selection of the bucket
|
|
|
|
to use, but at the expense of loosing the upper bits of the hash value.
|
|
|
|
For some specially designed hash functions it is possible to do this and
|
|
|
|
still get a good result but as the containers can take arbitrary hash
|
|
|
|
functions this can't be relied on.
|
|
|
|
|
|
|
|
To avoid this a transformation could be applied to the hash function, for an
|
|
|
|
example see __wang__. Unfortunately, a transformation like Wang's requires
|
|
|
|
knowledge of the number of bits in the hash value, so it isn't portable enough.
|
|
|
|
This leaves more expensive methods, such as Knuth's Multiplicative Method
|
|
|
|
(mentioned in Wang's article). These don't tend to work as well as taking the
|
2007-11-15 23:36:33 +00:00
|
|
|
modulus of a prime, and the extra computation required might negate
|
2007-05-20 16:48:52 +00:00
|
|
|
efficiency advantage of power of 2 hash tables.
|
|
|
|
|
|
|
|
So, this implementation uses a prime number for the hash table size.
|
|
|
|
|
|
|
|
[endsect]
|