29 #ifndef INCLUDED_MDDS_MULTI_TYPE_VECTOR_TYPES_2_HPP
30 #define INCLUDED_MDDS_MULTI_TYPE_VECTOR_TYPES_2_HPP
32 #include "../global.hpp"
39 #ifdef MDDS_MULTI_TYPE_VECTOR_USE_DEQUE
45 #if defined(MDDS_UNIT_TEST) || defined (MDDS_MULTI_TYPE_VECTOR_DEBUG)
53 namespace mdds {
namespace mtv {
55 using element_t = int;
57 constexpr element_t element_type_empty = -1;
59 constexpr element_t element_type_boolean = 0;
60 constexpr element_t element_type_int8 = 1;
61 constexpr element_t element_type_uint8 = 2;
62 constexpr element_t element_type_int16 = 3;
63 constexpr element_t element_type_uint16 = 4;
64 constexpr element_t element_type_int32 = 5;
65 constexpr element_t element_type_uint32 = 6;
66 constexpr element_t element_type_int64 = 7;
67 constexpr element_t element_type_uint64 = 8;
68 constexpr element_t element_type_float = 9;
69 constexpr element_t element_type_double = 10;
70 constexpr element_t element_type_string = 11;
72 constexpr element_t element_type_user_start = 50;
80 enum class lu_factor_t : int
88 sse2_x64_lu4 = 1 << 8 | 4,
89 sse2_x64_lu8 = 1 << 8 | 8,
90 sse2_x64_lu16 = 1 << 8 | 16,
92 avx2_x64_lu4 = 2 << 8 | 4,
93 avx2_x64_lu8 = 2 << 8 | 8,
120 template<
typename _Self, element_t _TypeId,
typename _Data>
123 #ifdef MDDS_UNIT_TEST
124 struct print_block_array
126 void operator() (
const _Data& val)
const
128 std::cout << val <<
" ";
134 #ifdef MDDS_MULTI_TYPE_VECTOR_USE_DEQUE
135 typedef std::deque<_Data> store_type;
137 typedef std::vector<_Data> store_type;
145 template<
typename _Iter>
149 static const element_t block_type = _TypeId;
151 typedef typename store_type::iterator iterator;
152 typedef typename store_type::reverse_iterator reverse_iterator;
153 typedef typename store_type::const_iterator const_iterator;
154 typedef typename store_type::const_reverse_iterator const_reverse_iterator;
155 typedef _Data value_type;
157 bool operator== (
const _Self& r)
const
159 return m_array == r.m_array;
162 bool operator!= (
const _Self& r)
const
164 return !operator==(r);
167 static const value_type& at(
const base_element_block& block,
typename store_type::size_type pos)
169 return get(block).m_array.at(pos);
174 return get(block).m_array.at(pos);
179 return get(block).m_array.data();
184 return get(block).m_array.size();
189 return get(block).m_array.begin();
194 return get(block).m_array.end();
199 return get(block).m_array.begin();
204 return get(block).m_array.end();
209 return get(block).m_array.begin();
214 return get(block).m_array.end();
219 return get(block).m_array.rbegin();
224 return get(block).m_array.rend();
229 return get(block).m_array.rbegin();
234 return get(block).m_array.rend();
239 return get(block).m_array.rbegin();
244 return get(block).m_array.rend();
249 #ifdef MDDS_MULTI_TYPE_VECTOR_DEBUG
252 std::ostringstream os;
253 os <<
"incorrect block type: expected block type=" << _TypeId <<
", passed block type=" <<
get_block_type(block);
257 return static_cast<_Self&
>(block);
262 #ifdef MDDS_MULTI_TYPE_VECTOR_DEBUG
265 std::ostringstream os;
266 os <<
"incorrect block type: expected block type=" << _TypeId <<
", passed block type=" <<
get_block_type(block);
270 return static_cast<const _Self&
>(block);
275 get(blk).m_array[pos] = val;
280 val = get(blk).m_array[pos];
285 return get(blk).m_array[pos];
290 get(blk).m_array.push_back(val);
295 store_type& blk2 = get(blk).m_array;
296 blk2.insert(blk2.begin(), val);
299 static _Self* create_block(
size_t init_size)
301 return new _Self(init_size);
306 delete static_cast<const _Self*
>(p);
311 store_type& st = get(blk).m_array;
316 if (new_size < (st.capacity() / 2))
320 #ifdef MDDS_UNIT_TEST
323 const store_type& blk2 = get(blk).m_array;
324 std::for_each(blk2.begin(), blk2.end(), print_block_array());
325 std::cout << std::endl;
333 store_type& blk2 = get(blk).m_array;
334 blk2.erase(blk2.begin()+pos);
339 store_type& blk2 = get(blk).m_array;
340 blk2.erase(blk2.begin()+pos, blk2.begin()+pos+size);
345 store_type& d = get(dest).m_array;
346 const store_type& s = get(src).m_array;
347 d.insert(d.end(), s.begin(), s.end());
350 static void append_values_from_block(
353 store_type& d = get(dest).m_array;
354 const store_type& s = get(src).m_array;
355 std::pair<const_iterator,const_iterator> its = get_iterator_pair(s, begin_pos, len);
356 #ifndef MDDS_MULTI_TYPE_VECTOR_USE_DEQUE
357 d.reserve(d.size() + len);
359 d.insert(d.end(), its.first, its.second);
362 static void assign_values_from_block(
365 store_type& d = get(dest).m_array;
366 const store_type& s = get(src).m_array;
367 std::pair<const_iterator,const_iterator> its = get_iterator_pair(s, begin_pos, len);
368 d.assign(its.first, its.second);
371 static void prepend_values_from_block(
374 store_type& d = get(dest).m_array;
375 const store_type& s = get(src).m_array;
376 std::pair<const_iterator,const_iterator> its = get_iterator_pair(s, begin_pos, len);
377 #ifndef MDDS_MULTI_TYPE_VECTOR_USE_DEQUE
378 d.reserve(d.size() + len);
380 d.insert(d.begin(), its.first, its.second);
383 static void swap_values(
386 store_type& st1 = get(blk1).m_array;
387 store_type& st2 = get(blk2).m_array;
388 assert(pos1 + len <= st1.size());
389 assert(pos2 + len <= st2.size());
391 typename store_type::iterator it1 = st1.begin(), it2 = st2.begin();
392 std::advance(it1, pos1);
393 std::advance(it2, pos2);
394 for (
size_t i = 0; i < len; ++i, ++it1, ++it2)
396 #ifdef MDDS_MULTI_TYPE_VECTOR_USE_DEQUE
397 std::swap(*it1, *it2);
399 value_type v1 = *it1, v2 = *it2;
406 template<
typename _Iter>
407 static void set_values(
410 store_type& d = get(block).m_array;
411 typename store_type::iterator it_dest = d.begin();
412 std::advance(it_dest, pos);
413 for (_Iter it = it_begin; it != it_end; ++it, ++it_dest)
417 template<
typename _Iter>
418 static void append_values(
base_element_block& block,
const _Iter& it_begin,
const _Iter& it_end)
420 store_type& d = get(block).m_array;
421 typename store_type::iterator it = d.end();
422 d.insert(it, it_begin, it_end);
425 template<
typename _Iter>
426 static void prepend_values(
base_element_block& block,
const _Iter& it_begin,
const _Iter& it_end)
428 store_type& d = get(block).m_array;
429 d.insert(d.begin(), it_begin, it_end);
432 template<
typename _Iter>
433 static void assign_values(
base_element_block& dest,
const _Iter& it_begin,
const _Iter& it_end)
435 store_type& d = get(dest).m_array;
436 d.assign(it_begin, it_end);
439 template<
typename _Iter>
440 static void insert_values(
443 store_type& blk = get(block).m_array;
444 blk.insert(blk.begin()+pos, it_begin, it_end);
449 #ifdef MDDS_MULTI_TYPE_VECTOR_USE_DEQUE
452 const store_type& blk = get(block).m_array;
453 return blk.capacity();
459 #ifndef MDDS_MULTI_TYPE_VECTOR_USE_DEQUE
460 get(block).m_array.shrink_to_fit();
465 static std::pair<const_iterator,const_iterator>
466 get_iterator_pair(
const store_type& array,
size_t begin_pos,
size_t len)
468 assert(begin_pos + len <= array.size());
469 const_iterator it = array.begin();
470 std::advance(it, begin_pos);
471 const_iterator it_end = it;
472 std::advance(it_end, len);
473 return std::pair<const_iterator,const_iterator>(it, it_end);
477 template<
typename _Self, element_t _TypeId,
typename _Data>
486 template<
typename _Iter>
490 using base_type::get;
495 return new _Self(get(blk));
499 template<
typename _Self, element_t _TypeId,
typename _Data>
508 template<
typename _Iter>
537 template<element_t _TypeId,
typename _Data>
547 template<
typename _Iter>
550 static self_type* create_block_with_value(
size_t init_size,
const _Data& val)
555 template<
typename _Iter>
556 static self_type* create_block_with_values(
const _Iter& it_begin,
const _Iter& it_end)
571 template<element_t _TypeId,
typename _Data>
577 using base_type::get;
578 using base_type::set_value;
579 using base_type::m_array;
585 #ifndef MDDS_MULTI_TYPE_VECTOR_USE_DEQUE
586 m_array.reserve(r.m_array.size());
588 typename managed_element_block::store_type::const_iterator it = r.m_array.begin(), it_end = r.m_array.end();
589 for (; it != it_end; ++it)
590 m_array.push_back(
new _Data(**it));
593 template<
typename _Iter>
598 std::for_each(m_array.begin(), m_array.end(), std::default_delete<_Data>());
601 static self_type* create_block_with_value(
size_t init_size, _Data* val)
605 throw general_error(
"You can't create a managed block with initial value.");
607 std::unique_ptr<self_type> blk = std::make_unique<self_type>(init_size);
609 set_value(*blk, 0, val);
611 return blk.release();
614 template<
typename _Iter>
615 static self_type* create_block_with_values(
const _Iter& it_begin,
const _Iter& it_end)
623 typename managed_element_block::store_type::iterator it = blk.m_array.begin() + pos;
624 typename managed_element_block::store_type::iterator it_end = it + len;
625 std::for_each(it, it_end, std::default_delete<_Data>());
629 template<element_t _TypeId,
typename _Data>
635 using base_type::get;
636 using base_type::m_array;
637 using base_type::set_value;
642 template<
typename _Iter>
647 std::for_each(m_array.begin(), m_array.end(), std::default_delete<_Data>());
650 static self_type* create_block_with_value(
size_t init_size, _Data* val)
654 throw general_error(
"You can't create a managed block with initial value.");
656 std::unique_ptr<self_type> blk = std::make_unique<self_type>(init_size);
658 set_value(*blk, 0, val);
660 return blk.release();
663 template<
typename _Iter>
664 static self_type* create_block_with_values(
const _Iter& it_begin,
const _Iter& it_end)
672 typename noncopyable_managed_element_block::store_type::iterator it = blk.m_array.begin() + pos;
673 typename noncopyable_managed_element_block::store_type::iterator it_end = it + len;
674 std::for_each(it, it_end, std::default_delete<_Data>());
Definition: global.hpp:82
Definition: types.hpp:113
friend element_t get_block_type(const base_element_block &)
Definition: types.hpp:528
Definition: types.hpp:479
Definition: types.hpp:100
Definition: types.hpp:122
Definition: types.hpp:501
Definition: types.hpp:539
Definition: types.hpp:573
Definition: types.hpp:631