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