3 #include <unordered_map>
5 #include "antares/io/outputs/columns.h"
10 void addStringColumn(
const std::string& name)
12 addColumn<StringColumn>(name);
16 void addIntegralColumn(
const std::string& name)
18 addColumn<IntegralColumn<T>>(name);
21 void addDoubleColumn(
const std::string& name)
23 addColumn<DoubleColumn>(name);
27 void addOptionalColumn(
const std::string& name)
29 addColumn<OptionalColumn<T>>(name);
33 void addValue(
const std::string& column_name,
const T& value)
35 if constexpr (std::is_same_v<T, std::string>)
37 getColumn<StringColumn>(column_name).add(value);
39 else if constexpr (std::is_integral_v<T>)
41 getColumn<IntegralColumn<T>>(column_name).add(value);
43 else if constexpr (std::is_floating_point_v<T>)
45 getColumn<DoubleColumn>(column_name).add(
static_cast<double>(value));
47 else if constexpr (is_optional_v<T>)
49 using Inner =
typename T::value_type;
50 getColumn<OptionalColumn<Inner>>(column_name).add(value);
54 throw std::runtime_error(
"Unsupported type");
58 size_t rowCount()
const
60 return columns_.empty() ? 0 : (*columns_.begin())->size();
63 const std::unordered_map<std::string, size_t>& columnsNameToIndex()
const
65 return name_to_index_;
68 const IColumn& getColumn(
const std::string& name)
const
70 const auto it = name_to_index_.find(name);
71 if (it == name_to_index_.end())
73 throw std::runtime_error(
"Column not found: " + name);
75 return *columns_.at(it->second);
78 [[nodiscard]]
const std::vector<std::unique_ptr<IColumn>>& columns()
const
83 [[nodiscard]]
const std::vector<std::string>& columnNames()
const
90 for (
auto& column: columns_)
97 template<
typename ColumnType>
98 void addColumn(
const std::string& name)
100 if (name_to_index_.contains(name))
102 throw std::runtime_error(
"Column already exists: " + name);
105 std::size_t index = columns_.size();
106 auto col = std::make_unique<ColumnType>();
107 columns_.push_back(std::move(col));
108 columnNames_.push_back(name);
109 name_to_index_.emplace(std::move(name), index);
113 template<
typename ColumnType>
114 ColumnType& getColumn(
const std::string& name)
116 const auto it = name_to_index_.find(name);
117 if (it == name_to_index_.end())
119 throw std::runtime_error(
"Column not found: " + name);
121 return dynamic_cast<ColumnType&
>(*columns_[it->second]);
125 template<
typename ColumnType>
126 ColumnType& getColumn(
const size_t index)
128 if (index >= columns_.size())
130 throw std::out_of_range(
"Column index out of range");
132 return dynamic_cast<ColumnType&
>(*columns_[index]);
135 std::vector<std::unique_ptr<IColumn>> columns_;
136 std::vector<std::string> columnNames_;
137 std::unordered_map<std::string, size_t> name_to_index_;