414class element_block :
public base_element_block
417 using store_type = StoreT<ValueT, std::allocator<ValueT>>;
418 static constexpr element_t block_type = TypeId;
423 element_block()
noexcept(std::is_nothrow_default_constructible_v<store_type>) : base_element_block(TypeId)
425 element_block(
size_t n) : base_element_block(TypeId), m_array(n)
427 element_block(
size_t n,
const ValueT& val) : base_element_block(TypeId), m_array(n, val)
430 template<
typename Iter>
431 element_block(
const Iter& it_begin,
const Iter& it_end) : base_element_block(TypeId), m_array(it_begin, it_end)
435 typedef typename store_type::iterator iterator;
436 typedef typename store_type::reverse_iterator reverse_iterator;
437 typedef typename store_type::const_iterator const_iterator;
438 typedef typename store_type::const_reverse_iterator const_reverse_iterator;
439 typedef ValueT value_type;
441 const store_type& store()
const noexcept
446 store_type& store()
noexcept
452 template<
bool Mutable>
453 class base_range_type
455 using elem_block_type = mdds::detail::mutable_t<base_element_block, Mutable>;
456 elem_block_type& m_blk;
459 using iter_type = std::conditional_t<Mutable, iterator, const_iterator>;
461 base_range_type(elem_block_type& blk) : m_blk(blk)
466 return element_block::begin(m_blk);
471 return element_block::end(m_blk);
475 static constexpr bool nothrow_eq_comparable_v =
noexcept(std::declval<store_type>() == std::declval<store_type>());
478 using range_type = base_range_type<true>;
479 using const_range_type = base_range_type<false>;
481 bool operator==(
const Self& r)
const noexcept(nothrow_eq_comparable_v)
483 return m_array == r.m_array;
486 bool operator!=(
const Self& r)
const noexcept(nothrow_eq_comparable_v)
488 return !operator==(r);
491 static const value_type& at(
const base_element_block& block,
typename store_type::size_type pos)
493 return get(block).m_array.at(pos);
496 static value_type& at(base_element_block& block,
typename store_type::size_type pos)
498 return get(block).m_array.at(pos);
501 static value_type* data(base_element_block& block)
503 return get(block).m_array.data();
506 static typename store_type::size_type size(
const base_element_block& block)
508 return get(block).m_array.size();
511 static iterator begin(base_element_block& block)
513 return get(block).m_array.begin();
516 static iterator end(base_element_block& block)
518 return get(block).m_array.end();
521 static const_iterator begin(
const base_element_block& block)
523 return get(block).m_array.begin();
526 static const_iterator end(
const base_element_block& block)
528 return get(block).m_array.end();
531 static const_iterator cbegin(
const base_element_block& block)
533 return get(block).m_array.begin();
536 static const_iterator cend(
const base_element_block& block)
538 return get(block).m_array.end();
541 static reverse_iterator rbegin(base_element_block& block)
543 return get(block).m_array.rbegin();
546 static reverse_iterator rend(base_element_block& block)
548 return get(block).m_array.rend();
551 static const_reverse_iterator rbegin(
const base_element_block& block)
553 return get(block).m_array.rbegin();
556 static const_reverse_iterator rend(
const base_element_block& block)
558 return get(block).m_array.rend();
561 static const_reverse_iterator crbegin(
const base_element_block& block)
563 return get(block).m_array.rbegin();
566 static const_reverse_iterator crend(
const base_element_block& block)
568 return get(block).m_array.rend();
571 static const_range_type range(
const base_element_block& block)
573 return const_range_type(block);
576 static range_type range(base_element_block& block)
578 return range_type(block);
581 static Self& get(base_element_block& block)
583#ifdef MDDS_MULTI_TYPE_VECTOR_DEBUG
586 std::ostringstream os;
587 os <<
"incorrect block type: expected block type=" << TypeId
592 return static_cast<Self&
>(block);
595 static const Self& get(
const base_element_block& block)
597#ifdef MDDS_MULTI_TYPE_VECTOR_DEBUG
600 std::ostringstream os;
601 os <<
"incorrect block type: expected block type=" << TypeId
606 return static_cast<const Self&
>(block);
609 static void set_value(base_element_block& blk,
size_t pos,
const ValueT& val)
611 get(blk).m_array[pos] = val;
614 static void get_value(
const base_element_block& blk,
size_t pos, ValueT& val)
616 val = get(blk).m_array[pos];
619 static value_type get_value(
const base_element_block& blk,
size_t pos)
621 return get(blk).m_array[pos];
624 template<
typename T = ValueT>
625 static void append_value(base_element_block& blk, T&& val)
627 get(blk).m_array.push_back(std::forward<T>(val));
630 template<
typename... Args>
631 static void emplace_back_value(base_element_block& blk, Args&&... args)
633 get(blk).m_array.emplace_back(std::forward<Args>(args)...);
636 static void prepend_value(base_element_block& blk,
const ValueT& val)
638 store_type& blk2 = get(blk).m_array;
639 blk2.insert(blk2.begin(), val);
642 static Self* create_block(
size_t init_size)
644 return new Self(init_size);
647 static void delete_block(
const base_element_block* p)
649 delete static_cast<const Self*
>(p);
652 static void resize_block(base_element_block& blk,
size_t new_size)
654 store_type& st = get(blk).m_array;
659 if (new_size < (detail::get_block_capacity(st) / 2))
660 detail::shrink_to_fit(st);
664 static void print_block(
const base_element_block& blk)
666 const store_type& blk2 = get(blk).m_array;
667 for (
const auto& val : blk2)
668 std::cout << val <<
" ";
670 std::cout << std::endl;
673 static void print_block(
const base_element_block&)
677 static void erase_value(base_element_block& blk,
size_t pos)
679 store_type& blk2 = get(blk).m_array;
680 blk2.erase(blk2.begin() + pos);
683 static void erase_values(base_element_block& blk,
size_t pos,
size_t size)
685 store_type& blk2 = get(blk).m_array;
686 blk2.erase(blk2.begin() + pos, blk2.begin() + pos + size);
689 static void append_block(base_element_block& dest,
const base_element_block& src)
691 store_type& d = get(dest).m_array;
692 const store_type& s = get(src).m_array;
693 d.insert(d.end(), s.begin(), s.end());
696 static void append_values_from_block(
697 base_element_block& dest,
const base_element_block& src,
size_t begin_pos,
size_t len)
699 store_type& d = get(dest).m_array;
700 const store_type& s = get(src).m_array;
701 std::pair<const_iterator, const_iterator> its = get_iterator_pair(s, begin_pos, len);
702 detail::reserve(d, d.size() + len);
703 d.insert(d.end(), its.first, its.second);
706 static void assign_values_from_block(
707 base_element_block& dest,
const base_element_block& src,
size_t begin_pos,
size_t len)
709 store_type& d = get(dest).m_array;
710 const store_type& s = get(src).m_array;
711 std::pair<const_iterator, const_iterator> its = get_iterator_pair(s, begin_pos, len);
712 d.assign(its.first, its.second);
715 static void prepend_values_from_block(
716 base_element_block& dest,
const base_element_block& src,
size_t begin_pos,
size_t len)
718 store_type& d = get(dest).m_array;
719 const store_type& s = get(src).m_array;
720 std::pair<const_iterator, const_iterator> its = get_iterator_pair(s, begin_pos, len);
721 detail::reserve(d, d.size() + len);
722 d.insert(d.begin(), its.first, its.second);
725 static void swap_values(base_element_block& blk1, base_element_block& blk2,
size_t pos1,
size_t pos2,
size_t len)
727 store_type& st1 = get(blk1).m_array;
728 store_type& st2 = get(blk2).m_array;
729 assert(pos1 + len <= st1.size());
730 assert(pos2 + len <= st2.size());
732 typename store_type::iterator it1 = st1.begin(), it2 = st2.begin();
733 std::advance(it1, pos1);
734 std::advance(it2, pos2);
736 for (
size_t i = 0; i < len; ++i, ++it1, ++it2)
737 std::iter_swap(it1, it2);
740 static bool equal_block(
const base_element_block& left,
const base_element_block& right)
742 return get(left) == get(right);
745 template<
typename Iter>
746 static void set_values(base_element_block& block,
size_t pos,
const Iter& it_begin,
const Iter& it_end)
748 store_type& d = get(block).m_array;
749 typename store_type::iterator it_dest = d.begin();
750 std::advance(it_dest, pos);
751 for (Iter it = it_begin; it != it_end; ++it, ++it_dest)
755 template<
typename Iter>
756 static void append_values(base_element_block& block,
const Iter& it_begin,
const Iter& it_end)
758 store_type& d = get(block).m_array;
759 typename store_type::iterator it = d.end();
760 d.insert(it, it_begin, it_end);
763 template<
typename Iter>
764 static void prepend_values(base_element_block& block,
const Iter& it_begin,
const Iter& it_end)
766 store_type& d = get(block).m_array;
767 d.insert(d.begin(), it_begin, it_end);
770 template<
typename Iter>
771 static void assign_values(base_element_block& dest,
const Iter& it_begin,
const Iter& it_end)
773 store_type& d = get(dest).m_array;
774 d.assign(it_begin, it_end);
777 template<
typename Iter>
778 static void insert_values(base_element_block& block,
size_t pos,
const Iter& it_begin,
const Iter& it_end)
780 store_type& blk = get(block).m_array;
781 blk.insert(blk.begin() + pos, it_begin, it_end);
784 static size_t capacity(
const base_element_block& block)
786 const store_type& blk = get(block).m_array;
787 return detail::get_block_capacity(blk);
790 static void reserve(base_element_block& block, std::size_t size)
792 store_type& blk = get(block).m_array;
793 detail::reserve(blk, size);
796 static void shrink_to_fit(base_element_block& block)
798 store_type& blk = get(block).m_array;
799 detail::shrink_to_fit(blk);
803 static std::pair<const_iterator, const_iterator> get_iterator_pair(
804 const store_type& array,
size_t begin_pos,
size_t len)
806 assert(begin_pos + len <= array.size());
807 const_iterator it = array.begin();
808 std::advance(it, begin_pos);
809 const_iterator it_end = it;
810 std::advance(it_end, len);
811 return std::pair<const_iterator, const_iterator>(it, it_end);