opm-common
ActionResult.hpp
1 /*
2  Copyright 2019 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 ACTION_RESULT_HPP
21 #define ACTION_RESULT_HPP
22 
23 #include <algorithm>
24 #include <memory>
25 #include <string>
26 #include <vector>
27 
28 namespace Opm::Action {
29 
67 
68 class Result
69 {
70 public:
78  template <typename T>
79  class ValueRange
80  {
81  public:
86  using RandIt = typename std::vector<T>::const_iterator;
87 
99  explicit ValueRange(RandIt first, RandIt last, bool isSorted = false)
100  : first_ { first }
101  , last_ { last }
102  , isSorted_ { isSorted }
103  {}
104 
106  auto begin() const { return this->first_; }
107 
109  auto end() const { return this->last_; }
110 
112  auto empty() const { return this->begin() == this->end(); }
113 
115  auto size() const { return std::distance(this->begin(), this->end()); }
116 
120  std::vector<T> asVector() const
121  {
122  return { this->begin(), this->end() };
123  }
124 
130  bool hasElement(const T& elem) const
131  {
132  return this->isSorted_
133  ? this->hasElementSorted(elem)
134  : this->hasElementUnsorted(elem);
135  }
136 
137  private:
139  RandIt first_{};
140 
142  RandIt last_{};
143 
145  bool isSorted_{false};
146 
152  bool hasElementSorted(const T& elem) const
153  {
154  return std::ranges::binary_search(*this, elem);
155  }
156 
162  bool hasElementUnsorted(const T& elem) const
163  {
164  return std::ranges::find(*this, elem) != this->end();
165  }
166  };
167 
173  {
174  public:
180 
186 
192 
197 
202  MatchingEntities& operator=(const MatchingEntities& that);
203 
211  MatchingEntities& operator=(MatchingEntities&& rhs);
212 
215  ValueRange<std::string> wells() const;
216 
222  bool hasWell(const std::string& well) const;
223 
231  bool operator==(const MatchingEntities& that) const;
232 
233  friend class Result;
234 
235  private:
237  class Impl;
238 
240  std::unique_ptr<Impl> pImpl_;
241 
248  void addWell(const std::string& well);
249 
256  void addWells(const std::vector<std::string>& wells);
257 
265  void makeIntersection(const MatchingEntities& rhs);
266 
274  void makeUnion(const MatchingEntities& rhs);
275 
279  void clear();
280  };
281 
287  explicit Result(bool result_arg);
288 
292  Result(const Result& rhs);
293 
298  Result(Result&& rhs);
299 
303  ~Result();
304 
310  Result& operator=(const Result& rhs);
311 
318  Result& operator=(Result&& rhs);
319 
327  Result& wells(const std::vector<std::string>& w);
328 
333  bool conditionSatisfied() const;
334 
347  Result& makeSetUnion(const Result& rhs);
348 
362  Result& makeSetIntersection(const Result& rhs);
363 
369  const MatchingEntities& matches() const;
370 
377  bool operator==(const Result& that) const;
378 
379 private:
381  class Impl;
382 
384  std::unique_ptr<Impl> pImpl_; // No in-class initializer to workaround nvcc issue
385 };
386 
387 } // Namespace Opm::Action
388 
389 #endif // ACTION_RESULT_HPP
MatchingEntities()
Default constructor.
Definition: ActionResult.cpp:491
Definition: Python.hpp:35
auto end() const
End of value range&#39;s elements.
Definition: ActionResult.hpp:109
Container of matching entities.
Definition: ActionResult.hpp:172
const MatchingEntities & matches() const
Retrieve set of matching entities.
Definition: ActionResult.cpp:716
~Result()
Destructor.
Result & makeSetUnion(const Result &rhs)
Assignment operator.
Definition: ActionResult.cpp:702
std::vector< T > asVector() const
Convert value range to a std::vector.
Definition: ActionResult.hpp:120
Implementation of Action::Result.
Definition: ActionResult.cpp:565
Result(bool result_arg)
Constructor.
Definition: ActionResult.cpp:661
auto empty() const
Predicate for an empty value range.
Definition: ActionResult.hpp:112
bool operator==(const Result &that) const
Equality predicate.
Definition: ActionResult.cpp:721
bool hasElement(const T &elem) const
Element existence predicate.
Definition: ActionResult.hpp:130
auto size() const
Number of elements in the value range.
Definition: ActionResult.hpp:115
typename std::vector< T >::const_iterator RandIt
Random access iterator.
Definition: ActionResult.hpp:86
Implementation of Result::MatchingEntities.
Definition: ActionResult.cpp:348
auto begin() const
Beginning of value range&#39;s elements.
Definition: ActionResult.hpp:106
bool operator==(const MatchingEntities &that) const
Assignment operator.
Definition: ActionResult.cpp:531
Random access range of values.
Definition: ActionResult.hpp:79
ValueRange(RandIt first, RandIt last, bool isSorted=false)
Constructor.
Definition: ActionResult.hpp:99
Result & makeSetIntersection(const Result &rhs)
Incorporate another result set into the current set as if by set intersection.
Definition: ActionResult.cpp:709
Class Action::Result holds the boolean result of a ACTIONX condition like.
Definition: ActionResult.hpp:68