mdds
custom_func2.hpp
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
3  *
4  * Copyright (c) 2021 Kohei Yoshida
5  *
6  * Permission is hereby granted, free of charge, to any person
7  * obtaining a copy of this software and associated documentation
8  * files (the "Software"), to deal in the Software without
9  * restriction, including without limitation the rights to use,
10  * copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following
13  * conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  * OTHER DEALINGS IN THE SOFTWARE.
26  *
27  ************************************************************************/
28 
29 #ifndef INCLUDED_MDDS_MULTI_TYPE_VECTOR_DIR_CUSTOM_FUNC2_HPP
30 #define INCLUDED_MDDS_MULTI_TYPE_VECTOR_DIR_CUSTOM_FUNC2_HPP
31 
32 #include "types.hpp"
33 #include "trait.hpp"
34 
35 namespace mdds { namespace mtv {
36 
40 template<typename _Block1, typename _Block2>
42 {
43  static base_element_block* create_new_block(element_t type, size_t init_size)
44  {
45  switch (type)
46  {
47  case _Block1::block_type:
48  return _Block1::create_block(init_size);
49  case _Block2::block_type:
50  return _Block2::create_block(init_size);
51  default:
52  ;
53  }
54 
55  return element_block_func::create_new_block(type, init_size);
56  }
57 
58  static base_element_block* clone_block(const base_element_block& block)
59  {
60  switch (get_block_type(block))
61  {
62  case _Block1::block_type:
63  return _Block1::clone_block(block);
64  case _Block2::block_type:
65  return _Block2::clone_block(block);
66  default:
67  ;
68  }
69 
70  return element_block_func::clone_block(block);
71  }
72 
73  static void delete_block(const base_element_block* p)
74  {
75  if (!p)
76  return;
77 
78  switch (get_block_type(*p))
79  {
80  case _Block1::block_type:
81  _Block1::delete_block(p);
82  break;
83  case _Block2::block_type:
84  _Block2::delete_block(p);
85  break;
86  default:
87  element_block_func::delete_block(p);
88  }
89  }
90 
91  static void resize_block(base_element_block& block, size_t new_size)
92  {
93  switch (get_block_type(block))
94  {
95  case _Block1::block_type:
96  _Block1::resize_block(block, new_size);
97  break;
98  case _Block2::block_type:
99  _Block2::resize_block(block, new_size);
100  break;
101  default:
102  element_block_func::resize_block(block, new_size);
103  }
104  }
105 
106  static void print_block(const base_element_block& block)
107  {
108  switch (get_block_type(block))
109  {
110  case _Block1::block_type:
111  _Block1::print_block(block);
112  break;
113  case _Block2::block_type:
114  _Block2::print_block(block);
115  break;
116  default:
117  element_block_func::print_block(block);
118  }
119  }
120 
121  static void erase(base_element_block& block, size_t pos)
122  {
123  switch (get_block_type(block))
124  {
125  case _Block1::block_type:
126  _Block1::erase_block(block, pos);
127  break;
128  case _Block2::block_type:
129  _Block2::erase_block(block, pos);
130  break;
131  default:
132  element_block_func::erase(block, pos);
133  }
134  }
135 
136  static void erase(base_element_block& block, size_t pos, size_t size)
137  {
138  switch (get_block_type(block))
139  {
140  case _Block1::block_type:
141  _Block1::erase_block(block, pos, size);
142  break;
143  case _Block2::block_type:
144  _Block2::erase_block(block, pos, size);
145  break;
146  default:
147  element_block_func_base::erase(block, pos, size);
148  }
149  }
150 
151  static void append_values_from_block(base_element_block& dest, const base_element_block& src)
152  {
153  switch (get_block_type(dest))
154  {
155  case _Block1::block_type:
156  _Block1::append_values_from_block(dest, src);
157  break;
158  case _Block2::block_type:
159  _Block2::append_values_from_block(dest, src);
160  break;
161  default:
162  element_block_func_base::append_values_from_block(dest, src);
163  }
164  }
165 
166  static void append_values_from_block(
167  base_element_block& dest, const base_element_block& src, size_t begin_pos, size_t len)
168  {
169  switch (get_block_type(dest))
170  {
171  case _Block1::block_type:
172  _Block1::append_values_from_block(dest, src, begin_pos, len);
173  break;
174  case _Block2::block_type:
175  _Block2::append_values_from_block(dest, src, begin_pos, len);
176  break;
177  default:
178  element_block_func_base::append_values_from_block(dest, src, begin_pos, len);
179  }
180  }
181 
182  static void assign_values_from_block(
183  base_element_block& dest, const base_element_block& src, size_t begin_pos, size_t len)
184  {
185  switch (get_block_type(dest))
186  {
187  case _Block1::block_type:
188  _Block1::assign_values_from_block(dest, src, begin_pos, len);
189  break;
190  case _Block2::block_type:
191  _Block2::assign_values_from_block(dest, src, begin_pos, len);
192  break;
193  default:
194  element_block_func_base::assign_values_from_block(dest, src, begin_pos, len);
195  }
196  }
197 
198  static void prepend_values_from_block(
199  base_element_block& dest, const base_element_block& src, size_t begin_pos, size_t len)
200  {
201  switch (get_block_type(dest))
202  {
203  case _Block1::block_type:
204  _Block1::prepend_values_from_block(dest, src, begin_pos, len);
205  break;
206  case _Block2::block_type:
207  _Block2::prepend_values_from_block(dest, src, begin_pos, len);
208  break;
209  default:
210  element_block_func_base::prepend_values_from_block(dest, src, begin_pos, len);
211  }
212  }
213 
214  static void swap_values(
215  base_element_block& blk1, base_element_block& blk2, size_t pos1, size_t pos2, size_t len)
216  {
217  switch (get_block_type(blk1))
218  {
219  case _Block1::block_type:
220  _Block1::swap_values(blk1, blk2, pos1, pos2, len);
221  break;
222  case _Block2::block_type:
223  _Block2::swap_values(blk1, blk2, pos1, pos2, len);
224  break;
225  default:
226  element_block_func_base::swap_values(blk1, blk2, pos1, pos2, len);
227  }
228  }
229 
230  static bool equal_block(
231  const base_element_block& left, const base_element_block& right)
232  {
233  if (get_block_type(left) == _Block1::block_type)
234  {
235  if (get_block_type(right) != _Block1::block_type)
236  return false;
237 
238  return _Block1::get(left) == _Block1::get(right);
239  }
240  else if (mtv::get_block_type(right) == _Block1::block_type)
241  return false;
242 
243  if (get_block_type(left) == _Block2::block_type)
244  {
245  if (get_block_type(right) != _Block2::block_type)
246  return false;
247 
248  return _Block2::get(left) == _Block2::get(right);
249  }
250  else if (mtv::get_block_type(right) == _Block2::block_type)
251  return false;
252 
253  return element_block_func::equal_block(left, right);
254  }
255 
256  static void overwrite_values(base_element_block& block, size_t pos, size_t len)
257  {
258  switch (get_block_type(block))
259  {
260  case _Block1::block_type:
261  _Block1::overwrite_values(block, pos, len);
262  break;
263  case _Block2::block_type:
264  _Block2::overwrite_values(block, pos, len);
265  break;
266  default:
267  element_block_func::overwrite_values(block, pos, len);
268  }
269  }
270 
271  static void shrink_to_fit(base_element_block& block)
272  {
273  switch (get_block_type(block))
274  {
275  case _Block1::block_type:
276  _Block1::shrink_to_fit(block);
277  break;
278  case _Block2::block_type:
279  _Block2::shrink_to_fit(block);
280  break;
281  default:
282  element_block_func::shrink_to_fit(block);
283  }
284  }
285 
286  static size_t size(const base_element_block& block)
287  {
288  switch (get_block_type(block))
289  {
290  case _Block1::block_type:
291  return _Block1::size(block);
292  case _Block2::block_type:
293  return _Block2::size(block);
294  default:
295  return element_block_func::size(block);
296  }
297  }
298 };
299 
300 }}
301 
302 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
303 
304 #endif
Definition: types.hpp:113
Definition: custom_func2.hpp:42
static void overwrite_values(base_element_block &block, size_t pos, size_t len)
Definition: trait.hpp:660