Alexandria  2.14.1
Please provide a description of the project.
AsciiWriterHelper.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2020 Euclid Science Ground Segment
3  *
4  * This library is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU Lesser General Public License as published by the Free
6  * Software Foundation; either version 3.0 of the License, or (at your option)
7  * any later version.
8  *
9  * This library is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
25 #include <algorithm>
26 #include <boost/lexical_cast.hpp>
28 #include "AsciiWriterHelper.h"
29 
30 namespace Euclid {
31 namespace Table {
32 
33 using NdArray::NdArray;
34 
36  if (type == typeid(bool)) {
37  return "bool";
38  } if (type == typeid(int32_t)) {
39  return "int";
40  } if (type == typeid(int64_t)) {
41  return "long";
42  } if (type == typeid(float)) {
43  return "float";
44  } if (type == typeid(double)) {
45  return "double";
46  } if (type == typeid(std::string)) {
47  return "string";
48  } if (type == typeid(std::vector<bool>)) {
49  return "[bool]";
50  } if (type == typeid(std::vector<int32_t>)) {
51  return "[int]";
52  } if (type == typeid(std::vector<int64_t>)) {
53  return "[long]";
54  } if (type == typeid(std::vector<float>)) {
55  return "[float]";
56  } if (type == typeid(std::vector<double>)) {
57  return "[double]";
58  } if (type == typeid(NdArray<bool>)) {
59  return "[bool+]";
60  } if (type == typeid(NdArray<int32_t>)) {
61  return "[int+]";
62  } if (type == typeid(NdArray<int64_t>)) {
63  return "[long+]";
64  } if (type == typeid(NdArray<float>)) {
65  return "[float+]";
66  } if (type == typeid(NdArray<double>)) {
67  return "[double+]";
68  }
69  throw Elements::Exception() << "Conversion to string for type " << type.name()
70  << " is not supported";
71 }
72 
74  std::vector<size_t> sizes {};
75  // We initialize the values to the required size for the column name
76  auto column_info = table.getColumnInfo();
77  for (size_t i=0; i<column_info->size(); ++i) {
78  sizes.push_back(column_info->getDescription(i).name.size());
79  }
80  for (auto row : table) {
81  for (size_t i=0; i<sizes.size(); ++i) {
82  sizes[i] = std::max(sizes[i], boost::lexical_cast<std::string>(row[i]).size());
83  }
84  }
85  for (auto& s : sizes) {
86  s += 1;
87  }
88  return sizes;
89 }
90 
91 }
92 } // end of namespace Euclid
constexpr double s
STL class.
NdArray(const std::vector< size_t > &shape)
Definition: NdArray.h:62
T max(T... args)
std::string typeToKeyword(std::type_index type)
Converts a type to its string representation.
Represents a table.
Definition: Table.h:49
STL class.
T name(T... args)
std::vector< size_t > calculateColumnLengths(const Table &table)
Calculates the sizes in characters each column of the table needs.