~ubuntu-branches/ubuntu/raring/mdds/raring

« back to all changes in this revision

Viewing changes to include/mdds/mixed_type_matrix.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Rene Engelhard
  • Date: 2010-12-21 03:00:49 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20101221030049-15awcps1vyzo90s0
Tags: 0.4.0-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*************************************************************************
 
2
 *
 
3
 * Copyright (c) 2010 Kohei Yoshida
 
4
 * 
 
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
 
12
 * conditions:
 
13
 * 
 
14
 * The above copyright notice and this permission notice shall be
 
15
 * included in all copies or substantial portions of the Software.
 
16
 * 
 
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.
 
25
 *
 
26
 ************************************************************************/
 
27
 
 
28
#ifndef __MDDS_QUAD_TYPE_MATRIX_HPP__
 
29
#define __MDDS_QUAD_TYPE_MATRIX_HPP__
 
30
 
 
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"
 
35
 
 
36
#include <iostream>
 
37
#include <cstdlib>
 
38
 
 
39
namespace mdds {
 
40
 
 
41
enum matrix_density_t
 
42
{
 
43
    matrix_density_filled_zero,
 
44
    matrix_density_filled_empty,
 
45
    matrix_density_sparse_zero,
 
46
    matrix_density_sparse_empty
 
47
};
 
48
 
 
49
class matrix_error : public ::mdds::general_error
 
50
{
 
51
public:
 
52
    matrix_error(const ::std::string& msg) : general_error(msg) {}
 
53
};
 
54
 
 
55
/**
 
56
 * This data structure represents a matrix where each individual element may
 
57
 * be of one of four types: value, boolean, string, or empty.
 
58
 */
 
59
template<typename _String, typename _Flag>
 
60
class mixed_type_matrix
 
61
{
 
62
public:
 
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;
 
68
 
 
69
private:
 
70
    struct size_pair_type_hash
 
71
    {
 
72
        size_t operator() (const size_pair_type& val) const
 
73
        {
 
74
            size_t n = val.first + (val.second << 8);
 
75
            return n;
 
76
        }
 
77
    };
 
78
    typedef ::mdds::storage_base<mixed_type_matrix> storage_base;
 
79
 
 
80
    static storage_base* create_storage(size_t rows, size_t cols, matrix_density_t density);
 
81
 
 
82
public:
 
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;
 
86
 
 
87
    typedef typename storage_base::const_iterator const_iterator;
 
88
 
 
89
    /**
 
90
     * Default constructor.
 
91
     */
 
92
    mixed_type_matrix();
 
93
 
 
94
    /**
 
95
     * Construct an empty matrix with specified density type.
 
96
     */
 
97
    mixed_type_matrix(matrix_density_t density);
 
98
 
 
99
    /**
 
100
     * Construct a matrix of specified size with specified density type. 
 
101
     */
 
102
    mixed_type_matrix(size_t rows, size_t cols, matrix_density_t density);
 
103
 
 
104
    mixed_type_matrix(const mixed_type_matrix& r);
 
105
    ~mixed_type_matrix();
 
106
 
 
107
    const_iterator begin() const;
 
108
    const_iterator end() const;
 
109
 
 
110
    mixed_type_matrix& operator= (const mixed_type_matrix& r);
 
111
 
 
112
    /**
 
113
     * Get the type of element specified by its position.  The type can be one 
 
114
     * of empty, string, numeric, or boolean. 
 
115
     * 
 
116
     * @return element type.
 
117
     */
 
118
    matrix_element_t get_type(size_t row, size_t col) const;
 
119
 
 
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;
 
123
 
 
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);
 
128
 
 
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);
 
132
 
 
133
    /**
 
134
     * Set flag value at specified position.
 
135
     * 
 
136
     * @param row row position
 
137
     * @param col column position
 
138
     * @param flag_type flag value
 
139
     */
 
140
    void set_flag(size_t row, size_t col, flag_type flag);
 
141
 
 
142
    /**
 
143
     * Get flag value at specified position. 
 
144
     *  
 
145
     * @param row row position 
 
146
     * @param col column position 
 
147
     * 
 
148
     * @return flag value stored at specified position
 
149
     */
 
150
    flag_type get_flag(size_t row, size_t col) const;
 
151
 
 
152
    void clear_flag(size_t row, size_t cols);
 
153
 
 
154
    /**
 
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.
 
157
     * 
 
158
     * @return matrix size as a value pair.
 
159
     */
 
160
    size_pair_type size() const;
 
161
    
 
162
    /** 
 
163
     * Transpose the stored matrix data. 
 
164
     *  
 
165
     * @return reference to this matrix instance. 
 
166
     */
 
167
    mixed_type_matrix& transpose();
 
168
 
 
169
    /**
 
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.
 
176
     * 
 
177
     * @param r passed matrix object to assign element values from.
 
178
     */
 
179
    void assign(const mixed_type_matrix& r);
 
180
 
 
181
    /**
 
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.
 
185
     * 
 
186
     * @param row new row size
 
187
     * @param col new column size
 
188
     */
 
189
    void resize(size_t row, size_t col);
 
190
 
 
191
    /**
 
192
     * Empty the matrix.
 
193
     */
 
194
    void clear();
 
195
 
 
196
    /**
 
197
     * Check whether or not this matrix is numeric.  A numeric matrix contains 
 
198
     * only numeric or boolean elements. 
 
199
     * 
 
200
     * @return true if the matrix contains only numeric or boolean elements, 
 
201
     *         or false otherwise.
 
202
     */
 
203
    bool numeric() const;
 
204
 
 
205
    /**
 
206
     * Check whether or not this matrix is empty. 
 
207
     * 
 
208
     * @return true if this matrix is empty, or false otherwise.
 
209
     */
 
210
    bool empty() const;
 
211
 
 
212
    /**
 
213
     * Swap the content of the matrix with another instance.
 
214
     */
 
215
    void swap(mixed_type_matrix& r);
 
216
 
 
217
#ifdef UNIT_TEST
 
218
    void dump() const;
 
219
    void dump_flags() const;
 
220
#endif
 
221
 
 
222
private:
 
223
    storage_base* mp_storage;
 
224
};
 
225
 
 
226
}
 
227
 
 
228
#include "mixed_type_matrix_def.inl"
 
229
 
 
230
#endif