Alexandria 2.31.0
SDC-CH common library for the Euclid project
Loading...
Searching...
No Matches
ColumnInfo.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2012-2021 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 "Table/ColumnInfo.h"
27#include <algorithm>
28#include <set>
29
30namespace Euclid {
31namespace Table {
32
33ColumnInfo::ColumnInfo(std::vector<info_type> info_list) : m_info_list{std::move(info_list)} {
34 if (m_info_list.empty()) {
35 throw Elements::Exception() << "Empty info_list is not allowed";
36 }
38 for (const auto& info : m_info_list) {
39 const auto& name = info.name;
40 if (!set.insert(name).second) { // Check for duplicate names
41 throw Elements::Exception() << "Duplicate column name " << name;
42 }
43 }
44}
45
46bool ColumnInfo::operator==(const ColumnInfo& other) const {
47 if (this->m_info_list.size() != other.m_info_list.size()) {
48 return false;
49 }
50 return std::equal(this->m_info_list.cbegin(), this->m_info_list.cend(), other.m_info_list.cbegin());
51}
52
53bool ColumnInfo::operator!=(const ColumnInfo& other) const {
54 return !(*this == other);
55}
56
58 return m_info_list.size();
59}
60
62 if (index >= size()) {
63 throw Elements::Exception("Index out of bounds");
64 }
65 return m_info_list[index];
66}
67
69 auto iter = std::find_if(m_info_list.begin(), m_info_list.end(),
70 [&name](const info_type& info) { return info.name == name; });
71 if (iter == m_info_list.end()) {
72 throw Elements::Exception() << "Column " << name << " does not exist";
73 }
74 return *iter;
75}
76
78 auto begin = m_info_list.begin();
79 auto end = m_info_list.end();
80 auto iter = std::find_if(begin, end, [&name](const info_type& info) { return info.name == name; });
81 if (iter != end) {
82 size_t index{static_cast<size_t>(std::distance(begin, iter))};
83 return std::unique_ptr<size_t>{new size_t{index}};
84 }
86}
87
88} // namespace Table
89} // end of namespace Euclid
Contains the description of a specific column of a Table.
Provides information about the columns of a Table.
Definition ColumnInfo.h:52
std::unique_ptr< std::size_t > find(const std::string &name) const
Returns the index of a column, given the name of it, or nullptr if there is no column with this name.
bool operator==(const ColumnInfo &other) const
Returns true if this ColumnInfo represents the same columns with the given one.
const ColumnDescription & getDescription(std::size_t index) const
Returns the description of the column with the given index or throws an exception if the index is big...
std::size_t size() const
Returns the number of columns represented by this ColumnInfo.
std::vector< info_type > m_info_list
Definition ColumnInfo.h:141
bool operator!=(const ColumnInfo &other) const
Returns false if this ColumnInfo represents the same columns with the given one.
ColumnInfo(std::vector< info_type > info_list)
Constructs a ColumnInfo representing the given column names and types.
T distance(T... args)
T equal(T... args)
T find_if(T... args)
STL namespace.