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:
/*
Copyright 2019, 2025 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 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]; }
using size_type = typename std::vector<ValueType>::size_type;
size_type size() const
{ return size_; }
using Iterator = ValueType*;
using ConstIterator = const ValueType*;
ConstIterator begin() const
{ return dataPtr_; }
ConstIterator end() const
{ return dataPtr_ + size_; }
ConstIterator cbegin() const
{ return dataPtr_; }
ConstIterator cend() const
{ return dataPtr_ + size_; }
Iterator begin()
{ return dataPtr_; }
Iterator end()
{ return dataPtr_ + size_; }
size_type capacity()
{ return size_ <= N ? N : data_.capacity(); }
void push_back(const ValueType& value)
{
if (size_ < N) {
// Data is contained in smallBuf_
smallBuf_[size_++] = value;
} else if (size_ == N) {
// Must switch from using smallBuf_ to using data_
data_.reserve(N + 1);
data_.assign(smallBuf_.begin(), smallBuf_.end());
data_.push_back(value);
++size_;
dataPtr_ = data_.data();
} else {
// Data is contained in data_
data_.push_back(value);
++size_;
dataPtr_ = data_.data();
}
}
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_;
size_type size_;
ValueType* dataPtr_;
};
} // namespace Opm
#endif // OPM_FAST_SMALL_VECTOR_HPP
Opm::FastSmallVector::FastSmallVector
FastSmallVector()
default constructor
Definition FastSmallVector.hpp:53
Opm::FastSmallVector::end
Iterator end()
To support range-for etc.
Definition FastSmallVector.hpp:175
Opm::FastSmallVector::ConstIterator
const ValueType * ConstIterator
ConstIterator type is a plain pointer, so be warned there is no validity checking.
Definition FastSmallVector.hpp:152
Opm::FastSmallVector::operator=
FastSmallVector & operator=(FastSmallVector &&other)
move assignment
Definition FastSmallVector.hpp:99
Opm::FastSmallVector::FastSmallVector
FastSmallVector(FastSmallVector &&other)
move constructor
Definition FastSmallVector.hpp:84
Opm::FastSmallVector::end
ConstIterator end() const
To support range-for etc.
Definition FastSmallVector.hpp:159
Opm::FastSmallVector::operator[]
const ValueType & operator[](size_t idx) const
const access the idx th element
Definition FastSmallVector.hpp:139
Opm::FastSmallVector::Iterator
ValueType * Iterator
Iterator type is a plain pointer, so be warned there is no validity checking.
Definition FastSmallVector.hpp:149
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:67
Opm::FastSmallVector::operator=
FastSmallVector & operator=(const FastSmallVector &other)
copy assignment
Definition FastSmallVector.hpp:118
Opm::FastSmallVector::~FastSmallVector
~FastSmallVector()
destructor
Definition FastSmallVector.hpp:93
Opm::FastSmallVector::FastSmallVector
FastSmallVector(const size_t numElem)
constructor based on the number of the element
Definition FastSmallVector.hpp:60
Opm::FastSmallVector::FastSmallVector
FastSmallVector(const FastSmallVector &other)
copy constructor
Definition FastSmallVector.hpp:75
Opm::FastSmallVector::cbegin
ConstIterator cbegin() const
To support range-for etc.
Definition FastSmallVector.hpp:163
Opm::FastSmallVector::begin
ConstIterator begin() const
To support range-for etc.
Definition FastSmallVector.hpp:155
Opm::FastSmallVector::cend
ConstIterator cend() const
To support range-for etc.
Definition FastSmallVector.hpp:167
Opm::FastSmallVector::operator[]
ValueType & operator[](size_t idx)
access the idx th element
Definition FastSmallVector.hpp:135
Opm::FastSmallVector::size
size_type size() const
number of elements
Definition FastSmallVector.hpp:145
Opm::FastSmallVector::begin
Iterator begin()
To support range-for etc.
Definition FastSmallVector.hpp:171
Opm
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition Exceptions.hpp:30