Re: hash function for std::vector<int> needed
On Nov 18, 7:21 am, Markus Dehmann <markus.dehm...@gmail.com> wrote:
On Nov 17, 9:47 pm, alan <almkg...@gmail.com> wrote:
On Nov 18, 10:23 am, Markus Dehmann <markus.dehm...@gmail.com> wrote:
I'd like to hash std::vector<int> objects using hash_set, but I'm not
sure what hash function to use. Does anybody have a suggestion?
Possibly you can use boost::hash_combine<int> across the vector
(warning, unchecked):
std::size_t vectorhash(std::vector<int> f){
size_t seed = 0;
for_each(f.start(), f.end(), boost::bind(hash_combine<int>, seed,
_1));
return seed;
}
Perfect! Apparently it gets even easier. After reading this I googled
for hash_combine and found hash_range:
std::vector<std::string> some_strings;
std::size_t hash = boost::hash_range(some_strings.begin(),
some_strings.end());
(http://www.boost.org/doc/html/hash/combine.html)
You might want to check the implementation, to see if the
generated hash code is adequate for your use. (The examples
suggest that it has problems for certain data sets, that e.g.
std::vector<int>( 1 ) and std::vector<int>( 100 ) will hash
equal. But you'd have to look at the actual implementation to
be sure.)
(My own implementation of generic hashing uses FNV, although I'm
not convinced that this is really better than a simple Mersenne
prime hash. it also defines a HashAccumulator, so you can use
std::accumulate, and easily combine several sequences, if that's
what you need.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34