mdds
multi_type_matrix.hpp
1 /*************************************************************************
2  *
3  * Copyright (c) 2012-2016 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  element_integer = mdds::mtv::element_type_int
54 };
55 
60 {
63 
65 };
66 
67 }
68 
75 template<typename _MtxTrait>
77 {
78  typedef _MtxTrait matrix_trait;
79 public:
80  typedef typename matrix_trait::string_element_block string_block_type;
81  typedef typename matrix_trait::integer_element_block integer_block_type;
82 
83  typedef typename string_block_type::value_type string_type;
84  typedef typename integer_block_type::value_type integer_type;
85  typedef size_t size_type;
86 
87 private:
89 
90 public:
91  typedef typename store_type::position_type position_type;
92  typedef typename store_type::const_position_type const_position_type;
93 
95 
98 
100  {
101  size_type row;
102  size_type column;
103  size_pair_type() : row(0), column(0) {}
104  size_pair_type(size_type _row, size_type _column) : row(_row), column(_column) {}
105 
106  bool operator== (const size_pair_type& r) const { return row == r.row && column == r.column; }
107  bool operator!= (const size_pair_type& r) const { return !operator== (r); }
108  };
109 
111  {
112  friend class multi_type_matrix;
113 
114  mtm::element_t type;
115  size_type offset;
116  size_type size;
117  const element_block_type* data;
118 
121 
122  template<typename _Blk>
123  typename _Blk::const_iterator begin() const;
124 
125  template<typename _Blk>
126  typename _Blk::const_iterator end() const;
127 
128  private:
129  void assign(const const_position_type& pos, size_type section_size);
130  };
131 
132  static mtm::element_t to_mtm_type(mdds::mtv::element_t mtv_type)
133  {
134  switch (mtv_type)
135  {
136  case string_block_type::block_type:
137  return mdds::mtm::element_string;
138  case integer_block_type::block_type:
139  return mdds::mtm::element_integer;
140  case mdds::mtv::element_type_numeric:
141  case mdds::mtv::element_type_boolean:
142  case mdds::mtv::element_type_empty:
143  // These types share the same numeric values.
144  return static_cast<mtm::element_t>(mtv_type);
145  default:
146  throw type_error("multi_type_matrix: unknown element type.");
147  }
148  }
149 
150 private:
151  template<typename _Func>
152  struct walk_func : std::unary_function<typename store_type::const_iterator::value_type, void>
153  {
154  _Func& m_func;
155  walk_func(_Func& func) : m_func(func) {}
156 
157  void operator() (const typename store_type::const_iterator::value_type& mtv_node)
158  {
159  element_block_node_type mtm_node;
160  mtm_node.type = to_mtm_type(mtv_node.type);
161  mtm_node.size = mtv_node.size;
162  mtm_node.data = mtv_node.data;
163  m_func(mtm_node);
164  }
165  };
166 
167 public:
177  static position_type next_position(const position_type& pos);
178 
188  static const_position_type next_position(const const_position_type& pos);
189 
194 
201  multi_type_matrix(size_type rows, size_type cols);
202 
211  template<typename _T>
212  multi_type_matrix(size_type rows, size_type cols, const _T& value);
213 
224  template<typename _T>
225  multi_type_matrix(size_type rows, size_type cols, const _T& it_begin, const _T& it_end);
226 
231 
236 
237  bool operator== (const multi_type_matrix& other) const;
238  bool operator!= (const multi_type_matrix& other) const;
239 
240  multi_type_matrix& operator= (const multi_type_matrix& r);
241 
253  position_type position(size_type row, size_type col);
254 
268  position_type position(const position_type& pos_hint, size_type row, size_type col);
269 
280  const_position_type position(size_type row, size_type col) const;
281 
294  const_position_type position(const const_position_type& pos_hint, size_type row, size_type col) const;
295 
304  size_pair_type matrix_position(const const_position_type& pos) const;
305 
313  position_type end_position();
314 
322  const_position_type end_position() const;
323 
332  mtm::element_t get_type(const const_position_type& pos) const;
333 
340  mtm::element_t get_type(size_type row, size_type col) const;
341 
353  double get_numeric(size_type row, size_type col) const;
354 
365  double get_numeric(const const_position_type& pos) const;
366 
378  integer_type get_integer(size_type row, size_type col) const;
379 
390  integer_type get_integer(const const_position_type& pos) const;
391 
403  bool get_boolean(size_type row, size_type col) const;
404 
415  bool get_boolean(const const_position_type& pos) const;
416 
426  const string_type& get_string(size_type row, size_type col) const;
427 
436  const string_type& get_string(const const_position_type& pos) const;
437 
448  template<typename _T>
449  _T get(size_type row, size_type col) const;
450 
457  void set_empty(size_type row, size_type col);
458 
466  void set_empty(size_type row, size_type col, size_type length);
467 
475  position_type set_empty(const position_type& pos);
476 
482  void set_column_empty(size_type col);
483 
489  void set_row_empty(size_type row);
490 
498  void set(size_type row, size_type col, double val);
499 
508  position_type set(const position_type& pos, double val);
509 
517  void set(size_type row, size_type col, bool val);
518 
527  position_type set(const position_type& pos, bool val);
528 
536  void set(size_type row, size_type col, const string_type& str);
537 
546  position_type set(const position_type& pos, const string_type& str);
547 
555  void set(size_type row, size_type col, integer_type val);
556 
565  position_type set(const position_type& pos, integer_type val);
566 
583  template<typename _T>
584  void set(size_type row, size_type col, const _T& it_begin, const _T& it_end);
585 
600  template<typename _T>
601  position_type set(const position_type& pos, const _T& it_begin, const _T& it_end);
602 
614  template<typename _T>
615  void set_column(size_type col, const _T& it_begin, const _T& it_end);
616 
623  size_pair_type size() const;
624 
630  multi_type_matrix& transpose();
631 
642  void copy(const multi_type_matrix& src);
643 
655  template<typename _T>
656  void copy(size_type rows, size_type cols, const _T& it_begin, const _T& it_end);
657 
668  void resize(size_type rows, size_type cols);
669 
679  template<typename _T>
680  void resize(size_type rows, size_type cols, const _T& value);
681 
685  void clear();
686 
694  bool numeric() const;
695 
701  bool empty() const;
702 
706  void swap(multi_type_matrix& r);
707 
714  template<typename _Func>
715  void walk(_Func& func) const;
716 
731  template<typename _Func>
732  void walk(_Func& func, const size_pair_type& start, const size_pair_type& end) const;
733 
744  template<typename _Func>
745  void walk(_Func& func, const multi_type_matrix& right) const;
746 
765  template<typename _Func>
766  void walk(_Func& func, const multi_type_matrix& right,
767  const size_pair_type& start, const size_pair_type& end) const;
768 
769 
770 #ifdef MDDS_MULTI_TYPE_MATRIX_DEBUG
771  void dump() const
772  {
773  m_store.dump_blocks(std::cout);
774  }
775 #endif
776 
777 private:
778 
789  inline size_type get_pos(size_type row, size_type col) const
790  {
791  return m_size.row * col + row;
792  }
793 
794  inline size_type get_pos(const const_position_type& pos) const
795  {
796  return pos.first->position + pos.second;
797  }
798 
799 private:
800  store_type m_store;
801  size_pair_type m_size;
802 };
803 
804 }
805 
806 #include "multi_type_matrix_def.inl"
807 
808 #endif
Definition: multi_type_vector_trait.hpp:692
Definition: multi_type_vector_types.hpp:88
Definition: global.hpp:84
Definition: multi_type_vector_itr.hpp:44
Definition: multi_type_matrix.hpp:99
Definition: multi_type_vector_types.hpp:490
Definition: multi_type_matrix.hpp:76
Definition: default_deleter.hpp:33
Definition: multi_type_matrix.hpp:59
Definition: multi_type_matrix.hpp:110