My Project
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 <unordered_map>
26 
27 namespace Opm {
28 
29 
30 class DeckView {
31 public:
32  typedef std::vector<std::reference_wrapper<const DeckKeyword>> storage_type;
33 
34 
35  struct Iterator : public storage_type::iterator {
36  Iterator(storage_type::const_iterator inner_iter) :
37  inner(inner_iter)
38  {}
39 
40  const DeckKeyword& operator*() { return this->inner->get(); }
41  const DeckKeyword* operator->() { return &this->inner->get(); }
42 
43  Iterator& operator++() { ++this->inner; return *this; }
44  Iterator operator++(int) { auto tmp = *this; ++this->inner; return tmp; }
45 
46 
47  Iterator& operator--() { --this->inner; return *this; }
48  Iterator operator--(int) { auto tmp = *this; --this->inner; return tmp; }
49 
50  Iterator::difference_type operator-(const Iterator &other) { return this->inner - other.inner; }
51  Iterator operator+(Iterator::difference_type shift) { Iterator tmp = *this; tmp.inner += shift; return tmp;}
52 
53  friend bool operator== (const Iterator& a, const Iterator& b) { return a.inner == b.inner; };
54  friend bool operator!= (const Iterator& a, const Iterator& b) { return a.inner != b.inner; };
55  private:
56  storage_type::const_iterator inner;
57  };
58 
59  Iterator begin() const { return Iterator(this->keywords.begin()); }
60  Iterator end() const { return Iterator(this->keywords.end()); }
61 
62  const DeckKeyword& operator[](std::size_t index) const;
63  DeckView operator[](const std::string& keyword) const;
64  std::vector<std::size_t> index(const std::string& keyword) const;
65  std::size_t count(const std::string& keyword) const;
66  const DeckKeyword& front() const;
67  const DeckKeyword& back() const;
68 
69  DeckView() = default;
70  void add_keyword(const DeckKeyword& kw);
71  bool has_keyword(const std::string& kw) const;
72  bool empty() const;
73  std::size_t size() const;
74 
75  template<class Keyword>
76  bool has_keyword() const {
77  return this->has_keyword( Keyword::keywordName );
78  }
79 
80  template<class Keyword>
81  DeckView get() const {
82  return this->operator[](Keyword::keywordName);
83  }
84 
85 private:
86  storage_type keywords;
87  std::unordered_map<std::string, std::vector<std::size_t>> keyword_index;
88 };
89 
90 }
91 #endif
Definition: DeckKeyword.hpp:36
Definition: DeckView.hpp:30
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition: Exceptions.hpp:29
Definition: DeckView.hpp:35