opm-common
DeckView.hpp
1 /*
2  Copyright 2021 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 #ifndef DECKVIEW_HPP
21 #define DECKVIEW_HPP
22 
23 #include <opm/input/eclipse/Deck/DeckKeyword.hpp>
24 
25 #include <iterator>
26 #include <unordered_map>
27 
28 namespace Opm {
29 
30 
31 class DeckView {
32 public:
33  typedef std::vector<std::reference_wrapper<const DeckKeyword>> storage_type;
34 
35 
36  struct Iterator {
37  explicit Iterator(storage_type::const_iterator inner_iter) :
38  inner(inner_iter)
39  {}
40 
41  using difference_type = storage_type::const_iterator::difference_type;
42  using iterator_category = storage_type::const_iterator::iterator_category;
43  using pointer = const DeckKeyword*;
44  using reference = const DeckKeyword&;
45  using value_type = DeckKeyword;
46 
47  const DeckKeyword& operator*() const { return this->inner->get(); }
48  const DeckKeyword* operator->() const { return &this->inner->get(); }
49 
50  Iterator& operator++() { ++this->inner; return *this; }
51  Iterator operator++(int) { auto tmp = *this; ++this->inner; return tmp; }
52 
53  Iterator& operator--() { --this->inner; return *this; }
54  Iterator operator--(int) { auto tmp = *this; --this->inner; return tmp; }
55 
56  Iterator::difference_type operator-(const Iterator &other) const { return this->inner - other.inner; }
57  Iterator operator+(Iterator::difference_type shift) const { Iterator tmp = *this; tmp.inner += shift; return tmp;}
58 
59  friend bool operator== (const Iterator& a, const Iterator& b) { return a.inner == b.inner; };
60  friend bool operator<= (const Iterator& a, const Iterator& b) { return a.inner <= b.inner; };
61  friend bool operator!= (const Iterator& a, const Iterator& b) { return a.inner != b.inner; };
62 
63  private:
64  storage_type::const_iterator inner;
65  };
66 
67  auto begin() const { return Iterator(this->keywords.begin()); }
68  auto end() const { return Iterator(this->keywords.end()); }
69 
70  const DeckKeyword& operator[](std::size_t index) const;
71  DeckView operator[](const std::string& keyword) const;
72  std::vector<std::size_t> index(const std::string& keyword) const;
73  std::size_t count(const std::string& keyword) const;
74  const DeckKeyword& front() const;
75  const DeckKeyword& back() const;
76 
77  DeckView() = default;
78  void add_keyword(const DeckKeyword& kw);
79  bool has_keyword(const std::string& kw) const;
80  bool empty() const;
81  std::size_t size() const;
82 
83  template<class Keyword>
84  bool has_keyword() const {
85  return this->has_keyword( Keyword::keywordName );
86  }
87 
88  template<class Keyword>
89  DeckView get() const {
90  return this->operator[](Keyword::keywordName);
91  }
92 
93 private:
94  storage_type keywords;
95  std::unordered_map<std::string, std::vector<std::size_t>> keyword_index;
96 };
97 
98 }
99 
100 template<>
101 struct std::iterator_traits<Opm::DeckView::Iterator>
102 {
103  using difference_type = Opm::DeckView::Iterator::difference_type;
104  using iterator_category = Opm::DeckView::Iterator::iterator_category;
108 };
109 
110 #endif
Definition: DeckView.hpp:31
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition: Exceptions.hpp:30
Definition: DeckView.hpp:36
Definition: DeckKeyword.hpp:36