dynamic_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 dynamic_2D_h
00014 #define dynamic_2D_h
00015 
00016 #include <vector>
00017 #include <cml/core/common.h>
00018 #include <cml/core/dynamic_1D.h>
00019 #include <cml/dynamic.h>
00020 
00021 namespace cml {
00022 
00032 template<typename Element, typename Layout, class Alloc>
00033 class dynamic_2D
00034 {
00035   public:
00036 
00037     /* Record the allocator type: */
00038     typedef typename Alloc::template rebind<Element>::other allocator_type;
00039 
00040     /* Record the generator: */
00041     typedef dynamic<Alloc> generator_type;
00042 
00043     /* Array implementation: */
00044     typedef std::vector<Element,allocator_type> array_impl;
00045 
00046     /* Standard: */
00047     typedef typename array_impl::value_type value_type;
00048     typedef typename array_impl::pointer pointer; 
00049     typedef typename array_impl::reference reference; 
00050     typedef typename array_impl::const_reference const_reference; 
00051     typedef typename array_impl::const_pointer const_pointer; 
00052 
00053     /* For matching by memory layout: */
00054     typedef Layout layout;
00055 
00056     /* For matching by memory type: */
00057     typedef dynamic_memory_tag memory_tag;
00058 
00059     /* For matching by size type: */
00060     typedef dynamic_size_tag size_tag;
00061 
00062     /* For matching by resizability: */
00063     typedef resizable_tag resizing_tag;
00064 
00065     /* For matching by dimensions: */
00066     typedef twod_tag dimension_tag;
00067 
00068     /* To simplify the matrix transpose operator: */
00069     typedef dynamic_2D<Element,Layout,Alloc> transposed_type;
00070 
00071     /* To simplify the matrix row and column operators: */
00072     typedef dynamic_1D<Element,Alloc> row_array_type;
00073     typedef dynamic_1D<Element,Alloc> col_array_type;
00074 
00075 
00076   protected:
00077 
00079     dynamic_2D() {}
00080 
00090     explicit dynamic_2D(size_t rows, size_t cols) 
00091         : m_rows(rows), m_cols(cols), m_data(rows*cols) {}
00092 
00093 
00094   public:
00095 
00096     enum { array_rows = -1, array_cols = -1 };
00097 
00098 
00099   public:
00100 
00102     size_t rows() const { return m_rows; }
00103 
00105     size_t cols() const { return m_cols; }
00106 
00107 
00108   public:
00109 
00116     reference operator()(size_t row, size_t col) {
00117         return get_element(row, col, layout());
00118     }
00119 
00126     const_reference operator()(size_t row, size_t col) const {
00127         return get_element(row, col, layout());
00128     }
00129 
00131     pointer data() { return &m_data[0]; }
00132 
00134     const_pointer data() const { return &m_data[0]; }
00135 
00136 
00137   public:
00138 
00143     void resize(size_t rows, size_t cols) {
00144         m_data.resize(rows*cols); m_rows = rows; m_cols = cols;
00145     }
00146 
00147 
00148   protected:
00149 
00150     reference get_element(size_t row, size_t col, row_major) {
00151         return m_data[row*m_cols + col];
00152     }
00153 
00154     const_reference get_element(size_t row, size_t col, row_major) const {
00155         return m_data[row*m_cols + col];
00156     }
00157 
00158     reference get_element(size_t row, size_t col, col_major) {
00159         return m_data[col*m_rows + row];
00160     }
00161 
00162     const_reference get_element(size_t row, size_t col, col_major) const {
00163         return m_data[col*m_rows + row];
00164     }
00165 
00166 
00167   protected:
00168 
00169     size_t                      m_rows, m_cols;
00170     array_impl                  m_data;
00171 };
00172 
00173 } // namespace cml
00174 
00175 #endif
00176 
00177 // -------------------------------------------------------------------------
00178 // vim:ft=cpp

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