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  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  default:;
66  }
67 
68  return element_block_func::clone_block(block);
69  }
70 
71  static void delete_block(const base_element_block* p)
72  {
73  if (!p)
74  return;
75 
76  switch (get_block_type(*p))
77  {
78  case _Block1::block_type:
79  _Block1::delete_block(p);
80  break;
81  case _Block2::block_type:
82  _Block2::delete_block(p);
83  break;
84  default:
85  element_block_func::delete_block(p);
86  }
87  }
88 
89  static void resize_block(base_element_block& block, size_t new_size)
90  {
91  switch (get_block_type(block))
92  {
93  case _Block1::block_type:
94  _Block1::resize_block(block, new_size);
95  break;
96  case _Block2::block_type:
97  _Block2::resize_block(block, new_size);
98  break;
99  default:
100  element_block_func::resize_block(block, new_size);
101  }
102  }
103 
104  static void print_block(const base_element_block& block)
105  {
106  switch (get_block_type(block))
107  {
108  case _Block1::block_type:
109  _Block1::print_block(block);
110  break;
111  case _Block2::block_type:
112  _Block2::print_block(block);
113  break;
114  default:
115  element_block_func::print_block(block);
116  }
117  }
118 
119  static void erase(base_element_block& block, size_t pos)
120  {
121  switch (get_block_type(block))
122  {
123  case _Block1::block_type:
124  _Block1::erase_block(block, pos);
125  break;
126  case _Block2::block_type:
127  _Block2::erase_block(block, pos);
128  break;
129  default:
130  element_block_func::erase(block, pos);
131  }
132  }
133 
134  static void erase(base_element_block& block, size_t pos, size_t size)
135  {
136  switch (get_block_type(block))
137  {
138  case _Block1::block_type:
139  _Block1::erase_block(block, pos, size);
140  break;
141  case _Block2::block_type:
142  _Block2::erase_block(block, pos, size);
143  break;
144  default:
145  element_block_func_base::erase(block, pos, size);
146  }
147  }
148 
149  static void append_values_from_block(base_element_block& dest, const base_element_block& src)
150  {
151  switch (get_block_type(dest))
152  {
153  case _Block1::block_type:
154  _Block1::append_values_from_block(dest, src);
155  break;
156  case _Block2::block_type:
157  _Block2::append_values_from_block(dest, src);
158  break;
159  default:
160  element_block_func_base::append_values_from_block(dest, src);
161  }
162  }
163 
164  static void append_values_from_block(
165  base_element_block& dest, const base_element_block& src, size_t begin_pos, size_t len)
166  {
167  switch (get_block_type(dest))
168  {
169  case _Block1::block_type:
170  _Block1::append_values_from_block(dest, src, begin_pos, len);
171  break;
172  case _Block2::block_type:
173  _Block2::append_values_from_block(dest, src, begin_pos, len);
174  break;
175  default:
176  element_block_func_base::append_values_from_block(dest, src, begin_pos, len);
177  }
178  }
179 
180  static void assign_values_from_block(
181  base_element_block& dest, const base_element_block& src, size_t begin_pos, size_t len)
182  {
183  switch (get_block_type(dest))
184  {
185  case _Block1::block_type:
186  _Block1::assign_values_from_block(dest, src, begin_pos, len);
187  break;
188  case _Block2::block_type:
189  _Block2::assign_values_from_block(dest, src, begin_pos, len);
190  break;
191  default:
192  element_block_func_base::assign_values_from_block(dest, src, begin_pos, len);
193  }
194  }
195 
196  static void prepend_values_from_block(
197  base_element_block& dest, const base_element_block& src, size_t begin_pos, size_t len)
198  {
199  switch (get_block_type(dest))
200  {
201  case _Block1::block_type:
202  _Block1::prepend_values_from_block(dest, src, begin_pos, len);
203  break;
204  case _Block2::block_type:
205  _Block2::prepend_values_from_block(dest, src, begin_pos, len);
206  break;
207  default:
208  element_block_func_base::prepend_values_from_block(dest, src, begin_pos, len);
209  }
210  }
211 
212  static void swap_values(base_element_block& blk1, base_element_block& blk2, size_t pos1, size_t pos2, size_t len)
213  {
214  switch (get_block_type(blk1))
215  {
216  case _Block1::block_type:
217  _Block1::swap_values(blk1, blk2, pos1, pos2, len);
218  break;
219  case _Block2::block_type:
220  _Block2::swap_values(blk1, blk2, pos1, pos2, len);
221  break;
222  default:
223  element_block_func_base::swap_values(blk1, blk2, pos1, pos2, len);
224  }
225  }
226 
227  static bool equal_block(const base_element_block& left, const base_element_block& right)
228  {
229  if (get_block_type(left) == _Block1::block_type)
230  {
231  if (get_block_type(right) != _Block1::block_type)
232  return false;
233 
234  return _Block1::get(left) == _Block1::get(right);
235  }
236  else if (mtv::get_block_type(right) == _Block1::block_type)
237  return false;
238 
239  if (get_block_type(left) == _Block2::block_type)
240  {
241  if (get_block_type(right) != _Block2::block_type)
242  return false;
243 
244  return _Block2::get(left) == _Block2::get(right);
245  }
246  else if (mtv::get_block_type(right) == _Block2::block_type)
247  return false;
248 
249  return element_block_func::equal_block(left, right);
250  }
251 
252  static void overwrite_values(base_element_block& block, size_t pos, size_t len)
253  {
254  switch (get_block_type(block))
255  {
256  case _Block1::block_type:
257  _Block1::overwrite_values(block, pos, len);
258  break;
259  case _Block2::block_type:
260  _Block2::overwrite_values(block, pos, len);
261  break;
262  default:
263  element_block_func::overwrite_values(block, pos, len);
264  }
265  }
266 
267  static void shrink_to_fit(base_element_block& block)
268  {
269  switch (get_block_type(block))
270  {
271  case _Block1::block_type:
272  _Block1::shrink_to_fit(block);
273  break;
274  case _Block2::block_type:
275  _Block2::shrink_to_fit(block);
276  break;
277  default:
278  element_block_func::shrink_to_fit(block);
279  }
280  }
281 
282  static size_t size(const base_element_block& block)
283  {
284  switch (get_block_type(block))
285  {
286  case _Block1::block_type:
287  return _Block1::size(block);
288  case _Block2::block_type:
289  return _Block2::size(block);
290  default:
291  return element_block_func::size(block);
292  }
293  }
294 };
295 
296 }} // namespace mdds::mtv
297 
298 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
299 
300 #endif
Definition: custom_func2.hpp:41
Definition: flat_segment_tree.hpp:46
Definition: types.hpp:173
static void overwrite_values(base_element_block &block, size_t pos, size_t len)
Definition: trait.hpp:658