mdds
multi_type_matrix.hpp
1 /*************************************************************************
2  *
3  * Copyright (c) 2012-2018 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  size_pair_type(std::initializer_list<size_type> vs)
106  {
107  if (vs.size() != 2)
108  throw invalid_arg_error("size_pair_type requires exactly 2 elements.");
109 
110  size_type* ptrs[2] = { &row, &column };
111  size_type** p = ptrs;
112 
113  for (size_type v : vs)
114  **p++ = v;
115  }
116 
117  bool operator== (const size_pair_type& r) const { return row == r.row && column == r.column; }
118  bool operator!= (const size_pair_type& r) const { return !operator== (r); }
119  };
120 
122  {
123  friend class multi_type_matrix;
124 
125  mtm::element_t type;
126  size_type offset;
127  size_type size;
128  const element_block_type* data;
129 
132 
133  template<typename _Blk>
134  typename _Blk::const_iterator begin() const;
135 
136  template<typename _Blk>
137  typename _Blk::const_iterator end() const;
138 
139  private:
140  void assign(const const_position_type& pos, size_type section_size);
141  };
142 
143  static mtm::element_t to_mtm_type(mdds::mtv::element_t mtv_type)
144  {
145  switch (mtv_type)
146  {
147  case string_block_type::block_type:
148  return mdds::mtm::element_string;
149  case integer_block_type::block_type:
150  return mdds::mtm::element_integer;
151  case mdds::mtv::element_type_numeric:
152  case mdds::mtv::element_type_boolean:
153  case mdds::mtv::element_type_empty:
154  // These types share the same numeric values.
155  return static_cast<mtm::element_t>(mtv_type);
156  default:
157  throw type_error("multi_type_matrix: unknown element type.");
158  }
159  }
160 
161 private:
162  template<typename _Func>
163  struct walk_func
164  {
165  _Func& m_func;
166  walk_func(_Func& func) : m_func(func) {}
167 
168  void operator() (const typename store_type::const_iterator::value_type& mtv_node)
169  {
170  element_block_node_type mtm_node;
171  mtm_node.type = to_mtm_type(mtv_node.type);
172  mtm_node.size = mtv_node.size;
173  mtm_node.data = mtv_node.data;
174  m_func(mtm_node);
175  }
176  };
177 
178 public:
188  static position_type next_position(const position_type& pos);
189 
199  static const_position_type next_position(const const_position_type& pos);
200 
205 
212  multi_type_matrix(size_type rows, size_type cols);
213 
222  template<typename _T>
223  multi_type_matrix(size_type rows, size_type cols, const _T& value);
224 
235  template<typename _T>
236  multi_type_matrix(size_type rows, size_type cols, const _T& it_begin, const _T& it_end);
237 
242 
247 
248  bool operator== (const multi_type_matrix& other) const;
249  bool operator!= (const multi_type_matrix& other) const;
250 
251  multi_type_matrix& operator= (const multi_type_matrix& r);
252 
264  position_type position(size_type row, size_type col);
265 
279  position_type position(const position_type& pos_hint, size_type row, size_type col);
280 
291  const_position_type position(size_type row, size_type col) const;
292 
305  const_position_type position(const const_position_type& pos_hint, size_type row, size_type col) const;
306 
315  size_pair_type matrix_position(const const_position_type& pos) const;
316 
324  position_type end_position();
325 
333  const_position_type end_position() const;
334 
343  mtm::element_t get_type(const const_position_type& pos) const;
344 
351  mtm::element_t get_type(size_type row, size_type col) const;
352 
364  double get_numeric(size_type row, size_type col) const;
365 
376  double get_numeric(const const_position_type& pos) const;
377 
389  integer_type get_integer(size_type row, size_type col) const;
390 
401  integer_type get_integer(const const_position_type& pos) const;
402 
414  bool get_boolean(size_type row, size_type col) const;
415 
426  bool get_boolean(const const_position_type& pos) const;
427 
437  const string_type& get_string(size_type row, size_type col) const;
438 
447  const string_type& get_string(const const_position_type& pos) const;
448 
459  template<typename _T>
460  _T get(size_type row, size_type col) const;
461 
468  void set_empty(size_type row, size_type col);
469 
477  void set_empty(size_type row, size_type col, size_type length);
478 
486  position_type set_empty(const position_type& pos);
487 
493  void set_column_empty(size_type col);
494 
500  void set_row_empty(size_type row);
501 
509  void set(size_type row, size_type col, double val);
510 
519  position_type set(const position_type& pos, double val);
520 
528  void set(size_type row, size_type col, bool val);
529 
538  position_type set(const position_type& pos, bool val);
539 
547  void set(size_type row, size_type col, const string_type& str);
548 
557  position_type set(const position_type& pos, const string_type& str);
558 
566  void set(size_type row, size_type col, integer_type val);
567 
576  position_type set(const position_type& pos, integer_type val);
577 
594  template<typename _T>
595  void set(size_type row, size_type col, const _T& it_begin, const _T& it_end);
596 
611  template<typename _T>
612  position_type set(const position_type& pos, const _T& it_begin, const _T& it_end);
613 
625  template<typename _T>
626  void set_column(size_type col, const _T& it_begin, const _T& it_end);
627 
634  size_pair_type size() const;
635 
641  multi_type_matrix& transpose();
642 
653  void copy(const multi_type_matrix& src);
654 
666  template<typename _T>
667  void copy(size_type rows, size_type cols, const _T& it_begin, const _T& it_end);
668 
679  void resize(size_type rows, size_type cols);
680 
690  template<typename _T>
691  void resize(size_type rows, size_type cols, const _T& value);
692 
696  void clear();
697 
705  bool numeric() const;
706 
712  bool empty() const;
713 
717  void swap(multi_type_matrix& r);
718 
727  template<typename _Func>
728  _Func walk(_Func func) const;
729 
746  template<typename _Func>
747  _Func walk(_Func func, const size_pair_type& start, const size_pair_type& end) const;
748 
759  template<typename _Func>
760  _Func walk(_Func func, const multi_type_matrix& right) const;
761 
780  template<typename _Func>
781  _Func walk(_Func func, const multi_type_matrix& right,
782  const size_pair_type& start, const size_pair_type& end) const;
783 
784 
785 #ifdef MDDS_MULTI_TYPE_MATRIX_DEBUG
786  void dump() const
787  {
788  m_store.dump_blocks(std::cout);
789  }
790 #endif
791 
792 private:
793 
804  inline size_type get_pos(size_type row, size_type col) const
805  {
806  return m_size.row * col + row;
807  }
808 
809  inline size_type get_pos(const const_position_type& pos) const
810  {
811  return pos.first->position + pos.second;
812  }
813 
814 private:
815  store_type m_store;
816  size_pair_type m_size;
817 };
818 
819 }
820 
821 #include "multi_type_matrix_def.inl"
822 
823 #endif
Definition: multi_type_vector_itr.hpp:44
Definition: multi_type_vector_trait.hpp:700
Definition: multi_type_vector_types.hpp:87
Definition: global.hpp:84
Definition: global.hpp:72
Definition: multi_type_matrix.hpp:99
Definition: multi_type_vector_types.hpp:513
Definition: multi_type_matrix.hpp:76
Definition: flat_segment_tree.hpp:46
Definition: multi_type_matrix.hpp:59
Definition: multi_type_matrix.hpp:121