4#include "singleDirectionIterator.h"
9 template <
typename TYPE>
11 class forwardChainNode final :
public wrapper<TYPE>{
14 friend class forwardChain;
17 forwardChainNode* next;
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);
32 forwardChainNode* begin_;
34 forwardChainNode* beginNode()
const;
35 forwardChainNode* findNode(int64_t index)
const;
37 void firstAdd(forwardChainNode* node);
38 forwardChainNode* lastDelete();
39 void chainDestruction();
41 class Iterator final :
public singleDirectionIterator<TYPE>
43 explicit Iterator(forwardChainNode* ptr);
46 Iterator(
const Iterator& other);
47 Iterator& operator=(
const Iterator& other);
48 Iterator* clone()
const override;
51 [[nodiscard]] std::string className()
const override;
54 explicit forwardChain();
55 forwardChain(
const forwardChain& other);
56 forwardChain(std::initializer_list<TYPE> list);
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;
74 [[nodiscard]] std::string className()
const override;
75 ~forwardChain()
override;
79 template <
typename TYPE>
80 original::forwardChain<TYPE>::forwardChainNode::forwardChainNode(
const TYPE& data, forwardChainNode* next)
81 : data_(data), next(next) {}
83 template<
typename TYPE>
84 original::forwardChain<TYPE>::forwardChainNode::forwardChainNode(
const forwardChainNode &other)
85 : data_(other.data_), next(other.next) {}
87 template<
typename TYPE>
88 auto original::forwardChain<TYPE>::forwardChainNode::operator=(
89 const forwardChainNode &other) -> forwardChainNode & {
97 template<
typename TYPE>
98 auto original::forwardChain<TYPE>::forwardChainNode::getVal() -> TYPE& {
102 template<
typename TYPE>
103 auto original::forwardChain<TYPE>::forwardChainNode::getVal() const -> const TYPE& {
107 template<
typename TYPE>
108 auto original::forwardChain<TYPE>::forwardChainNode::setVal(TYPE data) ->
void {
112 template<
typename TYPE>
113 auto original::forwardChain<TYPE>::forwardChainNode::getPPrev() const -> forwardChainNode* {
114 throw unSupportedMethodError();
117 template<
typename TYPE>
118 auto original::forwardChain<TYPE>::forwardChainNode::getPNext() const -> forwardChainNode* {
122 template<
typename TYPE>
123 auto original::forwardChain<TYPE>::forwardChainNode::setPNext(forwardChainNode *new_next) ->
void {
124 this->next = new_next;
127 template<
typename TYPE>
128 auto original::forwardChain<TYPE>::forwardChainNode::connect(
129 forwardChainNode *prev, forwardChainNode *next) ->
void {
130 if (prev !=
nullptr) prev->setPNext(next);
133 template <
typename TYPE>
134 auto original::forwardChain<TYPE>::beginNode() const -> forwardChainNode*
136 return this->begin_->getPNext();
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++)
145 cur = cur->getPNext();
150 template <
typename TYPE>
151 auto original::forwardChain<TYPE>::chainInit() ->
void
153 auto* pivot =
new forwardChainNode();
155 this->begin_ = pivot;
158 template <
typename TYPE>
159 auto original::forwardChain<TYPE>::firstAdd(forwardChainNode* node) ->
void
161 forwardChainNode::connect(this->findNode(0), node);
165 template <
typename TYPE>
166 auto original::forwardChain<TYPE>::lastDelete() -> forwardChainNode*
168 auto* last = this->beginNode();
174 template <
typename TYPE>
175 auto original::forwardChain<TYPE>::chainDestruction() ->
void
177 auto* cur = this->begin_;
180 auto* next = cur->getPNext();
186 template<
typename TYPE>
187 original::forwardChain<TYPE>::Iterator::Iterator(forwardChainNode *ptr)
188 : singleDirectionIterator<TYPE>(ptr) {}
190 template<
typename TYPE>
191 original::forwardChain<TYPE>::Iterator::Iterator(
const Iterator &other)
192 : singleDirectionIterator<TYPE>(nullptr) {
193 this->operator=(other);
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);
203 template<
typename TYPE>
204 auto original::forwardChain<TYPE>::Iterator::clone() const ->
Iterator* {
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;
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;
220 template<
typename TYPE>
221 auto original::forwardChain<TYPE>::Iterator::className() const -> std::
string {
222 return "forwardChain::Iterator";
225 template<
typename TYPE>
226 original::forwardChain<TYPE>::forwardChain() : size_(0)
231 template<
typename TYPE>
232 original::forwardChain<TYPE>::forwardChain(
const forwardChain &other) : forwardChain() {
233 this->operator=(other);
236 template<
typename TYPE>
237 original::forwardChain<TYPE>::forwardChain(std::initializer_list<TYPE> list) : forwardChain() {
239 auto* cur_node =
new forwardChainNode(e);
240 if (this->size() == 0)
242 this->firstAdd(cur_node);
245 auto* end = this->findNode(this->size() - 1);
246 forwardChainNode::connect(end, cur_node);
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)
258 this->firstAdd(cur_node);
261 auto* end = this->findNode(this->size() - 1);
262 forwardChainNode::connect(end, cur_node);
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();
288 template<
typename TYPE>
289 original::forwardChain<TYPE>::forwardChain(forwardChain &&other) noexcept : forwardChain() {
290 this->operator=(std::move(other));
293 template<
typename TYPE>
294 original::forwardChain<TYPE> & original::forwardChain<TYPE>::operator=(forwardChain &&other)
noexcept {
298 this->chainDestruction();
299 this->begin_ = other.begin_;
300 this->size_ = other.size_;
305 template<
typename TYPE>
306 auto original::forwardChain<TYPE>::size() const -> uint32_t {
310 template<
typename TYPE>
311 auto original::forwardChain<TYPE>::get(int64_t index)
const -> TYPE {
312 if (this->indexOutOfBound(index)){
313 throw outOfBoundError();
315 auto* cur = this->findNode(this->parseNegIndex(index));
316 return cur->getVal();
319 template<
typename TYPE>
320 auto original::forwardChain<TYPE>::operator[](int64_t index) -> TYPE& {
321 if (this->indexOutOfBound(index)){
322 throw outOfBoundError();
324 auto* cur = this->findNode(this->parseNegIndex(index));
325 return cur->getVal();
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();
333 auto* cur = this->findNode(this->parseNegIndex(index));
337 template<
typename TYPE>
338 auto original::forwardChain<TYPE>::indexOf(
const TYPE &e)
const -> uint32_t {
340 for (
auto* current = this->begin_; current !=
nullptr; current = current->getPNext()) {
341 if (current->getVal() == e) {
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);
355 auto* next = this->beginNode();
356 forwardChainNode::connect(this->begin_, new_node);
357 forwardChainNode::connect(new_node, next);
362 template<
typename TYPE>
363 auto original::forwardChain<TYPE>::push(int64_t index,
const TYPE &e) ->
void {
364 index = this->parseNegIndex(index);
367 }
else if (index == this->size()){
370 if (this->indexOutOfBound(index)){
371 throw outOfBoundError();
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);
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);
388 auto* end = this->findNode(this->size() - 1);
389 forwardChainNode::connect(end, new_node);
394 template<
typename TYPE>
395 auto original::forwardChain<TYPE>::popBegin() -> TYPE {
397 if (this->size() == 0){
398 throw noElementError();
401 res = this->beginNode()->getVal();
402 if (this->size() == 1){
403 delete this->lastDelete();
405 auto* del = this->beginNode();
406 auto* new_begin = del->getPNext();
408 forwardChainNode::connect(this->begin_, new_begin);
414 template<
typename TYPE>
415 auto original::forwardChain<TYPE>::pop(int64_t index) -> TYPE {
416 index = this->parseNegIndex(index);
418 return this->popBegin();
420 if (index == this->size() - 1){
421 return this->popEnd();
423 if (this->indexOutOfBound(index)){
424 throw outOfBoundError();
427 auto* prev = this->findNode(index - 1);
428 auto* cur = prev->getPNext();
430 auto* next = cur->getPNext();
431 forwardChainNode::connect(prev, next);
437 template<
typename TYPE>
438 auto original::forwardChain<TYPE>::popEnd() -> TYPE {
440 if (this->size() == 0){
441 throw noElementError();
443 if (this->size() == 1){
444 res = this->beginNode()->getVal();
445 delete this->lastDelete();
447 auto* new_end = this->findNode(this->size() - 2);
448 auto* end = new_end->getPNext();
451 forwardChainNode::connect(new_end,
nullptr);
457 template<
typename TYPE>
458 auto original::forwardChain<TYPE>::begins() const ->
Iterator* {
459 return new Iterator(this->beginNode());
462 template<
typename TYPE>
463 auto original::forwardChain<TYPE>::ends() const ->
Iterator* {
464 return new Iterator(this->findNode(this->size() - 1));
467 template<
typename TYPE>
468 auto original::forwardChain<TYPE>::className() const -> std::
string {
469 return "forwardChain";
472 template<
typename TYPE>
473 original::forwardChain<TYPE>::~forwardChain() {
474 this->chainDestruction();
Definition forwardChain.h:42
Definition iterationStream.h:12