opm-common
Loading...
Searching...
No Matches
IOrderSet.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 OPM_IORDER_SET_HPP
21#define OPM_IORDER_SET_HPP
22
23#include <algorithm>
24#include <cstddef>
25#include <iterator>
26#include <stdexcept>
27#include <string>
28#include <unordered_set>
29#include <vector>
30
31namespace Opm {
32
39template <typename T>
41{
42public:
47 IOrderSet() = default;
48
54 explicit IOrderSet(const std::vector<T>& data)
55 : index_ { data.begin(), data.end() }
56 , data_ { data }
57 {
58 if (this->data_.size() != this->index_.size()) {
59 throw std::invalid_argument {
60 "Initial sequence has duplicate elements"
61 };
62 }
63 }
64
66 auto size() const
67 {
68 return this->index_.size();
69 }
70
72 auto empty() const
73 {
74 return this->size() == 0;
75 }
76
82 auto contains(const T& value) const
83 {
84 return this->index_.find(value) != this->index_.end();
85 }
86
97 bool insert(const T& value)
98 {
99 const auto& stat = this->index_.insert(value);
100
101 if (stat.second) {
102 this->data_.push_back(value);
103 }
104
105 return stat.second;
106 }
107
116 std::size_t erase(const T& value)
117 {
118 if (!this->contains(value)) {
119 return 0;
120 }
121
122 this->index_.erase(value);
123
124 const auto data_iter = std::ranges::find(this->data_, value);
125 this->data_.erase(data_iter);
126
127 return 1;
128 }
129
131 auto begin() const { return this->data_.begin(); }
132
134 auto end() const { return this->data_.end(); }
135
144 const T& operator[](const std::size_t i) const
145 {
146 return this->data_.at(i);
147 }
148
150 const std::vector<T>& data() const
151 {
152 return this->data_;
153 }
154
161 bool operator==(const IOrderSet<T>& data) const
162 {
163 return (this->index_ == data.index_)
164 && (this->data_ == data.data_);
165 }
166
172 template <class Serializer>
173 void serializeOp(Serializer& serializer)
174 {
175 serializer(this->index_);
176 serializer(this->data_);
177 }
178
179private:
181 std::unordered_set<T> index_{};
182
184 std::vector<T> data_{};
185};
186
187} // namespace Opm
188
189#endif // OPM_IORDER_SET_HPP
const T & operator[](const std::size_t i) const
Access element by index in ordered collection view.
Definition IOrderSet.hpp:144
std::size_t erase(const T &value)
Remove element from collection.
Definition IOrderSet.hpp:116
auto begin() const
Iterator to first element in ordered collection view.
Definition IOrderSet.hpp:131
bool insert(const T &value)
Insert element into collection.
Definition IOrderSet.hpp:97
auto contains(const T &value) const
Whether or not a particular element exists in the collection.
Definition IOrderSet.hpp:82
IOrderSet(const std::vector< T > &data)
Constructor.
Definition IOrderSet.hpp:54
auto end() const
End of ordered collection view.
Definition IOrderSet.hpp:134
auto empty() const
Whether or not this collection is empty.
Definition IOrderSet.hpp:72
auto size() const
Number of elements in collection.
Definition IOrderSet.hpp:66
void serializeOp(Serializer &serializer)
Convert between byte array and object representation.
Definition IOrderSet.hpp:173
const std::vector< T > & data() const
Ordered collection view.
Definition IOrderSet.hpp:150
bool operator==(const IOrderSet< T > &data) const
Equality predicate.
Definition IOrderSet.hpp:161
IOrderSet()=default
Default constructor.
Class for (de-)serializing.
Definition Serializer.hpp:94
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition Exceptions.hpp:30