Loading...
Searching...
No Matches
IOrderSet.hpp
/*
Copyright 2019 Equinor ASA.
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef OPM_IORDER_SET_HPP
#define OPM_IORDER_SET_HPP
#include <algorithm>
#include <cstddef>
#include <iterator>
#include <stdexcept>
#include <string>
#include <unordered_set>
#include <vector>
namespace Opm {
template <typename T>
class IOrderSet
{
public:
IOrderSet() = default;
explicit IOrderSet(const std::vector<T>& data)
: index_ { data.begin(), data.end() }
, data_ { data }
{
if (this->data_.size() != this->index_.size()) {
throw std::invalid_argument {
"Initial sequence has duplicate elements"
};
}
}
auto size() const
{
return this->index_.size();
}
auto empty() const
{
return this->size() == 0;
}
auto contains(const T& value) const
{
return this->index_.find(value) != this->index_.end();
}
bool insert(const T& value)
{
const auto& stat = this->index_.insert(value);
if (stat.second) {
this->data_.push_back(value);
}
return stat.second;
}
std::size_t erase(const T& value)
{
if (!this->contains(value)) {
return 0;
}
this->index_.erase(value);
auto data_iter = std::find(this->data_.begin(), this->data_.end(), value);
this->data_.erase(data_iter);
return 1;
}
auto begin() const { return this->data_.begin(); }
auto end() const { return this->data_.end(); }
const T& operator[](const std::size_t i) const
{
return this->data_.at(i);
}
const std::vector<T>& data() const
{
return this->data_;
}
bool operator==(const IOrderSet<T>& data) const
{
return (this->index_ == data.index_)
&& (this->data_ == data.data_);
}
template <class Serializer>
void serializeOp(Serializer& serializer)
{
serializer(this->index_);
serializer(this->data_);
}
private:
std::unordered_set<T> index_{};
std::vector<T> data_{};
};
} // namespace Opm
#endif // OPM_IORDER_SET_HPP
Opm::IOrderSet::operator[]
const T & operator[](const std::size_t i) const
Access element by index in ordered collection view.
Definition IOrderSet.hpp:144
Opm::IOrderSet::erase
std::size_t erase(const T &value)
Remove element from collection.
Definition IOrderSet.hpp:116
Opm::IOrderSet::begin
auto begin() const
Iterator to first element in ordered collection view.
Definition IOrderSet.hpp:131
Opm::IOrderSet::insert
bool insert(const T &value)
Insert element into collection.
Definition IOrderSet.hpp:97
Opm::IOrderSet::contains
auto contains(const T &value) const
Whether or not a particular element exists in the collection.
Definition IOrderSet.hpp:82
Opm::IOrderSet::IOrderSet
IOrderSet(const std::vector< T > &data)
Constructor.
Definition IOrderSet.hpp:54
Opm::IOrderSet::end
auto end() const
End of ordered collection view.
Definition IOrderSet.hpp:134
Opm::IOrderSet::empty
auto empty() const
Whether or not this collection is empty.
Definition IOrderSet.hpp:72
Opm::IOrderSet::size
auto size() const
Number of elements in collection.
Definition IOrderSet.hpp:66
Opm::IOrderSet::serializeOp
void serializeOp(Serializer &serializer)
Convert between byte array and object representation.
Definition IOrderSet.hpp:173
Opm::IOrderSet::data
const std::vector< T > & data() const
Ordered collection view.
Definition IOrderSet.hpp:150
Opm::IOrderSet::operator==
bool operator==(const IOrderSet< T > &data) const
Equality predicate.
Definition IOrderSet.hpp:161
Opm::IOrderSet::IOrderSet
IOrderSet()=default
Default constructor.
Opm::Serializer
Class for (de-)serializing.
Definition Serializer.hpp:94
Opm
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition Exceptions.hpp:30