Alexandria  2.16
Please provide a description of the project.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ColumnInfo.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 <set>
28 #include "Table/ColumnInfo.h"
29 
30 namespace Euclid {
31 namespace Table {
32 
34  : m_info_list{std::move(info_list)} {
35  if (m_info_list.empty()) {
36  throw Elements::Exception() << "Empty info_list is not allowed";
37  }
38  std::set<std::string> set {};
39  for (const auto& info : m_info_list) {
40  const auto& name = info.name;
41  if (!set.insert(name).second) { // Check for duplicate names
42  throw Elements::Exception() << "Duplicate column name " << name;
43  }
44  }
45 }
46 
47 bool ColumnInfo::operator==(const ColumnInfo& other) const {
48  if (this->m_info_list.size() != other.m_info_list.size()) {
49  return false;
50  }
51  return std::equal(this->m_info_list.cbegin(), this->m_info_list.cend(), other.m_info_list.cbegin());
52 }
53 
54 bool ColumnInfo::operator !=(const ColumnInfo& other) const {
55  return !(*this == other);
56 }
57 
59  return m_info_list.size();
60 }
61 
63  if (index >= size()) {
64  throw Elements::Exception("Index out of bounds");
65  }
66  return m_info_list[index];
67 }
68 
70  auto iter = std::find_if(m_info_list.begin(), m_info_list.end(),
71  [&name](const info_type& info) { return info.name == name; });
72  if (iter == m_info_list.end()) {
73  throw Elements::Exception() << "Column " << name << " does not exist";
74  }
75  return *iter;
76 }
77 
79  auto begin = m_info_list.begin();
80  auto end = m_info_list.end();
81  auto iter = std::find_if(begin, end, [&name](const info_type& info){return info.name == name;});
82  if (iter != end) {
83  size_t index {static_cast<size_t>(std::distance(begin, iter))};
84  return std::unique_ptr<size_t> {new size_t {index}};
85  }
86  return std::unique_ptr<size_t> {};
87 }
88 
89 }
90 } // end of namespace Euclid
bool operator==(const ColumnInfo &other) const
Returns true if this ColumnInfo represents the same columns with the given one.
Definition: ColumnInfo.cpp:47
std::vector< info_type > m_info_list
Definition: ColumnInfo.h:142
T distance(T...args)
std::size_t size() const
Returns the number of columns represented by this ColumnInfo.
Definition: ColumnInfo.cpp:58
STL class.
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...
Definition: ColumnInfo.cpp:62
T move(T...args)
bool operator!=(const ColumnInfo &other) const
Returns false if this ColumnInfo represents the same columns with the given one.
Definition: ColumnInfo.cpp:54
T find_if(T...args)
STL class.
STL class.
Provides information about the columns of a Table.
Definition: ColumnInfo.h:52
STL class.
ColumnInfo(std::vector< info_type > info_list)
Constructs a ColumnInfo representing the given column names and types.
Definition: ColumnInfo.cpp:33
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...
Definition: ColumnInfo.cpp:78
T equal(T...args)
Contains the description of a specific column of a Table.