From 82384b4ec365f3d2ad2c9bca078a0b38d4be09c0 Mon Sep 17 00:00:00 2001 From: graehl Date: Wed, 11 Aug 2010 02:46:13 +0000 Subject: merge git-svn-id: https://ws10smt.googlecode.com/svn/trunk@511 ec762483-ff6d-05da-a07a-a48fb63a330f --- utils/hash.h | 6 ++++++ utils/small_vector.h | 10 ++++++++++ utils/swap_pod.h | 23 +++++++++++++++++++++++ 3 files changed, 39 insertions(+) create mode 100755 utils/swap_pod.h (limited to 'utils') diff --git a/utils/hash.h b/utils/hash.h index 3a60a429..5d7f2010 100755 --- a/utils/hash.h +++ b/utils/hash.h @@ -51,4 +51,10 @@ struct murmur_hash_array } }; +// adds default val to table if key wasn't found, returns ref to val +template +typename H::data_type & get_default(H &ht,K const& k,typename H::data_type const& v) { + return const_cast(ht.insert(typename H::value_type(k,v)).first->second); +} + #endif diff --git a/utils/small_vector.h b/utils/small_vector.h index 25c52359..ae1e5727 100644 --- a/utils/small_vector.h +++ b/utils/small_vector.h @@ -14,6 +14,7 @@ #include #include #include +#include "swap_pod.h" //sizeof(T)/sizeof(T*)>1?sizeof(T)/sizeof(T*):1 template @@ -245,6 +246,10 @@ public: return !(a==b); } + void swap(Self& o) const { + swap_pod(*this,o); + } + private: union StorageType { T vals[SV_MAX]; @@ -255,6 +260,11 @@ public: uint16_t capacity_; // only defined when size_ > __SV_MAX_STATIC }; +template +inline void swap(SmallVector &a,SmallVector &b) { + a.swap(b); +} + typedef SmallVector SmallVectorInt; template diff --git a/utils/swap_pod.h b/utils/swap_pod.h new file mode 100755 index 00000000..bb9a830d --- /dev/null +++ b/utils/swap_pod.h @@ -0,0 +1,23 @@ +#ifndef SWAP_POD_H +#define SWAP_POD_H + +//for swapping objects of the same concrete type where just swapping their bytes will work. will at least work on plain old data. + +#include // not used, but people who use this will want to bring std::swap in anyway +#include + +template +inline void swap_pod(T &a,T &b) { + using namespace std; + const unsigned s=sizeof(T); + char tmp[s]; + void *pt=(void*)tmp; + void *pa=(void*)&a; + void *pb=(void*)&b; + memcpy(pt,pa,s); + memcpy(pa,pb,s); + memcpy(pb,pt,s); +} + + +#endif -- cgit v1.2.3