37 #ifndef OPM_SPARSEVECTOR_HEADER 38 #define OPM_SPARSEVECTOR_HEADER 43 #include <boost/range/iterator_range.hpp> 44 #include <opm/common/ErrorMacros.hpp> 60 : size_(0), default_elem_()
67 : size_(sz), default_elem_()
76 template <
typename DataIter,
typename IntegerIter>
78 DataIter data_beg, DataIter data_end,
79 IntegerIter index_beg, IntegerIter index_end)
80 : size_(sz), data_(data_beg, data_end), indices_(index_beg, index_end),
84 OPM_ERROR_IF(sz < 0,
"The size of a SparseVector must be non-negative");
85 OPM_ERROR_IF(indices_.size() != data_.size(),
"The number of indices of a SparseVector must equal to the number of entries");
87 int num_ind = indices_.size();
88 for (
int i = 0; i < num_ind; ++i) {
89 int index = indices_[i];
90 if (index <= last_index || index >= sz) {
91 OPM_THROW(std::logic_error,
"Error in SparseVector construction, index is nonincreasing or out of range.");
104 assert(indices_.empty() || index > indices_.back());
105 assert(index < size_);
106 data_.push_back(elem);
107 indices_.push_back(index);
140 return size_ == other.size_ && data_ == other.data_ && indices_ == other.indices_;
150 OPM_ERROR_IF(index < 0,
"The index of a SparseVector must be non-negative (is " << index <<
")");
151 OPM_ERROR_IF(index >= size_,
"The index of a SparseVector must be smaller than the maximum value (is " << index <<
", max value: " << size_ <<
")");
153 std::vector<int>::const_iterator lb = std::lower_bound(indices_.begin(), indices_.end(), index);
154 if (lb != indices_.end() && *lb == index) {
155 return data_[lb - indices_.begin()];
157 return default_elem_;
167 OPM_ERROR_IF(nzindex < 0,
"The index of a SparseVector must be non-negative (is " << nzindex <<
")");
168 OPM_ERROR_IF(nzindex >=
nonzeroSize(),
"The index of a SparseVector must be smaller than the maximum value (is " << nzindex <<
", max value: " <<
nonzeroSize() <<
")");
170 return data_[nzindex];
178 assert(nzindex >= 0);
180 return indices_[nzindex];
189 std::vector<T> data_;
190 std::vector<int> indices_;
199 #endif // OPM_SPARSEVECTOR_HEADER int nonzeroIndex(int nzindex) const
O(1) index access.
Definition: SparseVector.hpp:176
bool operator==(const SparseVector &other) const
Equality.
Definition: SparseVector.hpp:138
int size() const
Returns the size of the vector.
Definition: SparseVector.hpp:118
A SparseVector stores a vector with possibly many empty elements as efficiently as possible...
Definition: SparseVector.hpp:55
const T & nonzeroElement(int nzindex) const
O(1) element access.
Definition: SparseVector.hpp:164
void clear()
Makes the vector empty().
Definition: SparseVector.hpp:130
const T & element(int index) const
O(log n) element access.
Definition: SparseVector.hpp:147
Definition: AnisotropicEikonal.cpp:446
SparseVector(int sz)
Constructs a SparseVector with a given size, but no nonzero elements.
Definition: SparseVector.hpp:66
int nonzeroSize() const
Returns the number of nonzero data elements.
Definition: SparseVector.hpp:124
bool empty() const
Definition: SparseVector.hpp:111
SparseVector(int sz, DataIter data_beg, DataIter data_end, IntegerIter index_beg, IntegerIter index_end)
A constructor taking all the element data for the vector and their indices.
Definition: SparseVector.hpp:77
void addElement(const T &elem, int index)
Appends an element to the vector.
Definition: SparseVector.hpp:102
SparseVector()
Default constructor. Yields an empty SparseVector.
Definition: SparseVector.hpp:59