From 4e4f99d51f29aff0d4fbe162362cce54dd6f1529 Mon Sep 17 00:00:00 2001 From: Daniel James Date: Sun, 6 Jul 2008 22:00:18 +0000 Subject: [PATCH] Make the unordered constructors from allocators explicit. And clean of the FNV-1 example & documentation a little. Merged revisions 47085,47132,47143-47146 via svnmerge from https://svn.boost.org/svn/boost/trunk ........ r47085 | danieljames | 2008-07-04 23:57:20 +0100 (Fri, 04 Jul 2008) | 4 lines Require explicit conversion from allocators. (Not what it says in the draft standard, but I think that might be a defect). ........ r47132 | danieljames | 2008-07-06 13:41:09 +0100 (Sun, 06 Jul 2008) | 2 lines 'Bias' should be 'basis'. ........ r47143 | danieljames | 2008-07-06 22:06:52 +0100 (Sun, 06 Jul 2008) | 1 line Clean up the FNV-1 comments. ........ r47144 | danieljames | 2008-07-06 22:07:31 +0100 (Sun, 06 Jul 2008) | 1 line I've only got one hash function for release, so no need for its own directory. ........ r47145 | danieljames | 2008-07-06 22:08:11 +0100 (Sun, 06 Jul 2008) | 1 line Remove the hash_functions directory (for now). ........ r47146 | danieljames | 2008-07-06 22:29:47 +0100 (Sun, 06 Jul 2008) | 1 line Update the docs for the new location of FNV-1. ........ [SVN r47150] --- doc/hash_equality.qbk | 4 +- doc/ref.xml | 10 ++--- doc/src_code/dictionary.cpp | 2 +- examples/fnv1.hpp | 66 ++++++++++++++++++++++++++++ examples/hash_functions/fnv-1.hpp | 71 ------------------------------- include/boost/unordered_map.hpp | 5 +-- include/boost/unordered_set.hpp | 6 +-- 7 files changed, 78 insertions(+), 86 deletions(-) create mode 100644 examples/fnv1.hpp delete mode 100644 examples/hash_functions/fnv-1.hpp diff --git a/doc/hash_equality.qbk b/doc/hash_equality.qbk index ca69f61f..7ca4396f 100644 --- a/doc/hash_equality.qbk +++ b/doc/hash_equality.qbk @@ -23,8 +23,8 @@ but not the equality predicate. For example, if you wanted to use the [import src_code/dictionary.cpp] [case_sensitive_dictionary_fnv] -An example implementation of FNV-1, and some other hash functions are supplied -in the examples directory. +There is an [@../../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 diff --git a/doc/ref.xml b/doc/ref.xml index da982b62..008e7c7c 100644 --- a/doc/ref.xml +++ b/doc/ref.xml @@ -181,7 +181,7 @@ file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) The copy constructor. Copies the contained elements, hash function, predicate, maximum load factor and allocator. - + Allocator const& @@ -928,7 +928,7 @@ file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) The copy constructor. Copies the contained elements, hash function, predicate, maximum load factor and allocator. - + Allocator const& @@ -1685,7 +1685,7 @@ file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) The copy constructor. Copies the contained elements, hash function, predicate, maximum load factor and allocator. - + Allocator const& @@ -2483,7 +2483,7 @@ file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) The copy constructor. Copies the contained elements, hash function, predicate, maximum load factor and allocator. - + Allocator const& @@ -3062,4 +3062,4 @@ file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) - \ No newline at end of file + diff --git a/doc/src_code/dictionary.cpp b/doc/src_code/dictionary.cpp index 721952a0..1f8ead92 100644 --- a/doc/src_code/dictionary.cpp +++ b/doc/src_code/dictionary.cpp @@ -6,7 +6,7 @@ #include #include #include -#include "../../examples/hash_functions/fnv-1.hpp" +#include "../../examples/fnv1.hpp" //[case_insensitive_functions struct iequal_to diff --git a/examples/fnv1.hpp b/examples/fnv1.hpp new file mode 100644 index 00000000..60bf8985 --- /dev/null +++ b/examples/fnv1.hpp @@ -0,0 +1,66 @@ + +// Copyright 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) + +// This code is also released into the public domain. + +// Algorithm from: http://www.isthe.com/chongo/tech/comp/fnv/ + +#include + +namespace hash +{ + template + struct basic_fnv_1 + { + std::size_t operator()(std::string const& text) const + { + std::size_t hash = OffsetBasis; + for(std::string::const_iterator it = text.begin(), end = text.end(); + it != end; ++it) + { + hash *= FnvPrime; + hash ^= *it; + } + + return hash; + } + }; + + template + struct basic_fnv_1a + { + std::size_t operator()(std::string const& text) const + { + std::size_t hash = OffsetBasis; + for(std::string::const_iterator it = text.begin(), end = text.end(); + it != end; ++it) + { + hash ^= *it; + hash *= FnvPrime; + } + + return hash; + } + }; + + // For 32 bit machines: + const std::size_t fnv_prime = 16777619u; + const std::size_t fnv_offset_basis = 2166136261u; + + // For 64 bit machines: + // const std::size_t fnv_prime = 1099511628211u; + // const std::size_t fnv_offset_basis = 14695981039346656037u; + + // For 128 bit machines: + // const std::size_t fnv_prime = 309485009821345068724781401u; + // const std::size_t fnv_offset_basis = 275519064689413815358837431229664493455u; + + // For 256 bit machines: + // const std::size_t fnv_prime = 374144419156711147060143317175368453031918731002211u; + // const std::size_t fnv_offset_basis = 100029257958052580907070968620625704837092796014241193945225284501741471925557u; + + typedef basic_fnv_1 fnv_1; + typedef basic_fnv_1a fnv_1a; +} diff --git a/examples/hash_functions/fnv-1.hpp b/examples/hash_functions/fnv-1.hpp deleted file mode 100644 index 9caf1687..00000000 --- a/examples/hash_functions/fnv-1.hpp +++ /dev/null @@ -1,71 +0,0 @@ - -// Copyright 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) - -// Algorithm from: http://www.isthe.com/chongo/tech/comp/fnv/ - -#include - -namespace hash -{ - template - struct basic_fnv_1 - { - std::size_t operator()(std::string const& text) const - { - std::size_t hash = OffsetBias; - for(std::string::const_iterator it = text.begin(), end = text.end(); - it != end; ++it) - { - hash *= FnvPrime; - hash ^= *it; - } - - return hash; - } - }; - - template - struct basic_fnv_1a - { - std::size_t operator()(std::string const& text) const - { - std::size_t hash = OffsetBias; - for(std::string::const_iterator it = text.begin(), end = text.end(); - it != end; ++it) - { - hash ^= *it; - hash *= FnvPrime; - } - - return hash; - } - }; - - // TODO: Select Bias & Prime base on the size of std::size_t. - // - // 32 bit FNV_prime = 16777619 - // 64 bit FNV_prime = 1099511628211 - // 128 bit FNV_prime = 309485009821345068724781401 - // 256 bit FNV_prime = 374144419156711147060143317175368453031918731002211 - // - // 32 bit offset_basis = 2166136261 - // 64 bit offset_basis = 14695981039346656037 - // 128 bit offset_basis = 275519064689413815358837431229664493455 - // 256 bit offset_basis = 100029257958052580907070968620625704837092796014241193945225284501741471925557 - - const std::size_t fnv_prime = 16777619; - // 64 bit FNV_prime = 1099511628211 - // 128 bit FNV_prime = 309485009821345068724781401 - // 256 bit FNV_prime = 374144419156711147060143317175368453031918731002211 - - const std::size_t fnv_offset_bias = 2166136261u; - // 64 bit offset_basis = 14695981039346656037 - // 128 bit offset_basis = 275519064689413815358837431229664493455 - // 256 bit offset_basis = 100029257958052580907070968620625704837092796014241193945225284501741471925557 - - typedef basic_fnv_1 fnv_1; - typedef basic_fnv_1a fnv_1a; - -} diff --git a/include/boost/unordered_map.hpp b/include/boost/unordered_map.hpp index 093c7697..5f711650 100644 --- a/include/boost/unordered_map.hpp +++ b/include/boost/unordered_map.hpp @@ -107,8 +107,7 @@ namespace boost { } - // TODO: Should this be explicit? - unordered_map(allocator_type const& a) + explicit unordered_map(allocator_type const& a) : base(boost::unordered_detail::default_initial_bucket_count, hasher(), key_equal(), a) { @@ -504,7 +503,7 @@ namespace boost { } - unordered_multimap(allocator_type const& a) + explicit unordered_multimap(allocator_type const& a) : base(boost::unordered_detail::default_initial_bucket_count, hasher(), key_equal(), a) { diff --git a/include/boost/unordered_set.hpp b/include/boost/unordered_set.hpp index 1addae10..0f34c0fd 100644 --- a/include/boost/unordered_set.hpp +++ b/include/boost/unordered_set.hpp @@ -104,8 +104,7 @@ namespace boost { } - // TODO: Should this be explicit? - unordered_set(allocator_type const& a) + explicit unordered_set(allocator_type const& a) : base(boost::unordered_detail::default_initial_bucket_count, hasher(), key_equal(), a) { @@ -473,8 +472,7 @@ namespace boost { } - // TODO: Should this be explicit? - unordered_multiset(allocator_type const& a) + explicit unordered_multiset(allocator_type const& a) : base(boost::unordered_detail::default_initial_bucket_count, hasher(), key_equal(), a) {