WindowedArray.hpp
Go to the documentation of this file.
1/*
2 Copyright (c) 2018 Statoil ASA
3
4 This file is part of the Open Porous Media project (OPM).
5
6 OPM is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 OPM is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with OPM. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#ifndef OPM_WINDOWED_ARRAY_HPP
21#define OPM_WINDOWED_ARRAY_HPP
22
23#include <cassert>
24#include <exception>
25#include <iterator>
26#include <stdexcept>
27#include <type_traits>
28#include <vector>
29
30#include <boost/range/iterator_range.hpp>
31
36
37namespace Opm { namespace RestartIO { namespace Helpers {
38
48 template <typename T>
50 {
51 public:
53 using WriteWindow = boost::iterator_range<
54 typename std::vector<T>::iterator>;
55
57 using ReadWindow = boost::iterator_range<
58 typename std::vector<T>::const_iterator>;
59
60 using Idx = typename std::vector<T>::size_type;
61
64 struct NumWindows { Idx value; };
65
68 struct WindowSize { Idx value; };
69
74 explicit WindowedArray(const NumWindows n, const WindowSize sz)
75 : x_ (n.value * sz.value)
76 , windowSize_(sz.value)
77 {
78 if (sz.value == 0)
79 throw std::invalid_argument("Window array with windowsize==0 is not permitted");
80 }
81
84 {
85 return this->x_.size() / this->windowSize_;
86 }
87
90 {
91 return this->windowSize_;
92 }
93
99 {
100 assert ((window < this->numWindows()) &&
101 "Window ID Out of Bounds");
102
103 auto b = std::begin(this->x_) + window*this->windowSize_;
104 auto e = b + this->windowSize_;
105
106 return { b, e };
107 }
108
113 ReadWindow operator[](const Idx window) const
114 {
115 assert ((window < this->numWindows()) &&
116 "Window ID Out of Bounds");
117
118 auto b = std::begin(this->x_) + window*this->windowSize_;
119 auto e = b + this->windowSize_;
120
121 return { b, e };
122 }
123
126 const std::vector<T>& data() const
127 {
128 return this->x_;
129 }
130
134 std::vector<T> getDataDestructively()
135 {
136 return std::move(this->x_);
137 }
138
139 private:
140 std::vector<T> x_;
141
142 Idx windowSize_;
143 };
144
145
157 template <typename T>
159 {
160 private:
161 using NumWindows = typename WindowedArray<T>::NumWindows;
162
163 public:
167 using Idx = typename WindowedArray<T>::Idx;
168
171 struct NumRows { Idx value; };
172
175 struct NumCols { Idx value; };
176
182 explicit WindowedMatrix(const NumRows& nRows,
183 const NumCols& nCols,
184 const WindowSize& sz)
185 : data_ (NumWindows{ nRows.value * nCols.value }, sz)
186 , numCols_(nCols.value)
187 {
188 if (nCols.value == 0)
189 throw std::invalid_argument("Window matrix with columns==0 is not permitted");
190 }
191
193 Idx numCols() const
194 {
195 return this->numCols_;
196 }
197
199 Idx numRows() const
200 {
201 return this->data_.numWindows() / this->numCols();
202 }
203
206 {
207 return this->data_.windowSize();
208 }
209
219 WriteWindow operator()(const Idx row, const Idx col)
220 {
221 return this->data_[ this->i(row, col) ];
222 }
223
233 ReadWindow operator()(const Idx row, const Idx col) const
234 {
235 return this->data_[ this->i(row, col) ];
236 }
237
240 auto data() const
241 -> decltype(std::declval<const WindowedArray<T>>().data())
242 {
243 return this->data_.data();
244 }
245
250 -> decltype(std::declval<WindowedArray<T>>()
252 {
253 return this->data_.getDataDestructively();
254 }
255
256 private:
257 WindowedArray<T> data_;
258
259 Idx numCols_;
260
262 Idx i(const Idx row, const Idx col) const
263 {
264 return row*this->numCols() + col;
265 }
266 };
267
268}}} // Opm::RestartIO::Helpers
269
270#endif // OPM_WINDOW_ARRAY_HPP
const cJSON *const b
Definition: cJSON.h:251
Definition: WindowedArray.hpp:50
typename std::vector< T >::size_type Idx
Definition: WindowedArray.hpp:60
std::vector< T > getDataDestructively()
Definition: WindowedArray.hpp:134
Idx numWindows() const
Retrieve number of windows allocated for this array.
Definition: WindowedArray.hpp:83
Idx windowSize() const
Retrieve number of data items per windows.
Definition: WindowedArray.hpp:89
boost::iterator_range< typename std::vector< T >::const_iterator > ReadWindow
Read-only access.
Definition: WindowedArray.hpp:58
const std::vector< T > & data() const
Definition: WindowedArray.hpp:126
WriteWindow operator[](const Idx window)
Definition: WindowedArray.hpp:98
boost::iterator_range< typename std::vector< T >::iterator > WriteWindow
Read/write access.
Definition: WindowedArray.hpp:54
WindowedArray(const NumWindows n, const WindowSize sz)
Definition: WindowedArray.hpp:74
ReadWindow operator[](const Idx window) const
Definition: WindowedArray.hpp:113
Definition: WindowedArray.hpp:159
typename WindowedArray< T >::WindowSize WindowSize
Definition: WindowedArray.hpp:166
ReadWindow operator()(const Idx row, const Idx col) const
Definition: WindowedArray.hpp:233
typename WindowedArray< T >::ReadWindow ReadWindow
Definition: WindowedArray.hpp:165
typename WindowedArray< T >::WriteWindow WriteWindow
Definition: WindowedArray.hpp:164
WriteWindow operator()(const Idx row, const Idx col)
Definition: WindowedArray.hpp:219
auto getDataDestructively() -> decltype(std::declval< WindowedArray< T > >() .getDataDestructively())
Definition: WindowedArray.hpp:249
WindowedMatrix(const NumRows &nRows, const NumCols &nCols, const WindowSize &sz)
Definition: WindowedArray.hpp:182
Idx windowSize() const
Retrieve number of data items per windows.
Definition: WindowedArray.hpp:205
Idx numRows() const
Retrieve number of rows allocated for this matrix.
Definition: WindowedArray.hpp:199
Idx numCols() const
Retrieve number of columns allocated for this matrix.
Definition: WindowedArray.hpp:193
typename WindowedArray< T >::Idx Idx
Definition: WindowedArray.hpp:167
auto data() const -> decltype(std::declval< const WindowedArray< T > >().data())
Definition: WindowedArray.hpp:240
not_this_one begin(...)
Definition: A.hpp:4
static const double e
Definition: exprtk.hpp:758
T value(details::expression_node< T > *n)
Definition: exprtk.hpp:12955
Idx value
Definition: WindowedArray.hpp:64
Idx value
Definition: WindowedArray.hpp:68
Definition: WindowedArray.hpp:175
Idx value
Definition: WindowedArray.hpp:175
Definition: WindowedArray.hpp:171
Idx value
Definition: WindowedArray.hpp:171