mdds
multi_type_matrix.hpp
1 /*************************************************************************
2  *
3  * Copyright (c) 2012 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_MULTI_TYPE_MATRIX_HPP__
29 #define __MDDS_MULTI_TYPE_MATRIX_HPP__
30 
31 #ifdef MDDS_MULTI_TYPE_MATRIX_DEBUG
32 #ifndef MDDS_MULTI_TYPE_VECTOR_DEBUG
33 #define MDDS_MULTI_TYPE_VECTOR_DEBUG 1
34 #endif
35 #endif
36 
37 #include "multi_type_vector.hpp"
38 #include "multi_type_vector_trait.hpp"
39 
40 namespace mdds {
41 
42 namespace mtm {
43 
47 enum element_t
48 {
49  element_empty = mdds::mtv::element_type_empty,
50  element_boolean = mdds::mtv::element_type_boolean,
51  element_string = mdds::mtv::element_type_string,
52  element_numeric = mdds::mtv::element_type_numeric
53 };
54 
56 {
57  typedef std::string string_type;
60 
61  static const mdds::mtv::element_t string_type_identifier = mdds::mtv::element_type_string;
62 };
63 
64 }
65 
71 template<typename _StringTrait>
73 {
74  typedef _StringTrait string_trait;
75 public:
76  typedef typename string_trait::string_type string_type;
77  typedef size_t size_type;
78 
79 private:
81 
82 public:
83  typedef typename store_type::position_type position_type;
84  typedef typename store_type::const_position_type const_position_type;
85 
87 
90  typedef typename string_trait::string_element_block string_block_type;
91 
93  {
94  size_type row;
95  size_type column;
96  size_pair_type() : row(0), column(0) {}
97  size_pair_type(size_type _row, size_type _column) : row(_row), column(_column) {}
98 
99  bool operator== (const size_pair_type& r) const { return row == r.row && column == r.column; }
100  bool operator!= (const size_pair_type& r) const { return !operator== (r); }
101  };
102 
104  {
105  mtm::element_t type;
106  size_t offset;
107  size_type size;
108  const element_block_type* data;
109 
110  element_block_node_type() : type(mtm::element_empty), offset(0), size(0), data(nullptr) {}
112  type(other.type), offset(other.offset), size(other.size), data(other.data) {}
113  };
114 
115  static mtm::element_t to_mtm_type(mdds::mtv::element_t mtv_type)
116  {
117  switch (mtv_type)
118  {
119  case string_trait::string_type_identifier:
120  return mdds::mtm::element_string;
121  case mdds::mtv::element_type_numeric:
122  case mdds::mtv::element_type_boolean:
123  case mdds::mtv::element_type_empty:
124  // These types share the same numeric values.
125  return static_cast<mtm::element_t>(mtv_type);
126  default:
127  throw general_error("multi_type_matrix: unknown element type.");
128  }
129  }
130 
131  template<typename _Func>
132  struct walk_func : std::unary_function<typename store_type::const_iterator::value_type, void>
133  {
134  _Func& m_func;
135  walk_func(_Func& func) : m_func(func) {}
136 
137  void operator() (const typename store_type::const_iterator::value_type& mtv_node)
138  {
139  element_block_node_type mtm_node;
140  mtm_node.type = to_mtm_type(mtv_node.type);
141  mtm_node.size = mtv_node.size;
142  mtm_node.data = mtv_node.data;
143  m_func(mtm_node);
144  }
145  };
146 
156  static position_type next_position(const position_type& pos);
157 
167  static const_position_type next_position(const const_position_type& pos);
168 
173 
177  multi_type_matrix(size_type rows, size_type cols);
178 
179  template<typename _T>
180  multi_type_matrix(size_type rows, size_type cols, const _T& value);
181 
182  template<typename _T>
183  multi_type_matrix(size_type rows, size_type cols, const _T& it_begin, const _T& it_end);
184 
187 
188  bool operator== (const multi_type_matrix& other) const;
189  bool operator!= (const multi_type_matrix& other) const;
190 
191  multi_type_matrix& operator= (const multi_type_matrix& r);
192 
204  position_type position(size_type row, size_type col);
205 
219  position_type position(const position_type& pos_hint, size_type row, size_type col);
220 
231  const_position_type position(size_type row, size_type col) const;
232 
245  const_position_type position(const const_position_type& pos_hint, size_type row, size_type col) const;
246 
255  size_pair_type matrix_position(const const_position_type& pos);
256 
264  position_type end_position();
265 
273  const_position_type end_position() const;
274 
283  mtm::element_t get_type(const const_position_type& pos) const;
284 
291  mtm::element_t get_type(size_type row, size_type col) const;
292 
304  double get_numeric(size_type row, size_type col) const;
305 
316  double get_numeric(const const_position_type& pos) const;
317 
329  bool get_boolean(size_type row, size_type col) const;
330 
341  bool get_boolean(const const_position_type& pos) const;
342 
352  const string_type& get_string(size_type row, size_type col) const;
353 
362  const string_type& get_string(const const_position_type& pos) const;
363 
374  template<typename _T>
375  _T get(size_type row, size_type col) const;
376 
383  void set_empty(size_type row, size_type col);
384 
392  void set_empty(size_type row, size_type col, size_type length);
393 
399  position_type set_empty(const position_type& pos);
400 
401  void set_column_empty(size_type col);
402  void set_row_empty(size_type row);
403 
404  void set(size_type row, size_type col, double val);
405  position_type set(const position_type& pos, double val);
406 
407  void set(size_type row, size_type col, bool val);
408  position_type set(const position_type& pos, bool val);
409 
410  void set(size_type row, size_type col, const string_type& str);
411  position_type set(const position_type& pos, const string_type& str);
412 
429  template<typename _T>
430  void set(size_type row, size_type col, const _T& it_begin, const _T& it_end);
431 
432  template<typename _T>
433  position_type set(const position_type& pos, const _T& it_begin, const _T& it_end);
434 
446  template<typename _T>
447  void set_column(size_type col, const _T& it_begin, const _T& it_end);
448 
455  size_pair_type size() const;
456 
462  multi_type_matrix& transpose();
463 
474  void copy(const multi_type_matrix& r);
475 
486  void resize(size_type rows, size_type cols);
487 
497  template<typename _T>
498  void resize(size_type rows, size_type cols, const _T& value);
499 
503  void clear();
504 
512  bool numeric() const;
513 
519  bool empty() const;
520 
524  void swap(multi_type_matrix& r);
525 
532  template<typename _Func>
533  void walk(_Func& func) const;
534 
549  template<typename _Func>
550  void walk(_Func& func, const size_pair_type& start, const size_pair_type& end) const;
551 
552 #ifdef MDDS_MULTI_TYPE_MATRIX_DEBUG
553  void dump() const
554  {
555  m_store.dump_blocks(std::cout);
556  }
557 #endif
558 
559 private:
560 
571  inline size_type get_pos(size_type row, size_type col) const
572  {
573  return m_size.row * col + row;
574  }
575 
576  inline size_type get_pos(const const_position_type& pos) const
577  {
578  return pos.first->position + pos.second;
579  }
580 
581  void copy_store(store_type& dest, size_type rows, size_type cols) const;
582 
583 private:
584  store_type m_store;
585  size_pair_type m_size;
586 };
587 
588 }
589 
590 #include "multi_type_matrix_def.inl"
591 
592 #endif
Definition: multi_type_vector_trait.hpp:659
Definition: multi_type_vector_types.hpp:88
Definition: multi_type_matrix.hpp:132
Definition: multi_type_vector_itr.hpp:44
Definition: multi_type_matrix.hpp:92
Definition: multi_type_vector_types.hpp:480
Definition: global.hpp:58
Definition: multi_type_matrix.hpp:72
Definition: default_deleter.hpp:33
Definition: multi_type_matrix.hpp:55
Definition: multi_type_matrix.hpp:103