opm-common
RegionSetMatcher.hpp
1 /*
2  Copyright 2024 Equinor 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 REGION_SET_MATCHER_HPP
21 #define REGION_SET_MATCHER_HPP
22 
23 #include <cstddef>
24 #include <iterator>
25 #include <memory>
26 #include <optional>
27 #include <string>
28 #include <string_view>
29 #include <utility>
30 #include <vector>
31 
33 
34 namespace Opm {
35  class FIPRegionStatistics;
36 } // namespace Opm
37 
38 namespace Opm {
39 
40 class RegionSetMatcher;
41 
44 {
45 public:
48  {
49  public:
51  class Iterator
52  {
53  public:
55  using iterator_category = std::forward_iterator_tag;
56 
58  using value_type = int;
59 
61  using difference_type = int;
62 
64  using pointer = int*;
65 
67  using reference = int&;
68 
73  {
74  ++this->i_;
75 
76  return *this;
77  }
78 
83  {
84  auto iter = *this;
85 
86  ++(*this);
87 
88  return iter;
89  }
90 
94  reference operator*() { return this->i_; }
95 
99  pointer operator->() { return &this->i_; }
100 
106  bool operator==(Iterator that) const
107  {
108  return this->i_ == that.i_;
109  }
110 
116  bool operator!=(Iterator that) const
117  {
118  return ! (*this == that);
119  }
120 
121  friend class RegionIndexRange;
122 
123  private:
129  explicit Iterator(int i) : i_{i} {}
130 
132  int i_;
133  };
134 
136  Iterator begin() const { return Iterator{this->begin_}; }
137 
139  Iterator end() const { return Iterator{this->end_}; }
140 
142  bool empty() const { return this->end_ <= this->begin_; }
143 
145  std::string_view regionSet() const { return this->region_; }
146 
147  friend class RegionSetMatchResult;
148 
149  private:
151  int begin_{};
152 
154  int end_{};
155 
157  std::string_view region_{};
158 
164  RegionIndexRange() = default;
165 
176  RegionIndexRange(int beginID, int endID, std::string_view region)
177  : begin_ { beginID }
178  , end_ { endID }
179  , region_ { region }
180  {}
181  };
182 
186  bool empty() const
187  {
188  return this->regionIDRange_.empty();
189  }
190 
198  bool isScalar() const
199  {
200  return (this->regionIDRange_.size() == std::vector<int>::size_type{2})
201  && (this->regionIDRange_.back() == this->regionIDRange_.front() + 1);
202  }
203 
207  std::vector<std::string_view> regionSets() const;
208 
212  std::size_t numRegionSets() const
213  {
214  return this->regionSets_.size();
215  }
216 
226  RegionIndexRange regions(std::string_view regSet) const;
227 
236  RegionIndexRange regions(const std::size_t regSet) const;
237 
238  friend class RegionSetMatcher;
239 
240 private:
242  std::vector<std::string> regionSets_{};
243 
248  std::vector<std::vector<std::string>::size_type> regionSetIndex_{};
249 
251  std::vector<int> regionIDRange_{};
252 
256  void establishNameLookupIndex();
257 
269  void addRegionIndices(const std::string& regSet,
270  int beginRegID,
271  int endRegID);
272 };
273 
297 class RegionSetMatcher
298 {
299 public:
303  class SetDescriptor
304  {
305  public:
313  SetDescriptor& regionID(const int region);
314 
326  SetDescriptor& regionID(std::string_view region);
327 
331  const std::optional<int>& regionID() const
332  {
333  return this->regionId_;
334  }
335 
341  SetDescriptor& vectorName(std::string_view vector);
342 
347  const std::optional<std::string>& regionSet() const
348  {
349  return this->regionSet_;
350  }
351 
352  private:
355  std::optional<std::string> regionSet_{};
356 
359  std::optional<int> regionId_{};
360  };
361 
365  RegionSetMatcher() = delete;
366 
370  explicit RegionSetMatcher(const FIPRegionStatistics& fipRegStats);
371 
377  RegionSetMatcher(const RegionSetMatcher& rhs) = delete;
378 
382  RegionSetMatcher(RegionSetMatcher&& rhs);
383 
391  RegionSetMatcher& operator=(const RegionSetMatcher& rhs) = delete;
392 
398  RegionSetMatcher& operator=(RegionSetMatcher&& rhs);
399 
403  ~RegionSetMatcher();
404 
424  RegionSetMatchResult findRegions(const SetDescriptor& regSet) const;
425 
426 private:
428  class Impl;
429 
431  std::unique_ptr<Impl> pImpl_{};
432 };
433 
434 } // namespace Opm
435 
436 #endif // REGION_SET_MATCHER_HPP
bool operator==(Iterator that) const
Equality predicate.
Definition: RegionSetMatcher.hpp:106
Simple forward iterator over a region index range.
Definition: RegionSetMatcher.hpp:51
int * pointer
Iterator&#39;s pointer type (return type from operator->())
Definition: RegionSetMatcher.hpp:64
std::forward_iterator_tag iterator_category
Iterator&#39;s category (forward iterator)
Definition: RegionSetMatcher.hpp:55
RegionSetMatcher()=delete
Default constructor.
int difference_type
Iterator&#39;s difference type.
Definition: RegionSetMatcher.hpp:61
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition: Exceptions.hpp:30
pointer operator->()
Indirection operator.
Definition: RegionSetMatcher.hpp:99
int & reference
Iterator&#39;s reference type (return type from operator*())
Definition: RegionSetMatcher.hpp:67
Region Index Range for Single Region Set.
Definition: RegionSetMatcher.hpp:47
Result Set From RegionSetMatcher&#39;s Matching Process.
Definition: RegionSetMatcher.hpp:43
reference operator*()
Dereference operator.
Definition: RegionSetMatcher.hpp:94
Iterator operator++(int)
Post-increment operator.
Definition: RegionSetMatcher.hpp:82
Iterator & operator++()
Pre-increment operator.
Definition: RegionSetMatcher.hpp:72
int value_type
Iterator&#39;s value type.
Definition: RegionSetMatcher.hpp:58
bool operator!=(const SummaryConfigNode &lhs, const SummaryConfigNode &rhs)
Inequality operator for SummaryConfigNode objects.
Definition: SummaryConfig.hpp:256