ORIGINAL
Loading...
Searching...
No Matches
stepIterator.h
1#ifndef STEPITERATOR_H
2#define STEPITERATOR_H
3
4#include <limits>
5
6#include "iterator.h"
7#include "wrapper.h"
8#include "error.h"
9
10namespace original
11{
12 template<typename TYPE>
13 class stepIterator : public baseIterator<TYPE>
14 {
15 protected:
16 mutable wrapper<TYPE>* _ptr;
17
18 explicit stepIterator(wrapper<TYPE>* ptr);
19 static int64_t ptrDistance(wrapper<TYPE>* start, wrapper<TYPE>* end);
20 bool equalPtr(const iterator<TYPE>* other) const override;
21 public:
22 stepIterator(const stepIterator& other);
23 stepIterator& operator=(const stepIterator& other);
24 stepIterator* clone() const override;
25 [[nodiscard]] bool hasNext() const override;
26 [[nodiscard]] bool hasPrev() const override;
27 bool atPrev(const iterator<TYPE>* other) const override;
28 bool atNext(const iterator<TYPE>* other) const override;
29 void next() const override;
30 void prev() const override;
31 void operator+=(int64_t steps) const override;
32 void operator-=(int64_t steps) const override;
33 int64_t operator-(const iterator<TYPE>& other) const override;
34 stepIterator* getNext() const override;
35 iterator<TYPE>* getPrev() const override;
36 TYPE& get() override;
37 TYPE get() const override;
38 void set(const TYPE& data) override;
39 [[nodiscard]] bool isValid() const override;
40 [[nodiscard]] std::string className() const override;
41 };
42}
43
44 template <typename TYPE>
45 original::stepIterator<TYPE>::stepIterator(wrapper<TYPE>* ptr)
46 : _ptr(ptr) {}
47
48 template <typename TYPE>
49 auto original::stepIterator<TYPE>::ptrDistance(wrapper<TYPE>* start, wrapper<TYPE>* end) -> int64_t
50 {
51 int64_t dis = 0;
52 auto* cur = start;
53 while (cur != nullptr)
54 {
55 if (cur == end)
56 {
57 return dis;
58 }
59 cur = cur->getPNext();
60 dis += 1;
61 }
62 return std::numeric_limits<int64_t>::max();
63 }
64
65 template<typename TYPE>
66 auto original::stepIterator<TYPE>::equalPtr(const iterator<TYPE> *other) const -> bool {
67 auto* other_it = dynamic_cast<const stepIterator*>(other);
68 return other_it != nullptr && _ptr == other_it->_ptr;
69 }
70
71 template <typename TYPE>
72 original::stepIterator<TYPE>::stepIterator(const stepIterator& other)
73 : _ptr(nullptr)
74 {
75 this->operator=(other);
76 }
77
78 template <typename TYPE>
79 auto original::stepIterator<TYPE>::operator=(
80 const stepIterator& other) -> stepIterator&
81 {
82 if (this == &other) {
83 return *this;
84 }
85 this->_ptr = other._ptr;
86 return *this;
87 }
88
89 template<typename TYPE>
90 auto original::stepIterator<TYPE>::clone() const -> stepIterator* {
91 return new stepIterator(*this);
92 }
93
94 template <typename TYPE>
95 auto original::stepIterator<TYPE>::hasNext() const -> bool
96 {
97 return this->isValid();
98 }
99
100 template<typename TYPE>
101 auto original::stepIterator<TYPE>::hasPrev() const -> bool {
103 }
104
105 template <typename TYPE>
106 auto original::stepIterator<TYPE>::atPrev(const iterator<TYPE>* other) const -> bool
107 {
109 }
110
111 template <typename TYPE>
112 auto original::stepIterator<TYPE>::atNext(const iterator<TYPE>* other) const -> bool
113 {
115 }
116
117 template <typename TYPE>
118 void original::stepIterator<TYPE>::next() const
119 {
120 if (!this->isValid()) throw nullPointerError();
121 this->_ptr = this->_ptr->getPNext();
122 }
123
124 template<typename TYPE>
125 auto original::stepIterator<TYPE>::prev() const -> void {
127 }
128
129 template <typename TYPE>
130 auto original::stepIterator<TYPE>::operator+=(const int64_t steps) const -> void
131 {
132 if (steps < 0) {
133 this->operator-=(abs(steps));
134 return;
135 }
136
137 for (int64_t i = 0; i < steps; i++)
138 {
139 this->next();
140 }
141 }
142
143 template <typename TYPE>
144 auto original::stepIterator<TYPE>::operator-=(const int64_t steps) const -> void
145 {
146 if (steps < 0) {
147 this->operator+=(abs(steps));
148 return;
149 }
150
151 for (int64_t i = 0; i < steps; i++)
152 {
153 this->prev();
154 }
155 }
156
157 template <typename TYPE>
158 auto original::stepIterator<TYPE>::operator-(const iterator<TYPE>& other) const -> int64_t
159 {
160 auto* other_it = dynamic_cast<const stepIterator*>(&other);
161 if (other_it == nullptr)
162 return this > &other ?
163 std::numeric_limits<int64_t>::max() :
164 std::numeric_limits<int64_t>::min();
165 if (const int64_t pos_dis = ptrDistance(other_it->_ptr, this->_ptr);
166 pos_dis != std::numeric_limits<int64_t>::max()) return pos_dis;
167 if (const int64_t neg_dis = ptrDistance(this->_ptr, other_it->_ptr);
168 neg_dis != std::numeric_limits<int64_t>::max()) return -neg_dis;
169 return this->_ptr > other_it->_ptr ?
170 std::numeric_limits<int64_t>::max() :
171 std::numeric_limits<int64_t>::min();
172 }
173
174 template <typename TYPE>
175 auto original::stepIterator<TYPE>::getNext() const -> stepIterator* {
176 return new stepIterator(this->_ptr->getPNext());
177 }
178
179 template<typename TYPE>
180 auto original::stepIterator<TYPE>::getPrev() const -> iterator<TYPE>* {
182 }
183
184 template <typename TYPE>
185 auto original::stepIterator<TYPE>::get() -> TYPE&
186 {
187 if (!this->isValid()) throw nullPointerError();
188 return this->_ptr->getVal();
189 }
190
191 template <typename TYPE>
192 auto original::stepIterator<TYPE>::get() const -> TYPE
193 {
194 if (!this->isValid()) throw nullPointerError();
195 return this->_ptr->getVal();
196 }
197
198 template <typename TYPE>
199 auto original::stepIterator<TYPE>::set(const TYPE& data) -> void
200 {
201 if (!this->isValid()) throw nullPointerError();
202 this->_ptr->setVal(data);
203 }
204
205 template <typename TYPE>
206 auto original::stepIterator<TYPE>::isValid() const -> bool
207 {
208 return this->_ptr != nullptr;
209 }
210
211 template <typename TYPE>
212 auto original::stepIterator<TYPE>::className() const -> std::string
213 {
214 return "stepIterator";
215 }
216
217#endif //STEPITERATOR_H
Definition iterator.h:56
Definition iterator.h:11
Definition error.h:28
Definition stepIterator.h:14
Definition wrapper.h:12