/*! \file utils.hpp \author Ha NGUYEN \since 06 Oct 2014 \date 10 Feb 2015 \brief Some utils for Xios */ #ifndef __XIOS_UTILS_HPP__ #define __XIOS_UTILS_HPP__ #include #include #include "array_new.hpp" #include "exception.hpp" namespace xios { template struct CArrayTraits { typedef K ArrayType; }; template struct CArrayBoolTraits : public CArrayTraits { typedef bool Type; }; template<> struct CArrayBoolTraits > { static inline void resizeArray(CArray& boolArray, const std::vector& dimensionSize) { if (1 != dimensionSize.size()) ERROR("utils::CArrayBoolTraits", <<"Dimension of resized array mismatch"< struct CArrayBoolTraits > { static inline void resizeArray(CArray& boolArray, const std::vector& dimensionSize) { if (2 != dimensionSize.size()) ERROR("utils::CArrayBoolTraits", <<"Dimension of resized array mismatch"< struct CArrayBoolTraits > { static inline void resizeArray(CArray& boolArray, const std::vector& dimensionSize) { if (3 != dimensionSize.size()) ERROR("utils::CArrayBoolTraits", <<"Dimension of resized array mismatch"< struct CArrayBoolTraits > { static inline void resizeArray(CArray& boolArray, const std::vector& dimensionSize) { if (4 != dimensionSize.size()) ERROR("utils::CArrayBoolTraits", <<"Dimension of resized array mismatch"< struct CArrayBoolTraits > { static inline void resizeArray(CArray& boolArray, const std::vector& dimensionSize) { if (5 != dimensionSize.size()) ERROR("utils::CArrayBoolTraits", <<"Dimension of resized array mismatch"< struct CArrayBoolTraits > { static inline void resizeArray(CArray& boolArray, const std::vector& dimensionSize) { if (6 != dimensionSize.size()) ERROR("utils::CArrayBoolTraits", <<"Dimension of resized array mismatch"< struct CArrayBoolTraits > { static inline void resizeArray(CArray& boolArray, const std::vector& dimensionSize) { if (7 != dimensionSize.size()) ERROR("utils::CArrayBoolTraits", <<"Dimension of resized array mismatch"< struct Int2Type { enum { value = v }; }; template union TypeToBytes { T value; unsigned char bytes[sizeof(value)]; }; template struct HashAlgorithm { /*! Adapted version of one-at-a-time hash by Bob Jenkins, which is an expanded version of his Dr. Dobbs article. */ static inline size_t jenkins_hash(const T& value) { TypeToBytes u; (u.value) = value; size_t hash = 0; size_t length = sizeof(value); for (size_t i = 0; i < length; ++i) { hash += u.bytes[i]; hash += (hash << 10); hash ^= (hash >> 6); } hash += (hash << 3); hash ^= (hash >> 11); hash += (hash << 15); return hash; } }; template > struct HashXIOS { std::size_t operator()(const T& val) { Algo al; return hash_value(val, al); } private: size_t hash_value(const T& val, Int2Type<0>) { return HashAlgorithm::jenkins_hash(val); } }; template struct NumTraits { typedef K Type; }; template<> struct NumTraits { typedef unsigned long Scalar; typedef Scalar magnitudeType; static inline Scalar sfmin() { return std::numeric_limits::min(); } static inline Scalar sfmax() { return std::numeric_limits::max(); } }; template<> struct NumTraits { typedef double Scalar; typedef Scalar magnitudeType; static inline Scalar sfmin() { return std::numeric_limits::min(); } static inline Scalar sfmax() { return std::numeric_limits::max(); } }; template class sorter { const std::vector& values; public: sorter(const std::vector &v) : values(v) {} bool operator()(int a, int b) { return values[a] < values[b]; } }; template void order(const std::vector& values, std::vector& rv) { std::sort(rv.begin(), rv.end(), sorter(values)); } } #endif // __UTILS_HPP__