ORIGINAL
Loading...
Searching...
No Matches
forwardChain.h
1#ifndef FORWARDCHAIN_H
2#define FORWARDCHAIN_H
3
4#include "singleDirectionIterator.h"
5#include "array.h"
6#include "baseList.h"
7
8namespace original {
9 template <typename TYPE>
10 class forwardChain final : public baseList<TYPE>, public iterationStream<TYPE, forwardChain<TYPE>>{
11 class forwardChainNode final : public wrapper<TYPE>{
12 public:
13 friend class iterator<TYPE>;
14 friend class forwardChain;
15 private:
16 TYPE data_;
17 forwardChainNode* next;
18 protected:
19 explicit forwardChainNode(const TYPE& data = TYPE{}, forwardChainNode* next = nullptr);
20 forwardChainNode(const forwardChainNode& other);
21 forwardChainNode& operator=(const forwardChainNode& other);
22 TYPE& getVal() override;
23 const TYPE& getVal() const override;
24 void setVal(TYPE data) override;
25 forwardChainNode* getPPrev() const override;
26 forwardChainNode* getPNext() const override;
27 void setPNext(forwardChainNode* new_next);
28 static void connect(forwardChainNode* prev, forwardChainNode* next);
29 };
30
31 uint32_t size_;
32 forwardChainNode* begin_;
33
34 forwardChainNode* beginNode() const;
35 forwardChainNode* findNode(int64_t index) const;
36 void chainInit();
37 void firstAdd(forwardChainNode* node);
38 forwardChainNode* lastDelete();
39 void chainDestruction();
40 public:
41 class Iterator final : public singleDirectionIterator<TYPE>
42 {
43 explicit Iterator(forwardChainNode* ptr);
44 public:
45 friend forwardChain;
46 Iterator(const Iterator& other);
47 Iterator& operator=(const Iterator& other);
48 Iterator* clone() const override;
49 bool atPrev(const iterator<TYPE> *other) const override;
50 bool atNext(const iterator<TYPE> *other) const override;
51 [[nodiscard]] std::string className() const override;
52 };
53
54 explicit forwardChain();
55 forwardChain(const forwardChain& other);
56 forwardChain(std::initializer_list<TYPE> list);
57 explicit forwardChain(const array<TYPE>& arr);
58 forwardChain& operator=(const forwardChain& other);
59 forwardChain(forwardChain&& other) noexcept;
60 forwardChain& operator=(forwardChain&& other) noexcept;
61 [[nodiscard]] uint32_t size() const override;
62 TYPE get(int64_t index) const override;
63 TYPE& operator[](int64_t index) override;
64 void set(int64_t index, const TYPE &e) override;
65 uint32_t indexOf(const TYPE &e) const override;
66 void pushBegin(const TYPE &e) override;
67 void push(int64_t index, const TYPE &e) override;
68 void pushEnd(const TYPE &e) override;
69 TYPE popBegin() override;
70 TYPE pop(int64_t index) override;
71 TYPE popEnd() override;
72 Iterator* begins() const override;
73 Iterator* ends() const override;
74 [[nodiscard]] std::string className() const override;
75 ~forwardChain() override;
76 };
77}
78
79 template <typename TYPE>
80 original::forwardChain<TYPE>::forwardChainNode::forwardChainNode(const TYPE& data, forwardChainNode* next)
81 : data_(data), next(next) {}
82
83 template<typename TYPE>
84 original::forwardChain<TYPE>::forwardChainNode::forwardChainNode(const forwardChainNode &other)
85 : data_(other.data_), next(other.next) {}
86
87 template<typename TYPE>
88 auto original::forwardChain<TYPE>::forwardChainNode::operator=(
89 const forwardChainNode &other) -> forwardChainNode & {
90 if (this != &other) {
91 data_ = other.data_;
92 next = other.next;
93 }
94 return *this;
95 }
96
97 template<typename TYPE>
98 auto original::forwardChain<TYPE>::forwardChainNode::getVal() -> TYPE& {
99 return this->data_;
100 }
101
102 template<typename TYPE>
103 auto original::forwardChain<TYPE>::forwardChainNode::getVal() const -> const TYPE& {
104 return this->data_;
105 }
106
107 template<typename TYPE>
108 auto original::forwardChain<TYPE>::forwardChainNode::setVal(TYPE data) -> void {
109 this->data_ = data;
110 }
111
112 template<typename TYPE>
113 auto original::forwardChain<TYPE>::forwardChainNode::getPPrev() const -> forwardChainNode* {
114 throw unSupportedMethodError();
115 }
116
117 template<typename TYPE>
118 auto original::forwardChain<TYPE>::forwardChainNode::getPNext() const -> forwardChainNode* {
119 return this->next;
120 }
121
122 template<typename TYPE>
123 auto original::forwardChain<TYPE>::forwardChainNode::setPNext(forwardChainNode *new_next) -> void {
124 this->next = new_next;
125 }
126
127 template<typename TYPE>
128 auto original::forwardChain<TYPE>::forwardChainNode::connect(
129 forwardChainNode *prev, forwardChainNode *next) -> void {
130 if (prev != nullptr) prev->setPNext(next);
131 }
132
133 template <typename TYPE>
134 auto original::forwardChain<TYPE>::beginNode() const -> forwardChainNode*
135 {
136 return this->begin_->getPNext();
137 }
138
139 template<typename TYPE>
140 auto original::forwardChain<TYPE>::findNode(const int64_t index) const -> forwardChainNode* {
141 if (this->size() == 0) return this->begin_;
142 auto* cur = this->beginNode();
143 for(uint32_t i = 0; i < index; i++)
144 {
145 cur = cur->getPNext();
146 }
147 return cur;
148 }
149
150 template <typename TYPE>
151 auto original::forwardChain<TYPE>::chainInit() -> void
152 {
153 auto* pivot = new forwardChainNode();
154 this->size_ = 0;
155 this->begin_ = pivot;
156 }
157
158 template <typename TYPE>
159 auto original::forwardChain<TYPE>::firstAdd(forwardChainNode* node) -> void
160 {
161 forwardChainNode::connect(this->findNode(0), node);
162 this->size_ += 1;
163 }
164
165 template <typename TYPE>
166 auto original::forwardChain<TYPE>::lastDelete() -> forwardChainNode*
167 {
168 auto* last = this->beginNode();
169 delete this->begin_;
170 this->chainInit();
171 return last;
172 }
173
174 template <typename TYPE>
175 auto original::forwardChain<TYPE>::chainDestruction() -> void
176 {
177 auto* cur = this->begin_;
178 while (cur)
179 {
180 auto* next = cur->getPNext();
181 delete cur;
182 cur = next;
183 }
184 }
185
186 template<typename TYPE>
187 original::forwardChain<TYPE>::Iterator::Iterator(forwardChainNode *ptr)
188 : singleDirectionIterator<TYPE>(ptr) {}
189
190 template<typename TYPE>
191 original::forwardChain<TYPE>::Iterator::Iterator(const Iterator &other)
192 : singleDirectionIterator<TYPE>(nullptr) {
193 this->operator=(other);
194 }
195
196 template<typename TYPE>
197 auto original::forwardChain<TYPE>::Iterator::operator=(const Iterator &other) -> Iterator & {
198 if (this == &other) return *this;
199 singleDirectionIterator<TYPE>::operator=(other);
200 return *this;
201 }
202
203 template<typename TYPE>
204 auto original::forwardChain<TYPE>::Iterator::clone() const -> Iterator* {
205 return new Iterator(*this);
206 }
207
208 template<typename TYPE>
209 auto original::forwardChain<TYPE>::Iterator::atPrev(const iterator<TYPE> *other) const -> bool {
210 auto other_it = dynamic_cast<const Iterator*>(other);
211 return other_it != nullptr && this->_ptr->getPNext() == other_it->_ptr;
212 }
213
214 template<typename TYPE>
215 auto original::forwardChain<TYPE>::Iterator::atNext(const iterator<TYPE> *other) const -> bool {
216 auto other_it = dynamic_cast<const Iterator*>(other);
217 return other_it != nullptr && other_it->_ptr->getPNext() == this->_ptr;
218 }
219
220 template<typename TYPE>
221 auto original::forwardChain<TYPE>::Iterator::className() const -> std::string {
222 return "forwardChain::Iterator";
223 }
224
225 template<typename TYPE>
226 original::forwardChain<TYPE>::forwardChain() : size_(0)
227 {
228 this->chainInit();
229 }
230
231 template<typename TYPE>
232 original::forwardChain<TYPE>::forwardChain(const forwardChain &other) : forwardChain() {
233 this->operator=(other);
234 }
235
236 template<typename TYPE>
237 original::forwardChain<TYPE>::forwardChain(std::initializer_list<TYPE> list) : forwardChain() {
238 for (auto e: list) {
239 auto* cur_node = new forwardChainNode(e);
240 if (this->size() == 0)
241 {
242 this->firstAdd(cur_node);
243 }else
244 {
245 auto* end = this->findNode(this->size() - 1);
246 forwardChainNode::connect(end, cur_node);
247 this->size_ += 1;
248 }
249 }
250 }
251
252 template<typename TYPE>
253 original::forwardChain<TYPE>::forwardChain(const array<TYPE>& arr) : forwardChain() {
254 for (uint32_t i = 0; i < arr.size(); i++) {
255 auto* cur_node = new forwardChainNode(arr.get(i));
256 if (this->size() == 0)
257 {
258 this->firstAdd(cur_node);
259 }else
260 {
261 auto* end = this->findNode(this->size() - 1);
262 forwardChainNode::connect(end, cur_node);
263 this->size_ += 1;
264 }
265 }
266 }
267
268 template<typename TYPE>
269 auto original::forwardChain<TYPE>::operator=(const forwardChain &other) -> forwardChain& {
270 if (this == &other) return *this;
271 this->chainDestruction();
272 this->size_ = other.size_;
273 if (this->size() != 0){
274 auto* other_ = other.begin_;
275 this->begin_ = new forwardChainNode(other_->getVal());
276 auto* this_ = this->begin_;
277 while (other_->getPNext() != nullptr){
278 other_ = other_->getPNext();
279 forwardChainNode::connect(this_, new forwardChainNode(other_->getVal()));
280 this_ = this_->getPNext();
281 }
282 } else{
283 this->chainInit();
284 }
285 return *this;
286 }
287
288 template<typename TYPE>
289 original::forwardChain<TYPE>::forwardChain(forwardChain &&other) noexcept : forwardChain() {
290 this->operator=(std::move(other));
291 }
292
293 template<typename TYPE>
294 original::forwardChain<TYPE> & original::forwardChain<TYPE>::operator=(forwardChain &&other) noexcept {
295 if (this == &other)
296 return *this;
297
298 this->chainDestruction();
299 this->begin_ = other.begin_;
300 this->size_ = other.size_;
301 other.chainInit();
302 return *this;
303 }
304
305 template<typename TYPE>
306 auto original::forwardChain<TYPE>::size() const -> uint32_t {
307 return this->size_;
308 }
309
310 template<typename TYPE>
311 auto original::forwardChain<TYPE>::get(int64_t index) const -> TYPE {
312 if (this->indexOutOfBound(index)){
313 throw outOfBoundError();
314 }
315 auto* cur = this->findNode(this->parseNegIndex(index));
316 return cur->getVal();
317 }
318
319 template<typename TYPE>
320 auto original::forwardChain<TYPE>::operator[](int64_t index) -> TYPE& {
321 if (this->indexOutOfBound(index)){
322 throw outOfBoundError();
323 }
324 auto* cur = this->findNode(this->parseNegIndex(index));
325 return cur->getVal();
326 }
327
328 template<typename TYPE>
329 auto original::forwardChain<TYPE>::set(int64_t index, const TYPE &e) -> void {
330 if (this->indexOutOfBound(index)){
331 throw outOfBoundError();
332 }
333 auto* cur = this->findNode(this->parseNegIndex(index));
334 cur->setVal(e);
335 }
336
337 template<typename TYPE>
338 auto original::forwardChain<TYPE>::indexOf(const TYPE &e) const -> uint32_t {
339 uint32_t i = 0;
340 for (auto* current = this->begin_; current != nullptr; current = current->getPNext()) {
341 if (current->getVal() == e) {
342 return i;
343 }
344 i += 1;
345 }
346 return this->size();
347 }
348
349 template<typename TYPE>
350 auto original::forwardChain<TYPE>::pushBegin(const TYPE &e) -> void {
351 auto* new_node = new forwardChainNode(e);
352 if (this->size() == 0){
353 this->firstAdd(new_node);
354 } else{
355 auto* next = this->beginNode();
356 forwardChainNode::connect(this->begin_, new_node);
357 forwardChainNode::connect(new_node, next);
358 this->size_ += 1;
359 }
360 }
361
362 template<typename TYPE>
363 auto original::forwardChain<TYPE>::push(int64_t index, const TYPE &e) -> void {
364 index = this->parseNegIndex(index);
365 if (index == 0){
366 this->pushBegin(e);
367 } else if (index == this->size()){
368 this->pushEnd(e);
369 } else{
370 if (this->indexOutOfBound(index)){
371 throw outOfBoundError();
372 }
373 auto* new_node = new forwardChainNode(e);
374 auto* prev = this->findNode(index - 1);
375 auto* cur = prev->getPNext();
376 forwardChainNode::connect(prev, new_node);
377 forwardChainNode::connect(new_node, cur);
378 this->size_ += 1;
379 }
380 }
381
382 template<typename TYPE>
383 auto original::forwardChain<TYPE>::pushEnd(const TYPE &e) -> void {
384 auto* new_node = new forwardChainNode(e);
385 if (this->size() == 0){
386 this->firstAdd(new_node);
387 } else{
388 auto* end = this->findNode(this->size() - 1);
389 forwardChainNode::connect(end, new_node);
390 this->size_ += 1;
391 }
392 }
393
394 template<typename TYPE>
395 auto original::forwardChain<TYPE>::popBegin() -> TYPE {
396 TYPE res;
397 if (this->size() == 0){
398 throw noElementError();
399 }
400
401 res = this->beginNode()->getVal();
402 if (this->size() == 1){
403 delete this->lastDelete();
404 } else{
405 auto* del = this->beginNode();
406 auto* new_begin = del->getPNext();
407 delete del;
408 forwardChainNode::connect(this->begin_, new_begin);
409 this->size_ -= 1;
410 }
411 return res;
412 }
413
414 template<typename TYPE>
415 auto original::forwardChain<TYPE>::pop(int64_t index) -> TYPE {
416 index = this->parseNegIndex(index);
417 if (index == 0){
418 return this->popBegin();
419 }
420 if (index == this->size() - 1){
421 return this->popEnd();
422 }
423 if (this->indexOutOfBound(index)){
424 throw outOfBoundError();
425 }
426 TYPE res;
427 auto* prev = this->findNode(index - 1);
428 auto* cur = prev->getPNext();
429 res = cur->getVal();
430 auto* next = cur->getPNext();
431 forwardChainNode::connect(prev, next);
432 delete cur;
433 this->size_ -= 1;
434 return res;
435 }
436
437 template<typename TYPE>
438 auto original::forwardChain<TYPE>::popEnd() -> TYPE {
439 TYPE res;
440 if (this->size() == 0){
441 throw noElementError();
442 }
443 if (this->size() == 1){
444 res = this->beginNode()->getVal();
445 delete this->lastDelete();
446 } else{
447 auto* new_end = this->findNode(this->size() - 2);
448 auto* end = new_end->getPNext();
449 res = end->getVal();
450 delete end;
451 forwardChainNode::connect(new_end, nullptr);
452 this->size_ -= 1;
453 }
454 return res;
455 }
456
457 template<typename TYPE>
458 auto original::forwardChain<TYPE>::begins() const -> Iterator* {
459 return new Iterator(this->beginNode());
460 }
461
462 template<typename TYPE>
463 auto original::forwardChain<TYPE>::ends() const -> Iterator* {
464 return new Iterator(this->findNode(this->size() - 1));
465 }
466
467 template<typename TYPE>
468 auto original::forwardChain<TYPE>::className() const -> std::string {
469 return "forwardChain";
470 }
471
472 template<typename TYPE>
473 original::forwardChain<TYPE>::~forwardChain() {
474 this->chainDestruction();
475 }
476
477#endif //FORWARDCHAIN_H
Definition array.h:15
Definition baseList.h:7
Definition forwardChain.h:42
Definition iterationStream.h:12
Definition iterator.h:11
Definition wrapper.h:12