opm-common
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 
37 namespace 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 
75  explicit WindowedArray(const NumWindows n,
76  const WindowSize sz,
77  const T initial = T{})
78  : x_ (n.value * sz.value, initial)
79  , windowSize_(sz.value)
80  {
81  if (sz.value == 0) {
82  throw std::invalid_argument {
83  "Zero-sized windows are not permitted"
84  };
85  }
86  }
87 
88  WindowedArray(const WindowedArray& rhs) = default;
89  WindowedArray(WindowedArray&& rhs) = default;
90  WindowedArray& operator=(const WindowedArray& rhs) = delete;
91  WindowedArray& operator=(WindowedArray&& rhs) = default;
92 
94  Idx numWindows() const
95  {
96  return this->x_.size() / this->windowSize_;
97  }
98 
100  Idx windowSize() const
101  {
102  return this->windowSize_;
103  }
104 
109  WriteWindow operator[](const Idx window)
110  {
111  assert ((window < this->numWindows()) &&
112  "Window ID Out of Bounds");
113 
114  auto b = std::begin(this->x_) + window*this->windowSize_;
115  auto e = b + this->windowSize_;
116 
117  return { b, e };
118  }
119 
124  ReadWindow operator[](const Idx window) const
125  {
126  assert ((window < this->numWindows()) &&
127  "Window ID Out of Bounds");
128 
129  auto b = std::begin(this->x_) + window*this->windowSize_;
130  auto e = b + this->windowSize_;
131 
132  return { b, e };
133  }
134 
137  const std::vector<T>& data() const
138  {
139  return this->x_;
140  }
141 
145  std::vector<T> getDataDestructively()
146  {
147  return std::move(this->x_);
148  }
149 
150  private:
151  std::vector<T> x_;
152 
153  Idx windowSize_;
154  };
155 
156 
168  template <typename T>
170  {
171  private:
172  using NumWindows = typename WindowedArray<T>::NumWindows;
173 
174  public:
175  using WriteWindow = typename WindowedArray<T>::WriteWindow;
176  using ReadWindow = typename WindowedArray<T>::ReadWindow;
177  using WindowSize = typename WindowedArray<T>::WindowSize;
178  using Idx = typename WindowedArray<T>::Idx;
179 
182  struct NumRows { Idx value; };
183 
186  struct NumCols { Idx value; };
187 
194  explicit WindowedMatrix(const NumRows& nRows,
195  const NumCols& nCols,
196  const WindowSize& sz,
197  const T initial = T{})
198  : data_ (NumWindows{ nRows.value * nCols.value }, sz, initial)
199  , numCols_(nCols.value)
200  {
201  if (nCols.value == 0) {
202  throw std::invalid_argument {
203  "Zero-columned windowed matrices are not permitted"
204  };
205  }
206  }
207 
209  Idx numCols() const
210  {
211  return this->numCols_;
212  }
213 
215  Idx numRows() const
216  {
217  return this->data_.numWindows() / this->numCols();
218  }
219 
221  Idx windowSize() const
222  {
223  return this->data_.windowSize();
224  }
225 
235  WriteWindow operator()(const Idx row, const Idx col)
236  {
237  return this->data_[ this->i(row, col) ];
238  }
239 
249  ReadWindow operator()(const Idx row, const Idx col) const
250  {
251  return this->data_[ this->i(row, col) ];
252  }
253 
256  auto data() const
257  -> decltype(std::declval<const WindowedArray<T>>().data())
258  {
259  return this->data_.data();
260  }
261 
266  -> decltype(std::declval<WindowedArray<T>>()
268  {
269  return this->data_.getDataDestructively();
270  }
271 
272  private:
273  WindowedArray<T> data_;
274 
275  Idx numCols_;
276 
278  Idx i(const Idx row, const Idx col) const
279  {
280  return row*this->numCols() + col;
281  }
282  };
283 
284 }}} // Opm::RestartIO::Helpers
285 
286 #endif // OPM_WINDOW_ARRAY_HPP
Distinct compile-time type for size of windows (number of data items per window.) ...
Definition: WindowedArray.hpp:68
Distinct compile-time type for number of windows in underlying storage.
Definition: WindowedArray.hpp:64
const std::vector< T > & data() const
Get read-only access to full, linearised data items for all windows.
Definition: WindowedArray.hpp:137
Distinct compile-time type for number of matrix rows in underlying storage.
Definition: WindowedArray.hpp:182
Idx numCols() const
Retrieve number of columns allocated for this matrix.
Definition: WindowedArray.hpp:209
ReadWindow operator()(const Idx row, const Idx col) const
Request read-only access to individual window.
Definition: WindowedArray.hpp:249
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition: Exceptions.hpp:30
boost::iterator_range< typename std::vector< int >::const_iterator > ReadWindow
Read-only access.
Definition: WindowedArray.hpp:58
WindowedArray(const NumWindows n, const WindowSize sz, const T initial=T{})
Constructor.
Definition: WindowedArray.hpp:75
Idx windowSize() const
Retrieve number of data items per windows.
Definition: WindowedArray.hpp:221
ReadWindow operator[](const Idx window) const
Request read-only access to individual window.
Definition: WindowedArray.hpp:124
Idx numRows() const
Retrieve number of rows allocated for this matrix.
Definition: WindowedArray.hpp:215
Distinct compile-time type for number of matrix columns in underlying storage.
Definition: WindowedArray.hpp:186
auto getDataDestructively() -> decltype(std::declval< WindowedArray< T >>() .getDataDestructively())
Extract full, linearised data items for all windows.
Definition: WindowedArray.hpp:265
WriteWindow operator[](const Idx window)
Request read/write access to individual window.
Definition: WindowedArray.hpp:109
Idx windowSize() const
Retrieve number of data items per windows.
Definition: WindowedArray.hpp:100
WriteWindow operator()(const Idx row, const Idx col)
Request read/write access to individual window.
Definition: WindowedArray.hpp:235
Provide read-only and read/write access to constantly sized portions/windows of a linearised buffer w...
Definition: WindowedArray.hpp:49
WindowedMatrix(const NumRows &nRows, const NumCols &nCols, const WindowSize &sz, const T initial=T{})
Constructor.
Definition: WindowedArray.hpp:194
Idx numWindows() const
Retrieve number of windows allocated for this array.
Definition: WindowedArray.hpp:94
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
std::vector< T > getDataDestructively()
Extract full, linearised data items for all windows.
Definition: WindowedArray.hpp:145
Provide read-only and read/write access to constantly sized portions/windows of a linearised buffer w...
Definition: WindowedArray.hpp:169
boost::iterator_range< typename std::vector< T >::iterator > WriteWindow
Read/write access.
Definition: WindowedArray.hpp:54