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,
const T initial = T{})
: x_ (n.value * sz.value, initial)
, windowSize_(sz.value)
{
if (sz.value == 0) {
throw std::invalid_argument {
"Zero-sized windows are 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,
const T initial = T{})
: data_ (NumWindows{ nRows.value * nCols.value }, sz, initial)
, numCols_(nCols.value)
{
if (nCols.value == 0) {
throw std::invalid_argument {
"Zero-columned windowed matrices are 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:145
Opm::RestartIO::Helpers::WindowedArray::numWindows
Idx numWindows() const
Retrieve number of windows allocated for this array.
Definition WindowedArray.hpp:94
Opm::RestartIO::Helpers::WindowedArray::windowSize
Idx windowSize() const
Retrieve number of data items per windows.
Definition WindowedArray.hpp:100
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:137
Opm::RestartIO::Helpers::WindowedArray::operator[]
WriteWindow operator[](const Idx window)
Request read/write access to individual window.
Definition WindowedArray.hpp:109
Opm::RestartIO::Helpers::WriteWindow::ReadWindow
boost::iterator_range< typename std::vector< double >::const_iterator > ReadWindow
Definition WindowedArray.hpp:57
Opm::RestartIO::Helpers::WindowedArray::operator[]
ReadWindow operator[](const Idx window) const
Request read-only access to individual window.
Definition WindowedArray.hpp:124
Opm::RestartIO::Helpers::WriteWindow::WriteWindow
boost::iterator_range< typename std::vector< double >::iterator > WriteWindow
Definition WindowedArray.hpp:53
Opm::RestartIO::Helpers::WindowedArray::WindowedArray
WindowedArray(const NumWindows n, const WindowSize sz, const T initial=T{})
Constructor.
Definition WindowedArray.hpp:75
Opm::RestartIO::Helpers::WindowedMatrix::operator()
ReadWindow operator()(const Idx row, const Idx col) const
Request read-only access to individual window.
Definition WindowedArray.hpp:249
Opm::RestartIO::Helpers::WindowedMatrix::operator()
WriteWindow operator()(const Idx row, const Idx col)
Request read/write access to individual window.
Definition WindowedArray.hpp:235
Opm::RestartIO::Helpers::WindowedMatrix::getDataDestructively
auto getDataDestructively() -> decltype(std::declval< WindowedArray< T > >() .getDataDestructively())
Extract full, linearised data items for all windows.
Definition WindowedArray.hpp:265
Opm::RestartIO::Helpers::WindowedMatrix::windowSize
Idx windowSize() const
Retrieve number of data items per windows.
Definition WindowedArray.hpp:221
Opm::RestartIO::Helpers::WindowedMatrix::numRows
Idx numRows() const
Retrieve number of rows allocated for this matrix.
Definition WindowedArray.hpp:215
Opm::RestartIO::Helpers::WindowedMatrix::numCols
Idx numCols() const
Retrieve number of columns allocated for this matrix.
Definition WindowedArray.hpp:209
Opm::RestartIO::Helpers::WindowedMatrix::WindowedMatrix
WindowedMatrix(const NumRows &nRows, const NumCols &nCols, const WindowSize &sz, const T initial=T{})
Constructor.
Definition WindowedArray.hpp:194
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:256
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:186
Opm::RestartIO::Helpers::WindowedMatrix::NumRows
Distinct compile-time type for number of matrix rows in underlying storage.
Definition WindowedArray.hpp:182