-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_iterator.hpp
132 lines (113 loc) · 3.56 KB
/
node_iterator.hpp
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#ifndef NODE_ITERATOR_HPP
#define NODE_ITERATOR_HPP
#include "iterator.hpp"
namespace ft {
struct bidirectional_iterator_tag {};
template <class T, class Alloc>
struct Avlnode;
template <typename T, class Alloc, class Pointer = T*, class Reference = T& >
class node_iterator : public general_iterator<bidirectional_iterator_tag, Avlnode<T, Alloc> > {
typedef general_iterator<bidirectional_iterator_tag, Avlnode<T, Alloc> > base_type;
private:
typename base_type::pointer _pointer;
public:
//Constructors
node_iterator(typename base_type::pointer p) : _pointer(p) {}
node_iterator() : _pointer(NULL) {}
//Copy constructor
node_iterator(const node_iterator& n) : _pointer(n._pointer) {}
//Copy assignment
node_iterator &operator=(const node_iterator &it) {
_pointer = it._pointer;
return *this;
}
//Destructor
~node_iterator() {}
//Comparison
bool operator == (const node_iterator &nodei) const { return _pointer == nodei._pointer; }
bool operator != (const node_iterator &nodei) const { return _pointer != nodei._pointer; }
//Dereference
//https://stackoverflow.com/questions/21569483/c-overloading-dereference-operators
T* operator->() const { return &(_pointer->_elem); }
T& operator*() const { return _pointer->_elem; }
//increment
node_iterator &operator++() {
_pointer = _pointer->next();
return *this;
}
node_iterator operator++(int) {
node_iterator tmp = *this;
_pointer = _pointer->next();
return tmp;
}
//decrement
node_iterator &operator--() {
_pointer = _pointer->previous();
return *this;
}
node_iterator operator--(int) {
node_iterator tmp = *this;
_pointer = _pointer->previous();
return tmp;
}
/*implicit conversion*/
operator node_iterator< T, Alloc, const T*, const T&> () const {
return (node_iterator< T, Alloc, const T*, const T&>(_pointer));
}
};
//https://www.cplusplus.com/reference/iterator/reverse_iterator/
template <typename T, class Alloc, class Pointer = T*, class Reference = T& >
class rev_node_iterator : public general_iterator<bidirectional_iterator_tag, Avlnode<T, Alloc> > {
typedef general_iterator<bidirectional_iterator_tag, Avlnode<T, Alloc> > base_type;
private:
node_iterator<T, Alloc> _base;
public:
//constructors
rev_node_iterator(typename base_type::pointer p) : _base(p) { }
rev_node_iterator() : _base(NULL) { }
rev_node_iterator(node_iterator<T, Alloc> b) : _base(b) { }
// copy constructor
template <class E>
rev_node_iterator(const rev_node_iterator<E, Alloc> &it) : _base(it.base()) {}
//copy assignment
rev_node_iterator &operator=(const rev_node_iterator &it) {
_base = it._base;
return *this;
}
//Base!
rev_node_iterator base() const {return _base;}
//Destuctor
~rev_node_iterator() {};
//dereference
//https://stackoverflow.com/questions/21569483/c-overloading-dereference-operators
T* operator->() const { return &(*_base); }
T& operator*() const { return *_base; }
bool operator == (const rev_node_iterator &nodei) const { return _base == nodei._base; }
bool operator != (const rev_node_iterator &nodei) const { return _base != nodei._base; }
//increment
rev_node_iterator &operator++() {
_base--;
return *this;
}
rev_node_iterator operator++(int) {
rev_node_iterator tmp = *this;
_base--;
return tmp;
}
//decrement
rev_node_iterator &operator--() {
_base++;
return *this;
}
rev_node_iterator operator--(int) {
rev_node_iterator tmp = *this;
_base++;
return tmp;
}
/*implicit conversion!*/
operator rev_node_iterator< T, Alloc, const T*, const T&> () const {
return (rev_node_iterator< T, Alloc, const T*, const T&>(_base));
}
};
}
#endif