fixed_2D.h

Go to the documentation of this file.
00001 /* -*- C++ -*- ------------------------------------------------------------
00002  
00003 Copyright (c) 2007 Jesse Anders and Demian Nave http://cmldev.net/
00004 
00005 The Configurable Math Library (CML) is distributed under the terms of the
00006 Boost Software License, v1.0 (see cml/LICENSE for details).
00007 
00008  *-----------------------------------------------------------------------*/
00013 #ifndef fixed_2D_h
00014 #define fixed_2D_h
00015 
00016 #include <cml/core/common.h>
00017 #include <cml/core/fixed_1D.h>
00018 
00019 /* This is used below to create a more meaningful compile-time error when
00020  * an unknown layout argument is given:
00021  */
00022 struct invalid_layout_type_error;
00023 
00024 /* This is used below to create a more meaningful compile-time error when
00025  * a negative size is given.
00026  */
00027 struct negative_array_size_error;
00028 
00029 namespace cml {
00030 
00060 template<typename Element, int Rows, int Cols, typename Layout>
00061 class fixed_2D
00062 {
00063   public:
00064 
00065     /* Require Rows > 0, Cols > 0: */
00066     CML_STATIC_REQUIRE_M(
00067             (Rows > 0) && (Cols > 0),
00068             negative_array_size_error);
00069 
00070     /* Require Layout to be row_major or col_major: */
00071     CML_STATIC_REQUIRE_M(
00072             (same_type<Layout,row_major>::is_true
00073              || same_type<Layout,col_major>::is_true),
00074             invalid_layout_type_error);
00075 
00076 
00077     /* Record the generator: */
00078     typedef fixed<Rows,Cols> generator_type;
00079 
00080     /* Standard: */
00081     typedef Element value_type;
00082     typedef Element* pointer;
00083     typedef Element& reference;
00084     typedef const Element& const_reference;
00085     typedef const Element* const_pointer;
00086 
00087     /* For matching by memory layout: */
00088     typedef Layout layout;
00089 
00090     /* For matching by memory type: */
00091     typedef fixed_memory_tag memory_tag;
00092 
00093     /* For matching by size type: */
00094     typedef fixed_size_tag size_tag;
00095 
00096     /* For matching by resizability: */
00097     typedef not_resizable_tag resizing_tag;
00098 
00099     /* For matching by dimensions: */
00100     typedef twod_tag dimension_tag;
00101 
00102     /* To simplify the matrix transpose operator: */
00103     typedef fixed_2D<Element,Cols,Rows,Layout> transposed_type;
00104 
00105     /* To simplify the matrix row and column operators: */
00106     typedef fixed_1D<Element,Rows> row_array_type;
00107     typedef fixed_1D<Element,Cols> col_array_type;
00108 
00109 
00110   public:
00111 
00112     enum { array_rows = Rows, array_cols = Cols };
00113 
00114 
00115   public:
00116 
00118     size_t rows() const { return size_t(array_rows); }
00119 
00121     size_t cols() const { return size_t(array_cols); }
00122 
00123 
00124   public:
00125 
00134     reference operator()(size_t row, size_t col) {
00135         /* Dispatch to the right function based on layout: */
00136         return get_element(row,col,layout());
00137     }
00138 
00147     const_reference operator()(size_t row, size_t col) const {
00148         /* Dispatch to the right function based on layout: */
00149         return get_element(row,col,layout());
00150     }
00151 
00153     pointer data() { return &m_data[0][0]; }
00154 
00156     const_pointer data() const { return &m_data[0][0]; }
00157 
00158 
00159   public:
00160 
00161     fixed_2D() {}
00162 
00163 
00164   protected:
00165 
00166     reference get_element(size_t row, size_t col, row_major) {
00167         return m_data[row][col];
00168     }
00169 
00170     const_reference get_element(size_t row, size_t col, row_major) const {
00171         return m_data[row][col];
00172     }
00173 
00174     reference get_element(size_t row, size_t col, col_major) {
00175         return m_data[col][row];
00176     }
00177 
00178     const_reference get_element(size_t row, size_t col, col_major) const {
00179         return m_data[col][row];
00180     }
00181 
00182 
00183   protected:
00184 
00185     /* Typedef the possible layouts: */
00186     typedef Element row_major_array[Rows][Cols];
00187     typedef Element col_major_array[Cols][Rows];
00188 
00189     /* Now, select the right layout for the current matrix: */
00190     typedef typename select_switch<
00191         Layout, row_major, row_major_array,     /* Case 1 */
00192                 col_major, col_major_array      /* Case 2 */
00193         >::result array_data;
00194 
00195     /* Declare the data array: */
00196     array_data                  m_data;
00197 };
00198 
00199 } // namespace cml
00200 
00201 #endif
00202 
00203 // -------------------------------------------------------------------------
00204 // vim:ft=cpp

Generated on Sat Jul 18 19:35:20 2009 for CML 1.0 by  doxygen 1.5.9