-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathIterator.cpp
71 lines (61 loc) · 1.88 KB
/
Iterator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "Node.h"
#include "bst.h"
#include "ap_error_ext.h"
#include "Iterator.h"
#ifndef ADVANCED_PROGRAMMING_EXAM_2020_ITERATOR_CPP
#define ADVANCED_PROGRAMMING_EXAM_2020_ITERATOR_CPP
template <typename value_t, typename key_type, typename OP>
template <typename O>
O* bst<value_t, key_type, OP>::Iterator<O>::
get_successor(O* node) noexcept
{
if (!node)
return nullptr;
if (node->get_right())
return get_minimum_left_node_in_subtree(node->get_right());
auto parent = node->get_parent();
while (parent && node == parent->get_right())
{
node = parent;
parent = node->get_parent();
}
return parent;
}
template <typename value_t, typename key_type, typename OP>
template <typename O>
typename bst<value_t, key_type, OP>::template Iterator<O>::reference
bst<value_t, key_type, OP>::Iterator<O>::
operator*() const
{
AP_ERROR_IF_NULLPTR_WITH_MSG(current_node, "Nullptr cannot be de-reference", false);
return *current_node;
}
template <typename value_t, typename key_type, typename OP>
template <typename O>
typename bst<value_t, key_type, OP>::template Iterator<O>::pointer
bst<value_t, key_type, OP>::Iterator<O>::
operator->() const
{
AP_ERROR_IF_NULLPTR_WITH_MSG(current_node, "Nullptr cannot access any member", false);
return &**this;
}
template <typename value_t, typename key_type, typename OP>
template <typename O>
typename bst<value_t, key_type, OP>::template Iterator<O>&
bst<value_t, key_type, OP>::Iterator<O>::
operator++() noexcept
{
current_node = get_successor(current_node);
return *this;
}
template <typename value_t, typename key_type, typename OP>
template <typename O>
typename bst<value_t, key_type, OP>::template Iterator<O>
bst<value_t, key_type, OP>::Iterator<O>::
operator++(int) noexcept
{
auto tmp{ *this };
++(*this);
return tmp;
}
#endif // ADVANCED_PROGRAMMING_EXAM_2020_ITERATOR_CPP