|
49 | 49 | _LIBCPP_PUSH_MACROS
|
50 | 50 | #include <__undef_macros>
|
51 | 51 |
|
| 52 | +// __tree is a red-black-tree implementation used for the associative containers (i.e. (multi)map/set). To allow for |
| 53 | +// constant time lookup, it stores |
| 54 | +// - (1) a pointer to the node with the smallest (i.e. leftmost) element, namely __begin_node_ |
| 55 | +// - (2) the number of nodes in the tree, namely __size_ |
| 56 | +// |
| 57 | +// A pointer to the root of the tree is stored in the __end_node_. |
| 58 | +// A tree looks like this in memory: |
| 59 | +// |
| 60 | +// __end_node_ |
| 61 | +// | |
| 62 | +// root |
| 63 | +// / \ |
| 64 | +// l1 r1 |
| 65 | +// / \ / \ |
| 66 | +// ... ... ... ... |
| 67 | +// |
| 68 | +// All nodes except __end_node_ have a __left_ and __right_ pointer as well as a __parent_ pointer. |
| 69 | +// __end_node_ only contains a __left_ pointer, which point to the root of the tree. |
| 70 | +// This layout allows for iteration through the tree without a need for special handling of the end node. See |
| 71 | +// __tree_next_iter and __tree_prev_iter for more details. |
| 72 | + |
52 | 73 | _LIBCPP_BEGIN_NAMESPACE_STD
|
53 | 74 |
|
54 | 75 | template <class _Tp, class _Compare, class _Allocator>
|
@@ -183,6 +204,11 @@ _LIBCPP_HIDE_FROM_ABI _NodePtr __tree_next(_NodePtr __x) _NOEXCEPT {
|
183 | 204 | return __x->__parent_unsafe();
|
184 | 205 | }
|
185 | 206 |
|
| 207 | +// __tree_next_iter and __tree_prev_iter implement iteration through the tree. The order is as follows: |
| 208 | +// left sub-tree -> node -> right sub-tree. When the right-most node of a sub-tree is reached, we walk up the tree until |
| 209 | +// we find a node where we were in the left sub-tree. We are _always_ in a left sub-tree, since the __end_node_ points |
| 210 | +// to the actual root of the tree through a __left_ pointer. incrementing the end() pointer is UB, so we can assume that |
| 211 | +// never happens. |
186 | 212 | template <class _EndNodePtr, class _NodePtr>
|
187 | 213 | inline _LIBCPP_HIDE_FROM_ABI _EndNodePtr __tree_next_iter(_NodePtr __x) _NOEXCEPT {
|
188 | 214 | _LIBCPP_ASSERT_INTERNAL(__x != nullptr, "node shouldn't be null");
|
|
0 commit comments