PolyDiM
C++ library for POLYtopal DIscretization Methods
Loading...
Searching...
No Matches
ImportExportUtilities.hpp
Go to the documentation of this file.
1// _LICENSE_HEADER_
2//
3// Copyright (C) 2019 - 2025.
4// Terms register on the GPL-3.0 license.
5//
6// This file can be redistributed and/or modified under the license terms.
7//
8// See top level LICENSE file for more details.
9//
10// This file can be used citing references in CITATION.cff file.
11
12#ifndef __ImportExportUtilities_H
13#define __ImportExportUtilities_H
14
15#include "Eigen/Eigen"
16
18{
19
20template <class T, int Rows, int Cols>
21std::ostream &operator<<(std::ostream &out, const Eigen::Matrix<T, Rows, Cols> &matrix)
22{
23 out << matrix.rows() << "," << matrix.cols() << ",";
24 out << "{";
25 for (unsigned int c = 0; c < matrix.cols(); c++)
26 {
27 out << (c != 0 ? ",{" : "{");
28 for (unsigned int r = 0; r < matrix.rows(); r++)
29 out << (r != 0 ? ", " : "") << matrix(r, c);
30 out << "}";
31 }
32 out << "}";
33
34 return out;
35}
36
37template <class T, std::size_t s> std::ostream &operator<<(std::ostream &out, const std::array<T, s> &elements)
38{
39 out << elements.size() << ",";
40 out << "{";
41 unsigned int i = 0;
42 for (const auto &element : elements)
43 {
44 out << (i != 0 ? "," : "") << element;
45 i++;
46 }
47 out << "}";
48
49 return out;
50}
51
52template <class T> std::ostream &operator<<(std::ostream &out, const std::vector<T> &elements)
53{
54 out << elements.size() << ",";
55 out << "{";
56 unsigned int i = 0;
57 for (const auto &element : elements)
58 {
59 out << (i != 0 ? "," : "") << element;
60 i++;
61 }
62 out << "}";
63
64 return out;
65}
66
67template <class T, int Rows, int Cols> std::istream &operator>>(std::istream &in, Eigen::Matrix<T, Rows, Cols> &matrix)
68{
69 char separator;
70 unsigned int rows = 0, cols = 0;
71 in >> rows;
72 in >> separator;
73 in >> cols;
74 matrix.resize(rows, cols);
75 in >> separator;
76
77 in >> separator;
78 for (unsigned int c = 0; c < matrix.cols(); c++)
79 {
80 in >> separator;
81 for (unsigned int r = 0; r < matrix.rows(); r++)
82 {
83 in >> matrix(r, c);
84 in >> separator;
85 }
86 in >> separator;
87 }
88
89 if (matrix.cols() == 0)
90 in >> separator;
91
92 return in;
93}
94
95template <class T, std::size_t s> std::istream &operator>>(std::istream &in, std::array<T, s> &elements)
96{
97 char separator;
98 unsigned int size = 0;
99 in >> size;
100 in >> separator;
101
102 in >> separator;
103 for (unsigned int v = 0; v < size; v++)
104 {
105 T element;
106 in >> element;
107 in >> separator;
108 elements[v] = element;
109 }
110
111 if (size == 0)
112 in >> separator;
113
114 return in;
115}
116
117template <class T> std::istream &operator>>(std::istream &in, std::vector<T> &elements)
118{
119 char separator;
120 unsigned int size = 0;
121 in >> size;
122 elements.resize(size);
123 in >> separator;
124
125 in >> separator;
126 for (unsigned int v = 0; v < size; v++)
127 {
128 T element;
129 in >> element;
130 in >> separator;
131 elements[v] = element;
132 }
133
134 if (size == 0)
135 in >> separator;
136
137 return in;
138}
139} // namespace Gedim_ImportExport_Utilities
140
141#endif // __IOUtilities_H
Definition ImportExportUtilities.hpp:18
std::ostream & operator<<(std::ostream &out, const Eigen::Matrix< T, Rows, Cols > &matrix)
Definition ImportExportUtilities.hpp:21
std::istream & operator>>(std::istream &in, Eigen::Matrix< T, Rows, Cols > &matrix)
Definition ImportExportUtilities.hpp:67