Reference documentation for deal.II version 8.5.1
array_view.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2004 - 2016 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE at
12 // the top level of the deal.II distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii__array_view_h
17 #define dealii__array_view_h
18 
19 #include <deal.II/base/config.h>
20 #include <deal.II/base/exceptions.h>
21 #include <deal.II/base/table.h>
22 
23 #include <boost/type_traits/remove_cv.hpp>
24 
25 #include <vector>
26 
27 DEAL_II_NAMESPACE_OPEN
28 
29 
70 template <typename ElementType>
71 class ArrayView
72 {
73 public:
78  typedef ElementType value_type;
79 
84  ArrayView ();
85 
106  const std::size_t n_elements);
107 
117  ArrayView (const ArrayView<typename boost::remove_cv<value_type>::type> &view);
118 
123  std::size_t size() const;
124 
134  value_type &operator[] (const std::size_t i) const;
135 
136 private:
142 
146  const std::size_t n_elements;
147 
148  friend class ArrayView<const ElementType>;
149 };
150 
151 
152 
153 //---------------------------------------------------------------------------
154 
155 
156 template <typename ElementType>
157 inline
159  :
160  starting_element (NULL),
161  n_elements(0)
162 {}
163 
164 
165 
166 template <typename ElementType>
167 inline
169  const std::size_t n_elements)
170  :
173 {}
174 
175 
176 
177 template <typename ElementType>
178 inline
179 ArrayView<ElementType>::ArrayView(const ArrayView<typename boost::remove_cv<value_type>::type> &view)
180  :
182  n_elements(view.n_elements)
183 {}
184 
185 
186 
187 template <typename ElementType>
188 inline
189 std::size_t
191 {
192  return n_elements;
193 }
194 
195 
196 template <typename ElementType>
197 inline
199 ArrayView<ElementType>::operator[](const std::size_t i) const
200 {
202 
203  return *(starting_element + i);
204 }
205 
206 
221 template <typename ElementType>
222 inline
224 make_array_view (std::vector<ElementType> &vector)
225 {
226  return ArrayView<ElementType> (&vector[0], vector.size());
227 }
228 
229 
230 
245 template <typename ElementType>
246 inline
248 make_array_view (const std::vector<ElementType> &vector)
249 {
250  return ArrayView<const ElementType> (&vector[0], vector.size());
251 }
252 
253 
254 
274 template <typename ElementType>
275 inline
277 make_array_view (std::vector<ElementType> &vector,
278  const std::size_t starting_index,
279  const std::size_t size_of_view)
280 {
281  Assert (starting_index + size_of_view <= vector.size(),
282  ExcMessage ("The starting index and size of the view you want to "
283  "create would lead to a view that extends beyond the end "
284  "of the given vector."));
285  return ArrayView<ElementType> (&vector[starting_index], size_of_view);
286 }
287 
288 
289 
309 template <typename ElementType>
310 inline
312 make_array_view (const std::vector<ElementType> &vector,
313  const std::size_t starting_index,
314  const std::size_t size_of_view)
315 {
316  Assert (starting_index + size_of_view <= vector.size(),
317  ExcMessage ("The starting index and size of the view you want to "
318  "create would lead to a view that extends beyond the end "
319  "of the given vector."));
320  return ArrayView<const ElementType> (&vector[starting_index], size_of_view);
321 }
322 
323 
324 
341 template <typename ElementType>
342 inline
345  const typename Table<2,ElementType>::size_type row)
346 {
347  AssertIndexRange (row, table.size()[0]);
348  return ArrayView<ElementType> (&table[row][0], table.size()[1]);
349 }
350 
351 
352 
369 template <typename ElementType>
370 inline
373  const typename Table<2,ElementType>::size_type row)
374 {
375  AssertIndexRange (row, table.size()[0]);
376  return ArrayView<const ElementType> (&table[row][0], table.size()[1]);
377 }
378 
379 
380 
400 template <typename ElementType>
401 inline
404  const typename Table<2,ElementType>::size_type row,
405  const typename Table<2,ElementType>::size_type starting_column,
406  const std::size_t size_of_view)
407 {
408  AssertIndexRange (row, table.size()[0]);
409  AssertIndexRange (starting_column, table.size()[1]);
410  Assert (starting_column + size_of_view <= table.size()[1],
411  ExcMessage ("The starting index and size of the view you want to "
412  "create would lead to a view that extends beyond the end "
413  "of a column of the given table."));
414  return ArrayView<ElementType> (&table[row][starting_column], size_of_view);
415 }
416 
417 
418 
438 template <typename ElementType>
439 inline
442  const typename Table<2,ElementType>::size_type row,
443  const typename Table<2,ElementType>::size_type starting_column,
444  const std::size_t size_of_view)
445 {
446  AssertIndexRange (row, table.size()[0]);
447  AssertIndexRange (starting_column, table.size()[1]);
448  Assert (starting_column + size_of_view <= table.size()[1],
449  ExcMessage ("The starting index and size of the view you want to "
450  "create would lead to a view that extends beyond the end "
451  "of a column of the given table."));
452  return ArrayView<const ElementType> (&table[row][starting_column], size_of_view);
453 }
454 
455 
456 
457 DEAL_II_NAMESPACE_CLOSE
458 
459 #endif
ArrayView< ElementType > make_array_view(std::vector< ElementType > &vector, const std::size_t starting_index, const std::size_t size_of_view)
Definition: array_view.h:277
const std::size_t n_elements
Definition: array_view.h:146
ElementType value_type
Definition: array_view.h:78
#define AssertIndexRange(index, range)
Definition: exceptions.h:1170
static ::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
ArrayView< const ElementType > make_array_view(const Table< 2, ElementType > &table, const typename Table< 2, ElementType >::size_type row, const typename Table< 2, ElementType >::size_type starting_column, const std::size_t size_of_view)
Definition: array_view.h:441
value_type & operator[](const std::size_t i) const
Definition: array_view.h:199
static ::ExceptionBase & ExcMessage(std::string arg1)
#define Assert(cond, exc)
Definition: exceptions.h:313
ArrayView< ElementType > make_array_view(Table< 2, ElementType > &table, const typename Table< 2, ElementType >::size_type row)
Definition: array_view.h:344
std::size_t size() const
Definition: array_view.h:190
ArrayView< const ElementType > make_array_view(const std::vector< ElementType > &vector, const std::size_t starting_index, const std::size_t size_of_view)
Definition: array_view.h:312
ArrayView< ElementType > make_array_view(std::vector< ElementType > &vector)
Definition: array_view.h:224
ArrayView< const ElementType > make_array_view(const Table< 2, ElementType > &table, const typename Table< 2, ElementType >::size_type row)
Definition: array_view.h:372
Definition: table.h:33
unsigned int size(const unsigned int i) const
ArrayView< ElementType > make_array_view(Table< 2, ElementType > &table, const typename Table< 2, ElementType >::size_type row, const typename Table< 2, ElementType >::size_type starting_column, const std::size_t size_of_view)
Definition: array_view.h:403
AlignedVector< T >::size_type size_type
Definition: table.h:411
ArrayView< const ElementType > make_array_view(const std::vector< ElementType > &vector)
Definition: array_view.h:248
value_type *const starting_element
Definition: array_view.h:141