]> Dogcows Code - chaz/yoink/blob - src/Moof/cml/core/external_2D.h
extreme refactoring
[chaz/yoink] / src / Moof / cml / core / external_2D.h
1 /* -*- C++ -*- ------------------------------------------------------------
2
3 Copyright (c) 2007 Jesse Anders and Demian Nave http://cmldev.net/
4
5 The Configurable Math Library (CML) is distributed under the terms of the
6 Boost Software License, v1.0 (see cml/LICENSE for details).
7
8 *-----------------------------------------------------------------------*/
9 /** @file
10 * @brief
11 *
12 * Defines the fixed-size and runtime-sized external 2D arrays.
13 *
14 * @todo Would casting get better performance in the external_2D<> element
15 * access methods?
16 */
17
18 #ifndef external_2D_h
19 #define external_2D_h
20
21 #include <cml/core/common.h>
22 #include <cml/core/fixed_1D.h>
23 #include <cml/core/fixed_2D.h>
24 #include <cml/core/dynamic_1D.h>
25 #include <cml/core/dynamic_2D.h>
26 #include <cml/external.h>
27
28 namespace cml {
29
30 /** Fixed-size external 2D array.
31 *
32 * Both the memory and the size are fixed at compile time, and cannot be
33 * changed.
34 */
35 template<typename Element, int Rows, int Cols, typename Layout>
36 class external_2D
37 {
38 public:
39
40 /* Require Rows > 0, Cols > 0: */
41 CML_STATIC_REQUIRE((Rows > 0) && (Cols > 0));
42
43 /* Record the generator: */
44 typedef external<Rows,Cols> generator_type;
45
46 /* Standard: */
47 typedef Element value_type;
48 typedef Element* pointer;
49 typedef Element& reference;
50 typedef const Element& const_reference;
51 typedef const Element* const_pointer;
52
53 /* For matching by memory layout: */
54 typedef Layout layout;
55
56 /* For matching by memory type: */
57 typedef external_memory_tag memory_tag;
58
59 /* For matching by size type: */
60 typedef fixed_size_tag size_tag;
61
62 /* For matching by resizability: */
63 typedef not_resizable_tag resizing_tag;
64
65 /* For matching by dimensions: */
66 typedef twod_tag dimension_tag;
67
68 /* To simplify the matrix transpose operator: */
69 typedef fixed_2D<Element,Cols,Rows,Layout> transposed_type;
70 /* Note: the transposed type must be fixed_2D, since an external array
71 * cannot be specified without a corresponding memory location.
72 */
73
74 /* To simplify the matrix row and column operators: */
75 typedef fixed_1D<Element,Rows> row_array_type;
76 typedef fixed_1D<Element,Cols> col_array_type;
77 /* Note: the row types must be fixed_1D, since external arrays cannot be
78 * specified without a memory location.
79 */
80
81
82 public:
83
84 enum { array_rows = Rows, array_cols = Cols };
85
86
87 public:
88
89 /** Construct an external array from a pointer. */
90 external_2D(value_type const ptr[Rows][Cols])
91 : m_data(const_cast<pointer>(&ptr[0][0])) {}
92
93 /** Construct an external array from a pointer. */
94 external_2D(value_type* const ptr) : m_data(ptr) {}
95
96
97 public:
98
99 /** Return the number of rows in the array. */
100 size_t rows() const { return size_t(array_rows); }
101
102 /** Return the number of cols in the array. */
103 size_t cols() const { return size_t(array_cols); }
104
105
106 public:
107
108 /** Access element (row,col) of the matrix.
109 *
110 * @param row row of element.
111 * @param col column of element.
112 * @returns mutable reference.
113 *
114 * @note This function does not range-check the arguments.
115 */
116 reference operator()(size_t row, size_t col) {
117 /* Dispatch to the right function based on layout: */
118 return get_element(row,col,layout());
119 }
120
121 /** Const access element (row,col) of the matrix.
122 *
123 * @param row row of element.
124 * @param col column of element.
125 * @returns const reference.
126 *
127 * @note This function does not range-check the arguments.
128 */
129 const_reference operator()(size_t row, size_t col) const {
130 /* Dispatch to the right function based on layout: */
131 return get_element(row,col,layout());
132 }
133
134 /** Return access to the data as a raw pointer. */
135 pointer data() { return m_data; }
136
137 /** Return access to the data as a raw pointer. */
138 const_pointer data() const { return m_data; }
139
140
141 protected:
142
143 /* XXX May be able to cast to get better performance? */
144 reference get_element(size_t row, size_t col, row_major) {
145 return m_data[row*Cols + col];
146 }
147
148 const_reference get_element(size_t row, size_t col, row_major) const {
149 return m_data[row*Cols + col];
150 }
151
152 reference get_element(size_t row, size_t col, col_major) {
153 return m_data[col*Rows + row];
154 }
155
156 const_reference get_element(size_t row, size_t col, col_major) const {
157 return m_data[col*Rows + row];
158 }
159
160
161 protected:
162
163 /* Declare the data array: */
164 pointer const m_data;
165 };
166
167 /** Run-time sized external 2D array.
168 *
169 * Both the memory and the size are fixed at run-time, but cannot be changed.
170 * This is a specialization for the case that Rows and Cols are not specified
171 * (i.e. given as the default of -1,-1).
172 */
173 template<typename Element, typename Layout>
174 class external_2D<Element,-1,-1,Layout>
175 {
176 public:
177
178 /* Record the generator. Note: this is *not* unique, as it is the same
179 * generator used by external_1D. However, external_1D is used only by
180 * vector<> classes, so this is not a problem.
181 */
182 typedef external<> generator_type;
183
184 /* Standard: */
185 typedef Element value_type;
186 typedef Element* pointer;
187 typedef Element& reference;
188 typedef const Element& const_reference;
189 typedef const Element* const_pointer;
190
191 /* For matching by memory layout: */
192 typedef Layout layout;
193
194 /* For matching by memory type: */
195 typedef external_memory_tag memory_tag;
196
197 /* For matching by size type: */
198 typedef dynamic_size_tag size_tag;
199
200 /* For matching by resizability: */
201 typedef not_resizable_tag resizing_tag;
202
203 /* For matching by dimensions: */
204 typedef twod_tag dimension_tag;
205
206 /* To simplify the matrix transpose operator: */
207 typedef dynamic_2D<Element,Layout, CML_DEFAULT_ARRAY_ALLOC>
208 transposed_type;
209
210 /* To simplify the matrix row and column operators: */
211 typedef dynamic_1D<Element, CML_DEFAULT_ARRAY_ALLOC> row_array_type;
212 typedef dynamic_1D<Element, CML_DEFAULT_ARRAY_ALLOC> col_array_type;
213
214
215 public:
216
217 enum { array_rows = -1, array_cols = -1 };
218
219
220 public:
221
222 /** Construct an external array with no size. */
223 external_2D(pointer const ptr, size_t rows, size_t cols)
224 : m_data(ptr), m_rows(rows), m_cols(cols) {}
225
226
227 public:
228
229 /** Return the number of rows in the array. */
230 size_t rows() const { return m_rows; }
231
232 /** Return the number of cols in the array. */
233 size_t cols() const { return m_cols; }
234
235
236 public:
237
238 /** Access element (row,col) of the matrix.
239 *
240 * @param row row of element.
241 * @param col column of element.
242 * @returns mutable reference.
243 *
244 * @note This function does not range-check the arguments.
245 */
246 reference operator()(size_t row, size_t col) {
247 /* Dispatch to the right function based on layout: */
248 return get_element(row,col,layout());
249 }
250
251 /** Const access element (row,col) of the matrix.
252 *
253 * @param row row of element.
254 * @param col column of element.
255 * @returns const reference.
256 *
257 * @note This function does not range-check the arguments.
258 */
259 const_reference operator()(size_t row, size_t col) const {
260 /* Dispatch to the right function based on layout: */
261 return get_element(row,col,layout());
262 }
263
264 /** Return access to the data as a raw pointer. */
265 pointer data() { return m_data; }
266
267 /** Return access to the data as a raw pointer. */
268 const_pointer data() const { return m_data; }
269
270
271 protected:
272
273 /* XXX May be able to cast to get better performance? */
274 reference get_element(size_t row, size_t col, row_major) {
275 return m_data[row*m_cols + col];
276 }
277
278 const_reference get_element(size_t row, size_t col, row_major) const {
279 return m_data[row*m_cols + col];
280 }
281
282 reference get_element(size_t row, size_t col, col_major) {
283 return m_data[col*m_rows + row];
284 }
285
286 const_reference get_element(size_t row, size_t col, col_major) const {
287 return m_data[col*m_rows + row];
288 }
289
290
291 protected:
292
293 /* Declare the data array: */
294 value_type* const m_data;
295 const size_t m_rows;
296 const size_t m_cols;
297 };
298
299 } // namespace cml
300
301 #endif
302
303 // -------------------------------------------------------------------------
304 // vim:ft=cpp
This page took 0.045143 seconds and 4 git commands to generate.