31#ifndef GRID_SERIALISATION_VECTORUTILS_H
32#define GRID_SERIALISATION_VECTORUTILS_H
40 template <
typename T1,
typename T2>
41 inline std::istream &
operator>>(std::istream &is, std::pair<T1, T2> &buf)
51 }
while (c !=
'(' && !is.eof());
54 int start = is.tellg();
58 }
while (c !=
')' && !is.eof());
62 int psize = end - start - 1;
66 std::string tmpstr(psize,
' ');
67 is.read(&tmpstr[0], psize);
68 std::istringstream temp(tmpstr);
70 buf = std::make_pair(buf1, buf2);
79 template <
class T1,
class T2>
80 inline std::ostream &
operator<<(std::ostream &os,
const std::pair<T1, T2> &p)
82 os <<
"(" << p.first <<
" " << p.second <<
")";
87 template<
typename T,
unsigned int Rank>
104 template <
typename T>
110 template <
typename T,
int N>
113 typedef typename std::vector<typename TensorToVec<T>::type>
type;
116 template <
typename T,
int N>
119 typedef typename std::vector<std::vector<typename TensorToVec<T>::type>>
type;
122 template <
typename T>
123 void tensorDim(std::vector<size_t> &dim,
const T &t,
const bool wipe =
true)
131 template <
typename T>
141 template <
typename T,
int N>
152 template <
typename T,
int N>
164 template <
typename T>
170 template <
typename T>
176 template <
typename T,
int N>
182 for (
unsigned int i = 0; i < N; i++)
190 template <
typename T,
int N>
196 for (
unsigned int i = 0; i < N; i++)
199 for (
unsigned int j = 0; j < N; j++)
208 template <
typename T>
215 template <
typename T>
221 template <
typename T,
int N>
224 for (
unsigned int i = 0; i < N; i++)
230 template <
typename T,
int N>
233 for (
unsigned int i = 0; i < N; i++)
234 for (
unsigned int j = 0; j < N; j++)
241 template <
typename T,
typename V =
void>
251 template <
typename T>
261 template <
typename T>
274 template <
typename V>
281 explicit Flatten(
const V &vector);
284 const std::vector<size_t> &
getDim(
void)
const {
return dim_; }
290 template <
typename W>
typename std::enable_if< is_flattenable<W>::value>
::type
296 template <
typename W>
typename std::enable_if< is_flattenable<W>::value>
::type
305 template <
typename V>
313 const std::vector<size_t> &dim);
316 const std::vector<size_t> &
getDim(
void)
const {
return dim_; }
322 template <
typename W>
typename std::enable_if< is_flattenable<W>::value>
::type
325 resize(
W &v,
const unsigned int dim);
328 template <
typename W>
typename std::enable_if<!is_flattenable<W>::isGridTensor>
::type
330 template <
typename W>
typename std::enable_if< is_flattenable<W>::isGridTensor>
::type
341 template <
typename V>
348 template <
typename V>
352 for (
const Scalar &x: e) {
353 flatVector_.push_back(x);
357 template <
typename V>
358 template <
typename W>
typename std::enable_if<is_flattenable<W>::value>::type
367 template <
typename V>
371 using Traits = GridTypeMapper<typename is_flattenable<W>::grid_type>;
372 for (
int rank=0; rank < Traits::Rank; ++rank)
373 dim_.push_back(Traits::Dimension(rank));
376 template <
typename V>
377 template <
typename W>
typename std::enable_if<is_flattenable<W>::value>::type
380 dim_.push_back(v.size());
384 template <
typename V>
389 std::size_t TotalSize{
dim_[0] };
390 for (
int i = 1; i <
dim_.size(); ++i) {
391 TotalSize *=
dim_[i];
398 template <
typename V>
405 template <
typename V>
411 e = flatVector_[ind_++];
415 template <
typename V>
416 template <
typename W>
typename std::enable_if<is_flattenable<W>::value>::type
425 template <
typename V>
432 template <
typename V>
443 template <
typename V>
444 template <
typename W>
typename std::enable_if<is_flattenable<W>::isGridTensor>::type
447 using Traits = GridTypeMapper<typename is_flattenable<W>::grid_type>;
448 const int gridRank{Traits::Rank};
449 const int dimRank{
static_cast<int>(dim_.size())};
450 assert(dimRank >= gridRank &&
"Tensor rank too low for Grid tensor");
451 for (
int i=0; i<gridRank; ++i) {
452 assert(dim_[dimRank - gridRank + i] == Traits::Dimension(i) &&
"Tensor dimension doesn't match Grid tensor");
454 dim_.resize(dimRank - gridRank);
457 template <
typename V>
459 const std::vector<size_t> &dim)
471 template <
typename T>
474 std::istringstream sstr(s);
477 for(T buf; sstr >> buf;)
487 inline std::ostream &
operator<<(std::ostream &os,
const std::vector<T> &v)
490 for (
unsigned int i = 0; i < v.size(); ++i)
493 if (i < v.size() - 1)
504 template <
typename T>
510 template <
typename T>
511 bool isRegularShapeHelper(
const std::vector<std::vector<T>> &v, std::vector<std::size_t> &Dims,
int Depth,
bool bFirst)
515 assert( Dims.size() == Depth &&
"Bug: Delete this message after testing" );
516 Dims.push_back(v[0].size());
522 assert( Dims.size() >= Depth + 1 &&
"Bug: Delete this message after testing" );
524 for (std::size_t i = 0; i < v.size(); ++i)
534 template <
typename T>
537 template <
typename T>
541 template <
typename T>
544 if (v.empty() || v[0].empty())
547 std::vector<std::size_t> Dims;
549 Dims.push_back(v.size());
550 Dims.push_back(v[0].size());
551 for (std::size_t i = 0; i < Dims[0]; ++i)
566 using Grid::operator<<;
568 std::ostringstream sstr;
std::string vecToStr(const std::vector< T > &v)
const std::vector< size_t > & getDim(void) const
static constexpr bool isGridTensor
std::enable_if<!is_flattenable< W >::value &&!is_flattenable< W >::isGridTensor >::type accumulate(const W &e)
const std::vector< Scalar > & getFlatVector(void) const
const V & getVector(void) const
typename is_flattenable< V >::type Scalar
std::enable_if<!is_flattenable< W >::value &&!is_flattenable< W >::isGridTensor >::type accumulateDim(const W &e)
std::enable_if< is_flattenable< W >::value >::type accumulateDim(const W &v)
std::enable_if< is_flattenable< W >::value >::type accumulate(const W &v)
std::enable_if<!is_flattenable< W >::value &&is_flattenable< W >::isGridTensor >::type accumulateDim(const W &e)
std::vector< size_t > dim_
std::enable_if<!is_flattenable< W >::value &&is_flattenable< W >::isGridTensor >::type accumulate(const W &e)
std::vector< Scalar > flatVector_
Reconstruct(const std::vector< Scalar > &flatVector, const std::vector< size_t > &dim)
std::enable_if< is_flattenable< W >::value &&is_flattenable< W >::vecRank==1 >::type resize(W &v, const unsigned int dim)
const std::vector< Scalar > & flatVector_
std::vector< size_t > dim_
std::enable_if<!is_flattenable< W >::isGridTensor >::type checkInnermost(const W &e)
const std::vector< Scalar > & getFlatVector(void) const
static constexpr bool isGridTensor
const std::vector< size_t > & getDim(void) const
typename is_flattenable< V >::type Scalar
const V & getVector(void) const
std::enable_if<!is_flattenable< W >::value &&is_flattenable< W >::isGridTensor >::type fill(W &v)
std::enable_if<!is_flattenable< W >::value &&!is_flattenable< W >::isGridTensor >::type fill(W &v)
bool isRegularShapeHelper(const std::vector< T > &, std::vector< std::size_t > &, int, bool)
bool isRegularShape(const T &t)
std::vector< T > strToVec(const std::string s)
std::istream & operator>>(std::istream &is, std::pair< T1, T2 > &buf)
void tensorDim(std::vector< size_t > &dim, const T &t, const bool wipe=true)
TensorToVec< T >::type tensorToVec(const T &t)
void vecToTensor(T &t, const typename TensorToVec< T >::type &v)
std::ostream & operator<<(std::ostream &os, const std::pair< T1, T2 > &p)
std::vector< typename NestedStdVector< T, Rank - 1 >::type > type
std::vector< std::vector< typename TensorToVec< T >::type > > type
TensorToVec< T >::type type
std::vector< typename TensorToVec< T >::type > type
static constexpr int vecRank
static constexpr bool children_flattenable
static constexpr bool isGridTensor
typename GridTypeMapper< T >::scalar_type type
typename is_flattenable< T >::grid_type grid_type
static constexpr int vecRank
static constexpr bool children_flattenable
typename is_flattenable< T >::type type
static constexpr bool isGridTensor
static constexpr bool isGridTensor
static constexpr bool children_flattenable
static constexpr int vecRank