1
0
mirror of https://github.com/paboyle/Grid.git synced 2025-06-12 20:27:06 +01:00

HDF5: better complex number support

This commit is contained in:
2017-01-20 12:10:41 -08:00
parent 6b5259cc10
commit afa095d33d
5 changed files with 51 additions and 21 deletions

View File

@ -68,20 +68,21 @@ namespace Grid {
return os;
}
// Vector element trait //////////////////////////////////////////////////////
// Vector element trait //////////////////////////////////////////////////////
template <typename T>
struct element
{
typedef T type;
static constexpr bool is_arithmetic = false;
static constexpr bool is_number = false;
};
template <typename T>
struct element<std::vector<T>>
{
typedef typename element<T>::type type;
static constexpr bool is_arithmetic = std::is_arithmetic<T>::value
or element<T>::is_arithmetic;
static constexpr bool is_number = std::is_arithmetic<T>::value
or is_complex<T>::value
or element<T>::is_number;
};
// Vector flatening utility class ////////////////////////////////////////////

View File

@ -32,10 +32,10 @@ namespace Grid
template <typename U>
void writeDefault(const std::string &s, const U &x);
template <typename U>
typename std::enable_if<element<std::vector<U>>::is_arithmetic, void>::type
typename std::enable_if<element<std::vector<U>>::is_number, void>::type
writeDefault(const std::string &s, const std::vector<U> &x);
template <typename U>
typename std::enable_if<!element<std::vector<U>>::is_arithmetic, void>::type
typename std::enable_if<!element<std::vector<U>>::is_number, void>::type
writeDefault(const std::string &s, const std::vector<U> &x);
private:
template <typename U>
@ -59,10 +59,10 @@ namespace Grid
template <typename U>
void readDefault(const std::string &s, U &output);
template <typename U>
typename std::enable_if<element<std::vector<U>>::is_arithmetic, void>::type
typename std::enable_if<element<std::vector<U>>::is_number, void>::type
readDefault(const std::string &s, std::vector<U> &x);
template <typename U>
typename std::enable_if<!element<std::vector<U>>::is_arithmetic, void>::type
typename std::enable_if<!element<std::vector<U>>::is_number, void>::type
readDefault(const std::string &s, std::vector<U> &x);
private:
template <typename U>
@ -99,7 +99,7 @@ namespace Grid
void Hdf5Writer::writeDefault(const std::string &s, const std::string &x);
template <typename U>
typename std::enable_if<element<std::vector<U>>::is_arithmetic, void>::type
typename std::enable_if<element<std::vector<U>>::is_number, void>::type
Hdf5Writer::writeDefault(const std::string &s, const std::vector<U> &x)
{
// alias to element type
@ -135,7 +135,7 @@ namespace Grid
}
template <typename U>
typename std::enable_if<!element<std::vector<U>>::is_arithmetic, void>::type
typename std::enable_if<!element<std::vector<U>>::is_number, void>::type
Hdf5Writer::writeDefault(const std::string &s, const std::vector<U> &x)
{
push(s);
@ -169,7 +169,7 @@ namespace Grid
void Hdf5Reader::readDefault(const std::string &s, std::string &x);
template <typename U>
typename std::enable_if<element<std::vector<U>>::is_arithmetic, void>::type
typename std::enable_if<element<std::vector<U>>::is_number, void>::type
Hdf5Reader::readDefault(const std::string &s, std::vector<U> &x)
{
// alias to element type
@ -222,7 +222,7 @@ namespace Grid
}
template <typename U>
typename std::enable_if<!element<std::vector<U>>::is_arithmetic, void>::type
typename std::enable_if<!element<std::vector<U>>::is_number, void>::type
Hdf5Reader::readDefault(const std::string &s, std::vector<U> &x)
{
uint64_t size;

View File

@ -10,13 +10,14 @@
#define HDF5_NATIVE_TYPE(predType, cType)\
template <>\
struct Hdf5Type<cType>\
class Hdf5Type<cType>\
{\
static inline const H5NS::PredType & type(void)\
{\
return H5NS::PredType::predType;\
}\
static constexpr bool isNative = true;\
public:\
static inline const H5NS::DataType & type(void)\
{\
return H5NS::PredType::predType;\
}\
static constexpr bool isNative = true;\
};
#define DEFINE_HDF5_NATIVE_TYPES \
@ -38,12 +39,36 @@ HDF5_NATIVE_TYPE(NATIVE_LDOUBLE, long double);
namespace Grid
{
template <typename T> struct Hdf5Type
template <typename T> class Hdf5Type
{
public:
static constexpr bool isNative = false;
};
DEFINE_HDF5_NATIVE_TYPES;
template <typename R>
class Hdf5Type<std::complex<R>>
{
public:
static inline const H5NS::DataType & type(void)
{
if (typePtr_ == nullptr)
{
typePtr_.reset(new H5NS::CompType(sizeof(std::complex<R>)));
typePtr_->insertMember("re", 0, Hdf5Type<R>::type());
typePtr_->insertMember("im", sizeof(R), Hdf5Type<R>::type());
}
return *typePtr_;
}
static constexpr bool isNative = false;
private:
static std::unique_ptr<H5NS::CompType> typePtr_;
};
template <typename R>
std::unique_ptr<H5NS::CompType> Hdf5Type<std::complex<R>>::typePtr_ = nullptr;
}
#undef HDF5_NATIVE_TYPE