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,
115 enum class trace_method_t : int
119 accessor_with_pos_hint = 1 << 8 | 1,
121 mutator_with_pos_hint = 1 << 8 | 2,
131 trace_method_t type = trace_method_t::unspecified;
183 template<
typename _Self, element_t _TypeId,
typename _Data>
186 #ifdef MDDS_UNIT_TEST
187 struct print_block_array
189 void operator()(
const _Data& val)
const
191 std::cout << val <<
" ";
197 #ifdef MDDS_MULTI_TYPE_VECTOR_USE_DEQUE
198 typedef std::deque<_Data> store_type;
200 typedef std::vector<_Data> store_type;
211 template<
typename _Iter>
216 static const element_t block_type = _TypeId;
218 typedef typename store_type::iterator iterator;
219 typedef typename store_type::reverse_iterator reverse_iterator;
220 typedef typename store_type::const_iterator const_iterator;
221 typedef typename store_type::const_reverse_iterator const_reverse_iterator;
222 typedef _Data value_type;
224 bool operator==(
const _Self& r)
const
226 return m_array == r.m_array;
229 bool operator!=(
const _Self& r)
const
231 return !operator==(r);
234 static const value_type& at(
const base_element_block& block,
typename store_type::size_type pos)
236 return get(block).m_array.at(pos);
241 return get(block).m_array.at(pos);
246 return get(block).m_array.data();
251 return get(block).m_array.size();
256 return get(block).m_array.begin();
261 return get(block).m_array.end();
266 return get(block).m_array.begin();
271 return get(block).m_array.end();
276 return get(block).m_array.begin();
281 return get(block).m_array.end();
286 return get(block).m_array.rbegin();
291 return get(block).m_array.rend();
296 return get(block).m_array.rbegin();
301 return get(block).m_array.rend();
306 return get(block).m_array.rbegin();
311 return get(block).m_array.rend();
316 #ifdef MDDS_MULTI_TYPE_VECTOR_DEBUG
319 std::ostringstream os;
320 os <<
"incorrect block type: expected block type=" << _TypeId
325 return static_cast<_Self&
>(block);
330 #ifdef MDDS_MULTI_TYPE_VECTOR_DEBUG
333 std::ostringstream os;
334 os <<
"incorrect block type: expected block type=" << _TypeId
339 return static_cast<const _Self&
>(block);
344 get(blk).m_array[pos] = val;
349 val =
get(blk).m_array[pos];
354 return get(blk).m_array[pos];
359 get(blk).m_array.push_back(val);
364 store_type& blk2 =
get(blk).m_array;
365 blk2.insert(blk2.begin(), val);
368 static _Self* create_block(
size_t init_size)
370 return new _Self(init_size);
375 delete static_cast<const _Self*
>(p);
380 store_type& st =
get(blk).m_array;
385 if (new_size < (st.capacity() / 2))
389 #ifdef MDDS_UNIT_TEST
392 const store_type& blk2 =
get(blk).m_array;
393 std::for_each(blk2.begin(), blk2.end(), print_block_array());
394 std::cout << std::endl;
403 store_type& blk2 =
get(blk).m_array;
404 blk2.erase(blk2.begin() + pos);
409 store_type& blk2 =
get(blk).m_array;
410 blk2.erase(blk2.begin() + pos, blk2.begin() + pos + size);
415 store_type& d =
get(dest).m_array;
416 const store_type& s =
get(src).m_array;
417 d.insert(d.end(), s.begin(), s.end());
420 static void append_values_from_block(
423 store_type& d =
get(dest).m_array;
424 const store_type& s =
get(src).m_array;
425 std::pair<const_iterator, const_iterator> its = get_iterator_pair(s, begin_pos, len);
426 #ifndef MDDS_MULTI_TYPE_VECTOR_USE_DEQUE
427 d.reserve(d.size() + len);
429 d.insert(d.end(), its.first, its.second);
432 static void assign_values_from_block(
435 store_type& d =
get(dest).m_array;
436 const store_type& s =
get(src).m_array;
437 std::pair<const_iterator, const_iterator> its = get_iterator_pair(s, begin_pos, len);
438 d.assign(its.first, its.second);
441 static void prepend_values_from_block(
444 store_type& d =
get(dest).m_array;
445 const store_type& s =
get(src).m_array;
446 std::pair<const_iterator, const_iterator> its = get_iterator_pair(s, begin_pos, len);
447 #ifndef MDDS_MULTI_TYPE_VECTOR_USE_DEQUE
448 d.reserve(d.size() + len);
450 d.insert(d.begin(), its.first, its.second);
455 store_type& st1 =
get(blk1).m_array;
456 store_type& st2 =
get(blk2).m_array;
457 assert(pos1 + len <= st1.size());
458 assert(pos2 + len <= st2.size());
460 typename store_type::iterator it1 = st1.begin(), it2 = st2.begin();
461 std::advance(it1, pos1);
462 std::advance(it2, pos2);
463 for (
size_t i = 0; i < len; ++i, ++it1, ++it2)
465 #ifdef MDDS_MULTI_TYPE_VECTOR_USE_DEQUE
466 std::swap(*it1, *it2);
468 value_type v1 = *it1, v2 = *it2;
475 template<
typename _Iter>
476 static void set_values(
base_element_block& block,
size_t pos,
const _Iter& it_begin,
const _Iter& it_end)
478 store_type& d =
get(block).m_array;
479 typename store_type::iterator it_dest = d.begin();
480 std::advance(it_dest, pos);
481 for (_Iter it = it_begin; it != it_end; ++it, ++it_dest)
485 template<
typename _Iter>
486 static void append_values(
base_element_block& block,
const _Iter& it_begin,
const _Iter& it_end)
488 store_type& d =
get(block).m_array;
489 typename store_type::iterator it = d.end();
490 d.insert(it, it_begin, it_end);
493 template<
typename _Iter>
494 static void prepend_values(
base_element_block& block,
const _Iter& it_begin,
const _Iter& it_end)
496 store_type& d =
get(block).m_array;
497 d.insert(d.begin(), it_begin, it_end);
500 template<
typename _Iter>
501 static void assign_values(
base_element_block& dest,
const _Iter& it_begin,
const _Iter& it_end)
503 store_type& d =
get(dest).m_array;
504 d.assign(it_begin, it_end);
507 template<
typename _Iter>
508 static void insert_values(
base_element_block& block,
size_t pos,
const _Iter& it_begin,
const _Iter& it_end)
510 store_type& blk =
get(block).m_array;
511 blk.insert(blk.begin() + pos, it_begin, it_end);
516 #ifdef MDDS_MULTI_TYPE_VECTOR_USE_DEQUE
519 const store_type& blk =
get(block).m_array;
520 return blk.capacity();
526 #ifndef MDDS_MULTI_TYPE_VECTOR_USE_DEQUE
527 get(block).m_array.shrink_to_fit();
532 static std::pair<const_iterator, const_iterator> get_iterator_pair(
533 const store_type& array,
size_t begin_pos,
size_t len)
535 assert(begin_pos + len <= array.size());
536 const_iterator it = array.begin();
537 std::advance(it, begin_pos);
538 const_iterator it_end = it;
539 std::advance(it_end, len);
540 return std::pair<const_iterator, const_iterator>(it, it_end);
544 template<
typename _Self, element_t _TypeId,
typename _Data>
557 template<
typename _Iter>
562 using base_type::get;
567 return new _Self(
get(blk));
571 template<
typename _Self, element_t _TypeId,
typename _Data>
584 template<
typename _Iter>
614 template<element_t _TypeId,
typename _Data>
627 template<
typename _Iter>
631 static self_type* create_block_with_value(
size_t init_size,
const _Data& val)
633 return new self_type(init_size, val);
636 template<
typename _Iter>
637 static self_type* create_block_with_values(
const _Iter& it_begin,
const _Iter& it_end)
639 return new self_type(it_begin, it_end);
652 template<element_t _TypeId,
typename _Data>
658 using base_type::get;
659 using base_type::m_array;
660 using base_type::set_value;
668 #ifndef MDDS_MULTI_TYPE_VECTOR_USE_DEQUE
669 m_array.reserve(r.m_array.size());
671 typename managed_element_block::store_type::const_iterator it = r.m_array.begin(), it_end = r.m_array.end();
672 for (; it != it_end; ++it)
673 m_array.push_back(
new _Data(**it));
676 template<
typename _Iter>
682 std::for_each(m_array.begin(), m_array.end(), std::default_delete<_Data>());
685 static self_type* create_block_with_value(
size_t init_size, _Data* val)
689 throw general_error(
"You can't create a managed block with initial value.");
691 std::unique_ptr<self_type> blk = std::make_unique<self_type>(init_size);
693 set_value(*blk, 0, val);
695 return blk.release();
698 template<
typename _Iter>
699 static self_type* create_block_with_values(
const _Iter& it_begin,
const _Iter& it_end)
701 return new self_type(it_begin, it_end);
707 typename managed_element_block::store_type::iterator it = blk.m_array.begin() + pos;
708 typename managed_element_block::store_type::iterator it_end = it + len;
709 std::for_each(it, it_end, std::default_delete<_Data>());
713 template<element_t _TypeId,
typename _Data>
720 using base_type::get;
721 using base_type::m_array;
722 using base_type::set_value;
729 template<
typename _Iter>
735 std::for_each(m_array.begin(), m_array.end(), std::default_delete<_Data>());
738 static self_type* create_block_with_value(
size_t init_size, _Data* val)
742 throw general_error(
"You can't create a managed block with initial value.");
744 std::unique_ptr<self_type> blk = std::make_unique<self_type>(init_size);
746 set_value(*blk, 0, val);
748 return blk.release();
751 template<
typename _Iter>
752 static self_type* create_block_with_values(
const _Iter& it_begin,
const _Iter& it_end)
754 return new self_type(it_begin, it_end);
760 typename noncopyable_managed_element_block::store_type::iterator it = blk.m_array.begin() + pos;
761 typename noncopyable_managed_element_block::store_type::iterator it_end = it + len;
762 std::for_each(it, it_end, std::default_delete<_Data>());
Definition: types.hpp:159
Definition: types.hpp:714
const char * filepath
Definition: types.hpp:150
std::string function_args
Definition: types.hpp:147
Definition: types.hpp:545
Definition: types.hpp:129
Definition: types.hpp:653
int line_number
Definition: types.hpp:153
const void * instance
Definition: types.hpp:138
Definition: types.hpp:184
Definition: types.hpp:615
Definition: global.hpp:83
friend element_t get_block_type(const base_element_block &)
Definition: types.hpp:605
const char * function_name
Definition: types.hpp:141
Definition: flat_segment_tree.hpp:46
Definition: types.hpp:572
Definition: types.hpp:173