From 1b57d4a77c5d32068d29bf3772d756c2c844e361 Mon Sep 17 00:00:00 2001 From: graehl Date: Thu, 19 Aug 2010 04:46:18 +0000 Subject: ValueArray instead of string for state is 10% faster git-svn-id: https://ws10smt.googlecode.com/svn/trunk@599 ec762483-ff6d-05da-a07a-a48fb63a330f --- utils/hash.h | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'utils/hash.h') 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(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) { -- cgit v1.2.3