mdds
Loading...
Searching...
No Matches
types.hpp
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3// SPDX-FileCopyrightText: 2021 - 2025 Kohei Yoshida
4//
5// SPDX-License-Identifier: MIT
6
7#pragma once
8
9#include "../global.hpp"
10#include "./types_util.hpp"
11#include "./delayed_delete_vector.hpp"
12
13#include <algorithm>
14#include <atomic>
15#include <cassert>
16#include <concepts>
17#include <memory>
18#include <cstddef>
19#include <cstdint>
20#include <vector>
21#include <ostream>
22#include <sstream>
23#include <tuple>
24#include <type_traits>
25
26#if defined(MDDS_UNIT_TEST) || defined(MDDS_MULTI_TYPE_VECTOR_DEBUG)
27#include <iostream>
28#endif
29
30namespace mdds { namespace mtv {
31
32using element_t = int;
33
34constexpr element_t element_type_empty = -1;
35
36constexpr element_t element_type_reserved_start = 0;
37constexpr element_t element_type_reserved_end = 49;
38
39constexpr element_t element_type_user_start = 50;
40
47enum class lu_factor_t : int
48{
49 none = 0,
50 lu4 = 4,
51 lu8 = 8,
52 lu16 = 16,
53 lu32 = 32,
54 sse2_x64 = 1 << 8,
55 sse2_x64_lu4 = 1 << 8 | 4,
56 sse2_x64_lu8 = 1 << 8 | 8,
57 sse2_x64_lu16 = 1 << 8 | 16,
58 avx2_x64 = 2 << 8,
59 avx2_x64_lu4 = 2 << 8 | 4,
60 avx2_x64_lu8 = 2 << 8 | 8,
61};
62
82enum class trace_method_t : int
83{
84 unspecified = 0,
85 accessor = 1,
86 accessor_with_pos_hint = 1 << 8 | 1,
87 mutator = 2,
88 mutator_with_pos_hint = 1 << 8 | 2,
89 constructor = 3,
90 destructor = 4
91};
92
97{
98 trace_method_t type = trace_method_t::unspecified;
99
105 const void* instance = nullptr;
106
108 const char* function_name = nullptr;
109
114 std::string function_args;
115
117 const char* filepath = nullptr;
118
120 int line_number = -1;
121};
122
126class element_block_error : public mdds::general_error
127{
128public:
129 element_block_error(const std::string& msg) : mdds::general_error(msg)
130 {}
131};
132
139class shared_block_error : public mdds::general_error
140{
141public:
142 shared_block_error(const std::string& msg) : mdds::general_error(msg)
143 {}
144};
145
157using container_id_t = std::uint64_t;
158
159namespace detail {
160
165inline container_id_t next_container_id() noexcept
166{
167 static std::atomic<container_id_t> counter{1}; // start with value 1
168 return counter.fetch_add(1, std::memory_order_relaxed);
169}
170
176template<typename T>
177concept has_position_hint_data = requires(const T& it) {
178 { it.get_node().__private_data.container_id } -> std::convertible_to<container_id_t>;
179 { it.get_node().__private_data.block_index } -> std::convertible_to<std::size_t>;
180};
181
182} // namespace detail
183
199struct position_hint
200{
201 container_id_t container_id = 0;
202 std::size_t block_index = 0;
203
204 position_hint() noexcept = default;
205
211 template<detail::has_position_hint_data Iter>
212 position_hint(const Iter& it) noexcept
213 : container_id(it.get_node().__private_data.container_id), block_index(it.get_node().__private_data.block_index)
214 {}
215};
216
217inline std::ostream& operator<<(std::ostream& os, const position_hint& hint)
218{
219 os << "{position-hint: container-id=" << hint.container_id << "; block-index=" << hint.block_index << "}";
220 return os;
221}
222
223class base_element_block;
224element_t get_block_type(const base_element_block&) noexcept;
225
230class base_element_block
231{
232 friend element_t get_block_type(const base_element_block&) noexcept;
233
234protected:
235 element_t type;
236 base_element_block(element_t _t) noexcept : type(_t)
237 {}
238};
239
260template<typename ValueT>
262
263namespace detail {
264
265template<typename ValueT, typename = void>
266inline constexpr bool has_clone_value = false;
267
268template<typename ValueT>
269inline constexpr bool has_clone_value<ValueT, std::void_t<decltype(clone_value<ValueT>{})>> = true;
270
274template<typename T>
275inline constexpr bool is_clone_value_v = false;
276
277template<typename ValueT>
278inline constexpr bool is_clone_value_v<clone_value<ValueT>> = true;
279
280} // namespace detail
281
289template<typename CV>
290concept stateful_clone_value = detail::is_clone_value_v<CV> && !detail::has_exec_policy<CV>;
291
298template<typename BlockT, typename = void>
300{
301 using store_type = typename BlockT::store_type;
302
303 BlockT* operator()(const BlockT& blk) const
304 {
305 auto bt = get_block_type(blk);
306 std::ostringstream os;
307 os << "cloning of this block type is not implemented (type=" << bt << ")";
308 throw element_block_error(os.str());
309 }
310};
311
317template<typename BlockT>
318 requires std::copy_constructible<BlockT>
319struct clone_block<BlockT>
320{
321 using store_type = typename BlockT::store_type;
322
323 BlockT* operator()(const BlockT& src) const
324 {
325 return new BlockT(src);
326 }
327};
328
341template<typename BlockT>
342 requires(!std::copy_constructible<BlockT>)
343struct clone_block<BlockT, std::void_t<decltype(clone_value<typename BlockT::value_type>{})>>
344{
345 using store_type = typename BlockT::store_type;
346 using value_type = typename BlockT::value_type;
347 using CV = clone_value<value_type>;
348
362 template<typename... Passed>
363 BlockT* operator()(const BlockT& src, Passed&... passed) const
364 {
365 if constexpr (detail::has_exec_policy<CV>)
366 {
367 // NB: Stateful-ness is inferred from operator() being non-const;
368 // it's possible that the user can still declare operator() const
369 // and mutate its state by marking a data member mutable.
370 static_assert(
371 std::is_invocable_r_v<value_type, const CV&, const value_type&>,
372 "a clone_value with an exec_policy must be stateless (const-invocable)");
373
374 auto dest_blk = std::make_unique<BlockT>(src.store().size());
375 std::transform(
376 typename CV::exec_policy{}, src.store().begin(), src.store().end(), dest_blk->store().begin(), CV{});
377
378 return dest_blk.release();
379 }
380 else
381 {
382 auto dest_blk = std::make_unique<BlockT>();
383 auto& dest_store = dest_blk->store();
384 detail::reserve(dest_store, src.store().size());
385
386 // check if the clone_value type is the type of one of the passed instances
387 if constexpr ((std::is_same_v<CV, Passed> || ...))
388 {
389 // pick the right type from the passed instances
390 CV& cloner = std::get<CV&>(std::forward_as_tuple(passed...));
391 clone_values(src, dest_store, cloner);
392 }
393 else
394 {
395 // One cloner cloning the stored values in order, so that it may
396 // carry state across the elements of the block.
397 CV cloner;
398 clone_values(src, dest_store, cloner);
399 }
400
401 return dest_blk.release();
402 }
403 }
404
405private:
406 static void clone_values(const BlockT& src, store_type& dest_store, CV& cloner)
407 {
408 for (const auto& v : src.store())
409 dest_store.push_back(cloner(v));
410 }
411};
412
413template<typename Self, element_t TypeId, typename ValueT, template<typename, typename> class StoreT>
414class element_block : public base_element_block
415{
416public:
417 using store_type = StoreT<ValueT, std::allocator<ValueT>>;
418 static constexpr element_t block_type = TypeId;
419
420protected:
421 store_type m_array;
422
423 element_block() noexcept(std::is_nothrow_default_constructible_v<store_type>) : base_element_block(TypeId)
424 {}
425 element_block(size_t n) : base_element_block(TypeId), m_array(n)
426 {}
427 element_block(size_t n, const ValueT& val) : base_element_block(TypeId), m_array(n, val)
428 {}
429
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)
432 {}
433
434public:
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;
440
441 const store_type& store() const noexcept
442 {
443 return m_array;
444 }
445
446 store_type& store() noexcept
447 {
448 return m_array;
449 }
450
451private:
452 template<bool Mutable>
453 class base_range_type
454 {
455 using elem_block_type = mdds::detail::mutable_t<base_element_block, Mutable>;
456 elem_block_type& m_blk;
457
458 public:
459 using iter_type = std::conditional_t<Mutable, iterator, const_iterator>;
460
461 base_range_type(elem_block_type& blk) : m_blk(blk)
462 {}
463
464 iter_type begin()
465 {
466 return element_block::begin(m_blk);
467 }
468
469 iter_type end()
470 {
471 return element_block::end(m_blk);
472 }
473 };
474
475 static constexpr bool nothrow_eq_comparable_v = noexcept(std::declval<store_type>() == std::declval<store_type>());
476
477public:
478 using range_type = base_range_type<true>;
479 using const_range_type = base_range_type<false>;
480
481 bool operator==(const Self& r) const noexcept(nothrow_eq_comparable_v)
482 {
483 return m_array == r.m_array;
484 }
485
486 bool operator!=(const Self& r) const noexcept(nothrow_eq_comparable_v)
487 {
488 return !operator==(r);
489 }
490
491 static const value_type& at(const base_element_block& block, typename store_type::size_type pos)
492 {
493 return get(block).m_array.at(pos);
494 }
495
496 static value_type& at(base_element_block& block, typename store_type::size_type pos)
497 {
498 return get(block).m_array.at(pos);
499 }
500
501 static value_type* data(base_element_block& block)
502 {
503 return get(block).m_array.data();
504 }
505
506 static typename store_type::size_type size(const base_element_block& block)
507 {
508 return get(block).m_array.size();
509 }
510
511 static iterator begin(base_element_block& block)
512 {
513 return get(block).m_array.begin();
514 }
515
516 static iterator end(base_element_block& block)
517 {
518 return get(block).m_array.end();
519 }
520
521 static const_iterator begin(const base_element_block& block)
522 {
523 return get(block).m_array.begin();
524 }
525
526 static const_iterator end(const base_element_block& block)
527 {
528 return get(block).m_array.end();
529 }
530
531 static const_iterator cbegin(const base_element_block& block)
532 {
533 return get(block).m_array.begin();
534 }
535
536 static const_iterator cend(const base_element_block& block)
537 {
538 return get(block).m_array.end();
539 }
540
541 static reverse_iterator rbegin(base_element_block& block)
542 {
543 return get(block).m_array.rbegin();
544 }
545
546 static reverse_iterator rend(base_element_block& block)
547 {
548 return get(block).m_array.rend();
549 }
550
551 static const_reverse_iterator rbegin(const base_element_block& block)
552 {
553 return get(block).m_array.rbegin();
554 }
555
556 static const_reverse_iterator rend(const base_element_block& block)
557 {
558 return get(block).m_array.rend();
559 }
560
561 static const_reverse_iterator crbegin(const base_element_block& block)
562 {
563 return get(block).m_array.rbegin();
564 }
565
566 static const_reverse_iterator crend(const base_element_block& block)
567 {
568 return get(block).m_array.rend();
569 }
570
571 static const_range_type range(const base_element_block& block)
572 {
573 return const_range_type(block);
574 }
575
576 static range_type range(base_element_block& block)
577 {
578 return range_type(block);
579 }
580
581 static Self& get(base_element_block& block)
582 {
583#ifdef MDDS_MULTI_TYPE_VECTOR_DEBUG
584 if (get_block_type(block) != TypeId)
585 {
586 std::ostringstream os;
587 os << "incorrect block type: expected block type=" << TypeId
588 << ", passed block type=" << get_block_type(block);
589 throw general_error(os.str());
590 }
591#endif
592 return static_cast<Self&>(block);
593 }
594
595 static const Self& get(const base_element_block& block)
596 {
597#ifdef MDDS_MULTI_TYPE_VECTOR_DEBUG
598 if (get_block_type(block) != TypeId)
599 {
600 std::ostringstream os;
601 os << "incorrect block type: expected block type=" << TypeId
602 << ", passed block type=" << get_block_type(block);
603 throw general_error(os.str());
604 }
605#endif
606 return static_cast<const Self&>(block);
607 }
608
609 static void set_value(base_element_block& blk, size_t pos, const ValueT& val)
610 {
611 get(blk).m_array[pos] = val;
612 }
613
614 static void get_value(const base_element_block& blk, size_t pos, ValueT& val)
615 {
616 val = get(blk).m_array[pos];
617 }
618
619 static value_type get_value(const base_element_block& blk, size_t pos)
620 {
621 return get(blk).m_array[pos];
622 }
623
624 template<typename T = ValueT>
625 static void append_value(base_element_block& blk, T&& val)
626 {
627 get(blk).m_array.push_back(std::forward<T>(val));
628 }
629
630 template<typename... Args>
631 static void emplace_back_value(base_element_block& blk, Args&&... args)
632 {
633 get(blk).m_array.emplace_back(std::forward<Args>(args)...);
634 }
635
636 static void prepend_value(base_element_block& blk, const ValueT& val)
637 {
638 store_type& blk2 = get(blk).m_array;
639 blk2.insert(blk2.begin(), val);
640 }
641
642 static Self* create_block(size_t init_size)
643 {
644 return new Self(init_size);
645 }
646
647 static void delete_block(const base_element_block* p)
648 {
649 delete static_cast<const Self*>(p);
650 }
651
652 static void resize_block(base_element_block& blk, size_t new_size)
653 {
654 store_type& st = get(blk).m_array;
655 st.resize(new_size);
656
657 // Test if the vector's capacity is larger than twice its current
658 // size, and if so, shrink its capacity to free up some memory.
659 if (new_size < (detail::get_block_capacity(st) / 2))
660 detail::shrink_to_fit(st);
661 }
662
663#ifdef MDDS_UNIT_TEST
664 static void print_block(const base_element_block& blk)
665 {
666 const store_type& blk2 = get(blk).m_array;
667 for (const auto& val : blk2)
668 std::cout << val << " ";
669
670 std::cout << std::endl;
671 }
672#else
673 static void print_block(const base_element_block&)
674 {}
675#endif
676
677 static void erase_value(base_element_block& blk, size_t pos)
678 {
679 store_type& blk2 = get(blk).m_array;
680 blk2.erase(blk2.begin() + pos);
681 }
682
683 static void erase_values(base_element_block& blk, size_t pos, size_t size)
684 {
685 store_type& blk2 = get(blk).m_array;
686 blk2.erase(blk2.begin() + pos, blk2.begin() + pos + size);
687 }
688
689 static void append_block(base_element_block& dest, const base_element_block& src)
690 {
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());
694 }
695
696 static void append_values_from_block(
697 base_element_block& dest, const base_element_block& src, size_t begin_pos, size_t len)
698 {
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);
704 }
705
706 static void assign_values_from_block(
707 base_element_block& dest, const base_element_block& src, size_t begin_pos, size_t len)
708 {
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);
713 }
714
715 static void prepend_values_from_block(
716 base_element_block& dest, const base_element_block& src, size_t begin_pos, size_t len)
717 {
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);
723 }
724
725 static void swap_values(base_element_block& blk1, base_element_block& blk2, size_t pos1, size_t pos2, size_t len)
726 {
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());
731
732 typename store_type::iterator it1 = st1.begin(), it2 = st2.begin();
733 std::advance(it1, pos1);
734 std::advance(it2, pos2);
735
736 for (size_t i = 0; i < len; ++i, ++it1, ++it2)
737 std::iter_swap(it1, it2);
738 }
739
740 static bool equal_block(const base_element_block& left, const base_element_block& right)
741 {
742 return get(left) == get(right);
743 }
744
745 template<typename Iter>
746 static void set_values(base_element_block& block, size_t pos, const Iter& it_begin, const Iter& it_end)
747 {
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)
752 *it_dest = *it;
753 }
754
755 template<typename Iter>
756 static void append_values(base_element_block& block, const Iter& it_begin, const Iter& it_end)
757 {
758 store_type& d = get(block).m_array;
759 typename store_type::iterator it = d.end();
760 d.insert(it, it_begin, it_end);
761 }
762
763 template<typename Iter>
764 static void prepend_values(base_element_block& block, const Iter& it_begin, const Iter& it_end)
765 {
766 store_type& d = get(block).m_array;
767 d.insert(d.begin(), it_begin, it_end);
768 }
769
770 template<typename Iter>
771 static void assign_values(base_element_block& dest, const Iter& it_begin, const Iter& it_end)
772 {
773 store_type& d = get(dest).m_array;
774 d.assign(it_begin, it_end);
775 }
776
777 template<typename Iter>
778 static void insert_values(base_element_block& block, size_t pos, const Iter& it_begin, const Iter& it_end)
779 {
780 store_type& blk = get(block).m_array;
781 blk.insert(blk.begin() + pos, it_begin, it_end);
782 }
783
784 static size_t capacity(const base_element_block& block)
785 {
786 const store_type& blk = get(block).m_array;
787 return detail::get_block_capacity(blk);
788 }
789
790 static void reserve(base_element_block& block, std::size_t size)
791 {
792 store_type& blk = get(block).m_array;
793 detail::reserve(blk, size);
794 }
795
796 static void shrink_to_fit(base_element_block& block)
797 {
798 store_type& blk = get(block).m_array;
799 detail::shrink_to_fit(blk);
800 }
801
802private:
803 static std::pair<const_iterator, const_iterator> get_iterator_pair(
804 const store_type& array, size_t begin_pos, size_t len)
805 {
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);
812 }
813};
814
815template<typename Self, element_t TypeId, typename ValueT, template<typename, typename> class StoreT>
816class copyable_element_block : public element_block<Self, TypeId, ValueT, StoreT>
817{
818 using base_type = element_block<Self, TypeId, ValueT, StoreT>;
819
820 static_assert(
821 !detail::has_clone_value<ValueT>,
822 "clone_value or clone_block must not be specialized for a copyable element block's value "
823 "type; it is reserved for non-copyable blocks, which are duplicated via clone.");
824
825protected:
826 copyable_element_block() noexcept(std::is_nothrow_default_constructible_v<base_type>) : base_type()
827 {}
828 copyable_element_block(size_t n) : base_type(n)
829 {}
830 copyable_element_block(size_t n, const ValueT& val) : base_type(n, val)
831 {}
832
833 template<typename Iter>
834 copyable_element_block(const Iter& it_begin, const Iter& it_end) : base_type(it_begin, it_end)
835 {}
836
837public:
838 using base_type::get;
839
840 static Self* copy_block(const base_element_block& blk)
841 {
842 // Use copy constructor to copy the data.
843 return new Self(get(blk));
844 }
845
851 template<typename... Passed>
852 static Self* clone_block(const base_element_block& src, Passed&... passed)
853 {
854 if constexpr (std::is_invocable_v<const mdds::mtv::clone_block<Self>&, const Self&, Passed&...>)
855 return mdds::mtv::clone_block<Self>{}(get(src), passed...);
856 else
857 return mdds::mtv::clone_block<Self>{}(get(src));
858 }
859};
860
864template<typename Self, element_t TypeId, typename ValueT, template<typename, typename> class StoreT>
865class noncopyable_element_block : public element_block<Self, TypeId, ValueT, StoreT>
866{
867 using base_type = element_block<Self, TypeId, ValueT, StoreT>;
868
869protected:
870 noncopyable_element_block() noexcept(std::is_nothrow_default_constructible_v<base_type>) : base_type()
871 {}
872 noncopyable_element_block(size_t n) : base_type(n)
873 {}
874 noncopyable_element_block(size_t n, const ValueT& val) : base_type(n, val)
875 {}
876
877 template<typename Iter>
878 noncopyable_element_block(const Iter& it_begin, const Iter& it_end) : base_type(it_begin, it_end)
879 {}
880
881public:
882 using base_type::get;
883
884 noncopyable_element_block(const noncopyable_element_block&) = delete;
885 noncopyable_element_block& operator=(const noncopyable_element_block&) = delete;
886
887 static Self* copy_block(const base_element_block&)
888 {
889 throw element_block_error("attempted to copy a noncopyable element block");
890 }
891
897 template<typename... Passed>
898 static Self* clone_block(const base_element_block& src, Passed&... passed)
899 {
900 if constexpr (std::is_invocable_v<const mdds::mtv::clone_block<Self>&, const Self&, Passed&...>)
901 return mdds::mtv::clone_block<Self>{}(get(src), passed...);
902 else
903 return mdds::mtv::clone_block<Self>{}(get(src));
904 }
905};
906
914inline element_t get_block_type(const base_element_block& blk) noexcept
915{
916 return blk.type;
917}
918
923template<element_t TypeId, typename ValueT, template<typename, typename> class StoreT = delayed_delete_vector>
924struct default_element_block
925 : public copyable_element_block<default_element_block<TypeId, ValueT, StoreT>, TypeId, ValueT, StoreT>
926{
927 using self_type = default_element_block<TypeId, ValueT, StoreT>;
928 using base_type = copyable_element_block<self_type, TypeId, ValueT, StoreT>;
929
930 default_element_block() noexcept(std::is_nothrow_default_constructible_v<base_type>) : base_type()
931 {}
932 default_element_block(size_t n) : base_type(n)
933 {}
934 default_element_block(size_t n, const ValueT& val) : base_type(n, val)
935 {}
936
937 template<typename Iter>
938 default_element_block(const Iter& it_begin, const Iter& it_end) : base_type(it_begin, it_end)
939 {}
940
941 static self_type* create_block_with_value(size_t init_size, const ValueT& val)
942 {
943 return new self_type(init_size, val);
944 }
945
946 template<typename Iter>
947 static self_type* create_block_with_values(const Iter& it_begin, const Iter& it_end)
948 {
949 return new self_type(it_begin, it_end);
950 }
951
952 static void overwrite_values(base_element_block&, size_t, size_t)
953 {
954 // Do nothing.
955 }
956};
957
962template<element_t TypeId, typename ValueT, template<typename, typename> class StoreT = delayed_delete_vector>
963struct managed_element_block
964 : public copyable_element_block<managed_element_block<TypeId, ValueT, StoreT>, TypeId, ValueT*, StoreT>
965{
966 using self_type = managed_element_block<TypeId, ValueT, StoreT>;
967 using base_type = copyable_element_block<self_type, TypeId, ValueT*, StoreT>;
968
969 using base_type::get;
970 using base_type::m_array;
971 using base_type::reserve;
972 using base_type::set_value;
973
974 managed_element_block() noexcept(std::is_nothrow_default_constructible_v<base_type>) : base_type()
975 {}
976 managed_element_block(size_t n) : base_type(n)
977 {}
978 managed_element_block(const managed_element_block& r) : base_type()
979 {
980 detail::reserve(m_array, r.m_array.size());
981 for (const auto& v : r.m_array)
982 m_array.push_back(new ValueT(*v));
983 }
984
985 template<typename Iter>
986 managed_element_block(const Iter& it_begin, const Iter& it_end) : base_type(it_begin, it_end)
987 {}
988
989 ~managed_element_block()
990 {
991 std::for_each(m_array.begin(), m_array.end(), std::default_delete<ValueT>());
992 }
993
994 static self_type* create_block_with_value(size_t init_size, ValueT* val)
995 {
996 // Managed blocks don't support initialization with value.
997 if (init_size > 1)
998 throw general_error("You can't create a managed block with initial value.");
999
1000 std::unique_ptr<self_type> blk = std::make_unique<self_type>(init_size);
1001 if (init_size == 1)
1002 set_value(*blk, 0, val);
1003
1004 return blk.release();
1005 }
1006
1007 template<typename Iter>
1008 static self_type* create_block_with_values(const Iter& it_begin, const Iter& it_end)
1009 {
1010 return new self_type(it_begin, it_end);
1011 }
1012
1013 static void overwrite_values(base_element_block& block, size_t pos, size_t len)
1014 {
1015 managed_element_block& blk = get(block);
1016 typename managed_element_block::store_type::iterator it = blk.m_array.begin() + pos;
1017 typename managed_element_block::store_type::iterator it_end = it + len;
1018 std::for_each(it, it_end, std::default_delete<ValueT>());
1019 }
1020};
1021
1027template<element_t TypeId, typename ValueT, template<typename, typename> class StoreT = delayed_delete_vector>
1028struct noncopyable_managed_element_block
1029 : public noncopyable_element_block<
1030 noncopyable_managed_element_block<TypeId, ValueT, StoreT>, TypeId, ValueT*, StoreT>
1031{
1032 using self_type = noncopyable_managed_element_block<TypeId, ValueT, StoreT>;
1033 using base_type = noncopyable_element_block<self_type, TypeId, ValueT*, StoreT>;
1034
1035 using base_type::get;
1036 using base_type::m_array;
1037 using base_type::set_value;
1038
1039 noncopyable_managed_element_block() noexcept(std::is_nothrow_default_constructible_v<base_type>) : base_type()
1040 {}
1041 noncopyable_managed_element_block(size_t n) : base_type(n)
1042 {}
1043
1044 template<typename Iter>
1045 noncopyable_managed_element_block(const Iter& it_begin, const Iter& it_end) : base_type(it_begin, it_end)
1046 {}
1047
1048 ~noncopyable_managed_element_block()
1049 {
1050 std::for_each(m_array.begin(), m_array.end(), std::default_delete<ValueT>());
1051 }
1052
1053 static self_type* create_block_with_value(size_t init_size, ValueT* val)
1054 {
1055 // Managed blocks don't support initialization with value.
1056 if (init_size > 1)
1057 throw general_error("You can't create a managed block with initial value.");
1058
1059 std::unique_ptr<self_type> blk = std::make_unique<self_type>(init_size);
1060 if (init_size == 1)
1061 set_value(*blk, 0, val);
1062
1063 return blk.release();
1064 }
1065
1066 template<typename Iter>
1067 static self_type* create_block_with_values(const Iter& it_begin, const Iter& it_end)
1068 {
1069 return new self_type(it_begin, it_end);
1070 }
1071
1072 static void overwrite_values(base_element_block& block, size_t pos, size_t len)
1073 {
1074 noncopyable_managed_element_block& blk = get(block);
1075 typename noncopyable_managed_element_block::store_type::iterator it = blk.m_array.begin() + pos;
1076 typename noncopyable_managed_element_block::store_type::iterator it_end = it + len;
1077 std::for_each(it, it_end, std::default_delete<ValueT>());
1078 }
1079
1080 static bool equal_block(const base_element_block& left, const base_element_block& right)
1081 {
1082 const auto& array1 = get(left).m_array;
1083 const auto& array2 = get(right).m_array;
1084
1085 if (array1.size() != array2.size())
1086 return false;
1087
1088 return std::equal(array1.begin(), array1.end(), array2.begin(), [](const ValueT* p1, const ValueT* p2) {
1089 return *p1 == *p2;
1090 });
1091 }
1092};
1093
1094namespace detail {
1095
1096template<typename Blk>
1097bool get_block_element_at(const mdds::mtv::base_element_block& data, size_t offset, std::true_type)
1098{
1099 auto it = Blk::cbegin(data);
1100 std::advance(it, offset);
1101 return *it;
1102}
1103
1104template<typename Blk>
1105typename Blk::value_type get_block_element_at(const mdds::mtv::base_element_block& data, size_t offset, std::false_type)
1106{
1107 return Blk::at(data, offset);
1108}
1109
1110template<typename Blk>
1111typename Blk::value_type get_block_element_at(const mdds::mtv::base_element_block& data, size_t offset)
1112{
1113 typename mdds::mtv::detail::has_std_vector_bool_store<Blk>::type v;
1114 return get_block_element_at<Blk>(data, offset, v);
1115}
1116
1117} // namespace detail
1118
1119}} // namespace mdds::mtv
1120
1121/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Definition global.hpp:54
Definition types.hpp:231
friend element_t get_block_type(const base_element_block &) noexcept
Definition types.hpp:914
static Self * clone_block(const base_element_block &src, Passed &... passed)
Definition types.hpp:852
Definition delayed_delete_vector.hpp:22
Definition types.hpp:127
static Self * clone_block(const base_element_block &src, Passed &... passed)
Definition types.hpp:898
Definition types_util.hpp:71
Definition types.hpp:290
BlockT * operator()(const BlockT &src, Passed &... passed) const
Definition types.hpp:363
Definition types.hpp:300
Definition types.hpp:261
position_hint(const Iter &it) noexcept
Definition types.hpp:212
std::string function_args
Definition types.hpp:114
const char * function_name
Definition types.hpp:108
int line_number
Definition types.hpp:120
const void * instance
Definition types.hpp:105
const char * filepath
Definition types.hpp:117