Iros
 
Loading...
Searching...
No Matches
owning_rb_tree.h
Go to the documentation of this file.
1#pragma once
2
9
10namespace di::container {
11template<typename Self, typename T>
12struct OwningRBTreeTag;
13
14template<typename T, typename Tag>
16public:
17 template<typename... Args>
18 requires(concepts::ConstructibleFrom<T, Args...>)
19 constexpr OwningRBTreeNode(InPlace, Args&&... args) : m_value(util::forward<Args>(args)...) {}
20
21 constexpr auto value() -> T& { return m_value; }
22
23private:
24 T m_value;
25};
26
27template<typename Self, typename T>
28struct OwningRBTreeTag : IntrusiveTagBase<OwningRBTreeNode<T, Self>> {
30
31 constexpr static auto is_sized(InPlaceType<T>) -> bool { return true; }
32 constexpr static auto down_cast(InPlaceType<T>, Node& node) -> T& { return node.value(); }
33
34 constexpr static void did_remove(auto& self, auto& node) {
35 util::destroy_at(util::addressof(node));
36 di::deallocate_one<Node>(self.allocator(), util::addressof(node));
37 }
38};
39
40template<typename Value, typename Comp, typename Tag, concepts::Allocator Alloc, typename Interface, bool is_multi>
42 : public RBTree<Value, Comp, Tag, Interface, is_multi, OwningRBTree<Value, Comp, Tag, Alloc, Interface, is_multi>> {
43private:
44 using Base =
46
47 using Node = RBTreeNode<Tag>;
48 using Iterator = RBTreeIterator<Value, Tag>;
49 using ConstIterator = container::ConstIteratorImpl<Iterator>;
50
51 using AllocResult = meta::AllocatorResult<Alloc>;
52
53 template<typename T>
55
56public:
57 using Base::Base;
58
59 constexpr auto allocator() -> Alloc& { return m_allocator; }
60
61 template<typename U, concepts::Invocable F>
63 constexpr auto insert_with_factory(U&& needle, F&& factory) {
64 auto position = this->insert_position(needle);
65 if constexpr (!is_multi) {
66 if (position.parent && this->compare(this->node_value(*position.parent), needle) == 0) {
67 return Result<Tuple<Iterator, bool>>(Tuple(Iterator(position.parent, false), false));
68 }
69 }
70
71 return as_fallible(this->create_node(function::invoke(util::forward<F>(factory)))) % [&](auto* node) {
72 this->insert_node(position, *node);
73 if constexpr (!is_multi) {
74 return Tuple(Iterator(node, false), true);
75 } else {
76 return Iterator(node, false);
77 }
79 }
80
81 template<typename U, concepts::Invocable F>
83 constexpr auto insert_with_factory(ConstIterator, U&& needle, F&& factory) {
84 auto position = this->insert_position(needle);
85 if constexpr (!is_multi) {
86 if (position.parent && this->compare(this->node_value(*position.parent), needle) == 0) {
87 return Result<Tuple<Iterator, bool>>(Tuple(Iterator(position.parent, false), false));
88 }
89 }
90
91 return as_fallible(this->create_node(function::invoke(util::forward<F>(factory)))) % [&](auto* node) {
92 this->insert_node(position, *node);
93 return Iterator(node, false);
95 }
96
97private:
98 template<typename... Args>
99 requires(concepts::ConstructibleFrom<Value, Args...>)
100 constexpr auto create_node(Args&&... args) {
102 [&](OwningRBTreeNode<Value, Tag>* pointer) {
103 util::construct_at(pointer, in_place, util::forward<Args>(args)...);
104 return static_cast<Node*>(pointer);
105 } |
107 }
108
109 [[no_unique_address]] Alloc m_allocator {};
110};
111}
Definition const_iterator_impl.h:19
Definition owning_rb_tree.h:42
constexpr auto allocator() -> Alloc &
Definition owning_rb_tree.h:59
constexpr auto insert_with_factory(ConstIterator, U &&needle, F &&factory)
Definition owning_rb_tree.h:83
constexpr auto insert_with_factory(U &&needle, F &&factory)
Definition owning_rb_tree.h:63
Definition rb_tree_iterator.h:10
Definition tuple_forward_declaration.h:5
Definition operations.h:11
Definition vocab.h:74
Definition relation.h:31
Definition sequence.h:12
constexpr auto invoke
Definition invoke.h:100
meta::LikeExpected< decltype(di::allocate(util::declval< Alloc & >(), 0, 0)), T > AllocatorResult
Definition allocator.h:25
Type< detail::LikeExpectedHelper< T, U > > LikeExpected
Definition vocab.h:60
constexpr auto destroy_at
Definition destroy_at.h:24
constexpr auto construct_at
Definition construct_at.h:27
constexpr auto allocate_one
Definition allocate_one.h:29
constexpr auto as_fallible
Definition as_fallible.h:26
constexpr auto try_infallible
Definition try_infallible.h:31
constexpr auto in_place
Definition in_place.h:8
constexpr auto deallocate_one
Definition deallocate_one.h:27
Definition intrusive_tag_base.h:8
Definition owning_rb_tree.h:15
constexpr auto value() -> T &
Definition owning_rb_tree.h:21
constexpr OwningRBTreeNode(InPlace, Args &&... args)
Definition owning_rb_tree.h:19
Definition owning_rb_tree.h:28
static constexpr auto down_cast(InPlaceType< T >, Node &node) -> T &
Definition owning_rb_tree.h:32
static constexpr auto is_sized(InPlaceType< T >) -> bool
Definition owning_rb_tree.h:31
static constexpr void did_remove(auto &self, auto &node)
Definition owning_rb_tree.h:34
OwningRBTreeNode< T, Self > Node
Definition owning_rb_tree.h:29
Definition rb_tree_node.h:9
Definition in_place_type.h:5
Definition in_place.h:4