Last active
April 19, 2020 20:38
-
-
Save eudoxos/43f11cfb04d33b4f3e5cf309ad628365 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<vector> | |
#include<type_traits> | |
#include<Eigen/Eigen> | |
#include<xtensor/xtensor.hpp> | |
#include<xtensor/xarray.hpp> | |
#include<iostream> | |
using std::cerr; | |
using std::endl; | |
// fake types, for compat with HighFive decls | |
typedef const int File; | |
typedef void Dataset; | |
namespace detail | |
{ | |
// fallback version, base template | |
template<typename T, typename = void> | |
struct io_impl{ | |
static T load(const File& file, const std::string& path){ | |
cerr<<"load generic"<<endl; | |
return T(); | |
} | |
static Dataset dump(File& file, const std::string& path, const T& data){ | |
cerr<<"dump generic"<<endl; | |
} | |
// add: dump_overwrite, dump_extend | |
}; | |
// Eigen | |
template<typename T> | |
struct io_impl<T, typename std::enable_if<std::is_base_of<Eigen::DenseBase<T>,T>::value>::type>{ | |
static T load(const File& file, const std::string& path){ | |
cerr<<"load Eigen::DenseBase"<<endl; | |
return T(); | |
} | |
static Dataset dump(File& file, const std::string& path, const T& data){ | |
cerr<<"dump Eigen::DenseBase"<<endl; | |
} | |
}; | |
// xtensor | |
template<typename T> | |
struct io_impl<T, typename std::enable_if<std::is_same<xt::xarray<typename T::value_type>,T>::value>::type>{ | |
static T load(const File& file, const std::string& path){ | |
cerr<<"load xt::xarray"<<endl; | |
return T(); | |
} | |
static Dataset dump(File& file, const std::string& path, const T& data){ | |
cerr<<"dump xt::xarray"<<endl; | |
} | |
}; | |
// std::vector | |
template<typename T> | |
struct io_impl<T, typename std::enable_if<std::is_same<std::vector<typename T::value_type>,T>::value>::type>{ | |
static T load(const File& file, const std::string& path){ | |
cerr<<"load std::vector"<<endl; | |
return T(); | |
} | |
static Dataset dump(File& file, const std::string& path, const T& data){ | |
cerr<<"dump std::vector"<<endl; | |
} | |
}; | |
}; | |
// frontend | |
template<class T> | |
inline Dataset dump(File& file, const std::string& path, const T& data) | |
{ | |
// this will normally select dump, dump_extend, dump_overwrite | |
return detail::io_impl<T>::dump(file,path,data); | |
} | |
template<class T> | |
inline T load(File& file, const std::string& path) | |
{ | |
return detail::io_impl<T>::load(file,path); | |
} | |
int main() | |
{ | |
Eigen::ArrayXf A=Eigen::ArrayXf::Random(50); | |
Eigen::MatrixXd B=Eigen::MatrixXd::Random(20,5); | |
xt::xarray<double> C={{1,2,3},{4,5,6}}; | |
std::vector<double> D={1,2,3}; | |
int E=0; | |
dump(0,"",A); | |
dump(0,"",B); | |
dump(0,"",C); | |
dump(0,"",D); | |
dump(0,"",E); | |
load<decltype(A)>(0,""); | |
load<decltype(B)>(0,""); | |
load<decltype(C)>(0,""); | |
load<decltype(D)>(0,""); | |
load<decltype(E)>(0,""); | |
return 0; | |
} | |
/* | |
output: | |
$ g++ -std=c++14 highfive-partial-specialization.cc -I/usr/include/eigen3 -o aa2 | |
$ ./aa2 | |
dump Eigen::DenseBase | |
dump Eigen::DenseBase | |
dump xt::xarray | |
dump std::vector | |
dump generic | |
load Eigen::DenseBase | |
load Eigen::DenseBase | |
load xt::xarray | |
load std::vector | |
load generic | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment