14 template <
typename T,
typename Allocator>
27 using index_type = std::uint32_t;
30 static constexpr index_type null_value = 0;
44 template <
typename Container>
47 template <
typename T1,
typename Allocator1>
50 template <
typename Container1>
53 template <
typename Container1,
typename Container2>
56 template <
typename Container1,
typename Container2>
60 using container = Container;
61 using node_type =
typename container::value_type;
62 using index_type =
typename node_type::index_type;
64 static constexpr index_type null_value = node_type::null_value;
67 using value_type =
typename node_type::value_type;
68 using difference_type = std::ptrdiff_t;
69 using reference = value_type&;
70 using const_reference = value_type
const&;
71 using pointer = value_type*;
72 using const_pointer = value_type
const*;
73 using iterator_category = std::bidirectional_iterator_tag;
75 using reverse_iterator = std::reverse_iterator<tree_node_iterator<container>>;
82 , m_parent(null_value)
93 : m_c(std::addressof(c))
106 result.m_parent = m_parent;
107 result.m_index = m_index;
115 assert(m_index != null_value);
117 return (m_c->data() + m_index)->m_value;
124 assert(m_index != null_value);
126 return std::addressof((m_c->data() + m_index)->m_value);
133 assert(m_index != null_value);
135 m_index = (m_c->data() + m_index)->m_next;
144 assert(m_index != null_value);
156 auto const* d = m_c->data();
160 assert(m_parent != 0 && (d + m_parent)->m_child != m_index);
161 m_index = (d + m_index)->m_prev;
163 else if (m_parent != 0)
165 index_type child = (d + m_parent)->m_child;
166 m_index = (d + child)->m_prev;
170 m_index = d->m_child;
171 assert(m_index != 0);
179 tree_node_iterator operator--(
int)
190 assert(m_index != null_value);
198 assert(m_index != null_value);
206 return std::make_reverse_iterator(
end());
213 return std::make_reverse_iterator(
begin());
224 auto* d = m_c->data();
226 index_type p = m_parent;
227 index_type pp = (p == null_value) ? null_value : (d + p)->m_parent;
238 template <
typename Container1,
typename Container2>
239 inline bool operator==(tree_node_iterator<Container1>
const& lhs, tree_node_iterator<Container2>
const& rhs)
241 return !(lhs != rhs);
244 template <
typename Container1,
typename Container2>
245 inline bool operator!=(tree_node_iterator<Container1>
const& lhs, tree_node_iterator<Container2>
const& rhs)
248 Container1
const* c1 =
const_cast<Container1 const*
>(lhs.m_c);
249 Container2
const* c2 =
const_cast<Container1 const*
>(lhs.m_c);
250 assert(c1 == c2 || c1 ==
nullptr || c2 ==
nullptr);
253 return lhs.m_index != rhs.m_index || lhs.m_parent != rhs.m_parent;
333 template <
typename T,
typename Allocator = std::allocator<detail::tree_node<T>>>
336 template <
typename Container1>
337 friend class detail::tree_node_iterator;
340 using node_type = detail::tree_node<T>;
341 using index_type =
typename node_type::index_type;
342 using container = std::vector<node_type, Allocator>;
344 static constexpr index_type null_value = node_type::null_value;
348 using value_type = T;
349 using allocator_type = Allocator;
350 using size_type = std::size_t;
351 using difference_type = std::ptrdiff_t;
352 using reference = value_type&;
353 using const_reference = value_type
const&;
354 using pointer = value_type*;
355 using const_pointer = value_type
const*;
356 using iterator = detail::tree_node_iterator<container>;
357 using const_iterator = detail::tree_node_iterator<container const>;
371 explicit tree(allocator_type
const& alloc)
381 explicit tree(value_type
const& value, allocator_type
const& alloc = allocator_type())
392 explicit tree(value_type&& value, allocator_type
const& alloc = allocator_type())
398 void assign(value_type
const& value)
404 void assign(value_type&& value)
410 allocator_type get_allocator()
const {
return m_c.get_allocator(); }
420 const_reference front()
const
426 reference back() {
return front(); }
428 const_reference back()
const {
return front(); }
503 index_type idx = pos.m_index;
504 if (idx == 0)
return 0;
510 std::vector<index_type> stack(1, idx);
511 while (!stack.empty())
513 index_type parent = stack.back();
517 for (index_type i = (d + parent)->m_child; i != 0; i = (d + i)->m_next) stack.push_back(i);
520 return static_cast<std::make_unsigned_t<index_type>
>(n);
527 size_type
max_size() const noexcept {
return m_c.max_size(); }
539 void swap(
tree& other) { m_c.swap(other.m_c); }
553 index_type idx =
allocate(std::move(value));
555 link(pos.m_parent, pos.m_index, idx);
557 return iterator(m_c, pos.m_parent, idx);
571 return insert(pos, std::move(value_type(value)));
614 std::vector<std::array<const_iterator, 2>> x;
615 std::vector<const_iterator> v;
619 x.push_back({ ret, sub });
622 auto it2 = sub.
rend();
623 while (it1 != it2) v.push_back((++it1).base());
632 if (it.
parent() != x.back()[1]) x.pop_back();
638 auto it2 = it.
rend();
639 while(it1 != it2) v.push_back((++it1).base());
641 x.push_back({ p, it });
648 template <
typename... Args>
649 iterator emplace(const_iterator pos, Args&&... args)
651 return insert(pos, value_type(std::forward<Args>(args)...));
676 for (index_type i = pos.m_index; i != 0; i = (d + i)->m_parent)
678 if (i == sub.m_index)
throw std::invalid_argument(
"");
682 link(pos.m_parent, pos.m_index, sub.m_index);
684 return iterator(m_c, pos.m_parent, sub.m_index);
696 assert(pos.m_index != 0);
700 index_type idx = pos.m_index;
701 index_type parent = pos.m_parent;
702 index_type after = (d + idx)->m_next;
707 std::vector<index_type> v(1, idx);
710 index_type i = v.back();
712 for (index_type j = (d + i)->m_child; j != 0; j = (d + j)->m_next) v.push_back(j);
717 return iterator(m_c, parent, after);
732 index_type idx = d->m_next;
737 m_c.emplace_back(
node_type{ 0, 0, 0, 0, std::move(val) });
741 index_type before = (d + idx)->m_prev;
742 index_type after = (d + idx)->m_next;
743 (d + idx)->m_value = std::move(val);
745 (d + before)->m_next = after;
746 (d + after)->m_prev = before;
758 assert(idx < m_c.size());
763 index_type before = 0;
764 for (index_type i = d->m_next; i != 0 && i < idx; i = (d + before)->m_next) before = i;
767 index_type after = (d + before)->m_next;
770 (d + before)->m_next = idx;
772 (d + idx)->m_prev = before;
773 (d + idx)->m_next = after;
775 (d + after)->m_prev = idx;
778 (d + idx)->m_parent = 0;
779 (d + idx)->m_child = 0;
907 void link(index_type parent, index_type pos, index_type idx)
911 index_type child = (d + parent)->m_child;
912 index_type after = pos;
913 index_type before = 0;
914 if (after != 0) before = (child != after) ? (d + after)->m_prev : 0;
915 else before = (child != 0) ? (d + child)->m_prev : 0;
919 (d + parent)->m_child = idx;
920 index_type tail = (after != 0) ? (d + after)->m_prev : idx;
921 (d + idx)->m_prev = tail;
925 (d + before)->m_next = idx;
926 (d + idx)->m_prev = before;
931 (d + idx)->m_next = 0;
932 if (before != 0) (d + child)->m_prev = idx;
936 (d + after)->m_prev = idx;
937 (d + idx)->m_next = after;
939 (d + idx)->m_parent = parent;
1034 index_type parent = (d + idx)->m_parent;
1035 index_type child = (d + parent)->m_child;
1036 index_type before = (child != idx) ? (d + idx)->m_prev : 0;
1037 index_type after = (d + idx)->m_next;
1041 if (after == 0) (d + parent)->m_child = 0;
1044 (d + parent)->m_child = after;
1045 (d + after)->m_prev = (d + idx)->m_prev;
1048 else (d + before)->m_next = after;
1052 if (before != 0) (d + child)->m_prev = before;
1054 else (d + after)->m_prev = (d + idx)->m_prev;