]> Dogcows Code - chaz/yoink/blob - src/cml/core/dynamic_2D.h
now using cml for vectors and math stuff
[chaz/yoink] / src / cml / core / dynamic_2D.h
1 /* -*- C++ -*- ------------------------------------------------------------
2
3 Copyright (c) 2007 Jesse Anders and Demian Nave http://cmldev.net/
4
5 The Configurable Math Library (CML) is distributed under the terms of the
6 Boost Software License, v1.0 (see cml/LICENSE for details).
7
8 *-----------------------------------------------------------------------*/
9 /** @file
10 * @brief
11 */
12
13 #ifndef dynamic_2D_h
14 #define dynamic_2D_h
15
16 #include <vector>
17 #include <cml/core/common.h>
18 #include <cml/core/dynamic_1D.h>
19 #include <cml/dynamic.h>
20
21 namespace cml {
22
23 /** Dynamically-sized and allocated 2D array.
24 *
25 * @note The allocator should be an STL-compatible allocator.
26 *
27 * @internal The internal array type <em>must</em> have the proper copy
28 * semantics, otherwise copy construction will fail.
29 *
30 * @internal This class does not need a destructor.
31 */
32 template<typename Element, typename Layout, class Alloc>
33 class dynamic_2D
34 {
35 public:
36
37 /* Record the allocator type: */
38 typedef typename Alloc::template rebind<Element>::other allocator_type;
39
40 /* Record the generator: */
41 typedef dynamic<Alloc> generator_type;
42
43 /* Array implementation: */
44 typedef std::vector<Element,allocator_type> array_impl;
45
46 /* Standard: */
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;
52
53 /* For matching by memory layout: */
54 typedef Layout layout;
55
56 /* For matching by memory type: */
57 typedef dynamic_memory_tag memory_tag;
58
59 /* For matching by size type: */
60 typedef dynamic_size_tag size_tag;
61
62 /* For matching by resizability: */
63 typedef resizable_tag resizing_tag;
64
65 /* For matching by dimensions: */
66 typedef twod_tag dimension_tag;
67
68 /* To simplify the matrix transpose operator: */
69 typedef dynamic_2D<Element,Layout,Alloc> transposed_type;
70
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;
74
75
76 protected:
77
78 /** Construct a dynamic array with no size. */
79 dynamic_2D() {}
80
81 /** Construct a dynamic matrix given the dimensions.
82 *
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
86 * exception-safe.
87 *
88 * @throws only if the allocator throws during an allocation.
89 */
90 explicit dynamic_2D(size_t rows, size_t cols)
91 : m_rows(rows), m_cols(cols), m_data(rows*cols) {}
92
93
94 public:
95
96 enum { array_rows = -1, array_cols = -1 };
97
98
99 public:
100
101 /** Return the number of rows in the array. */
102 size_t rows() const { return m_rows; }
103
104 /** Return the number of cols in the array. */
105 size_t cols() const { return m_cols; }
106
107
108 public:
109
110 /** Access the given element of the matrix.
111 *
112 * @param row row of element.
113 * @param col column of element.
114 * @returns mutable reference.
115 */
116 reference operator()(size_t row, size_t col) {
117 return get_element(row, col, layout());
118 }
119
120 /** Access the given element of the matrix.
121 *
122 * @param row row of element.
123 * @param col column of element.
124 * @returns const reference.
125 */
126 const_reference operator()(size_t row, size_t col) const {
127 return get_element(row, col, layout());
128 }
129
130 /** Return access to the data as a raw pointer. */
131 pointer data() { return &m_data[0]; }
132
133 /** Return access to the data as a raw pointer. */
134 const_pointer data() const { return &m_data[0]; }
135
136
137 public:
138
139 /** Resize the array.
140 *
141 * @warning This is not guaranteed to preserve the original data.
142 */
143 void resize(size_t rows, size_t cols) {
144 m_data.resize(rows*cols); m_rows = rows; m_cols = cols;
145 }
146
147
148 protected:
149
150 reference get_element(size_t row, size_t col, row_major) {
151 return m_data[row*m_cols + col];
152 }
153
154 const_reference get_element(size_t row, size_t col, row_major) const {
155 return m_data[row*m_cols + col];
156 }
157
158 reference get_element(size_t row, size_t col, col_major) {
159 return m_data[col*m_rows + row];
160 }
161
162 const_reference get_element(size_t row, size_t col, col_major) const {
163 return m_data[col*m_rows + row];
164 }
165
166
167 protected:
168
169 size_t m_rows, m_cols;
170 array_impl m_data;
171 };
172
173 } // namespace cml
174
175 #endif
176
177 // -------------------------------------------------------------------------
178 // vim:ft=cpp
This page took 0.037519 seconds and 4 git commands to generate.