#ifndef CDEC_HASH_H #define CDEC_HASH_H #include #include "murmur_hash.h" #ifdef HAVE_CONFIG_H #include "config.h" #endif #ifdef HAVE_SPARSEHASH # include # include # include # define SPARSE_HASH_MAP google::sparse_hash_map # define HASH_MAP google::dense_hash_map # define HASH_SET google::dense_hash_set # define HASH_MAP_DELETED(h,deleted) do { (h).set_deleted_key(deleted); } while(0) # define HASH_MAP_RESERVED(h,empty,deleted) do { (h).set_empty_key(empty); (h).set_deleted_key(deleted); } while(0) # define HASH_MAP_EMPTY(h,empty) do { (h).set_empty_key(empty); } while(0) #else #ifdef HAVE_CXX11 # include # include #else # include # include namespace std { using std::tr1::unordered_map; using std::tr1::unordered_set; } #endif # define SPARSE_HASH_MAP std::unordered_map # define HASH_MAP std::unordered_map # define HASH_SET std::unordered_set # define HASH_MAP_DELETED(h,deleted) # define HASH_MAP_RESERVED(h,empty,deleted) # define HASH_MAP_EMPTY(h,empty) #endif #define BOOST_HASHED_MAP(k,v) HASH_MAP > namespace { const unsigned GOLDEN_MEAN_FRACTION=2654435769U; } // assumes C is POD template struct murmur_hash { typedef MurmurInt result_type; typedef C /*const&*/ argument_type; result_type operator()(argument_type const& c) const { return MurmurHash((void*)&c,sizeof(c)); } }; // murmur_hash_array isn't std guaranteed safe (you need to use string::data()) template <> struct murmur_hash { typedef MurmurInt result_type; typedef std::string /*const&*/ argument_type; result_type operator()(argument_type const& c) const { return MurmurHash(c.data(),c.size()); } }; // uses begin(),size() assuming contiguous layout and POD template struct murmur_hash_array { typedef MurmurInt result_type; typedef C /*const&*/ argument_type; result_type operator()(argument_type const& c) const { return MurmurHash(&*c.begin(),c.size()*sizeof(*c.begin())); } }; // adds default val to table if key wasn't found, returns ref to val template typename H::mapped_type & get_default(H &ht,K const& k,typename H::mapped_type const& v) { return const_cast(ht.insert(typename H::value_type(k,v)).first->second); } // get_or_construct w/ no arg: just use ht[k] template typename H::mapped_type & get_or_construct(H &ht,K const& k,C0 const& c0) { typedef typename H::mapped_type V; typedef typename H::value_type KV; typename H::iterator_type i=ht.find(k); if (i==ht.end()) { return const_cast(ht.insert(KV(k,V(c0))).first->second); } else { return i->second; } } // get_or_call (0 arg) template typename H::mapped_type & get_or_call(H &ht,K const& k,F const& f) { typedef typename H::mapped_type V; typedef typename H::value_type KV; typename H::iterator_type i=ht.find(k); if (i==ht.end()) { return const_cast(ht.insert(KV(k,f())).first->second); } else { return i->second; } } // the below could also return a ref to the mapped max/min. they have the advantage of not falsely claiming an improvement when an equal value already existed. otherwise you could just modify the get_default and if equal assume new. template bool improve_mapped_max(H &ht,K const& k,typename H::mapped_type const& v) { std::pair inew=ht.insert(typename H::value_type(k,v)); if (inew.second) return true; typedef typename H::mapped_type V; V &oldv=const_cast(inew.first->second); if (oldv bool put(H &ht,K const& k,typename H::mapped_type const& v) { std::pair inew=ht.insert(typename H::value_type(k,v)); if (inew.second) return true; inew.first->second=v; return false; } // does not update old value (returns false) if one exists, otherwise add template bool maybe_add(H &ht,K const& k,typename H::mapped_type const& v) { std::pair inew=ht.insert(typename H::value_type(k,v)); return inew.second; } // ht[k] must not exist (yet) template void add(H &ht,K const& k,typename H::mapped_type const& v) { maybe_add(ht,k,v); } template bool improve_mapped_min(H &ht,K const& k,typename H::mapped_type const& v) { std::pair inew=ht.insert(typename H::value_type(k,v)); if (inew.second) return true; typedef typename H::mapped_type V; V &oldv=const_cast(inew.first->second); if (v