BEM++  2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
_2d_array.hpp
1 // Copyright (C) 2011-2012 by the Bem++ Authors
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 // THE SOFTWARE.
20 
21 #ifndef fiber_2d_array_hpp
22 #define fiber_2d_array_hpp
23 
24 #include "../common/common.hpp"
25 #include "boost/operators.hpp"
26 
27 #include <stdexcept>
28 
29 #ifndef NDEBUG
30 #define FIBER_CHECK_ARRAY_BOUNDS
31 #endif
32 
33 namespace Fiber
34 {
35 
40 template <typename T>
41 class _2dArray : boost::additive<_2dArray<T>
42  , boost::multiplicative<_2dArray<T>,T > >
43 {
44 public:
45  _2dArray();
46  _2dArray(size_t extent0, size_t extent1);
47  _2dArray(size_t extent0, size_t extent1, T* data);
48  _2dArray(const _2dArray& other);
49  _2dArray& operator=(const _2dArray& rhs);
50 
51  ~_2dArray();
52 
53  T& operator()(size_t index0, size_t index1);
54  const T& operator()(size_t index0, size_t index1) const;
55 
56  _2dArray<T>& operator+=(const _2dArray<T>& other);
57  _2dArray<T>& operator*=(const T& other);
58 
59 
60  size_t extent(size_t dimension) const;
61  void set_size(size_t extent0, size_t extent1);
62 
63  typedef T* iterator;
64  typedef const T* const_iterator;
65 
66  iterator begin();
67  const_iterator begin() const;
68  iterator end();
69  const_iterator end() const;
70 
71 private:
72  void init_memory(size_t extent0, size_t extent1);
73  void free_memory();
74 
75 #ifdef FIBER_CHECK_ARRAY_BOUNDS
76  void check_dimension(size_t dimension) const;
77  void check_extents(size_t extent0, size_t extent1) const;
78  void check_indices(size_t index0, size_t index1) const;
79 #endif
80 
81 private:
82  size_t m_extents[2];
83  bool m_owns;
84  T* m_storage;
85 };
86 
88 template <typename T>
90 {
91 public:
93  _1dSliceOf2dArray(_2dArray<T>& array, size_t index1);
94 
102  _1dSliceOf2dArray& self();
103 
104  const T& operator()(size_t index0) const;
105  T& operator()(size_t index0);
106 
107  size_t extent(size_t dimension) const;
108 
109 private:
110  void check_dimension(size_t dimension) const;
111 
112 private:
113  _2dArray<T>& m_array;
114  size_t m_index1;
115 };
116 
118 template <typename T>
120 {
121 public:
122  _1dSliceOfConst2dArray(const _2dArray<T>& array, size_t index1);
123 
124  const T& operator()(size_t index0) const;
125 
126  size_t extent(size_t dimension) const;
127 
128 private:
129  void check_dimension(size_t dimension) const;
130 
131 private:
132  const _2dArray<T>& m_array;
133  size_t m_index1;
134 };
135 
136 } // namespace Fiber
137 
138 #include "_2d_array_imp.hpp"
139 
140 #endif
Lightweight encapsulation of a 1D slice of a 2D array.
Definition: _2d_array.hpp:89
_1dSliceOf2dArray(_2dArray< T > &array, size_t index1)
Construct a slice consisting of the elements array(:,index1)
Definition: _2d_array_imp.hpp:210
Simple implementation of a 2D Fortran-ordered array.
Definition: _2d_array.hpp:41
Lightweight encapsulation of a 1D slice of a constant 2D array.
Definition: _2d_array.hpp:119