hash_equality.qbk 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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:hash_equality Equality Predicates and Hash Functions]
  5. While the associative containers use an ordering relation to specify how the
  6. elements are stored, the unordered associative containers use an equality
  7. predicate and a hash function. For example, [classref boost::unordered_map]
  8. is declared as:
  9. template <
  10. class Key, class Mapped,
  11. class Hash = ``[classref boost::hash]``<Key>,
  12. class Pred = std::equal_to<Key>,
  13. class Alloc = std::allocator<std::pair<Key const, Mapped> > >
  14. class ``[classref boost::unordered_map unordered_map]``;
  15. The hash function comes first as you might want to change the hash function
  16. but not the equality predicate. For example, if you wanted to use the
  17. [@http://www.isthe.com/chongo/tech/comp/fnv/ FNV-1 hash] you could write:
  18. [import src_code/dictionary.cpp]
  19. [case_sensitive_dictionary_fnv]
  20. There is an [@boost:/libs/unordered/examples/fnv1.hpp implementation
  21. of FNV-1] in the examples directory.
  22. If you wish to use a different equality function,
  23. you will also need to use a matching hash function. For
  24. example, to implement a case insensitive dictionary you need to define a
  25. case insensitive equality predicate and hash function:
  26. [case_insensitive_functions]
  27. Which you can then use in a case insensitive dictionary:
  28. [case_insensitive_dictionary]
  29. This is a simplified version of the example at
  30. [@boost:/libs/unordered/examples/case_insensitive.hpp /libs/unordered/examples/case_insensitive.hpp]
  31. which supports other locales and string types.
  32. [caution
  33. Be careful when using the equality (`==`) operator with custom equality
  34. predicates, especially if you're using a function pointer. If you compare two
  35. containers with different equality predicates then the result is undefined.
  36. For most stateless function objects this is impossible - since you can only
  37. compare objects with the same equality predicate you know the equality
  38. predicates must be equal. But if you're using function pointers or a stateful
  39. equality predicate (e.g. boost::function) then you can get into trouble.
  40. ]
  41. [h2 Custom Types]
  42. Similarly, a custom hash function can be used for custom types:
  43. [import src_code/point1.cpp]
  44. [point_example1]
  45. Since the default hash function is [link hash Boost.Hash],
  46. we can [link hash.custom extend it to support the type]
  47. so that the hash function doesn't need to be explicitly given:
  48. [import src_code/point2.cpp]
  49. [point_example2]
  50. See the [link hash.custom Boost.Hash documentation] for more detail on how to
  51. do this. Remember that it relies on extensions to the draft standard - so it
  52. won't work for other implementations of the unordered associative containers,
  53. you'll need to explicitly use Boost.Hash.
  54. [table:access_methods Methods for accessing the hash and equality functions.
  55. [[Method] [Description]]
  56. [
  57. [`hasher hash_function() const`]
  58. [Returns the container's hash function.]
  59. ]
  60. [
  61. [`key_equal key_eq() const`]
  62. [Returns the container's key equality function.]
  63. ]
  64. ]
  65. [endsect]