45 template <
typename T,
size_t N = 64,
typename Alloc = std::allocator<T> >
55 typedef typename std::allocator_traits<allocator_type>::pointer
pointer;
56 typedef typename std::allocator_traits<allocator_type>::const_pointer
const_pointer;
57 typedef typename std::allocator_traits<allocator_type>::difference_type
difference_type;
58 typedef typename std::allocator_traits<allocator_type>::size_type
size_type;
64 typedef typename std::allocator_traits<allocator_type>:: template rebind_alloc<node> node_allocator_type;
67 struct root_place_holder {};
71 iSkipChildren{
false },
72 iDescendentCount{ 0 },
73 iSkippedDescendentCount{ 0 },
74 iContents{ root_place_holder{} }
76 node(
const node& other) :
77 iParent{ other.iParent },
78 iChildren( other.iChildren ),
79 iSkipChildren{ other.iSkipChildren },
80 iDescendentCount{ other.iDescendentCount },
81 iSkippedDescendentCount{ other.iSkippedDescendentCount },
82 iContents{ other.iContents }
84 update_parents(*
this);
88 iSkipChildren{
false },
89 iDescendentCount{ 0 },
90 iSkippedDescendentCount{ 0 },
91 iContents{ root_place_holder{} }
95 std::swap(iSkipChildren, other.iSkipChildren);
96 std::swap(iDescendentCount, other.iDescendentCount);
97 std::swap(iSkippedDescendentCount, other.iSkippedDescendentCount);
99 update_parents(*
this);
103 iSkipChildren{
false },
104 iDescendentCount{ 0 },
105 iSkippedDescendentCount{ 0 },
113 node& operator=(
const node& other)
115 iParent = other.iParent;
116 iChildren = other.iChildren;
117 iSkipChildren = other.iSkipChildren;
118 iDescendentCount = other.iDescendentCount;
119 iSkippedDescendentCount = other.iSkippedDescendentCount;
120 iContents = other.iContents;
121 update_parents(*
this);
124 node& operator=(node&& other)
128 std::swap(iSkipChildren, other.iSkipChildren);
129 std::swap(iDescendentCount, other.iDescendentCount);
130 std::swap(iSkippedDescendentCount, other.iSkippedDescendentCount);
132 update_parents(*
this);
138 return iParent ==
nullptr;
140 const node& parent()
const
154 return std::get<value_type>(iContents);
158 return std::get<value_type>(iContents);
160 const child_list& children()
const
164 child_list& children()
170 return iChildren.empty();
172 std::size_t depth()
const
174 std::size_t result = 0;
175 node
const* n =
this;
176 while (!n->parent().is_root())
183 bool children_skipped()
const
185 return iSkipChildren;
191 iSkipChildren =
true;
192 parent().increment_skipped_descendent_count(descendent_count() - skipped_descendent_count());
195 void unskip_children()
199 iSkipChildren =
false;
200 parent().decrement_skipped_descendent_count(descendent_count() - skipped_descendent_count());
204 std::size_t descendent_count()
const
206 return iDescendentCount;
208 void increment_descendent_count()
212 parent().increment_descendent_count();
214 void decrement_descendent_count()
218 parent().decrement_descendent_count();
220 std::size_t skipped_descendent_count()
const
222 return iSkippedDescendentCount;
224 void increment_skipped_descendent_count(std::size_t aCount)
226 iSkippedDescendentCount += aCount;
228 parent().increment_skipped_descendent_count(aCount);
230 void decrement_skipped_descendent_count(std::size_t aCount)
232 iSkippedDescendentCount -= aCount;
234 parent().decrement_skipped_descendent_count(aCount);
236 void update_parents(node& parent)
238 for (
auto& child : parent.children())
240 child.iParent = &parent;
241 update_parents(child);
246 child_list iChildren;
248 std::size_t iDescendentCount;
249 std::size_t iSkippedDescendentCount;
250 std::variant<value_type, root_place_holder> iContents;
252 typedef typename node::child_list node_child_list;
253 typedef typename node::child_list::const_iterator node_child_list_const_iterator;
254 typedef typename node::child_list::iterator node_child_list_iterator;
262 template <iterator_type Type>
263 class basic_const_iterator;
264 template <iterator_type Type>
277 basic_iterator(node& parentNode, node_child_list_iterator childIterator) : iParentNode{ &parentNode }, iBaseIterator{ childIterator } {}
278 template <iterator_type Type2>
281 template <iterator_type Type2>
284 return iParentNode == other.iParentNode && iBaseIterator == other.iBaseIterator;
286 template <iterator_type Type2>
289 return !(*
this == other);
301 while (iBaseIterator == parent_node().children().end() && !parent_node().is_root())
302 *
this = self_type{ parent_node().parent(),
std::next(parent_node().
parent().children().iter(parent_node())) };
305 *
this = self_type{ our_node(), children().
begin() };
311 self_type temp = *
this;
321 if (iBaseIterator == parent_node().children().begin())
322 *
this = self_type{ parent_node().parent(), parent_node().parent().children().iter(parent_node()) };
327 *
this = self_type{ our_node(),
std::prev(children().
end()) };
334 self_type temp = *
this;
341 return our_node().value();
384 std::reverse_iterator<basic_const_iterator<iterator_type::Sibling>>
crbegin()
const
386 return std::make_reverse_iterator(
cend());
388 std::reverse_iterator<basic_const_iterator<iterator_type::Sibling>>
rbegin()
const
390 return std::make_reverse_iterator(
cend());
392 std::reverse_iterator<basic_iterator<iterator_type::Sibling>>
rbegin()
394 return std::make_reverse_iterator(
end());
396 std::reverse_iterator<basic_const_iterator<iterator_type::Sibling>>
crend()
const
398 return std::make_reverse_iterator(
cbegin());
400 std::reverse_iterator<basic_const_iterator<iterator_type::Sibling>>
rend()
const
402 return std::make_reverse_iterator(
cbegin());
404 std::reverse_iterator<basic_iterator<iterator_type::Sibling>>
rend()
406 return std::make_reverse_iterator(
begin());
411 return parent_node().is_root();
415 return our_node().depth();
419 return our_node().descendent_count();
423 return our_node().children_skipped();
427 our_node().skip_children();
431 our_node().unskip_children();
434 bool is_singular()
const {
return iParentNode ==
nullptr; }
435 node& parent_node()
const {
if (is_singular())
throw singular_iterator();
return *iParentNode; }
436 node& our_node()
const {
if (is_singular())
throw singular_iterator();
return *base(); }
437 node_child_list_iterator base()
const {
if (is_singular())
throw singular_iterator();
return iBaseIterator; }
438 node_child_list& children()
const {
if (is_singular())
throw singular_iterator();
return our_node().children(); }
441 node_child_list_iterator iBaseIterator;
443 template <iterator_type Type>
456 basic_const_iterator(node
const& parentNode, node_child_list_const_iterator childIterator) : iParentNode{ &parentNode }, iBaseIterator{ childIterator } {}
457 template <iterator_type Type2>
459 template <iterator_type Type2>
462 template <iterator_type Type2>
465 return iParentNode == other.iParentNode && iBaseIterator == other.iBaseIterator;
467 template <iterator_type Type2>
470 return !(*
this == other);
482 while (iBaseIterator == parent_node().children().end() && !parent_node().is_root())
483 *
this = self_type{ parent_node().parent(),
std::next(parent_node().
parent().children().iter(parent_node())) };
486 *
this = self_type{ our_node(), children().
begin() };
492 self_type temp = *
this;
502 if (iBaseIterator == parent_node().children().begin())
503 *
this = self_type{ parent_node().parent(), parent_node().parent().children().iter(parent_node()) };
508 *
this = self_type{ our_node(),
std::prev(children().
end()) };
515 self_type temp = *
this;
522 return our_node().value();
553 std::reverse_iterator<basic_const_iterator<iterator_type::Sibling>>
crbegin()
const
555 return std::make_reverse_iterator(
cend());
557 std::reverse_iterator<basic_const_iterator<iterator_type::Sibling>>
rbegin()
const
559 return std::make_reverse_iterator(
cend());
561 std::reverse_iterator<basic_const_iterator<iterator_type::Sibling>>
crend()
const
563 return std::make_reverse_iterator(
cbegin());
565 std::reverse_iterator<basic_const_iterator<iterator_type::Sibling>>
rend()
const
567 return std::make_reverse_iterator(
cbegin());
572 return parent_node().is_root();
576 return our_node().depth();
580 return our_node().descendent_count();
584 return our_node().children_skipped();
587 bool is_singular()
const {
return iParentNode ==
nullptr; }
588 node
const& parent_node()
const {
if (is_singular())
throw singular_iterator();
return *iParentNode; }
589 node
const& our_node()
const {
if (is_singular())
throw singular_iterator();
return *base(); }
590 node_child_list_const_iterator base()
const {
if (is_singular())
throw singular_iterator();
return iBaseIterator; }
591 node_child_list
const& children()
const {
if (is_singular())
throw singular_iterator();
return our_node().children(); }
593 node
const* iParentNode;
594 node_child_list_const_iterator iBaseIterator;
616 return root().empty();
620 return root().descendent_count();
624 return root().descendent_count() - root().skipped_descendent_count();
636 return iterator{ root(), root().children().begin() };
648 return iterator{ root(), root().children().end() };
698 std::reverse_iterator<const_iterator>
crbegin()
const
700 return std::make_reverse_iterator(
cend());
702 std::reverse_iterator<const_iterator>
rbegin()
const
704 return std::make_reverse_iterator(
cend());
708 return std::make_reverse_iterator(
end());
710 std::reverse_iterator<const_iterator>
crend()
const
712 return std::make_reverse_iterator(
cbegin());
714 std::reverse_iterator<const_iterator>
rend()
const
716 return std::make_reverse_iterator(
cbegin());
718 std::reverse_iterator<iterator>
rend()
720 return std::make_reverse_iterator(
begin());
722 std::reverse_iterator<const_sibling_iterator>
crsbegin()
const
724 return std::make_reverse_iterator(
csend());
726 std::reverse_iterator<const_sibling_iterator>
rsbegin()
const
728 return std::make_reverse_iterator(
csend());
730 std::reverse_iterator<sibling_iterator>
rsbegin()
732 return std::make_reverse_iterator(
send());
734 std::reverse_iterator<const_sibling_iterator>
crsend()
const
736 return std::make_reverse_iterator(
csbegin());
738 std::reverse_iterator<const_sibling_iterator>
rsend()
const
740 return std::make_reverse_iterator(
csbegin());
742 std::reverse_iterator<sibling_iterator>
rsend()
744 return std::make_reverse_iterator(
sbegin());
746 std::reverse_iterator<const_skip_iterator>
crkbegin()
const
748 return std::make_reverse_iterator(
ckend());
750 std::reverse_iterator<const_skip_iterator>
rkbegin()
const
752 return std::make_reverse_iterator(
ckend());
754 std::reverse_iterator<skip_iterator>
rkbegin()
756 return std::make_reverse_iterator(
kend());
758 std::reverse_iterator<const_skip_iterator>
crkend()
const
760 return std::make_reverse_iterator(
ckbegin());
762 std::reverse_iterator<const_skip_iterator>
rkend()
const
764 return std::make_reverse_iterator(
ckbegin());
766 std::reverse_iterator<skip_iterator>
rkend()
768 return std::make_reverse_iterator(
kbegin());
780 auto& parent =
const_cast<node&
>(position.parent_node());
783 parent.increment_descendent_count();
805 node& parent = mutablePos.parent_node();
806 auto result =
iterator{ parent, parent.children().erase(mutablePos.base()) };
807 parent.decrement_descendent_count();
812 sort(std::less<value_type>{});
814 template <
typename Predicate>
822 node
const& root()
const
832 if (!pos.is_singular())
833 return pos.our_node();
838 if (!pos.is_singular())
842 node
const& first_node()
const
846 n = &n->children()[0];
851 return const_cast<node&
>(
const_cast<const self_type&
>(*this).first_node());
853 node
const& last_node()
const
862 return const_cast<node&
>(
const_cast<const self_type&
>(*this).last_node());
867 parent.children().emplace_back(parent, value);
868 parent.increment_descendent_count();
872 parent.children().emplace_front(parent, value);
873 parent.increment_descendent_count();
876 template <
typename Predicate>
877 void sort(node& parent, Predicate pred)
879 std::sort(parent.children().begin(), parent.children().end(),
880 [&pred](
const node& lhs,
const node& rhs)
882 auto const& lhsValue = lhs.value();
883 auto const& rhsValue = rhs.value();
884 auto result = pred(lhsValue, rhsValue);
887 if (parent.children().size() != parent.descendent_count())
888 for (
auto& child : parent.children())
const_iterator end() const
const_iterator begin() const
iterator emplace_insert(const_iterator aPosition, Args &&... aArguments)
basic_const_iterator< iterator_type::Sibling > cparent() const
std::bidirectional_iterator_tag iterator_category
basic_const_iterator(basic_const_iterator< Type2 > const &other)
pointer operator->() const
basic_const_iterator< iterator_type::Sibling > begin() const
tree_type::value_type value_type
std::reverse_iterator< basic_const_iterator< iterator_type::Sibling > > crend() const
bool operator!=(basic_const_iterator< Type2 > const &other) const
std::size_t descendent_count() const
std::reverse_iterator< basic_const_iterator< iterator_type::Sibling > > rbegin() const
bool children_skipped() const
bool parent_is_root() const
tree_type::difference_type difference_type
self_type operator++(int) const
reference operator*() const
bool operator==(basic_const_iterator< Type2 > const &other) const
basic_const_iterator< iterator_type::Sibling > cbegin() const
basic_const_iterator(basic_iterator< Type2 > const &other)
basic_const_iterator< iterator_type::Sibling > parent() const
basic_const_iterator< iterator_type::Sibling > end() const
tree_type::const_pointer pointer
std::reverse_iterator< basic_const_iterator< iterator_type::Sibling > > crbegin() const
basic_const_iterator< iterator_type::Sibling > cend() const
basic_const_iterator(node const &parentNode, node_child_list_const_iterator childIterator)
self_type operator--(int) const
std::reverse_iterator< basic_const_iterator< iterator_type::Sibling > > rend() const
tree_type::const_reference reference
std::size_t depth() const
bool parent_is_root() const
std::reverse_iterator< basic_iterator< iterator_type::Sibling > > rbegin()
basic_const_iterator< iterator_type::Sibling > end() const
std::size_t descendent_count() const
std::reverse_iterator< basic_const_iterator< iterator_type::Sibling > > rend() const
self_type operator--(int) const
basic_const_iterator< iterator_type::Sibling > begin() const
basic_const_iterator< iterator_type::Sibling > cbegin() const
std::bidirectional_iterator_tag iterator_category
std::reverse_iterator< basic_iterator< iterator_type::Sibling > > rend()
tree_type::pointer pointer
basic_const_iterator< iterator_type::Sibling > parent() const
self_type operator++(int) const
reference operator*() const
std::size_t depth() const
tree_type::value_type value_type
std::reverse_iterator< basic_const_iterator< iterator_type::Sibling > > rbegin() const
basic_iterator< iterator_type::Sibling > parent()
bool children_skipped() const
basic_iterator< iterator_type::Sibling > end()
basic_const_iterator< iterator_type::Sibling > cend() const
basic_iterator(basic_iterator< Type2 > const &other)
basic_iterator< iterator_type::Sibling > begin()
pointer operator->() const
std::reverse_iterator< basic_const_iterator< iterator_type::Sibling > > crbegin() const
bool operator!=(basic_iterator< Type2 > const &other) const
basic_const_iterator< iterator_type::Sibling > cparent() const
bool operator==(basic_iterator< Type2 > const &other) const
std::reverse_iterator< basic_const_iterator< iterator_type::Sibling > > crend() const
tree_type::reference reference
basic_iterator(node &parentNode, node_child_list_iterator childIterator)
tree_type::difference_type difference_type
const_sibling_iterator csbegin() const
void push_front(const value_type &value)
const_skip_iterator ckend() const
const_sibling_iterator sbegin() const
std::reverse_iterator< const_iterator > crend() const
const_iterator begin() const
std::reverse_iterator< skip_iterator > rkend()
std::reverse_iterator< const_skip_iterator > rkbegin() const
sibling_iterator insert(const_sibling_iterator position, const value_type &value)
const_iterator cbegin() const
basic_iterator< iterator_type::Normal > iterator
std::reverse_iterator< sibling_iterator > rsend()
std::reverse_iterator< const_skip_iterator > crkbegin() const
std::reverse_iterator< iterator > rend()
std::reverse_iterator< const_iterator > rend() const
std::reverse_iterator< const_sibling_iterator > rsend() const
std::allocator_traits< allocator_type >::const_pointer const_pointer
std::size_t ksize() const
std::reverse_iterator< const_sibling_iterator > crsbegin() const
basic_iterator< iterator_type::Sibling > sibling_iterator
void push_back(const_iterator pos, const value_type &value)
const_iterator end() const
const_iterator cend() const
iterator erase(const_iterator pos)
const_skip_iterator ckbegin() const
std::reverse_iterator< iterator > rbegin()
std::reverse_iterator< const_sibling_iterator > rsbegin() const
std::allocator_traits< allocator_type >::difference_type difference_type
basic_const_iterator< iterator_type::Skip > const_skip_iterator
std::allocator_traits< allocator_type >::size_type size_type
std::reverse_iterator< sibling_iterator > rsbegin()
const_sibling_iterator csend() const
std::reverse_iterator< const_skip_iterator > rkend() const
const_skip_iterator kbegin() const
std::reverse_iterator< const_iterator > rbegin() const
sibling_iterator sbegin()
std::reverse_iterator< const_sibling_iterator > crsend() const
void push_back(const value_type &value)
const_sibling_iterator send() const
void push_front(const_iterator pos, const value_type &value)
basic_const_iterator< iterator_type::Normal > const_iterator
basic_const_iterator< iterator_type::Sibling > const_sibling_iterator
std::allocator_traits< allocator_type >::pointer pointer
std::reverse_iterator< const_iterator > crbegin() const
const value_type & const_reference
const_skip_iterator kend() const
void sort(Predicate pred)
std::reverse_iterator< const_skip_iterator > crkend() const
basic_iterator< iterator_type::Skip > skip_iterator
std::reverse_iterator< skip_iterator > rkbegin()
void swap(plf::hive< element_type, allocator_type > &a, plf::hive< element_type, allocator_type > &b) noexcept(std::allocator_traits< allocator_type >::propagate_on_container_swap::value||std::allocator_traits< allocator_type >::is_always_equal::value)
it_type next(it_type it, const typename iterator_traits< it_type >::difference_type distance=1)
iterator_traits< it_type >::difference_type distance(const it_type first, const it_type last)
it_type prev(it_type it, const typename iterator_traits< it_type >::difference_type distance=1)