Loading...
Searching...
No Matches
FastSmallVector.hpp
Go to the documentation of this file.
// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
// vi: set et ts=4 sw=4 sts=4:
/*
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 2 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/>.
Consult the COPYING file in the top-level source directory of this
module for the precise wording of the license and the list of
copyright holders.
*/
#ifndef OPM_FAST_SMALL_VECTOR_HPP
#define OPM_FAST_SMALL_VECTOR_HPP
#include <array>
#include <algorithm>
#include <vector>
namespace Opm {
template <typename ValueType, unsigned N>
class FastSmallVector
{
public:
FastSmallVector()
{
size_ = 0;
dataPtr_ = smallBuf_.data();
}
explicit FastSmallVector(const size_t numElem)
{
init_(numElem);
}
FastSmallVector(const size_t numElem, const ValueType value)
{
init_(numElem);
std::fill(dataPtr_, dataPtr_ + size_, value);
}
FastSmallVector(const FastSmallVector& other)
{
size_ = 0;
dataPtr_ = smallBuf_.data();
(*this) = other;
}
FastSmallVector(FastSmallVector&& other)
{
size_ = 0;
dataPtr_ = smallBuf_.data();
(*this) = std::move(other);
}
~FastSmallVector()
{
}
FastSmallVector& operator=(FastSmallVector&& other)
{
size_ = other.size_;
if (size_ <= N) {
smallBuf_ = std::move(other.smallBuf_);
dataPtr_ = smallBuf_.data();
}
else {
data_ = std::move(other.data_);
dataPtr_ = data_.data();
}
other.dataPtr_ = nullptr;
other.size_ = 0;
return (*this);
}
FastSmallVector& operator=(const FastSmallVector& other)
{
size_ = other.size_;
if (size_ <= N) {
smallBuf_ = other.smallBuf_;
dataPtr_ = smallBuf_.data();
}
else if (dataPtr_ != other.dataPtr_) {
data_ = other.data_;
dataPtr_ = data_.data();
}
return (*this);
}
ValueType& operator[](size_t idx)
{ return dataPtr_[idx]; }
const ValueType& operator[](size_t idx) const
{ return dataPtr_[idx]; }
size_t size() const
{ return size_; }
private:
void init_(size_t numElem)
{
size_ = numElem;
if (size_ > N) {
data_.resize(size_);
dataPtr_ = data_.data();
} else
dataPtr_ = smallBuf_.data();
}
std::array<ValueType, N> smallBuf_;
std::vector<ValueType> data_;
std::size_t size_;
ValueType* dataPtr_;
};
} // namespace Opm
#endif // OPM_FAST_SMALL_VECTOR_HPP
Opm::FastSmallVector
An implementation of vector/array based on small object optimization.
Definition FastSmallVector.hpp:48
Opm::FastSmallVector::FastSmallVector
FastSmallVector()
default constructor
Definition FastSmallVector.hpp:51
Opm::FastSmallVector::size
size_t size() const
number of the element
Definition FastSmallVector.hpp:141
Opm::FastSmallVector::operator=
FastSmallVector & operator=(FastSmallVector &&other)
move assignment
Definition FastSmallVector.hpp:97
Opm::FastSmallVector::FastSmallVector
FastSmallVector(FastSmallVector &&other)
move constructor
Definition FastSmallVector.hpp:82
Opm::FastSmallVector::operator[]
const ValueType & operator[](size_t idx) const
const access the idx th element
Definition FastSmallVector.hpp:137
Opm::FastSmallVector::FastSmallVector
FastSmallVector(const size_t numElem, const ValueType value)
constructor based on the number of the element, and all the elements will have the same value
Definition FastSmallVector.hpp:65
Opm::FastSmallVector::operator=
FastSmallVector & operator=(const FastSmallVector &other)
copy assignment
Definition FastSmallVector.hpp:116
Opm::FastSmallVector::~FastSmallVector
~FastSmallVector()
destructor
Definition FastSmallVector.hpp:91
Opm::FastSmallVector::FastSmallVector
FastSmallVector(const size_t numElem)
constructor based on the number of the element
Definition FastSmallVector.hpp:58
Opm::FastSmallVector::FastSmallVector
FastSmallVector(const FastSmallVector &other)
copy constructor
Definition FastSmallVector.hpp:73
Opm::FastSmallVector::operator[]
ValueType & operator[](size_t idx)
access the idx th element
Definition FastSmallVector.hpp:133
Opm
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition Exceptions.hpp:30