My Project
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 
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 
82  WindowedArray(const WindowedArray& rhs) = default;
83  WindowedArray(WindowedArray&& rhs) = default;
84  WindowedArray& operator=(const WindowedArray& rhs) = delete;
85  WindowedArray& operator=(WindowedArray&& rhs) = default;
86 
88  Idx numWindows() const
89  {
90  return this->x_.size() / this->windowSize_;
91  }
92 
94  Idx windowSize() const
95  {
96  return this->windowSize_;
97  }
98 
103  WriteWindow operator[](const Idx window)
104  {
105  assert ((window < this->numWindows()) &&
106  "Window ID Out of Bounds");
107 
108  auto b = std::begin(this->x_) + window*this->windowSize_;
109  auto e = b + this->windowSize_;
110 
111  return { b, e };
112  }
113 
118  ReadWindow operator[](const Idx window) const
119  {
120  assert ((window < this->numWindows()) &&
121  "Window ID Out of Bounds");
122 
123  auto b = std::begin(this->x_) + window*this->windowSize_;
124  auto e = b + this->windowSize_;
125 
126  return { b, e };
127  }
128 
131  const std::vector<T>& data() const
132  {
133  return this->x_;
134  }
135 
139  std::vector<T> getDataDestructively()
140  {
141  return std::move(this->x_);
142  }
143 
144  private:
145  std::vector<T> x_;
146 
147  Idx windowSize_;
148  };
149 
150 
162  template <typename T>
164  {
165  private:
166  using NumWindows = typename WindowedArray<T>::NumWindows;
167 
168  public:
169  using WriteWindow = typename WindowedArray<T>::WriteWindow;
170  using ReadWindow = typename WindowedArray<T>::ReadWindow;
171  using WindowSize = typename WindowedArray<T>::WindowSize;
172  using Idx = typename WindowedArray<T>::Idx;
173 
176  struct NumRows { Idx value; };
177 
180  struct NumCols { Idx value; };
181 
187  explicit WindowedMatrix(const NumRows& nRows,
188  const NumCols& nCols,
189  const WindowSize& sz)
190  : data_ (NumWindows{ nRows.value * nCols.value }, sz)
191  , numCols_(nCols.value)
192  {
193  if (nCols.value == 0)
194  throw std::invalid_argument("Window matrix with columns==0 is not permitted");
195  }
196 
198  Idx numCols() const
199  {
200  return this->numCols_;
201  }
202 
204  Idx numRows() const
205  {
206  return this->data_.numWindows() / this->numCols();
207  }
208 
210  Idx windowSize() const
211  {
212  return this->data_.windowSize();
213  }
214 
224  WriteWindow operator()(const Idx row, const Idx col)
225  {
226  return this->data_[ this->i(row, col) ];
227  }
228 
238  ReadWindow operator()(const Idx row, const Idx col) const
239  {
240  return this->data_[ this->i(row, col) ];
241  }
242 
245  auto data() const
246  -> decltype(std::declval<const WindowedArray<T>>().data())
247  {
248  return this->data_.data();
249  }
250 
255  -> decltype(std::declval<WindowedArray<T>>()
257  {
258  return this->data_.getDataDestructively();
259  }
260 
261  private:
262  WindowedArray<T> data_;
263 
264  Idx numCols_;
265 
267  Idx i(const Idx row, const Idx col) const
268  {
269  return row*this->numCols() + col;
270  }
271  };
272 
273 }}} // Opm::RestartIO::Helpers
274 
275 #endif // OPM_WINDOW_ARRAY_HPP
Provide read-only and read/write access to constantly sized portions/windows of a linearised buffer w...
Definition: WindowedArray.hpp:50
Idx numWindows() const
Retrieve number of windows allocated for this array.
Definition: WindowedArray.hpp:88
Idx windowSize() const
Retrieve number of data items per windows.
Definition: WindowedArray.hpp:94
const std::vector< T > & data() const
Get read-only access to full, linearised data items for all windows.
Definition: WindowedArray.hpp:131
boost::iterator_range< typename std::vector< T >::const_iterator > ReadWindow
Read-only access.
Definition: WindowedArray.hpp:58
WriteWindow operator[](const Idx window)
Request read/write access to individual window.
Definition: WindowedArray.hpp:103
boost::iterator_range< typename std::vector< T >::iterator > WriteWindow
Read/write access.
Definition: WindowedArray.hpp:54
WindowedArray(const NumWindows n, const WindowSize sz)
Constructor.
Definition: WindowedArray.hpp:74
ReadWindow operator[](const Idx window) const
Request read-only access to individual window.
Definition: WindowedArray.hpp:118
std::vector< T > getDataDestructively()
Extract full, linearised data items for all windows.
Definition: WindowedArray.hpp:139
Provide read-only and read/write access to constantly sized portions/windows of a linearised buffer w...
Definition: WindowedArray.hpp:164
ReadWindow operator()(const Idx row, const Idx col) const
Request read-only access to individual window.
Definition: WindowedArray.hpp:238
WriteWindow operator()(const Idx row, const Idx col)
Request read/write access to individual window.
Definition: WindowedArray.hpp:224
WindowedMatrix(const NumRows &nRows, const NumCols &nCols, const WindowSize &sz)
Constructor.
Definition: WindowedArray.hpp:187
Idx windowSize() const
Retrieve number of data items per windows.
Definition: WindowedArray.hpp:210
Idx numRows() const
Retrieve number of rows allocated for this matrix.
Definition: WindowedArray.hpp:204
Idx numCols() const
Retrieve number of columns allocated for this matrix.
Definition: WindowedArray.hpp:198
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
auto getDataDestructively() -> decltype(std::declval< WindowedArray< T >>() .getDataDestructively())
Extract full, linearised data items for all windows.
Definition: WindowedArray.hpp:254
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition: Exceptions.hpp:29
Distinct compile-time type for number of windows in underlying storage.
Definition: WindowedArray.hpp:64
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 matrix columns in underlying storage.
Definition: WindowedArray.hpp:180
Distinct compile-time type for number of matrix rows in underlying storage.
Definition: WindowedArray.hpp:176