mirror of
https://github.com/paboyle/Grid.git
synced 2025-04-09 21:50:45 +01:00
Conversion of Grid tensors to std::vector made more elegant, also pair syntax changed to (x y) to avoid issues with JSON/XML
This commit is contained in:
parent
485c5db0fe
commit
8b14096990
@ -34,74 +34,76 @@ Author: Guido Cossu <guido.cossu@ed.ac.uk>
|
|||||||
#include <Grid/tensors/Tensors.h>
|
#include <Grid/tensors/Tensors.h>
|
||||||
|
|
||||||
namespace Grid {
|
namespace Grid {
|
||||||
// Vector IO utilities ///////////////////////////////////////////////////////
|
// Grid scalar tensors to nested std::vectors //////////////////////////////////
|
||||||
// helper function to read space-separated values
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
std::vector<T> strToVec(const std::string s)
|
struct TensorToVec
|
||||||
{
|
{
|
||||||
std::istringstream sstr(s);
|
typedef T type;
|
||||||
T buf;
|
};
|
||||||
std::vector<T> v;
|
|
||||||
|
template <typename T>
|
||||||
while(!sstr.eof())
|
struct TensorToVec<iScalar<T>>
|
||||||
{
|
|
||||||
sstr >> buf;
|
|
||||||
v.push_back(buf);
|
|
||||||
}
|
|
||||||
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
|
|
||||||
// output to streams for vectors
|
|
||||||
template < class T >
|
|
||||||
inline std::ostream & operator<<(std::ostream &os, const std::vector<T> &v)
|
|
||||||
{
|
{
|
||||||
os << "[";
|
typedef TensorToVec<T>::type type;
|
||||||
for (auto &x: v)
|
};
|
||||||
{
|
|
||||||
os << x << " ";
|
template <typename T>
|
||||||
}
|
struct TensorToVec<iScalar<T>>
|
||||||
if (v.size() > 0)
|
{
|
||||||
{
|
typedef TensorToVec<T>::type type;
|
||||||
os << "\b";
|
};
|
||||||
}
|
|
||||||
os << "]";
|
template <typename T, int N>
|
||||||
|
struct TensorToVec<iVector<T, N>>
|
||||||
return os;
|
{
|
||||||
}
|
typedef std::vector<TensorToVec<T>::type> type;
|
||||||
|
};
|
||||||
// convert Grid scalar tensors to std::vectors
|
|
||||||
template <typename T, typename V>
|
template <typename T, int N>
|
||||||
void tensorToVec(V &v, const T& t)
|
struct TensorToVec<iMatrix<T, N>>
|
||||||
|
{
|
||||||
|
typedef std::vector<std::vector<TensorToVec<T>::type>> type;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
TensorToVec<T>::type tensorToVec(const T &t)
|
||||||
{
|
{
|
||||||
v = t;
|
v = t;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T, typename V>
|
template <typename T, typename V>
|
||||||
void tensorToVec(V &v, const iScalar<T>& t)
|
TensorToVec<iScalar<T>>::type tensorToVec(V &v, const iScalar<T>& t)
|
||||||
{
|
{
|
||||||
tensorToVec(v, t._internal);
|
return tensorToVec(t._internal);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T, typename V, int N>
|
template <typename T, int N>
|
||||||
void tensorToVec(std::vector<V> &v, const iVector<T, N>& t)
|
TensorToVec<iVector<T, N>>::type tensorToVec(const iVector<T, N>& t)
|
||||||
{
|
{
|
||||||
|
TensorToVec<iVector<T, N>>::type v;
|
||||||
|
|
||||||
v.resize(N);
|
v.resize(N);
|
||||||
for (unsigned int i = 0; i < N; i++)
|
for (unsigned int i = 0; i < N; i++)
|
||||||
{
|
{
|
||||||
tensorToVec(v[i], t._internal[i]);
|
v[i] = tensorToVec(t._internal[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T, typename V, int N>
|
template <typename T, int N>
|
||||||
void tensorToVec(std::vector<std::vector<V>> &v, const iMatrix<T, N>& t)
|
TensorToVec<iMatrix<T, N>>::type tensorToVec(const iMatrix<T, N>& t)
|
||||||
{
|
{
|
||||||
|
TensorToVec<iMatrix<T, N>>::type v;
|
||||||
|
|
||||||
v.resize(N, std::vector<V>(N));
|
v.resize(N, std::vector<V>(N));
|
||||||
for (unsigned int i = 0; i < N; i++)
|
for (unsigned int i = 0; i < N; i++)
|
||||||
for (unsigned int j = 0; j < N; j++)
|
for (unsigned int j = 0; j < N; j++)
|
||||||
{
|
{
|
||||||
tensorToVec(v[i][j], t._internal[i][j]);
|
v[i][j] = tensorToVec(t._internal[i][j]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Vector element trait //////////////////////////////////////////////////////
|
// Vector element trait //////////////////////////////////////////////////////
|
||||||
@ -217,7 +219,43 @@ namespace Grid {
|
|||||||
template <class T1, class T2>
|
template <class T1, class T2>
|
||||||
inline std::ostream & operator<<(std::ostream &os, const std::pair<T1, T2> &p)
|
inline std::ostream & operator<<(std::ostream &os, const std::pair<T1, T2> &p)
|
||||||
{
|
{
|
||||||
os << "<" << p.first << " " << p.second << ">";
|
os << "{" << p.first << " " << p.second << "}";
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Vector IO utilities ///////////////////////////////////////////////////////
|
||||||
|
// helper function to read space-separated values
|
||||||
|
template <typename T>
|
||||||
|
std::vector<T> strToVec(const std::string s)
|
||||||
|
{
|
||||||
|
std::istringstream sstr(s);
|
||||||
|
T buf;
|
||||||
|
std::vector<T> v;
|
||||||
|
|
||||||
|
while(!sstr.eof())
|
||||||
|
{
|
||||||
|
sstr >> buf;
|
||||||
|
v.push_back(buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
// output to streams for vectors
|
||||||
|
template < class T >
|
||||||
|
inline std::ostream & operator<<(std::ostream &os, const std::vector<T> &v)
|
||||||
|
{
|
||||||
|
os << "[";
|
||||||
|
for (auto &x: v)
|
||||||
|
{
|
||||||
|
os << x << " ";
|
||||||
|
}
|
||||||
|
if (v.size() > 0)
|
||||||
|
{
|
||||||
|
os << "\b";
|
||||||
|
}
|
||||||
|
os << "]";
|
||||||
|
|
||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user