1
/*************************************************************************
3
* Copyright (c) 2010 Kohei Yoshida
5
* Permission is hereby granted, free of charge, to any person
6
* obtaining a copy of this software and associated documentation
7
* files (the "Software"), to deal in the Software without
8
* restriction, including without limitation the rights to use,
9
* copy, modify, merge, publish, distribute, sublicense, and/or sell
10
* copies of the Software, and to permit persons to whom the
11
* Software is furnished to do so, subject to the following
14
* The above copyright notice and this permission notice shall be
15
* included in all copies or substantial portions of the Software.
17
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24
* OTHER DEALINGS IN THE SOFTWARE.
26
************************************************************************/
28
#ifndef __MDDS_QUAD_TYPE_MATRIX_HPP__
29
#define __MDDS_QUAD_TYPE_MATRIX_HPP__
31
#include "mdds/global.hpp"
32
#include "mdds/mixed_type_matrix_element.hpp"
33
#include "mdds/mixed_type_matrix_storage.hpp"
34
#include "mdds/mixed_type_matrix_flag_storage.hpp"
43
matrix_density_filled_zero,
44
matrix_density_filled_empty,
45
matrix_density_sparse_zero,
46
matrix_density_sparse_empty
49
class matrix_error : public ::mdds::general_error
52
matrix_error(const ::std::string& msg) : general_error(msg) {}
56
* This data structure represents a matrix where each individual element may
57
* be of one of four types: value, boolean, string, or empty.
59
template<typename _String, typename _Flag>
60
class mixed_type_matrix
63
typedef _String string_type;
64
typedef _Flag flag_type;
65
typedef size_t size_type;
66
typedef ::std::pair<size_type, size_type> size_pair_type;
67
typedef ::mdds::element<string_type> element;
70
struct size_pair_type_hash
72
size_t operator() (const size_pair_type& val) const
74
size_t n = val.first + (val.second << 8);
78
typedef ::mdds::storage_base<mixed_type_matrix> storage_base;
80
static storage_base* create_storage(size_t rows, size_t cols, matrix_density_t density);
83
typedef ::mdds::flag_storage<flag_type, size_pair_type, size_pair_type_hash> flag_storage;
84
typedef ::mdds::storage_filled<mixed_type_matrix> filled_storage_type;
85
typedef ::mdds::storage_sparse<mixed_type_matrix> sparse_storage_type;
87
typedef typename storage_base::const_iterator const_iterator;
90
* Default constructor.
95
* Construct an empty matrix with specified density type.
97
mixed_type_matrix(matrix_density_t density);
100
* Construct a matrix of specified size with specified density type.
102
mixed_type_matrix(size_t rows, size_t cols, matrix_density_t density);
104
mixed_type_matrix(const mixed_type_matrix& r);
105
~mixed_type_matrix();
107
const_iterator begin() const;
108
const_iterator end() const;
110
mixed_type_matrix& operator= (const mixed_type_matrix& r);
113
* Get the type of element specified by its position. The type can be one
114
* of empty, string, numeric, or boolean.
116
* @return element type.
118
matrix_element_t get_type(size_t row, size_t col) const;
120
double get_numeric(size_t row, size_t col) const;
121
bool get_boolean(size_t row, size_t col) const;
122
const string_type* get_string(size_t row, size_t col) const;
124
void set_numeric(size_t row, size_t col, double val);
125
void set_boolean(size_t row, size_t col, bool val);
126
void set_string(size_t row, size_t col, string_type* str);
127
void set_empty(size_t row, size_t col);
129
void set(size_t row, size_t col, double val);
130
void set(size_t row, size_t col, bool val);
131
void set(size_t row, size_t col, string_type* str);
134
* Set flag value at specified position.
136
* @param row row position
137
* @param col column position
138
* @param flag_type flag value
140
void set_flag(size_t row, size_t col, flag_type flag);
143
* Get flag value at specified position.
145
* @param row row position
146
* @param col column position
148
* @return flag value stored at specified position
150
flag_type get_flag(size_t row, size_t col) const;
152
void clear_flag(size_t row, size_t cols);
155
* Return the size of matrix as a pair. The first value is the row size,
156
* while the second value is the column size.
158
* @return matrix size as a value pair.
160
size_pair_type size() const;
163
* Transpose the stored matrix data.
165
* @return reference to this matrix instance.
167
mixed_type_matrix& transpose();
170
* Assign values from the passed matrix instance. If the size of the
171
* passed matrix is smaller, then the element values are assigned by their
172
* positions, while the rest of the elements that fall outside the size of
173
* the passed matrix instance will remain unmodified. If the size of the
174
* pass matrix instance is larger, then only the elements within the size
175
* of this matrix instance will get assigned.
177
* @param r passed matrix object to assign element values from.
179
void assign(const mixed_type_matrix& r);
182
* Resize the matrix to specified size. This method supports resizing to
183
* zero-sized matrix; however, either specifying the row or column size to
184
* zero will resize the matrix to 0 x 0.
186
* @param row new row size
187
* @param col new column size
189
void resize(size_t row, size_t col);
197
* Check whether or not this matrix is numeric. A numeric matrix contains
198
* only numeric or boolean elements.
200
* @return true if the matrix contains only numeric or boolean elements,
201
* or false otherwise.
203
bool numeric() const;
206
* Check whether or not this matrix is empty.
208
* @return true if this matrix is empty, or false otherwise.
213
* Swap the content of the matrix with another instance.
215
void swap(mixed_type_matrix& r);
219
void dump_flags() const;
223
storage_base* mp_storage;
228
#include "mixed_type_matrix_def.inl"