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 *-----------------------------------------------------------------------*/
17 #include <cml/core/common.h>
18 #include <cml/core/dynamic_1D.h>
19 #include <cml/dynamic.h>
23 /** Dynamically-sized and allocated 2D array.
25 * @note The allocator should be an STL-compatible allocator.
27 * @internal The internal array type <em>must</em> have the proper copy
28 * semantics, otherwise copy construction will fail.
30 * @internal This class does not need a destructor.
32 template<typename Element
, typename Layout
, class Alloc
>
37 /* Record the allocator type: */
38 typedef typename
Alloc::template rebind
<Element
>::other allocator_type
;
40 /* Record the generator: */
41 typedef dynamic
<Alloc
> generator_type
;
43 /* Array implementation: */
44 typedef std::vector
<Element
,allocator_type
> array_impl
;
47 typedef typename
array_impl::value_type value_type
;
48 typedef typename
array_impl::pointer pointer
;
49 typedef typename
array_impl::reference reference
;
50 typedef typename
array_impl::const_reference const_reference
;
51 typedef typename
array_impl::const_pointer const_pointer
;
53 /* For matching by memory layout: */
54 typedef Layout layout
;
56 /* For matching by memory type: */
57 typedef dynamic_memory_tag memory_tag
;
59 /* For matching by size type: */
60 typedef dynamic_size_tag size_tag
;
62 /* For matching by resizability: */
63 typedef resizable_tag resizing_tag
;
65 /* For matching by dimensions: */
66 typedef twod_tag dimension_tag
;
68 /* To simplify the matrix transpose operator: */
69 typedef dynamic_2D
<Element
,Layout
,Alloc
> transposed_type
;
71 /* To simplify the matrix row and column operators: */
72 typedef dynamic_1D
<Element
,Alloc
> row_array_type
;
73 typedef dynamic_1D
<Element
,Alloc
> col_array_type
;
78 /** Construct a dynamic array with no size. */
81 /** Construct a dynamic matrix given the dimensions.
83 * This constructor is guaranteed to throw only if the allocator throws.
84 * If the array implementation guarantees that the array data structure is
85 * not modified after an exception, then this constructor is
88 * @throws only if the allocator throws during an allocation.
90 explicit dynamic_2D(size_t rows
, size_t cols
)
91 : m_rows(rows
), m_cols(cols
), m_data(rows
*cols
) {}
96 enum { array_rows
= -1, array_cols
= -1 };
101 /** Return the number of rows in the array. */
102 size_t rows() const { return m_rows
; }
104 /** Return the number of cols in the array. */
105 size_t cols() const { return m_cols
; }
110 /** Access the given element of the matrix.
112 * @param row row of element.
113 * @param col column of element.
114 * @returns mutable reference.
116 reference
operator()(size_t row
, size_t col
) {
117 return get_element(row
, col
, layout());
120 /** Access the given element of the matrix.
122 * @param row row of element.
123 * @param col column of element.
124 * @returns const reference.
126 const_reference
operator()(size_t row
, size_t col
) const {
127 return get_element(row
, col
, layout());
130 /** Return access to the data as a raw pointer. */
131 pointer
data() { return &m_data
[0]; }
133 /** Return access to the data as a raw pointer. */
134 const_pointer
data() const { return &m_data
[0]; }
139 /** Resize the array.
141 * @warning This is not guaranteed to preserve the original data.
143 void resize(size_t rows
, size_t cols
) {
144 m_data
.resize(rows
*cols
); m_rows
= rows
; m_cols
= cols
;
150 reference
get_element(size_t row
, size_t col
, row_major
) {
151 return m_data
[row
*m_cols
+ col
];
154 const_reference
get_element(size_t row
, size_t col
, row_major
) const {
155 return m_data
[row
*m_cols
+ col
];
158 reference
get_element(size_t row
, size_t col
, col_major
) {
159 return m_data
[col
*m_rows
+ row
];
162 const_reference
get_element(size_t row
, size_t col
, col_major
) const {
163 return m_data
[col
*m_rows
+ row
];
169 size_t m_rows
, m_cols
;
177 // -------------------------------------------------------------------------