diff options
Diffstat (limited to 'utils')
-rwxr-xr-x | utils/hash.h | 28 | ||||
-rwxr-xr-x | utils/maybe_update_bound.h | 17 |
2 files changed, 45 insertions, 0 deletions
diff --git a/utils/hash.h b/utils/hash.h index fbe10b4e..7e38bb2c 100755 --- a/utils/hash.h +++ b/utils/hash.h @@ -60,6 +60,34 @@ typename H::mapped_type & get_default(H &ht,K const& k,typename H::mapped_type c return const_cast<typename H::mapped_type &>(ht.insert(typename H::value_type(k,v)).first->second); } +// get_or_construct w/ no arg: just use ht[k] +template <class H,class K,class C0> +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<V &>(ht.insert(KV(k,V(c0))).first->second); + } else { + return i->second; + } +} + + +// get_or_call (0 arg) +template <class H,class K,class F> +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<V &>(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 <class H,class K> bool improve_mapped_max(H &ht,K const& k,typename H::mapped_type const& v) { diff --git a/utils/maybe_update_bound.h b/utils/maybe_update_bound.h new file mode 100755 index 00000000..d57215d0 --- /dev/null +++ b/utils/maybe_update_bound.h @@ -0,0 +1,17 @@ +#ifndef MAYBE_UPDATE_BOUND_H +#define MAYBE_UPDATE_BOUND_H + +template <class To,class From> +inline void maybe_increase_max(To &to,const From &from) { + if (to<from) + to=from; +} + +template <class To,class From> +inline void maybe_decrease_min(To &to,const From &from) { + if (from<to) + to=from; +} + + +#endif |