1 /* -*- C++ -*- ------------------------------------------------------------
3 Copyright (c) 2007 Jesse Anders and Demian Nave http://cmldev.net/
5 The Configurable Math Library (CML) is distributed under the terms of the
6 Boost Software License, v1.0 (see cml/LICENSE for details).
8 *-----------------------------------------------------------------------*/
16 #include <cml/core/common.h>
17 #include <cml/core/fixed_1D.h>
19 /* This is used below to create a more meaningful compile-time error when
20 * an unknown layout argument is given:
22 struct invalid_layout_type_error
;
24 /* This is used below to create a more meaningful compile-time error when
25 * a negative size is given.
27 struct negative_array_size_error
;
31 /** The internal statically-allocated 2D-array implementation class.
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.
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:
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];
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
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.
60 template<typename Element
, int Rows
, int Cols
, typename Layout
>
65 /* Require Rows > 0, Cols > 0: */
67 (Rows
> 0) && (Cols
> 0),
68 negative_array_size_error
);
70 /* Require Layout to be row_major or col_major: */
72 (same_type
<Layout
,row_major
>::is_true
73 || same_type
<Layout
,col_major
>::is_true
),
74 invalid_layout_type_error
);
77 /* Record the generator: */
78 typedef fixed
<Rows
,Cols
> generator_type
;
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
;
87 /* For matching by memory layout: */
88 typedef Layout layout
;
90 /* For matching by memory type: */
91 typedef fixed_memory_tag memory_tag
;
93 /* For matching by size type: */
94 typedef fixed_size_tag size_tag
;
96 /* For matching by resizability: */
97 typedef not_resizable_tag resizing_tag
;
99 /* For matching by dimensions: */
100 typedef twod_tag dimension_tag
;
102 /* To simplify the matrix transpose operator: */
103 typedef fixed_2D
<Element
,Cols
,Rows
,Layout
> transposed_type
;
105 /* To simplify the matrix row and column operators: */
106 typedef fixed_1D
<Element
,Rows
> row_array_type
;
107 typedef fixed_1D
<Element
,Cols
> col_array_type
;
112 enum { array_rows
= Rows
, array_cols
= Cols
};
117 /** Return the number of rows in the array. */
118 size_t rows() const { return size_t(array_rows
); }
120 /** Return the number of cols in the array. */
121 size_t cols() const { return size_t(array_cols
); }
126 /** Access element (row,col) of the matrix.
128 * @param row row of element.
129 * @param col column of element.
130 * @returns mutable reference.
132 * @note This function does not range-check the arguments.
134 reference
operator()(size_t row
, size_t col
) {
135 /* Dispatch to the right function based on layout: */
136 return get_element(row
,col
,layout());
139 /** Const access element (row,col) of the matrix.
141 * @param row row of element.
142 * @param col column of element.
143 * @returns const reference.
145 * @note This function does not range-check the arguments.
147 const_reference
operator()(size_t row
, size_t col
) const {
148 /* Dispatch to the right function based on layout: */
149 return get_element(row
,col
,layout());
152 /** Return access to the data as a raw pointer. */
153 pointer
data() { return &m_data
[0][0]; }
155 /** Return access to the data as a raw pointer. */
156 const_pointer
data() const { return &m_data
[0][0]; }
166 reference
get_element(size_t row
, size_t col
, row_major
) {
167 return m_data
[row
][col
];
170 const_reference
get_element(size_t row
, size_t col
, row_major
) const {
171 return m_data
[row
][col
];
174 reference
get_element(size_t row
, size_t col
, col_major
) {
175 return m_data
[col
][row
];
178 const_reference
get_element(size_t row
, size_t col
, col_major
) const {
179 return m_data
[col
][row
];
185 /* Typedef the possible layouts: */
186 typedef Element row_major_array
[Rows
][Cols
];
187 typedef Element col_major_array
[Cols
][Rows
];
189 /* Now, select the right layout for the current matrix: */
190 typedef typename select_switch
<
191 Layout
, row_major
, row_major_array
, /* Case 1 */
192 col_major
, col_major_array
/* Case 2 */
193 >::result array_data
;
195 /* Declare the data array: */
203 // -------------------------------------------------------------------------