]> Dogcows Code - chaz/yoink/blob - src/moof/cml/core/fixed_2D.h
bugfix: win32 packaging script temp directories
[chaz/yoink] / src / moof / cml / core / fixed_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
13 #ifndef fixed_2D_h
14 #define fixed_2D_h
15
16 #include <cml/core/common.h>
17 #include <cml/core/fixed_1D.h>
18
19 /* This is used below to create a more meaningful compile-time error when
20 * an unknown layout argument is given:
21 */
22 struct invalid_layout_type_error;
23
24 /* This is used below to create a more meaningful compile-time error when
25 * a negative size is given.
26 */
27 struct negative_array_size_error;
28
29 namespace cml {
30
31 /** The internal statically-allocated 2D-array implementation class.
32 *
33 * This uses an internal class to setup the data matrix with the proper
34 * layout. The alternative is to use a 1D array with size Rows*Cols and a
35 * multiplication to dereference an element, but it seems that compilers
36 * better optimize 2D array dereferences. This is different from
37 * dynamic_2D<>, which must use the 1D array method.
38 *
39 * @sa cml::fixed
40 *
41 * @note This class is designed to have the same size as a C array with the
42 * same dimensions. It's therefore possible (but not recommended!) to coerce
43 * a normal C array into a fixed_2D<> like this:
44 *
45 * typedef fixed_2D<double,10,10,row_major> array;
46 * double c_array[10][10];
47 * array& array_object = *((array*)&c_array);
48 * double e11 = array_object[1][1];
49 *
50 * It's also possible to do this with a pointer to an array of values (e.g. a
51 * double*), whether or not it was actually declared as a fixed C array. This
52 * is HIGHLY DISCOURAGED, though, since it's relatively straightforward to
53 * implement a separate class to take a C array (or pointer) and turn it into
54 * an array object.
55 *
56 * @internal Do <em>not</em> add the empty constructor and destructor; at
57 * least one compiler (Intel C++ 9.0) fails to optimize them away, and they
58 * aren't needed anyway here.
59 */
60 template<typename Element, int Rows, int Cols, typename Layout>
61 class fixed_2D
62 {
63 public:
64
65 /* Require Rows > 0, Cols > 0: */
66 CML_STATIC_REQUIRE_M(
67 (Rows > 0) && (Cols > 0),
68 negative_array_size_error);
69
70 /* Require Layout to be row_major or col_major: */
71 CML_STATIC_REQUIRE_M(
72 (same_type<Layout,row_major>::is_true
73 || same_type<Layout,col_major>::is_true),
74 invalid_layout_type_error);
75
76
77 /* Record the generator: */
78 typedef fixed<Rows,Cols> generator_type;
79
80 /* Standard: */
81 typedef Element value_type;
82 typedef Element* pointer;
83 typedef Element& reference;
84 typedef const Element& const_reference;
85 typedef const Element* const_pointer;
86
87 /* For matching by memory layout: */
88 typedef Layout layout;
89
90 /* For matching by memory type: */
91 typedef fixed_memory_tag memory_tag;
92
93 /* For matching by size type: */
94 typedef fixed_size_tag size_tag;
95
96 /* For matching by resizability: */
97 typedef not_resizable_tag resizing_tag;
98
99 /* For matching by dimensions: */
100 typedef twod_tag dimension_tag;
101
102 /* To simplify the matrix transpose operator: */
103 typedef fixed_2D<typename cml::remove_const<Element>::type,
104 Cols,Rows,Layout> transposed_type;
105
106 /* To simplify the matrix row and column operators: */
107 typedef fixed_1D<Element,Rows> row_array_type;
108 typedef fixed_1D<Element,Cols> col_array_type;
109
110
111 public:
112
113 enum { array_rows = Rows, array_cols = Cols };
114
115
116 public:
117
118 /** Return the number of rows in the array. */
119 size_t rows() const { return size_t(array_rows); }
120
121 /** Return the number of cols in the array. */
122 size_t cols() const { return size_t(array_cols); }
123
124
125 public:
126
127 /** Access element (row,col) of the matrix.
128 *
129 * @param row row of element.
130 * @param col column of element.
131 * @returns mutable reference.
132 *
133 * @note This function does not range-check the arguments.
134 */
135 reference operator()(size_t row, size_t col) {
136 /* Dispatch to the right function based on layout: */
137 return get_element(row,col,layout());
138 }
139
140 /** Const access element (row,col) of the matrix.
141 *
142 * @param row row of element.
143 * @param col column of element.
144 * @returns const reference.
145 *
146 * @note This function does not range-check the arguments.
147 */
148 const_reference operator()(size_t row, size_t col) const {
149 /* Dispatch to the right function based on layout: */
150 return get_element(row,col,layout());
151 }
152
153 /** Return access to the data as a raw pointer. */
154 pointer data() { return &m_data[0][0]; }
155
156 /** Return access to the data as a raw pointer. */
157 const_pointer data() const { return &m_data[0][0]; }
158
159
160 public:
161
162 fixed_2D() {}
163
164
165 protected:
166
167 reference get_element(size_t row, size_t col, row_major) {
168 return m_data[row][col];
169 }
170
171 const_reference get_element(size_t row, size_t col, row_major) const {
172 return m_data[row][col];
173 }
174
175 reference get_element(size_t row, size_t col, col_major) {
176 return m_data[col][row];
177 }
178
179 const_reference get_element(size_t row, size_t col, col_major) const {
180 return m_data[col][row];
181 }
182
183
184 protected:
185
186 /* Typedef the possible layouts: */
187 typedef Element row_major_array[Rows][Cols];
188 typedef Element col_major_array[Cols][Rows];
189
190 /* Now, select the right layout for the current matrix: */
191 typedef typename select_switch<
192 Layout, row_major, row_major_array, /* Case 1 */
193 col_major, col_major_array /* Case 2 */
194 >::result array_data;
195
196 /* Declare the data array: */
197 array_data m_data;
198 };
199
200 } // namespace cml
201
202 #endif
203
204 // -------------------------------------------------------------------------
205 // vim:ft=cpp
This page took 0.041846 seconds and 4 git commands to generate.