diff options
Diffstat (limited to 'utils')
-rwxr-xr-x | utils/hash.h | 6 | ||||
-rw-r--r-- | utils/small_vector.h | 10 | ||||
-rwxr-xr-x | utils/swap_pod.h | 23 |
3 files changed, 39 insertions, 0 deletions
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 <class H,class K> +typename H::data_type & get_default(H &ht,K const& k,typename H::data_type const& v) { + return const_cast<typename H::data_type &>(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 <stdint.h> #include <new> #include <stdint.h> +#include "swap_pod.h" //sizeof(T)/sizeof(T*)>1?sizeof(T)/sizeof(T*):1 template <class T,int SV_MAX=2> @@ -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 <class T,int SV_MAX> +inline void swap(SmallVector<T,SV_MAX> &a,SmallVector<T,SV_MAX> &b) { + a.swap(b); +} + typedef SmallVector<int,2> SmallVectorInt; template <class T,int N> 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 <algorithm> // not used, but people who use this will want to bring std::swap in anyway +#include <cstring> + +template <class T> +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 |