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