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
<typename
cml::remove_const
<Element
>::type
,
104 Cols
,Rows
,Layout
> transposed_type
;
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
;
113 enum { array_rows
= Rows
, array_cols
= Cols
};
118 /** Return the number of rows in the array. */
119 size_t rows() const { return size_t(array_rows
); }
121 /** Return the number of cols in the array. */
122 size_t cols() const { return size_t(array_cols
); }
127 /** Access element (row,col) of the matrix.
129 * @param row row of element.
130 * @param col column of element.
131 * @returns mutable reference.
133 * @note This function does not range-check the arguments.
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());
140 /** Const access element (row,col) of the matrix.
142 * @param row row of element.
143 * @param col column of element.
144 * @returns const reference.
146 * @note This function does not range-check the arguments.
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());
153 /** Return access to the data as a raw pointer. */
154 pointer
data() { return &m_data
[0][0]; }
156 /** Return access to the data as a raw pointer. */
157 const_pointer
data() const { return &m_data
[0][0]; }
167 reference
get_element(size_t row
, size_t col
, row_major
) {
168 return m_data
[row
][col
];
171 const_reference
get_element(size_t row
, size_t col
, row_major
) const {
172 return m_data
[row
][col
];
175 reference
get_element(size_t row
, size_t col
, col_major
) {
176 return m_data
[col
][row
];
179 const_reference
get_element(size_t row
, size_t col
, col_major
) const {
180 return m_data
[col
][row
];
186 /* Typedef the possible layouts: */
187 typedef Element row_major_array
[Rows
][Cols
];
188 typedef Element col_major_array
[Cols
][Rows
];
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
;
196 /* Declare the data array: */
204 // -------------------------------------------------------------------------