mdds
custom_func3.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_FUNC3_HPP
30 #define INCLUDED_MDDS_MULTI_TYPE_VECTOR_DIR_CUSTOM_FUNC3_HPP
31 
32 #include "types.hpp"
33 #include "trait.hpp"
34 
35 namespace mdds { namespace mtv {
36 
37 template<typename _Block1, typename _Block2, typename _Block3>
39 {
40  static base_element_block* create_new_block(element_t type, size_t init_size)
41  {
42  switch (type)
43  {
44  case _Block1::block_type:
45  return _Block1::create_block(init_size);
46  case _Block2::block_type:
47  return _Block2::create_block(init_size);
48  case _Block3::block_type:
49  return _Block3::create_block(init_size);
50  default:
51  ;
52  }
53 
54  return element_block_func::create_new_block(type, init_size);
55  }
56 
57  static base_element_block* clone_block(const base_element_block& block)
58  {
59  switch (get_block_type(block))
60  {
61  case _Block1::block_type:
62  return _Block1::clone_block(block);
63  case _Block2::block_type:
64  return _Block2::clone_block(block);
65  case _Block3::block_type:
66  return _Block3::clone_block(block);
67  default:
68  ;
69  }
70 
71  return element_block_func::clone_block(block);
72  }
73 
74  static void delete_block(const base_element_block* p)
75  {
76  if (!p)
77  return;
78 
79  switch (get_block_type(*p))
80  {
81  case _Block1::block_type:
82  _Block1::delete_block(p);
83  break;
84  case _Block2::block_type:
85  _Block2::delete_block(p);
86  break;
87  case _Block3::block_type:
88  _Block3::delete_block(p);
89  break;
90  default:
91  element_block_func::delete_block(p);
92  }
93  }
94 
95  static void resize_block(base_element_block& block, size_t new_size)
96  {
97  switch (get_block_type(block))
98  {
99  case _Block1::block_type:
100  _Block1::resize_block(block, new_size);
101  break;
102  case _Block2::block_type:
103  _Block2::resize_block(block, new_size);
104  break;
105  case _Block3::block_type:
106  _Block3::resize_block(block, new_size);
107  break;
108  default:
109  element_block_func::resize_block(block, new_size);
110  }
111  }
112 
113  static void print_block(const base_element_block& block)
114  {
115  switch (get_block_type(block))
116  {
117  case _Block1::block_type:
118  _Block1::print_block(block);
119  break;
120  case _Block2::block_type:
121  _Block2::print_block(block);
122  break;
123  case _Block3::block_type:
124  _Block3::print_block(block);
125  break;
126  default:
127  element_block_func::print_block(block);
128  }
129  }
130 
131  static void erase(base_element_block& block, size_t pos)
132  {
133  switch (get_block_type(block))
134  {
135  case _Block1::block_type:
136  _Block1::erase_block(block, pos);
137  break;
138  case _Block2::block_type:
139  _Block2::erase_block(block, pos);
140  break;
141  case _Block3::block_type:
142  _Block3::erase_block(block, pos);
143  break;
144  default:
145  element_block_func::erase(block, pos);
146  }
147  }
148 
149  static void erase(base_element_block& block, size_t pos, size_t size)
150  {
151  switch (get_block_type(block))
152  {
153  case _Block1::block_type:
154  _Block1::erase_block(block, pos, size);
155  break;
156  case _Block2::block_type:
157  _Block2::erase_block(block, pos, size);
158  break;
159  case _Block3::block_type:
160  _Block3::erase_block(block, pos, size);
161  break;
162  default:
163  element_block_func_base::erase(block, pos, size);
164  }
165  }
166 
167  static void append_values_from_block(base_element_block& dest, const base_element_block& src)
168  {
169  switch (get_block_type(dest))
170  {
171  case _Block1::block_type:
172  _Block1::append_values_from_block(dest, src);
173  break;
174  case _Block2::block_type:
175  _Block2::append_values_from_block(dest, src);
176  break;
177  case _Block3::block_type:
178  _Block3::append_values_from_block(dest, src);
179  break;
180  default:
181  element_block_func_base::append_values_from_block(dest, src);
182  }
183  }
184 
185  static void append_values_from_block(
186  base_element_block& dest, const base_element_block& src, size_t begin_pos, size_t len)
187  {
188  switch (get_block_type(dest))
189  {
190  case _Block1::block_type:
191  _Block1::append_values_from_block(dest, src, begin_pos, len);
192  break;
193  case _Block2::block_type:
194  _Block2::append_values_from_block(dest, src, begin_pos, len);
195  break;
196  case _Block3::block_type:
197  _Block3::append_values_from_block(dest, src, begin_pos, len);
198  break;
199  default:
200  element_block_func_base::append_values_from_block(dest, src, begin_pos, len);
201  }
202  }
203 
204  static void assign_values_from_block(
205  base_element_block& dest, const base_element_block& src, size_t begin_pos, size_t len)
206  {
207  switch (get_block_type(dest))
208  {
209  case _Block1::block_type:
210  _Block1::assign_values_from_block(dest, src, begin_pos, len);
211  break;
212  case _Block2::block_type:
213  _Block2::assign_values_from_block(dest, src, begin_pos, len);
214  break;
215  case _Block3::block_type:
216  _Block3::assign_values_from_block(dest, src, begin_pos, len);
217  break;
218  default:
219  element_block_func_base::assign_values_from_block(dest, src, begin_pos, len);
220  }
221  }
222 
223  static void prepend_values_from_block(
224  base_element_block& dest, const base_element_block& src, size_t begin_pos, size_t len)
225  {
226  switch (get_block_type(dest))
227  {
228  case _Block1::block_type:
229  _Block1::prepend_values_from_block(dest, src, begin_pos, len);
230  break;
231  case _Block2::block_type:
232  _Block2::prepend_values_from_block(dest, src, begin_pos, len);
233  break;
234  case _Block3::block_type:
235  _Block3::prepend_values_from_block(dest, src, begin_pos, len);
236  break;
237  default:
238  element_block_func_base::prepend_values_from_block(dest, src, begin_pos, len);
239  }
240  }
241 
242  static void swap_values(
243  base_element_block& blk1, base_element_block& blk2, size_t pos1, size_t pos2, size_t len)
244  {
245  switch (get_block_type(blk1))
246  {
247  case _Block1::block_type:
248  _Block1::swap_values(blk1, blk2, pos1, pos2, len);
249  break;
250  case _Block2::block_type:
251  _Block2::swap_values(blk1, blk2, pos1, pos2, len);
252  break;
253  case _Block3::block_type:
254  _Block3::swap_values(blk1, blk2, pos1, pos2, len);
255  break;
256  default:
257  element_block_func_base::swap_values(blk1, blk2, pos1, pos2, len);
258  }
259  }
260 
261  static bool equal_block(
262  const base_element_block& left, const base_element_block& right)
263  {
264  if (get_block_type(left) == _Block1::block_type)
265  {
266  if (get_block_type(right) != _Block1::block_type)
267  return false;
268 
269  return _Block1::get(left) == _Block1::get(right);
270  }
271  else if (mtv::get_block_type(right) == _Block1::block_type)
272  return false;
273 
274  if (get_block_type(left) == _Block2::block_type)
275  {
276  if (get_block_type(right) != _Block2::block_type)
277  return false;
278 
279  return _Block2::get(left) == _Block2::get(right);
280  }
281  else if (mtv::get_block_type(right) == _Block2::block_type)
282  return false;
283 
284  if (get_block_type(left) == _Block3::block_type)
285  {
286  if (get_block_type(right) != _Block3::block_type)
287  return false;
288 
289  return _Block3::get(left) == _Block3::get(right);
290  }
291  else if (mtv::get_block_type(right) == _Block3::block_type)
292  return false;
293 
294  return element_block_func::equal_block(left, right);
295  }
296 
297  static void overwrite_values(base_element_block& block, size_t pos, size_t len)
298  {
299  switch (get_block_type(block))
300  {
301  case _Block1::block_type:
302  _Block1::overwrite_values(block, pos, len);
303  break;
304  case _Block2::block_type:
305  _Block2::overwrite_values(block, pos, len);
306  break;
307  case _Block3::block_type:
308  _Block3::overwrite_values(block, pos, len);
309  break;
310  default:
311  element_block_func::overwrite_values(block, pos, len);
312  }
313  }
314 
315  static void shrink_to_fit(base_element_block& block)
316  {
317  switch (get_block_type(block))
318  {
319  case _Block1::block_type:
320  _Block1::shrink_to_fit(block);
321  break;
322  case _Block2::block_type:
323  _Block2::shrink_to_fit(block);
324  break;
325  case _Block3::block_type:
326  _Block3::shrink_to_fit(block);
327  break;
328  default:
329  element_block_func::shrink_to_fit(block);
330  }
331  }
332 
333  static size_t size(const base_element_block& block)
334  {
335  switch (get_block_type(block))
336  {
337  case _Block1::block_type:
338  return _Block1::size(block);
339  case _Block2::block_type:
340  return _Block2::size(block);
341  case _Block3::block_type:
342  return _Block3::size(block);
343  default:
344  return element_block_func::size(block);
345  }
346  }
347 };
348 
349 }}
350 
351 #endif
352 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
353 
Definition: types.hpp:173
Definition: custom_func3.hpp:39
static void overwrite_values(base_element_block &block, size_t pos, size_t len)
Definition: trait.hpp:660