mdds
Loading...
Searching...
No Matches
multi_type_matrix.hpp
1// SPDX-FileCopyrightText: 2012 - 2025 Kohei Yoshida
2//
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
7#ifdef MDDS_MULTI_TYPE_MATRIX_DEBUG
8#ifndef MDDS_MULTI_TYPE_VECTOR_DEBUG
9#define MDDS_MULTI_TYPE_VECTOR_DEBUG 1
10#endif
11#endif
12
13#include "multi_type_vector.hpp"
14
15#include <array>
16#include <concepts>
17#include <tuple>
18#include <type_traits>
19
20namespace mdds {
21
22namespace mtm {
23
29using element_t = mdds::mtv::element_t;
30
34inline constexpr element_t element_empty = mdds::mtv::element_type_empty;
35
36namespace detail {
37
38template<typename T>
39concept element_block_type = std::derived_from<T, mdds::mtv::base_element_block> && requires {
40 { T::block_type } -> std::convertible_to<mdds::mtv::element_t>;
41 typename T::value_type;
42};
43
44template<typename T>
45struct is_element_block_list : std::false_type
46{
47};
48
49template<typename... Ts>
50struct is_element_block_list<std::tuple<Ts...>> : std::bool_constant<(element_block_type<Ts> && ...)>
51{
52};
53
54template<typename Traits>
56{
57 using type = std::tuple<>;
58};
59
60template<typename Traits>
61 requires requires { typename Traits::element_blocks; }
62struct element_blocks_of<Traits>
63{
64 using type = typename Traits::element_blocks;
65};
66
67template<typename Blk, typename Tuple>
69
70template<typename Blk, typename... Ts>
71struct prepend_block<Blk, std::tuple<Ts...>>
72{
73 using type = std::tuple<Blk, Ts...>;
74};
75
76template<typename Traits>
82
83template<typename Tuple>
85
86template<typename... Ts>
87struct make_block_funcs<std::tuple<Ts...>>
88{
89 using type = mdds::mtv::element_block_funcs<Ts...>;
90};
91
92template<typename Blk, typename Tuple>
94
95template<typename Blk, typename... Ts>
96struct is_block_in_list<Blk, std::tuple<Ts...>> : std::bool_constant<(std::is_same_v<Blk, Ts> || ...)>
97{
98};
99
100template<typename Tuple>
102
103template<typename... Ts>
104struct has_distinct_block_ids<std::tuple<Ts...>>
105{
106 static constexpr bool check()
107 {
108 constexpr std::array<mdds::mtv::element_t, sizeof...(Ts)> ids = {Ts::block_type...};
109
110 for (std::size_t i = 0; i < ids.size(); ++i)
111 {
112 for (std::size_t j = i + 1; j < ids.size(); ++j)
113 {
114 if (ids[i] == ids[j])
115 return false;
116 }
117 }
118
119 return true;
120 }
121
122 static constexpr bool value = check();
123};
124
125template<typename T, typename Tuple>
126struct is_listed_value : std::false_type
127{
128};
129
130template<typename T, typename... Ts>
131struct is_listed_value<T, std::tuple<Ts...>> : std::bool_constant<(std::is_same_v<T, typename Ts::value_type> || ...)>
132{
133};
134
139template<typename T, typename Blocks>
141
142template<typename T, typename Tuple>
143struct block_of;
144
145template<typename T>
146struct block_of<T, std::tuple<>>
147{
148 using type = void;
149};
150
151template<typename T, typename Blk, typename... Ts>
152struct block_of<T, std::tuple<Blk, Ts...>>
153{
154 using type = std::conditional_t<
155 std::is_same_v<T, typename Blk::value_type>, Blk, typename block_of<T, std::tuple<Ts...>>::type>;
156};
157
163template<typename T, typename Blocks>
165 !std::is_void_v<typename block_of<T, Blocks>::type> &&
166 std::is_lvalue_reference_v<typename block_of<T, Blocks>::type::store_type::const_reference>;
167
168} // namespace detail
169
191template<typename T>
193 requires { typename T::numeric_element_block; } && detail::element_block_type<typename T::numeric_element_block> &&
194 std::constructible_from<typename T::numeric_element_block::value_type, int> &&
196
203{
204 using numeric_element_block = mdds::mtv::double_element_block;
205
206 using element_blocks =
207 std::tuple<mdds::mtv::boolean_element_block, mdds::mtv::int32_element_block, mdds::mtv::string_element_block>;
208};
209
210} // namespace mtm
211
224template<mtm::matrix_traits Traits>
226{
227 using traits_type = Traits;
228
229 using element_blocks_type = typename mtm::detail::element_blocks_of<traits_type>::type;
230
231public:
232 using numeric_block_type = typename traits_type::numeric_element_block;
233 using numeric_type = typename numeric_block_type::value_type;
234 using size_type = std::size_t;
235
239 static constexpr mtm::element_t element_empty = mtm::element_empty;
240
245 static constexpr mtm::element_t element_numeric = numeric_block_type::block_type;
246
247private:
248 using block_list_type = typename mtm::detail::block_list_of<traits_type>::type;
249
250 static_assert(
252 "The numeric element block must not also appear in element_blocks.");
253
254 static_assert(
256 "All element blocks of the matrix must have distinct block type ids.");
257
258 struct mtv_traits : public mdds::mtv::default_traits
259 {
261 };
262
263 using store_type = mdds::multi_type_vector<mtv_traits>;
264
265public:
266 using position_type = typename store_type::position_type;
267 using const_position_type = typename store_type::const_position_type;
268
269 using element_block_type = typename mtv::base_element_block;
270
271 struct size_pair_type
272 {
273 size_type row;
274 size_type column;
275 size_pair_type() noexcept(std::is_fundamental_v<size_type>) : row(0), column(0)
276 {}
277 size_pair_type(size_type _row, size_type _column) noexcept(std::is_fundamental_v<size_type>)
278 : row(_row), column(_column)
279 {}
280 size_pair_type(std::initializer_list<size_type> vs)
281 {
282 if (vs.size() != 2)
283 throw invalid_arg_error("size_pair_type requires exactly 2 elements.");
284
285 size_type* ptrs[2] = {&row, &column};
286 size_type** p = ptrs;
287
288 for (size_type v : vs)
289 **p++ = v;
290 }
291
292 bool operator==(const size_pair_type& r) const noexcept(std::is_fundamental_v<size_type>)
293 {
294 return row == r.row && column == r.column;
295 }
296 };
297
298 struct element_block_node_type
299 {
300 friend class multi_type_matrix;
301
302 mtm::element_t type;
303 size_type offset;
304 size_type size;
305 const element_block_type* data;
306
307 element_block_node_type() noexcept(std::is_fundamental_v<size_type>);
308 element_block_node_type(const element_block_node_type& other) noexcept(std::is_fundamental_v<size_type>);
309
310 template<typename _Blk>
311 typename _Blk::const_iterator begin() const;
312
313 template<typename _Blk>
314 typename _Blk::const_iterator end() const;
315
316 private:
317 void assign(const const_position_type& pos, size_type section_size);
318 };
319
320private:
321 template<typename FuncT>
322 struct walk_func
323 {
324 FuncT& m_func;
325 walk_func(FuncT& func) noexcept : m_func(func)
326 {}
327
328 void operator()(const typename store_type::const_iterator::value_type& mtv_node)
329 {
330 element_block_node_type mtm_node;
331 mtm_node.type = mtv_node.type;
332 mtm_node.size = mtv_node.size;
333 mtm_node.data = mtv_node.data;
334 m_func(mtm_node);
335 }
336 };
337
344 static void validate_type(mtm::element_t type);
345
355 template<typename FuncT>
356 static void dispatch_block(mtm::element_t type, FuncT&& func);
357
362 template<typename T>
363 static store_type create_store(size_type init_size, const T& it_begin, const T& it_end);
364
365 static constexpr bool nothrow_default_constructible_v =
366 std::is_nothrow_default_constructible_v<store_type> && std::is_nothrow_default_constructible_v<size_pair_type>;
367
368 static constexpr bool nothrow_eq_comparable_v =
369 noexcept(std::declval<store_type>() == std::declval<store_type>()) &&
370 noexcept(std::declval<size_pair_type>() == std::declval<size_pair_type>());
371
372public:
382 static position_type next_position(const position_type& pos);
383
393 static const_position_type next_position(const const_position_type& pos);
394
398 multi_type_matrix() noexcept(nothrow_default_constructible_v);
399
406 multi_type_matrix(size_type rows, size_type cols);
407
416 template<typename _T>
417 multi_type_matrix(size_type rows, size_type cols, const _T& value);
418
431 template<typename _T>
432 multi_type_matrix(size_type rows, size_type cols, const _T& it_begin, const _T& it_end);
433
438
443
447 ~multi_type_matrix() = default;
448
449 bool operator==(const multi_type_matrix& other) const noexcept(nothrow_eq_comparable_v);
450
451 multi_type_matrix& operator=(const multi_type_matrix& other) = default;
452 multi_type_matrix& operator=(multi_type_matrix&& other) = default;
453
465 position_type position(size_type row, size_type col);
466
480 position_type position(const position_type& pos_hint, size_type row, size_type col);
481
492 const_position_type position(size_type row, size_type col) const;
493
506 const_position_type position(const const_position_type& pos_hint, size_type row, size_type col) const;
507
516 size_pair_type matrix_position(const const_position_type& pos) const;
517
525 position_type end_position();
526
534 const_position_type end_position() const;
535
545 mtm::element_t get_type(const const_position_type& pos) const;
546
557 mtm::element_t get_type(size_type row, size_type col) const;
558
569 numeric_type get_numeric(size_type row, size_type col) const;
570
581 numeric_type get_numeric(const const_position_type& pos) const;
582
594 template<typename T>
595 T get(size_type row, size_type col) const;
596
607 template<typename T>
608 T get(const const_position_type& pos) const;
609
628 template<mtm::detail::referenceable_value_type<typename mtm::detail::block_list_of<Traits>::type> T>
629 const T& get_ref(size_type row, size_type col) const;
630
648 template<mtm::detail::referenceable_value_type<typename mtm::detail::block_list_of<Traits>::type> T>
649 const T& get_ref(const const_position_type& pos) const;
650
657 void set_empty(size_type row, size_type col);
658
670 void set_empty(size_type row, size_type col, size_type length);
671
679 position_type set_empty(const position_type& pos);
680
686 void set_column_empty(size_type col);
687
693 void set_row_empty(size_type row);
694
702 void set(size_type row, size_type col, numeric_type val);
703
712 position_type set(const position_type& pos, numeric_type val);
713
723 template<mtm::detail::listed_value_type<typename mtm::detail::element_blocks_of<Traits>::type> T>
724 void set(size_type row, size_type col, const T& val);
725
736 template<mtm::detail::listed_value_type<typename mtm::detail::element_blocks_of<Traits>::type> T>
737 position_type set(const position_type& pos, const T& val);
738
755 template<typename _T>
756 void set(size_type row, size_type col, const _T& it_begin, const _T& it_end);
757
772 template<typename _T>
773 position_type set(const position_type& pos, const _T& it_begin, const _T& it_end);
774
786 template<typename _T>
787 void set_column(size_type col, const _T& it_begin, const _T& it_end);
788
796
803
814 void copy(const multi_type_matrix& src);
815
827 template<typename _T>
828 void copy(size_type rows, size_type cols, const _T& it_begin, const _T& it_end);
829
840 void resize(size_type rows, size_type cols);
841
851 template<typename _T>
852 void resize(size_type rows, size_type cols, const _T& value);
853
857 void clear();
858
866 bool numeric() const;
867
879 template<typename... Blks>
880 bool all_of() const;
881
887 bool empty() const;
888
893
902 template<typename FuncT>
903 FuncT walk(FuncT func) const;
904
921 template<typename FuncT>
922 FuncT walk(FuncT func, const size_pair_type& start, const size_pair_type& end) const;
923
934 template<typename FuncT>
935 FuncT walk(FuncT func, const multi_type_matrix& right) const;
936
955 template<typename FuncT>
956 FuncT walk(
957 FuncT func, const multi_type_matrix& right, const size_pair_type& start, const size_pair_type& end) const;
958
959#ifdef MDDS_MULTI_TYPE_MATRIX_DEBUG
960 void dump() const
961 {
962 m_store.dump_blocks(std::cout);
963 }
964#endif
965
966private:
977 inline size_type get_pos(size_type row, size_type col) const
978 {
979 return m_size.row * col + row;
980 }
981
982 inline size_type get_pos(const const_position_type& pos) const
983 {
984 return pos.first->position + pos.second;
985 }
986
987private:
988 store_type m_store;
989 size_pair_type m_size;
990};
991
992} // namespace mdds
993
994#include "multi_type_matrix_def.inl"
Definition global.hpp:71
Definition types.hpp:231
mdds::detail::mtv::iterator_value_node< multi_type_vector, size_type > value_type
Definition soa/main.hpp:283
void set_column_empty(size_type col)
void copy(const multi_type_matrix &src)
FuncT walk(FuncT func) const
position_type end_position()
size_pair_type matrix_position(const const_position_type &pos) const
void set_empty(size_type row, size_type col)
void swap(multi_type_matrix &r)
static const_position_type next_position(const const_position_type &pos)
multi_type_matrix() noexcept(nothrow_default_constructible_v)
void resize(size_type rows, size_type cols)
mtm::element_t get_type(const const_position_type &pos) const
multi_type_matrix & transpose()
T get(size_type row, size_type col) const
const T & get_ref(size_type row, size_type col) const
position_type position(size_type row, size_type col)
static constexpr mtm::element_t element_empty
Definition multi_type_matrix.hpp:239
void set_row_empty(size_type row)
static position_type next_position(const position_type &pos)
static constexpr mtm::element_t element_numeric
Definition multi_type_matrix.hpp:245
void set(size_type row, size_type col, numeric_type val)
size_pair_type size() const
numeric_type get_numeric(size_type row, size_type col) const
void set_column(size_type col, const _T &it_begin, const _T &it_end)
Definition multi_type_matrix.hpp:39
Definition multi_type_matrix.hpp:140
Definition multi_type_matrix.hpp:164
Definition multi_type_matrix.hpp:192
Definition multi_type_matrix.hpp:78
Definition multi_type_matrix.hpp:143
Definition multi_type_matrix.hpp:56
Definition multi_type_matrix.hpp:101
Definition multi_type_matrix.hpp:93
Definition multi_type_matrix.hpp:46
Definition multi_type_matrix.hpp:127
Definition multi_type_matrix.hpp:84
Definition multi_type_matrix.hpp:68
Definition multi_type_matrix.hpp:203
Definition util.hpp:56
element_block_funcs<> block_funcs
Definition util.hpp:90
Definition block_funcs.hpp:44
Definition multi_type_matrix.hpp:272