mdds
multi_type_matrix.hpp
1 /*************************************************************************
2  *
3  * Copyright (c) 2012-2021 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_double,
53  element_integer = mdds::mtv::element_type_int32
54 };
55 
60 {
63 
65 };
66 
67 } // namespace mtm
68 
78 template<typename _MtxTrait>
80 {
81  typedef _MtxTrait matrix_trait;
82 
83 public:
84  typedef typename matrix_trait::string_element_block string_block_type;
85  typedef typename matrix_trait::integer_element_block integer_block_type;
86 
87  typedef typename string_block_type::value_type string_type;
88  typedef typename integer_block_type::value_type integer_type;
89  typedef size_t size_type;
90 
91 private:
93 
94 public:
95  typedef typename store_type::position_type position_type;
96  typedef typename store_type::const_position_type const_position_type;
97 
99 
102 
104  {
105  size_type row;
106  size_type column;
107  size_pair_type() : row(0), column(0)
108  {}
109  size_pair_type(size_type _row, size_type _column) : row(_row), column(_column)
110  {}
111  size_pair_type(std::initializer_list<size_type> vs)
112  {
113  if (vs.size() != 2)
114  throw invalid_arg_error("size_pair_type requires exactly 2 elements.");
115 
116  size_type* ptrs[2] = {&row, &column};
117  size_type** p = ptrs;
118 
119  for (size_type v : vs)
120  **p++ = v;
121  }
122 
123  bool operator==(const size_pair_type& r) const
124  {
125  return row == r.row && column == r.column;
126  }
127  bool operator!=(const size_pair_type& r) const
128  {
129  return !operator==(r);
130  }
131  };
132 
134  {
135  friend class multi_type_matrix;
136 
137  mtm::element_t type;
138  size_type offset;
139  size_type size;
140  const element_block_type* data;
141 
144 
145  template<typename _Blk>
146  typename _Blk::const_iterator begin() const;
147 
148  template<typename _Blk>
149  typename _Blk::const_iterator end() const;
150 
151  private:
152  void assign(const const_position_type& pos, size_type section_size);
153  };
154 
155  static mtm::element_t to_mtm_type(mdds::mtv::element_t mtv_type)
156  {
157  switch (mtv_type)
158  {
159  case string_block_type::block_type:
160  return mdds::mtm::element_string;
161  case integer_block_type::block_type:
162  return mdds::mtm::element_integer;
163  case mdds::mtv::element_type_double:
164  case mdds::mtv::element_type_boolean:
165  case mdds::mtv::element_type_empty:
166  // These types share the same numeric values.
167  return static_cast<mtm::element_t>(mtv_type);
168  default:
169  throw type_error("multi_type_matrix: unknown element type.");
170  }
171  }
172 
173 private:
174  template<typename _Func>
175  struct walk_func
176  {
177  _Func& m_func;
178  walk_func(_Func& func) : m_func(func)
179  {}
180 
181  void operator()(const typename store_type::const_iterator::value_type& mtv_node)
182  {
183  element_block_node_type mtm_node;
184  mtm_node.type = to_mtm_type(mtv_node.type);
185  mtm_node.size = mtv_node.size;
186  mtm_node.data = mtv_node.data;
187  m_func(mtm_node);
188  }
189  };
190 
191 public:
201  static position_type next_position(const position_type& pos);
202 
212  static const_position_type next_position(const const_position_type& pos);
213 
218 
225  multi_type_matrix(size_type rows, size_type cols);
226 
235  template<typename _T>
236  multi_type_matrix(size_type rows, size_type cols, const _T& value);
237 
250  template<typename _T>
251  multi_type_matrix(size_type rows, size_type cols, const _T& it_begin, const _T& it_end);
252 
257 
262 
263  bool operator==(const multi_type_matrix& other) const;
264  bool operator!=(const multi_type_matrix& other) const;
265 
266  multi_type_matrix& operator=(const multi_type_matrix& r);
267 
279  position_type position(size_type row, size_type col);
280 
294  position_type position(const position_type& pos_hint, size_type row, size_type col);
295 
306  const_position_type position(size_type row, size_type col) const;
307 
320  const_position_type position(const const_position_type& pos_hint, size_type row, size_type col) const;
321 
330  size_pair_type matrix_position(const const_position_type& pos) const;
331 
339  position_type end_position();
340 
348  const_position_type end_position() const;
349 
358  mtm::element_t get_type(const const_position_type& pos) const;
359 
366  mtm::element_t get_type(size_type row, size_type col) const;
367 
379  double get_numeric(size_type row, size_type col) const;
380 
391  double get_numeric(const const_position_type& pos) const;
392 
404  integer_type get_integer(size_type row, size_type col) const;
405 
416  integer_type get_integer(const const_position_type& pos) const;
417 
429  bool get_boolean(size_type row, size_type col) const;
430 
441  bool get_boolean(const const_position_type& pos) const;
442 
452  const string_type& get_string(size_type row, size_type col) const;
453 
462  const string_type& get_string(const const_position_type& pos) const;
463 
474  template<typename _T>
475  _T get(size_type row, size_type col) const;
476 
483  void set_empty(size_type row, size_type col);
484 
496  void set_empty(size_type row, size_type col, size_type length);
497 
505  position_type set_empty(const position_type& pos);
506 
512  void set_column_empty(size_type col);
513 
519  void set_row_empty(size_type row);
520 
528  void set(size_type row, size_type col, double val);
529 
538  position_type set(const position_type& pos, double val);
539 
547  void set(size_type row, size_type col, bool val);
548 
557  position_type set(const position_type& pos, bool val);
558 
566  void set(size_type row, size_type col, const string_type& str);
567 
576  position_type set(const position_type& pos, const string_type& str);
577 
585  void set(size_type row, size_type col, integer_type val);
586 
595  position_type set(const position_type& pos, integer_type val);
596 
613  template<typename _T>
614  void set(size_type row, size_type col, const _T& it_begin, const _T& it_end);
615 
630  template<typename _T>
631  position_type set(const position_type& pos, const _T& it_begin, const _T& it_end);
632 
644  template<typename _T>
645  void set_column(size_type col, const _T& it_begin, const _T& it_end);
646 
653  size_pair_type size() const;
654 
661 
672  void copy(const multi_type_matrix& src);
673 
685  template<typename _T>
686  void copy(size_type rows, size_type cols, const _T& it_begin, const _T& it_end);
687 
698  void resize(size_type rows, size_type cols);
699 
709  template<typename _T>
710  void resize(size_type rows, size_type cols, const _T& value);
711 
715  void clear();
716 
724  bool numeric() const;
725 
731  bool empty() const;
732 
736  void swap(multi_type_matrix& r);
737 
746  template<typename _Func>
747  _Func walk(_Func func) const;
748 
765  template<typename _Func>
766  _Func walk(_Func func, const size_pair_type& start, const size_pair_type& end) const;
767 
778  template<typename _Func>
779  _Func walk(_Func func, const multi_type_matrix& right) const;
780 
799  template<typename _Func>
800  _Func walk(
801  _Func func, const multi_type_matrix& right, const size_pair_type& start, const size_pair_type& end) const;
802 
803 #ifdef MDDS_MULTI_TYPE_MATRIX_DEBUG
804  void dump() const
805  {
806  m_store.dump_blocks(std::cout);
807  }
808 #endif
809 
810 private:
821  inline size_type get_pos(size_type row, size_type col) const
822  {
823  return m_size.row * col + row;
824  }
825 
826  inline size_type get_pos(const const_position_type& pos) const
827  {
828  return pos.first->position + pos.second;
829  }
830 
831 private:
832  store_type m_store;
833  size_pair_type m_size;
834 };
835 
836 } // namespace mdds
837 
838 #include "multi_type_matrix_def.inl"
839 
840 #endif
void set_column(size_type col, const _T &it_begin, const _T &it_end)
Definition: trait.hpp:745
bool get_boolean(size_type row, size_type col) const
static position_type next_position(const position_type &pos)
const string_type & get_string(size_type row, size_type col) const
Definition: global.hpp:114
void swap(multi_type_matrix &r)
size_pair_type size() const
position_type end_position()
void set_empty(size_type row, size_type col)
Definition: global.hpp:100
_Func walk(_Func func) const
Definition: multi_type_matrix.hpp:103
multi_type_matrix & transpose()
Definition: types.hpp:615
Definition: multi_type_matrix.hpp:79
void copy(const multi_type_matrix &src)
void set_column_empty(size_type col)
void resize(size_type rows, size_type cols)
Definition: flat_segment_tree.hpp:46
mtm::element_t get_type(const const_position_type &pos) const
void set(size_type row, size_type col, double val)
void set_row_empty(size_type row)
position_type position(size_type row, size_type col)
Definition: types.hpp:173
double get_numeric(size_type row, size_type col) const
Definition: multi_type_matrix.hpp:59
integer_type get_integer(size_type row, size_type col) const
size_pair_type matrix_position(const const_position_type &pos) const
Definition: main.hpp:72
Definition: multi_type_matrix.hpp:133