PolyDiM
C++ library for POLYtopal DIscretization Methods
Loading...
Searching...
No Matches
IOStream.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 __GEDIM_IOStream_H
13#define __GEDIM_IOStream_H
14
15#include "Gedim_Macro.hpp"
16#include <unordered_set>
17
18#if USE_MPI == 1
19#include <mpi.h>
20#endif
21
22#include "IOUtilities.hpp"
23#include <cstdarg>
24#include <iostream>
25#include <list>
26#include <map>
27#include <set>
28#include <sstream>
29#include <unordered_map>
30#include <vector>
31
32namespace Gedim
33{
34template <typename matrixType>
35std::string MatrixToString(const matrixType &mat, const std::string &matrixTypeStr, const std::string &matrixName)
36{
37 std::ostringstream str;
38 str.precision(16);
39 str << matrixName << " = " << matrixTypeStr << "(" << mat.rows() << ", " << mat.cols() << ");" << std::endl;
40 for (unsigned int c = 0; c < mat.cols(); c++)
41 {
42 str << matrixName << ".col(" << c << ")<< ";
43 for (unsigned int r = 0; r < mat.rows(); r++)
44 str << std::scientific << (r == 0 ? "" : ",") << mat(r, c);
45 str << ";" << std::endl;
46 }
47
48 return str.str();
49}
50
51template <typename matrixType>
52std::string MatrixCollectionToString(const std::vector<matrixType> &matCollection,
53 const std::string &matrixTypeStr,
54 const std::string &matrixName)
55{
56 std::ostringstream str;
57 str.precision(16);
58
59 str << matrixName << " = std::vector<" << matrixTypeStr << ">(" << matCollection.size() << ");" << std::endl;
60 for (unsigned int v = 0; v < matCollection.size(); v++)
61 {
63 }
64
65 return str.str();
66}
67
68template <typename matrixType>
69std::string MatrixCollectionToString(const std::vector<std::vector<matrixType>> &matCollection,
70 const std::string &matrixTypeStr,
71 const std::string &matrixName)
72{
73 std::ostringstream str;
74 str.precision(16);
75
76 str << matrixName << " = std::vector<std::vector<" << matrixTypeStr << ">>(" << matCollection.size() << ");" << std::endl;
77 for (unsigned int v = 0; v < matCollection.size(); v++)
78 {
80 }
81
82 return str.str();
83}
84
85template <typename matrixType>
86std::string MatrixCollectionToString(const std::vector<std::vector<std::vector<matrixType>>> &matCollection,
87 const std::string &matrixTypeStr,
88 const std::string &matrixName)
89{
90 std::ostringstream str;
91 str.precision(16);
92
93 str << matrixName << " = std::vector<std::vector<std::vector<" << matrixTypeStr << ">>>(" << matCollection.size()
94 << ");" << std::endl;
95 for (unsigned int v = 0; v < matCollection.size(); v++)
96 {
98 }
99
100 return str.str();
101}
102
104template <typename T> std::ostream &operator<<(std::ostream &out, const std::vector<T> &vec)
105{
108
109 unsigned int sizeVector = (maxElementToPrint == 0 || maxElementToPrint > (int)vec.size()) ? vec.size() : maxElementToPrint;
110 unsigned int startIndexVector = (startingIndex >= (int)vec.size()) ? 0 : startingIndex;
111
112 out << "[";
113 for (unsigned int i = startIndexVector; i < startIndexVector + sizeVector && i < vec.size(); i++)
114 out << (i != startIndexVector ? "," : "") << vec.at(i);
115 out << "]";
116
117 return out;
118}
120template <typename T> std::ostream &operator<<(std::ostream &out, const std::vector<T *> &vec)
121{
124
125 unsigned int sizeVector = (maxElementToPrint == 0 || maxElementToPrint > (int)vec.size()) ? vec.size() : maxElementToPrint;
126 unsigned int startIndexVector = (startingIndex >= (int)vec.size()) ? 0 : startingIndex;
127
128 out << "[";
129 for (unsigned int i = startIndexVector; i < startIndexVector + sizeVector && i < vec.size(); i++)
130 out << (i != startIndexVector ? "," : "") << *vec.at(i);
131 out << "]";
132
133 return out;
134}
136template <typename T, size_t s> std::ostream &operator<<(std::ostream &out, const std::array<T, s> &vec)
137{
140
141 unsigned int sizeVector = (maxElementToPrint == 0 || maxElementToPrint > (int)vec.size()) ? vec.size() : maxElementToPrint;
142 unsigned int startIndexVector = (startingIndex >= (int)vec.size()) ? 0 : startingIndex;
143
144 out << "[";
145 for (unsigned int i = startIndexVector; i < startIndexVector + sizeVector && i < vec.size(); i++)
146 out << (i != startIndexVector ? "," : "") << vec.at(i);
147 out << "]";
148
149 return out;
150}
152template <typename T, size_t s> std::ostream &operator<<(std::ostream &out, const std::array<T *, s> &vec)
153{
156
157 unsigned int sizeVector = (maxElementToPrint == 0 || maxElementToPrint > (int)vec.size()) ? vec.size() : maxElementToPrint;
158 unsigned int startIndexVector = (startingIndex >= (int)vec.size()) ? 0 : startingIndex;
159
160 out << "[";
161 for (unsigned int i = startIndexVector; i < startIndexVector + sizeVector && i < vec.size(); i++)
162 out << (i != startIndexVector ? "," : "") << *vec.at(i);
163 out << "]";
164
165 return out;
166}
168template <typename T> std::ostream &operator<<(std::ostream &out, const std::list<T> &listToPrint)
169{
172
173 unsigned int sizeList = (maxElementToPrint == 0 || maxElementToPrint > (int)listToPrint.size()) ? listToPrint.size()
175 unsigned int startIndexList = (startingIndex >= (int)listToPrint.size()) ? 0 : startingIndex;
176 unsigned int counter = 0, elementPrinted = 0;
177
178 out << "{";
179 for (typename std::list<T>::const_iterator iterator = listToPrint.begin(); iterator != listToPrint.end(); iterator++)
180 {
181 if (counter >= startIndexList)
182 {
183 out << (counter != startIndexList ? "," : "") << *iterator;
185 }
186
188 break;
189
190 counter++;
191 }
192 out << "}";
193
194 return out;
195}
197template <typename T> std::ostream &operator<<(std::ostream &out, const std::list<T *> &listToPrint)
198{
201
202 unsigned int sizeList = (maxElementToPrint == 0 || maxElementToPrint > (int)listToPrint.size()) ? listToPrint.size()
204 unsigned int startIndexList = (startingIndex >= (int)listToPrint.size()) ? 0 : startingIndex;
205 unsigned int counter = 0, elementPrinted = 0;
206
207 out << "{";
208 for (typename std::list<T *>::const_iterator iterator = listToPrint.begin(); iterator != listToPrint.end(); iterator++)
209 {
210 if (counter >= startIndexList)
211 {
212 out << (counter != startIndexList ? "," : "") << **iterator;
214 }
215
217 break;
218
219 counter++;
220 }
221 out << "}";
222
223 return out;
224}
226template <typename T> std::ostream &operator<<(std::ostream &out, const std::unordered_set<T> &setToPrint)
227{
230
231 unsigned int sizeList = (maxElementToPrint == 0 || maxElementToPrint > (int)setToPrint.size()) ? setToPrint.size() : maxElementToPrint;
232 unsigned int startIndexList = (startingIndex >= (int)setToPrint.size()) ? 0 : startingIndex;
233 unsigned int counter = 0, elementPrinted = 0;
234
235 out << "{";
236 for (typename std::unordered_set<T>::const_iterator iterator = setToPrint.begin(); iterator != setToPrint.end(); iterator++)
237 {
238 if (counter >= startIndexList)
239 {
240 out << (counter != startIndexList ? "," : "") << *iterator;
242 }
243
245 break;
246
247 counter++;
248 }
249 out << "}";
250
251 return out;
252}
254template <typename T> std::ostream &operator<<(std::ostream &out, const std::unordered_set<T *> &setToPrint)
255{
258
259 unsigned int sizeList = (maxElementToPrint == 0 || maxElementToPrint > (int)setToPrint.size()) ? setToPrint.size() : maxElementToPrint;
260 unsigned int startIndexList = (startingIndex >= (int)setToPrint.size()) ? 0 : startingIndex;
261 unsigned int counter = 0, elementPrinted = 0;
262
263 out << "{";
264 for (typename std::unordered_set<T *>::const_iterator iterator = setToPrint.begin(); iterator != setToPrint.end(); iterator++)
265 {
266 if (counter >= startIndexList)
267 {
268 out << (counter != startIndexList ? "," : "") << **iterator;
270 }
271
273 break;
274
275 counter++;
276 }
277 out << "}";
278
279 return out;
280}
282template <typename T> std::ostream &operator<<(std::ostream &out, const std::set<T> &setToPrint)
283{
286
287 unsigned int sizeList = (maxElementToPrint == 0 || maxElementToPrint > (int)setToPrint.size()) ? setToPrint.size() : maxElementToPrint;
288 unsigned int startIndexList = (startingIndex >= (int)setToPrint.size()) ? 0 : startingIndex;
289 unsigned int counter = 0, elementPrinted = 0;
290
291 out << "{";
292 for (typename std::set<T>::const_iterator iterator = setToPrint.begin(); iterator != setToPrint.end(); iterator++)
293 {
294 if (counter >= startIndexList)
295 {
296 out << (counter != startIndexList ? "," : "") << *iterator;
298 }
299
301 break;
302
303 counter++;
304 }
305 out << "}";
306
307 return out;
308}
310template <typename T> std::ostream &operator<<(std::ostream &out, const std::set<T *> &setToPrint)
311{
314
315 unsigned int sizeList = (maxElementToPrint == 0 || maxElementToPrint > (int)setToPrint.size()) ? setToPrint.size() : maxElementToPrint;
316 unsigned int startIndexList = (startingIndex >= (int)setToPrint.size()) ? 0 : startingIndex;
317 unsigned int counter = 0, elementPrinted = 0;
318
319 out << "{";
320 for (typename std::set<T *>::const_iterator iterator = setToPrint.begin(); iterator != setToPrint.end(); iterator++)
321 {
322 if (counter >= startIndexList)
323 {
324 out << (counter != startIndexList ? "," : "") << **iterator;
326 }
327
329 break;
330
331 counter++;
332 }
333 out << "}";
334
335 return out;
336}
338template <typename map_key, typename map_val>
339std::ostream &operator<<(std::ostream &out, const std::map<map_key, map_val> &mapToPrint)
340{
341 unsigned int counter = 0;
342
343 out << "{";
344 for (typename std::map<map_key, map_val>::const_iterator it = mapToPrint.begin(); it != mapToPrint.end(); ++it)
345 {
346 out << (counter != 0 ? "," : "") << "{" << it->first << ", " << it->second << "}";
347 counter++;
348 }
349 out << "}";
350
351 return out;
352}
354template <typename map_key, typename map_val>
355std::ostream &operator<<(std::ostream &out, const std::map<map_key, map_val *> &mapToPrint)
356{
357 unsigned int counter = 0;
358
359 out << "{";
360 for (typename std::map<map_key, map_val *>::const_iterator it = mapToPrint.begin(); it != mapToPrint.end(); ++it)
361 {
362 out << (counter != 0 ? "," : "") << "{" << it->first << ", " << (it->second == nullptr ? "NULL" : (*it->second)) << "}";
363 counter++;
364 }
365 out << "}";
366
367 return out;
368}
370template <typename map_key, typename map_val>
371std::ostream &operator<<(std::ostream &out, const std::unordered_map<map_key, map_val> &mapToPrint)
372{
373 unsigned int counter = 0;
374
375 out << "{";
376 for (typename std::unordered_map<map_key, map_val>::const_iterator it = mapToPrint.begin(); it != mapToPrint.end(); ++it)
377 {
378 out << (counter != 0 ? "," : "") << "{" << it->first << ", " << it->second << "}";
379 counter++;
380 }
381 out << "}";
382
383 return out;
384}
385
386} // namespace Gedim
387
388#endif
Eigen column vector.
Definition Eigen_Array.hpp:23
static int MaxElementToPrint
Max elements to print in vectors.
Definition IOUtilities.hpp:55
static int StartingIndexToPrint
Starting index to print in vectors.
Definition IOUtilities.hpp:56
Definition Eigen_Array.cpp:22
std::ostream & operator<<(std::ostream &out, const Eigen::VectorXd &vec)
Definition Eigen_Utilities.cpp:21
std::string MatrixToString(const matrixType &mat, const std::string &matrixTypeStr, const std::string &matrixName)
Definition IOStream.hpp:35
std::string MatrixCollectionToString(const std::vector< matrixType > &matCollection, const std::string &matrixTypeStr, const std::string &matrixName)
Definition IOStream.hpp:52