hash.h

Go to the documentation of this file.
00001 #ifndef hash_HEADER
00002 #define hash_HEADER
00003 
00004 /***************************************************************************
00005  * Benoit Hudson (C) 2007.
00006  ***************************************************************************/
00007 
00008 #include <boost/static_assert.hpp>
00009 
00010 #include "hash_set.h"
00011 #include "hash_map.h"
00012 #include "hash_by_call.h"
00013 #include "hash_by_cast.h"
00014 #include "details/hash.h"
00015 
00016 namespace hudson {
00017 
00018   /**** Hashing a single word or word-sized item at a time. */
00019   inline size_t hashWord(size_t a) {
00020     return details::hash_single<sizeof(size_t)> :: hash(a);
00021   }
00022 
00023   template <class Int> struct HashWord {
00024     size_t operator() (const Int& i) const { return hashWord(i); }
00025   };
00026 
00027   template <class T>
00028   size_t hashPointer(const T *p) {
00029     return details::hash_single<sizeof(size_t)> :: hash((size_t)p);
00030   }
00031 
00032   template <class T> struct HashPointer {
00033     size_t operator() (const T* p) const { return hashPointer(p); }
00034   };
00035 
00036   /**** Hashing multiple values at once. */
00037 
00038   inline size_t hashWords(size_t a, size_t b) {
00039     // choose the best implementation
00040     return details::hash_pair<sizeof(size_t)>::hash(a, b);
00041   }
00042 
00043   inline size_t hashWords(size_t a, size_t b, size_t c) {
00044     // probably can't do better than jenkin's 3-at-a-time
00045     return details::mixer<sizeof(size_t)>::final(a, b, c);
00046   }
00047 
00048   inline size_t hashWords(size_t a, size_t b, size_t c, size_t d) {
00049     // only mix, don't need final, for the first three; then, we
00050     // hashPair them to get all our properties.
00051     typedef details::mixer<sizeof(size_t)> M;
00052     M::word aa = a, bb = b, cc = c;
00053     M::mix(aa, bb, cc);
00054     return hashWords(cc, d);
00055   }
00056 
00057   template <class iterator, class hasher>
00058   size_t hashItems(const iterator& begin, const iterator& end, const hasher& h) {
00059     BOOST_STATIC_ASSERT(sizeof(h(*begin)) == sizeof(size_t));
00060     return details::hash_range<sizeof(size_t)>::hash(begin, end, h);
00061   }
00062 
00063 }
00064 
00065 namespace hashers {
00066   template <> struct hash<std::string> {
00067     unsigned long operator() (const std::string& s) const {
00068       // TODO: use jenkin's hash
00069       return hash<const char*>()(s.c_str());
00070     }
00071   };
00072 }
00073 
00074 #endif

Generated on Thu Mar 27 19:04:14 2008 by  doxygen 1.4.6