Loading...
Searching...
No Matches
WindowedArray.hpp
Go to the documentation of this file.
/*
Copyright (c) 2018 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_WINDOWED_ARRAY_HPP
#define OPM_WINDOWED_ARRAY_HPP
#include <cassert>
#include <exception>
#include <iterator>
#include <stdexcept>
#include <type_traits>
#include <vector>
#include <boost/range/iterator_range.hpp>
namespace Opm { namespace RestartIO { namespace Helpers {
template <typename T>
class WindowedArray
{
public:
using WriteWindow = boost::iterator_range<
typename std::vector<T>::iterator>;
using ReadWindow = boost::iterator_range<
typename std::vector<T>::const_iterator>;
using Idx = typename std::vector<T>::size_type;
struct NumWindows { Idx value; };
struct WindowSize { Idx value; };
explicit WindowedArray(const NumWindows n, const WindowSize sz)
: x_ (n.value * sz.value)
, windowSize_(sz.value)
{
if (sz.value == 0)
throw std::invalid_argument("Window array with windowsize==0 is not permitted");
}
WindowedArray(const WindowedArray& rhs) = default;
WindowedArray(WindowedArray&& rhs) = default;
WindowedArray& operator=(const WindowedArray& rhs) = delete;
WindowedArray& operator=(WindowedArray&& rhs) = default;
Idx numWindows() const
{
return this->x_.size() / this->windowSize_;
}
Idx windowSize() const
{
return this->windowSize_;
}
WriteWindow operator[](const Idx window)
{
assert ((window < this->numWindows()) &&
"Window ID Out of Bounds");
auto b = std::begin(this->x_) + window*this->windowSize_;
auto e = b + this->windowSize_;
return { b, e };
}
ReadWindow operator[](const Idx window) const
{
assert ((window < this->numWindows()) &&
"Window ID Out of Bounds");
auto b = std::begin(this->x_) + window*this->windowSize_;
auto e = b + this->windowSize_;
return { b, e };
}
const std::vector<T>& data() const
{
return this->x_;
}
std::vector<T> getDataDestructively()
{
return std::move(this->x_);
}
private:
std::vector<T> x_;
Idx windowSize_;
};
template <typename T>
class WindowedMatrix
{
private:
using NumWindows = typename WindowedArray<T>::NumWindows;
public:
using WriteWindow = typename WindowedArray<T>::WriteWindow;
using ReadWindow = typename WindowedArray<T>::ReadWindow;
using WindowSize = typename WindowedArray<T>::WindowSize;
using Idx = typename WindowedArray<T>::Idx;
struct NumRows { Idx value; };
struct NumCols { Idx value; };
explicit WindowedMatrix(const NumRows& nRows,
const NumCols& nCols,
const WindowSize& sz)
: data_ (NumWindows{ nRows.value * nCols.value }, sz)
, numCols_(nCols.value)
{
if (nCols.value == 0)
throw std::invalid_argument("Window matrix with columns==0 is not permitted");
}
Idx numCols() const
{
return this->numCols_;
}
Idx numRows() const
{
return this->data_.numWindows() / this->numCols();
}
Idx windowSize() const
{
return this->data_.windowSize();
}
WriteWindow operator()(const Idx row, const Idx col)
{
return this->data_[ this->i(row, col) ];
}
ReadWindow operator()(const Idx row, const Idx col) const
{
return this->data_[ this->i(row, col) ];
}
auto data() const
-> decltype(std::declval<const WindowedArray<T>>().data())
{
return this->data_.data();
}
auto getDataDestructively()
-> decltype(std::declval<WindowedArray<T>>()
.getDataDestructively())
{
return this->data_.getDataDestructively();
}
private:
WindowedArray<T> data_;
Idx numCols_;
Idx i(const Idx row, const Idx col) const
{
return row*this->numCols() + col;
}
};
}}} // Opm::RestartIO::Helpers
#endif // OPM_WINDOW_ARRAY_HPP
Opm::RestartIO::Helpers::WindowedArray
Provide read-only and read/write access to constantly sized portions/windows of a linearised buffer w...
Definition WindowedArray.hpp:50
Opm::RestartIO::Helpers::WindowedArray::getDataDestructively
std::vector< T > getDataDestructively()
Extract full, linearised data items for all windows.
Definition WindowedArray.hpp:139
Opm::RestartIO::Helpers::WindowedArray::numWindows
Idx numWindows() const
Retrieve number of windows allocated for this array.
Definition WindowedArray.hpp:88
Opm::RestartIO::Helpers::WindowedArray::windowSize
Idx windowSize() const
Retrieve number of data items per windows.
Definition WindowedArray.hpp:94
Opm::RestartIO::Helpers::WindowedArray::ReadWindow
boost::iterator_range< typename std::vector< T >::const_iterator > ReadWindow
Read-only access.
Definition WindowedArray.hpp:58
Opm::RestartIO::Helpers::WindowedArray::data
const std::vector< T > & data() const
Get read-only access to full, linearised data items for all windows.
Definition WindowedArray.hpp:131
Opm::RestartIO::Helpers::WindowedArray::operator[]
WriteWindow operator[](const Idx window)
Request read/write access to individual window.
Definition WindowedArray.hpp:103
Opm::RestartIO::Helpers::WindowedArray::WriteWindow
boost::iterator_range< typename std::vector< T >::iterator > WriteWindow
Read/write access.
Definition WindowedArray.hpp:54
Opm::RestartIO::Helpers::WindowedArray::WindowedArray
WindowedArray(const NumWindows n, const WindowSize sz)
Constructor.
Definition WindowedArray.hpp:74
Opm::RestartIO::Helpers::WindowedArray::operator[]
ReadWindow operator[](const Idx window) const
Request read-only access to individual window.
Definition WindowedArray.hpp:118
Opm::RestartIO::Helpers::WindowedMatrix
Provide read-only and read/write access to constantly sized portions/windows of a linearised buffer w...
Definition WindowedArray.hpp:164
Opm::RestartIO::Helpers::WindowedMatrix::operator()
ReadWindow operator()(const Idx row, const Idx col) const
Request read-only access to individual window.
Definition WindowedArray.hpp:238
Opm::RestartIO::Helpers::WindowedMatrix::operator()
WriteWindow operator()(const Idx row, const Idx col)
Request read/write access to individual window.
Definition WindowedArray.hpp:224
Opm::RestartIO::Helpers::WindowedMatrix::getDataDestructively
auto getDataDestructively() -> decltype(std::declval< WindowedArray< T > >() .getDataDestructively())
Extract full, linearised data items for all windows.
Definition WindowedArray.hpp:254
Opm::RestartIO::Helpers::WindowedMatrix::WindowedMatrix
WindowedMatrix(const NumRows &nRows, const NumCols &nCols, const WindowSize &sz)
Constructor.
Definition WindowedArray.hpp:187
Opm::RestartIO::Helpers::WindowedMatrix::windowSize
Idx windowSize() const
Retrieve number of data items per windows.
Definition WindowedArray.hpp:210
Opm::RestartIO::Helpers::WindowedMatrix::numRows
Idx numRows() const
Retrieve number of rows allocated for this matrix.
Definition WindowedArray.hpp:204
Opm::RestartIO::Helpers::WindowedMatrix::numCols
Idx numCols() const
Retrieve number of columns allocated for this matrix.
Definition WindowedArray.hpp:198
Opm::RestartIO::Helpers::WindowedMatrix::data
auto data() const -> decltype(std::declval< const WindowedArray< T > >().data())
Get read-only access to full, linearised data items for all windows.
Definition WindowedArray.hpp:245
Opm
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition Exceptions.hpp:30
Opm::RestartIO::Helpers::WindowedArray::NumWindows
Distinct compile-time type for number of windows in underlying storage.
Definition WindowedArray.hpp:64
Opm::RestartIO::Helpers::WindowedArray::WindowSize
Distinct compile-time type for size of windows (number of data items per window.)
Definition WindowedArray.hpp:68
Opm::RestartIO::Helpers::WindowedMatrix::NumCols
Distinct compile-time type for number of matrix columns in underlying storage.
Definition WindowedArray.hpp:180
Opm::RestartIO::Helpers::WindowedMatrix::NumRows
Distinct compile-time type for number of matrix rows in underlying storage.
Definition WindowedArray.hpp:176