RecordVector.hpp
Go to the documentation of this file.
1 /*
2  Copyright 2014 Statoil ASA.
3 
4  This file is part of the Open Porous Media project (OPM).
5 
6  OPM is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  OPM is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with OPM. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 
21 #ifndef _RECORD_VECTOR_
22 #define _RECORD_VECTOR_
23 
24 #include <vector>
25 #include <stdexcept>
26 
27 /*
28  A vector like container which will return the last valid element
29  when the lookup index is out of range.
30 */
31 
32 namespace Opm {
33 
34 template <typename T>
35 class RecordVector {
36 private:
37  std::vector<T> m_data;
38 public:
39  size_t size() const {
40  return m_data.size();
41  }
42 
43  T get(size_t index) const {
44  if (m_data.size() > 0) {
45  if (index >= m_data.size())
46  return m_data.back();
47  else
48  return m_data[index];
49  } else
50  throw std::invalid_argument("Trying to get from empty RecordVector");
51  }
52 
53 
54  void push_back(T value) {
55  m_data.push_back(value);
56  }
57 
58  typename std::vector<T>::const_iterator begin() const {
59  return m_data.begin();
60  }
61 
62 
63  typename std::vector<T>::const_iterator end() const {
64  return m_data.end();
65  }
66 
67 };
68 }
69 
70 #endif
Definition: Deck.hpp:29
std::vector< T >::const_iterator end() const
Definition: RecordVector.hpp:63
std::vector< T >::const_iterator begin() const
Definition: RecordVector.hpp:58
size_t size() const
Definition: RecordVector.hpp:39
Definition: RecordVector.hpp:35
void push_back(T value)
Definition: RecordVector.hpp:54