mirror of
https://github.com/boostorg/unordered.git
synced 2025-11-07 11:11:41 +01:00
Remove deprecated headers, move hash_fwd.hpp into hash subdirectory. And several minor internal changes. Mostly minor internal details. Merged revisions 51262-51263,51407-51409,51504-51505,51644-51646,51667 via svnmerge from https://svn.boost.org/svn/boost/trunk ........ r51262 | danieljames | 2009-02-15 19:32:04 +0000 (Sun, 15 Feb 2009) | 1 line Use the new 'boost:' links for the hash, unordered and quickbook documentation. ........ r51263 | danieljames | 2009-02-15 19:32:19 +0000 (Sun, 15 Feb 2009) | 2 lines Don't copy images for the standalone hash and unordered documentation, was only really required before the libraries were integrated into boost. ........ r51407 | danieljames | 2009-02-22 23:49:51 +0000 (Sun, 22 Feb 2009) | 1 line Fix the hash dirname. ........ r51408 | danieljames | 2009-02-22 23:50:04 +0000 (Sun, 22 Feb 2009) | 1 line Make copy_buckets and move_buckets member functions - so that calling them is a bit simpler. ........ r51409 | danieljames | 2009-02-22 23:50:20 +0000 (Sun, 22 Feb 2009) | 1 line Move some of the data structure classes out of hash table data. ........ r51504 | danieljames | 2009-03-01 14:15:09 +0000 (Sun, 01 Mar 2009) | 1 line Add missing return for operator=. ........ r51505 | danieljames | 2009-03-01 14:15:39 +0000 (Sun, 01 Mar 2009) | 3 lines Make the sort stable. Doesn't really matter, but it might as well be. ........ r51644 | danieljames | 2009-03-08 09:44:51 +0000 (Sun, 08 Mar 2009) | 1 line Detab. ........ r51645 | danieljames | 2009-03-08 09:45:11 +0000 (Sun, 08 Mar 2009) | 4 lines Move hash_fwd into the hash subdirectory. I should have done this in the last release. But now all of the hash implementation is in the hash subdirectory. ........ r51646 | danieljames | 2009-03-08 09:45:30 +0000 (Sun, 08 Mar 2009) | 3 lines Remove deprecated headers. Fixes #2412. ........ r51667 | danieljames | 2009-03-09 20:56:23 +0000 (Mon, 09 Mar 2009) | 1 line Update copyright dates in hash and unordered. ........ [SVN r51729]
86 lines
3.1 KiB
Plaintext
86 lines
3.1 KiB
Plaintext
[/ Copyright 2006-2008 Daniel James.
|
|
/ 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) ]
|
|
|
|
[section:hash_equality Equality Predicates and Hash Functions]
|
|
|
|
While the associative containers use an ordering relation to specify how the
|
|
elements are stored, the unordered associative containers use an equality
|
|
predicate and a hash function. For example, [classref boost::unordered_map]
|
|
is declared as:
|
|
|
|
template <
|
|
class Key, class Mapped,
|
|
class Hash = ``[classref boost::hash]``<Key>,
|
|
class Pred = std::equal_to<Key>,
|
|
class Alloc = std::allocator<Key> >
|
|
class ``[classref boost::unordered_map unordered_map]``;
|
|
|
|
The hash function comes first as you might want to change the hash function
|
|
but not the equality predicate. For example, if you wanted to use the
|
|
[@http://www.isthe.com/chongo/tech/comp/fnv/ FNV-1 hash] you could write:
|
|
|
|
[import src_code/dictionary.cpp]
|
|
[case_sensitive_dictionary_fnv]
|
|
|
|
There is an [@boost:/libs/unordered/examples/fnv1.hpp implementation
|
|
of FNV-1] in the examples directory.
|
|
|
|
If you wish to use a different equality function,
|
|
you will also need to use a matching hash function. For
|
|
example, to implement a case insensitive dictionary you need to define a
|
|
case insensitive equality predicate and hash function:
|
|
|
|
[case_insensitive_functions]
|
|
|
|
Which you can then use in a case insensitive dictionary:
|
|
|
|
[case_insensitive_dictionary]
|
|
|
|
This is a simplified version of the example at
|
|
[@boost:/libs/unordered/examples/case_insensitive.hpp /libs/unordered/examples/case_insensitive.hpp]
|
|
which supports other locales and string types.
|
|
|
|
[caution
|
|
Be careful when using the equality (`==`) operator with custom equality
|
|
predicates, especially if you're using a function pointer. If you compare two
|
|
containers with different equality predicates then the result is undefined.
|
|
For most stateless function objects this is impossible - since you can only
|
|
compare objects with the same equality predicate you know the equality
|
|
predicates must be equal. But if you're using function pointers or a stateful
|
|
equality predicate (e.g. boost::function) then you can get into trouble.
|
|
]
|
|
|
|
[h2 Custom Types]
|
|
|
|
Similarly, a custom hash function can be used for custom types:
|
|
|
|
[import src_code/point1.cpp]
|
|
[point_example1]
|
|
|
|
Since the default hash function is [link hash Boost.Hash],
|
|
we can [link hash.custom extend it to support the type]
|
|
so that the hash function doesn't need to be explicitly given:
|
|
|
|
[import src_code/point2.cpp]
|
|
[point_example2]
|
|
|
|
See the [link hash.custom Boost.Hash documentation] for more detail on how to
|
|
do this. Remember that it relies on extensions to the draft standard - so it
|
|
won't work on other implementations of the unordered associative containers.
|
|
|
|
[table Methods for accessing the hash and equality functions.
|
|
[[Method] [Description]]
|
|
|
|
[
|
|
[`hasher hash_function() const`]
|
|
[Returns the container's hash function.]
|
|
]
|
|
[
|
|
[`key_equal key_eq() const`]
|
|
[Returns the container's key equality function.]
|
|
]
|
|
]
|
|
|
|
[endsect]
|