mdds
custom_func1.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_FUNC1_HPP
30 #define INCLUDED_MDDS_MULTI_TYPE_VECTOR_DIR_CUSTOM_FUNC1_HPP
31 
32 #include "types.hpp"
33 #include "trait.hpp"
34 
35 namespace mdds { namespace mtv {
36 
40 template<typename _Block>
42 {
43  static base_element_block* create_new_block(element_t type, size_t init_size)
44  {
45  switch (type)
46  {
47  case _Block::block_type:
48  return _Block::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 _Block::block_type:
61  return _Block::clone_block(block);
62  default:
63  ;
64  }
65 
66  return element_block_func::clone_block(block);
67  }
68 
69  static void delete_block(const base_element_block* p)
70  {
71  if (!p)
72  return;
73 
74  switch (get_block_type(*p))
75  {
76  case _Block::block_type:
77  _Block::delete_block(p);
78  break;
79  default:
80  element_block_func::delete_block(p);
81  }
82  }
83 
84  static void resize_block(base_element_block& block, size_t new_size)
85  {
86  switch (get_block_type(block))
87  {
88  case _Block::block_type:
89  _Block::resize_block(block, new_size);
90  break;
91  default:
92  element_block_func::resize_block(block, new_size);
93  }
94  }
95 
96  static void print_block(const base_element_block& block)
97  {
98  switch (get_block_type(block))
99  {
100  case _Block::block_type:
101  _Block::print_block(block);
102  break;
103  default:
104  element_block_func::print_block(block);
105  }
106  }
107 
108  static void erase(base_element_block& block, size_t pos)
109  {
110  switch (get_block_type(block))
111  {
112  case _Block::block_type:
113  _Block::erase_block(block, pos);
114  break;
115  default:
116  element_block_func::erase(block, pos);
117  }
118  }
119 
120  static void erase(base_element_block& block, size_t pos, size_t size)
121  {
122  switch (get_block_type(block))
123  {
124  case _Block::block_type:
125  _Block::erase_block(block, pos, size);
126  break;
127  default:
128  element_block_func_base::erase(block, pos, size);
129  }
130  }
131 
132  static void append_values_from_block(base_element_block& dest, const base_element_block& src)
133  {
134  switch (get_block_type(dest))
135  {
136  case _Block::block_type:
137  _Block::append_values_from_block(dest, src);
138  break;
139  default:
140  element_block_func_base::append_values_from_block(dest, src);
141  }
142  }
143 
144  static void append_values_from_block(
145  base_element_block& dest, const base_element_block& src, size_t begin_pos, size_t len)
146  {
147  switch (get_block_type(dest))
148  {
149  case _Block::block_type:
150  _Block::append_values_from_block(dest, src, begin_pos, len);
151  break;
152  default:
153  element_block_func_base::append_values_from_block(dest, src, begin_pos, len);
154  }
155  }
156 
157  static void assign_values_from_block(
158  base_element_block& dest, const base_element_block& src, size_t begin_pos, size_t len)
159  {
160  switch (get_block_type(dest))
161  {
162  case _Block::block_type:
163  _Block::assign_values_from_block(dest, src, begin_pos, len);
164  break;
165  default:
166  element_block_func_base::assign_values_from_block(dest, src, begin_pos, len);
167  }
168  }
169 
170  static void prepend_values_from_block(
171  base_element_block& dest, const base_element_block& src, size_t begin_pos, size_t len)
172  {
173  switch (get_block_type(dest))
174  {
175  case _Block::block_type:
176  _Block::prepend_values_from_block(dest, src, begin_pos, len);
177  break;
178  default:
179  element_block_func_base::prepend_values_from_block(dest, src, begin_pos, len);
180  }
181  }
182 
183  static void swap_values(
184  base_element_block& blk1, base_element_block& blk2, size_t pos1, size_t pos2, size_t len)
185  {
186  switch (get_block_type(blk1))
187  {
188  case _Block::block_type:
189  _Block::swap_values(blk1, blk2, pos1, pos2, len);
190  break;
191  default:
192  element_block_func_base::swap_values(blk1, blk2, pos1, pos2, len);
193  }
194  }
195 
196  static bool equal_block(
197  const base_element_block& left, const base_element_block& right)
198  {
199  if (get_block_type(left) == _Block::block_type)
200  {
201  if (get_block_type(right) != _Block::block_type)
202  return false;
203 
204  return _Block::get(left) == _Block::get(right);
205  }
206  else if (mtv::get_block_type(right) == _Block::block_type)
207  return false;
208 
209  return element_block_func::equal_block(left, right);
210  }
211 
212  static void overwrite_values(base_element_block& block, size_t pos, size_t len)
213  {
214  switch (get_block_type(block))
215  {
216  case _Block::block_type:
217  _Block::overwrite_values(block, pos, len);
218  break;
219  default:
220  element_block_func::overwrite_values(block, pos, len);
221  }
222  }
223 
224  static void shrink_to_fit(base_element_block& block)
225  {
226  switch (get_block_type(block))
227  {
228  case _Block::block_type:
229  _Block::shrink_to_fit(block);
230  break;
231  default:
232  element_block_func::shrink_to_fit(block);
233  }
234  }
235 
236  static size_t size(const base_element_block& block)
237  {
238  switch (get_block_type(block))
239  {
240  case _Block::block_type:
241  return _Block::size(block);
242  default:
243  return element_block_func::size(block);
244  }
245  }
246 };
247 
248 }}
249 
250 #endif
251 
252 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
253 
Definition: types.hpp:113
Definition: custom_func1.hpp:42
static void overwrite_values(base_element_block &block, size_t pos, size_t len)
Definition: trait.hpp:660