Loading...
Searching...
No Matches
SparseVector.hpp
//===========================================================================
//
// File: SparseVector.hpp
//
// Created: Mon Jun 29 15:28:59 2009
//
// Author(s): Atgeirr F Rasmussen <atgeirr@sintef.no>
// Bård Skaflestad <bard.skaflestad@sintef.no>
//
// $Date$
//
// $Revision$
//
//===========================================================================
/*
Copyright 2009, 2010 SINTEF ICT, Applied Mathematics.
Copyright 2009, 2010 Statoil 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_SPARSEVECTOR_HEADER
#define OPM_SPARSEVECTOR_HEADER
#include <vector>
#include <numeric>
#include <algorithm>
#include <opm/common/ErrorMacros.hpp>
namespace Opm
{
template <typename T>
class SparseVector
{
public:
SparseVector()
: size_(0), default_elem_()
{
}
explicit SparseVector(int sz)
: size_(sz), default_elem_()
{
}
template <typename DataIter, typename IntegerIter>
SparseVector(int sz,
DataIter data_beg, DataIter data_end,
IntegerIter index_beg, IntegerIter index_end)
: size_(sz), data_(data_beg, data_end), indices_(index_beg, index_end),
default_elem_()
{
#ifndef NDEBUG
OPM_ERROR_IF(sz < 0, "The size of a SparseVector must be non-negative");
OPM_ERROR_IF(indices_.size() != data_.size(), "The number of indices of a SparseVector must equal to the number of entries");
int last_index = -1;
int num_ind = indices_.size();
for (int i = 0; i < num_ind; ++i) {
int index = indices_[i];
if (index <= last_index || index >= sz) {
OPM_THROW(std::logic_error, "Error in SparseVector construction, index is nonincreasing or out of range.");
}
last_index = index;
}
#endif
}
void addElement(const T& elem, int index)
{
assert(indices_.empty() || index > indices_.back());
assert(index < size_);
data_.push_back(elem);
indices_.push_back(index);
}
bool empty() const
{
return size_ == 0;
}
int size() const
{
return size_;
}
int nonzeroSize() const
{
return data_.size();
}
void clear()
{
data_.clear();
indices_.clear();
size_ = 0;
}
bool operator==(const SparseVector& other) const
{
return size_ == other.size_ && data_ == other.data_ && indices_ == other.indices_;
}
const T& element(int index) const
{
#ifndef NDEBUG
OPM_ERROR_IF(index < 0,
"The index of a SparseVector must be non-negative "
"(is " + std::to_string(index) + ")");
OPM_ERROR_IF(index >= size_,
"The index of a SparseVector must be smaller "
"than the maximum value "
"(is " + std::to_string(index) +
", max value: " + std::to_string(size_) + ")");
#endif
std::vector<int>::const_iterator lb = std::lower_bound(indices_.begin(), indices_.end(), index);
if (lb != indices_.end() && *lb == index) {
return data_[lb - indices_.begin()];
} else {
return default_elem_;
}
}
const T& nonzeroElement(int nzindex) const
{
#ifndef NDEBUG
OPM_ERROR_IF(nzindex < 0,
"The index of a SparseVector must be non-negative "
"(is " + std::to_string(nzindex) + ")");
OPM_ERROR_IF(nzindex >= nonzeroSize(),
"The index of a SparseVector must be smaller "
"than the maximum value " +
"(is " + std::to_string(nzindex) +
", max value: " + std::to_string(nonzeroSize()) + ")");
#endif
return data_[nzindex];
}
int nonzeroIndex(int nzindex) const
{
assert(nzindex >= 0);
assert(nzindex < nonzeroSize());
return indices_[nzindex];
}
private:
// The vectors data_ and indices_ are always the same size.
// The indices are supposed to be stored in increasing order,
// to be unique, and to be in [0, size_ - 1].
// default_elem_ is returned when a default element is requested.
int size_;
std::vector<T> data_;
std::vector<int> indices_;
T default_elem_;
};
} // namespace Opm
#endif // OPM_SPARSEVECTOR_HEADER
Opm::SparseVector::size
int size() const
Returns the size of the vector.
Definition SparseVector.hpp:118
Opm::SparseVector::clear
void clear()
Makes the vector empty().
Definition SparseVector.hpp:130
Opm::SparseVector::addElement
void addElement(const T &elem, int index)
Appends an element to the vector.
Definition SparseVector.hpp:102
Opm::SparseVector::nonzeroIndex
int nonzeroIndex(int nzindex) const
O(1) index access.
Definition SparseVector.hpp:188
Opm::SparseVector::SparseVector
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
Opm::SparseVector::nonzeroSize
int nonzeroSize() const
Returns the number of nonzero data elements.
Definition SparseVector.hpp:124
Opm::SparseVector::empty
bool empty() const
Definition SparseVector.hpp:111
Opm::SparseVector::nonzeroElement
const T & nonzeroElement(int nzindex) const
O(1) element access.
Definition SparseVector.hpp:170
Opm::SparseVector::element
const T & element(int index) const
O(log n) element access.
Definition SparseVector.hpp:147
Opm::SparseVector::SparseVector
SparseVector()
Default constructor. Yields an empty SparseVector.
Definition SparseVector.hpp:58
Opm::SparseVector::operator==
bool operator==(const SparseVector &other) const
Equality.
Definition SparseVector.hpp:138
Opm::SparseVector::SparseVector
SparseVector(int sz)
Constructs a SparseVector with a given size, but no nonzero elements.
Definition SparseVector.hpp:65
Opm
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition Exceptions.hpp:30