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 }
68 
78 template<typename _MtxTrait>
80 {
81  typedef _MtxTrait matrix_trait;
82 public:
83  typedef typename matrix_trait::string_element_block string_block_type;
84  typedef typename matrix_trait::integer_element_block integer_block_type;
85 
86  typedef typename string_block_type::value_type string_type;
87  typedef typename integer_block_type::value_type integer_type;
88  typedef size_t size_type;
89 
90 private:
92 
93 public:
94  typedef typename store_type::position_type position_type;
95  typedef typename store_type::const_position_type const_position_type;
96 
98 
101 
103  {
104  size_type row;
105  size_type column;
106  size_pair_type() : row(0), column(0) {}
107  size_pair_type(size_type _row, size_type _column) : row(_row), column(_column) {}
108  size_pair_type(std::initializer_list<size_type> vs)
109  {
110  if (vs.size() != 2)
111  throw invalid_arg_error("size_pair_type requires exactly 2 elements.");
112 
113  size_type* ptrs[2] = { &row, &column };
114  size_type** p = ptrs;
115 
116  for (size_type v : vs)
117  **p++ = v;
118  }
119 
120  bool operator== (const size_pair_type& r) const { return row == r.row && column == r.column; }
121  bool operator!= (const size_pair_type& r) const { return !operator== (r); }
122  };
123 
125  {
126  friend class multi_type_matrix;
127 
128  mtm::element_t type;
129  size_type offset;
130  size_type size;
131  const element_block_type* data;
132 
135 
136  template<typename _Blk>
137  typename _Blk::const_iterator begin() const;
138 
139  template<typename _Blk>
140  typename _Blk::const_iterator end() const;
141 
142  private:
143  void assign(const const_position_type& pos, size_type section_size);
144  };
145 
146  static mtm::element_t to_mtm_type(mdds::mtv::element_t mtv_type)
147  {
148  switch (mtv_type)
149  {
150  case string_block_type::block_type:
151  return mdds::mtm::element_string;
152  case integer_block_type::block_type:
153  return mdds::mtm::element_integer;
154  case mdds::mtv::element_type_double:
155  case mdds::mtv::element_type_boolean:
156  case mdds::mtv::element_type_empty:
157  // These types share the same numeric values.
158  return static_cast<mtm::element_t>(mtv_type);
159  default:
160  throw type_error("multi_type_matrix: unknown element type.");
161  }
162  }
163 
164 private:
165  template<typename _Func>
166  struct walk_func
167  {
168  _Func& m_func;
169  walk_func(_Func& func) : m_func(func) {}
170 
171  void operator() (const typename store_type::const_iterator::value_type& mtv_node)
172  {
173  element_block_node_type mtm_node;
174  mtm_node.type = to_mtm_type(mtv_node.type);
175  mtm_node.size = mtv_node.size;
176  mtm_node.data = mtv_node.data;
177  m_func(mtm_node);
178  }
179  };
180 
181 public:
191  static position_type next_position(const position_type& pos);
192 
202  static const_position_type next_position(const const_position_type& pos);
203 
208 
215  multi_type_matrix(size_type rows, size_type cols);
216 
225  template<typename _T>
226  multi_type_matrix(size_type rows, size_type cols, const _T& value);
227 
240  template<typename _T>
241  multi_type_matrix(size_type rows, size_type cols, const _T& it_begin, const _T& it_end);
242 
247 
252 
253  bool operator== (const multi_type_matrix& other) const;
254  bool operator!= (const multi_type_matrix& other) const;
255 
256  multi_type_matrix& operator= (const multi_type_matrix& r);
257 
269  position_type position(size_type row, size_type col);
270 
284  position_type position(const position_type& pos_hint, size_type row, size_type col);
285 
296  const_position_type position(size_type row, size_type col) const;
297 
310  const_position_type position(const const_position_type& pos_hint, size_type row, size_type col) const;
311 
320  size_pair_type matrix_position(const const_position_type& pos) const;
321 
329  position_type end_position();
330 
338  const_position_type end_position() const;
339 
348  mtm::element_t get_type(const const_position_type& pos) const;
349 
356  mtm::element_t get_type(size_type row, size_type col) const;
357 
369  double get_numeric(size_type row, size_type col) const;
370 
381  double get_numeric(const const_position_type& pos) const;
382 
394  integer_type get_integer(size_type row, size_type col) const;
395 
406  integer_type get_integer(const const_position_type& pos) const;
407 
419  bool get_boolean(size_type row, size_type col) const;
420 
431  bool get_boolean(const const_position_type& pos) const;
432 
442  const string_type& get_string(size_type row, size_type col) const;
443 
452  const string_type& get_string(const const_position_type& pos) const;
453 
464  template<typename _T>
465  _T get(size_type row, size_type col) const;
466 
473  void set_empty(size_type row, size_type col);
474 
486  void set_empty(size_type row, size_type col, size_type length);
487 
495  position_type set_empty(const position_type& pos);
496 
502  void set_column_empty(size_type col);
503 
509  void set_row_empty(size_type row);
510 
518  void set(size_type row, size_type col, double val);
519 
528  position_type set(const position_type& pos, double val);
529 
537  void set(size_type row, size_type col, bool val);
538 
547  position_type set(const position_type& pos, bool val);
548 
556  void set(size_type row, size_type col, const string_type& str);
557 
566  position_type set(const position_type& pos, const string_type& str);
567 
575  void set(size_type row, size_type col, integer_type val);
576 
585  position_type set(const position_type& pos, integer_type val);
586 
603  template<typename _T>
604  void set(size_type row, size_type col, const _T& it_begin, const _T& it_end);
605 
620  template<typename _T>
621  position_type set(const position_type& pos, const _T& it_begin, const _T& it_end);
622 
634  template<typename _T>
635  void set_column(size_type col, const _T& it_begin, const _T& it_end);
636 
644 
651 
662  void copy(const multi_type_matrix& src);
663 
675  template<typename _T>
676  void copy(size_type rows, size_type cols, const _T& it_begin, const _T& it_end);
677 
688  void resize(size_type rows, size_type cols);
689 
699  template<typename _T>
700  void resize(size_type rows, size_type cols, const _T& value);
701 
705  void clear();
706 
714  bool numeric() const;
715 
721  bool empty() const;
722 
727 
736  template<typename _Func>
737  _Func walk(_Func func) const;
738 
755  template<typename _Func>
756  _Func walk(_Func func, const size_pair_type& start, const size_pair_type& end) const;
757 
768  template<typename _Func>
769  _Func walk(_Func func, const multi_type_matrix& right) const;
770 
789  template<typename _Func>
790  _Func walk(_Func func, const multi_type_matrix& right,
791  const size_pair_type& start, const size_pair_type& end) const;
792 
793 
794 #ifdef MDDS_MULTI_TYPE_MATRIX_DEBUG
795  void dump() const
796  {
797  m_store.dump_blocks(std::cout);
798  }
799 #endif
800 
801 private:
802 
813  inline size_type get_pos(size_type row, size_type col) const
814  {
815  return m_size.row * col + row;
816  }
817 
818  inline size_type get_pos(const const_position_type& pos) const
819  {
820  return pos.first->position + pos.second;
821  }
822 
823 private:
824  store_type m_store;
825  size_pair_type m_size;
826 };
827 
828 }
829 
830 #include "multi_type_matrix_def.inl"
831 
832 #endif
Definition: global.hpp:96
Definition: types.hpp:113
Definition: soa/main.hpp:69
Definition: multi_type_matrix.hpp:80
integer_type get_integer(const const_position_type &pos) const
void set(size_type row, size_type col, const string_type &str)
void set_row_empty(size_type row)
multi_type_matrix & transpose()
void swap(multi_type_matrix &r)
multi_type_matrix(size_type rows, size_type cols, const _T &value)
position_type set(const position_type &pos, integer_type val)
void set_empty(size_type row, size_type col)
position_type set(const position_type &pos, bool val)
void copy(const multi_type_matrix &src)
void set_empty(size_type row, size_type col, size_type length)
bool get_boolean(const const_position_type &pos) const
const string_type & get_string(const const_position_type &pos) const
void resize(size_type rows, size_type cols)
_Func walk(_Func func) const
position_type position(size_type row, size_type col)
void resize(size_type rows, size_type cols, const _T &value)
void set(size_type row, size_type col, bool val)
mtm::element_t get_type(const const_position_type &pos) const
void set(size_type row, size_type col, const _T &it_begin, const _T &it_end)
static const_position_type next_position(const const_position_type &pos)
bool get_boolean(size_type row, size_type col) const
position_type set(const position_type &pos, const string_type &str)
double get_numeric(size_type row, size_type col) const
mtm::element_t get_type(size_type row, size_type col) const
void copy(size_type rows, size_type cols, const _T &it_begin, const _T &it_end)
_T get(size_type row, size_type col) const
size_pair_type size() const
double get_numeric(const const_position_type &pos) const
position_type set(const position_type &pos, const _T &it_begin, const _T &it_end)
_Func walk(_Func func, const size_pair_type &start, const size_pair_type &end) const
void set(size_type row, size_type col, integer_type val)
multi_type_matrix(const multi_type_matrix &r)
void set_column(size_type col, const _T &it_begin, const _T &it_end)
void set(size_type row, size_type col, double val)
multi_type_matrix(size_type rows, size_type cols, const _T &it_begin, const _T &it_end)
size_pair_type matrix_position(const const_position_type &pos) const
const_position_type end_position() const
position_type position(const position_type &pos_hint, size_type row, size_type col)
position_type set_empty(const position_type &pos)
const string_type & get_string(size_type row, size_type col) const
void set_column_empty(size_type col)
static position_type next_position(const position_type &pos)
const_position_type position(const const_position_type &pos_hint, size_type row, size_type col) const
integer_type get_integer(size_type row, size_type col) const
_Func walk(_Func func, const multi_type_matrix &right, const size_pair_type &start, const size_pair_type &end) const
position_type end_position()
const_position_type position(size_type row, size_type col) const
multi_type_matrix(size_type rows, size_type cols)
position_type set(const position_type &pos, double val)
_Func walk(_Func func, const multi_type_matrix &right) const
Definition: global.hpp:108
Definition: multi_type_matrix.hpp:60
Definition: types.hpp:539
Definition: trait.hpp:747
Definition: multi_type_matrix.hpp:125
Definition: multi_type_matrix.hpp:103