Alexandria  2.25.0
SDC-CH common library for the Euclid project
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Table.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/Table.h"
27 
28 namespace Euclid {
29 namespace Table {
30 
31 Table::Table(std::vector<Row> row_list) : m_row_list{std::move(row_list)}, m_column_info{} {
32  // Check we have some rows
33  if (m_row_list.empty()) {
34  throw Elements::Exception() << "Construction of empty tables is not allowed";
35  }
36  // We cannot initialize the m_column_info before this point because we must
37  // be sure the row list is not empty
38  m_column_info = m_row_list[0].getColumnInfo();
39  // Check that all the rows have the same column info
40  for (auto row : m_row_list) {
41  if (*row.getColumnInfo() != *m_column_info) {
42  throw Elements::Exception() << "Construction of table from rows with different "
43  << "columns is not allowed";
44  }
45  }
46 }
47 
48 Table::Table(std::shared_ptr<ColumnInfo> column_info) : m_column_info(std::move(column_info)) {}
49 
50 std::shared_ptr<ColumnInfo> Table::getColumnInfo() const {
51  return m_column_info;
52 }
53 
54 std::size_t Table::size() const {
55  return m_row_list.size();
56 }
57 
58 const Row& Table::operator[](std::size_t index) const {
59  if (index >= m_row_list.size()) {
60  throw Elements::Exception("Index out of bounds");
61  }
62  return m_row_list[index];
63 }
64 
65 Table::const_iterator Table::begin() const {
66  return m_row_list.cbegin();
67 }
68 
69 Table::const_iterator Table::end() const {
70  return m_row_list.cend();
71 }
72 
73 } // namespace Table
74 } // end of namespace Euclid
std::vector< Row >::const_iterator const_iterator
Definition: Table.h:52
std::shared_ptr< ColumnInfo > m_column_info
Definition: Table.h:123
T move(T...args)
Represents one row of a Table.
Definition: Row.h:64
Represents a table.
Definition: Table.h:49
STL class.