7 |
|
#ifndef _NullNode_hpp_ |
8 |
|
#define _NullNode_hpp_ |
9 |
|
|
10 |
< |
#include "Visitor.hpp" |
10 |
> |
#include "Node.hpp" |
11 |
|
|
12 |
|
template<typename Type> |
13 |
< |
class NullNode |
13 |
> |
class NullNode : public Node<Type> |
14 |
|
{ |
15 |
– |
private: |
16 |
– |
Type value; |
17 |
– |
NullNode<Type>* next; |
18 |
– |
NullNode<Type>* previous; |
19 |
– |
bool null; |
15 |
|
public: |
16 |
< |
NullNode() : null(true) {} |
17 |
< |
NullNode(const Type& value) : value(value), null(false) {} |
18 |
< |
void setValue(const Type& value) { this->value = value; } |
19 |
< |
const Type& getValue(void) const { return value; } |
20 |
< |
void addNext(const Type& value); |
26 |
< |
void addPrevious(const Type& value); |
27 |
< |
void removeNext(void); |
28 |
< |
void removePrevious(void); |
29 |
< |
void setNext(NullNode<Type>* next) { this->next = next; } |
30 |
< |
void setPrevious(NullNode<Type>* previous) { this->previous = previous; } |
31 |
< |
NullNode<Type>* getNext(void) const { return next; } |
32 |
< |
NullNode<Type>* getPrevious(void) const { return next; } |
33 |
< |
bool contains(const Type& value, NullNode<Type>* end, bool forward = true); |
34 |
< |
void remove(const Type& value, NullNode<Type>* end, bool forward = true); |
35 |
< |
void accept(Visitor<Type>* visitor, NullNode<Type>* end, bool forward = true); |
16 |
> |
void setNext(NullNode<Type>* next) { this->next = dynamic_cast<Node<Type>*>(next); } |
17 |
> |
void setPrevious(NullNode<Type>* previous) { this->previous = dynamic_cast<Node<Type>*>(previous); } |
18 |
> |
bool contains(const Type& value, Node<Type>* end, bool forward); |
19 |
> |
void remove(const Type& value, Node<Type>* end, bool forward); |
20 |
> |
void accept(Visitor<Type>* visitor, Node<Type>* end, bool forward); |
21 |
|
}; |
22 |
|
|
23 |
|
template<typename Type> |
24 |
< |
void NullNode<Type>::addNext(const Type& value) |
24 |
> |
bool NullNode<Type>::contains(const Type& value, Node<Type>* end, bool forward) |
25 |
|
{ |
26 |
< |
NullNode<Type>* behind = next; |
26 |
> |
Node<Type>* another = forward ? Node<Type>::next : Node<Type>::previous; |
27 |
|
|
28 |
< |
next = new NullNode<Type>(value); |
44 |
< |
next->previous = this; |
45 |
< |
next->next = behind; |
46 |
< |
behind->previous = next; |
28 |
> |
if (end == this) return false; else return another->contains(value, end, forward); |
29 |
|
} |
30 |
|
|
31 |
|
template<typename Type> |
32 |
< |
void NullNode<Type>::addPrevious(const Type& value) |
32 |
> |
void NullNode<Type>::remove(const Type& value, Node<Type>* end, bool forward) |
33 |
|
{ |
34 |
< |
NullNode<Type>* ahead = previous; |
34 |
> |
Node<Type>* another = forward ? Node<Type>::next : Node<Type>::previous; |
35 |
|
|
36 |
< |
previous = new NullNode<Type>(value); |
55 |
< |
previous->next = this; |
56 |
< |
previous->previous = ahead; |
57 |
< |
ahead->next = previous; |
36 |
> |
if (end != this) another->remove(value, end, forward); |
37 |
|
} |
38 |
|
|
39 |
|
template<typename Type> |
40 |
< |
void NullNode<Type>::removeNext(void) |
40 |
> |
void NullNode<Type>::accept(Visitor<Type>* visitor, Node<Type>* end, bool forward) |
41 |
|
{ |
42 |
< |
NullNode<Type>* behind = next->next; |
42 |
> |
Node<Type>* another = forward ? Node<Type>::next : Node<Type>::previous; |
43 |
|
|
65 |
– |
delete next; |
66 |
– |
|
67 |
– |
next = behind; |
68 |
– |
next->previous = this; |
69 |
– |
} |
70 |
– |
|
71 |
– |
template<typename Type> |
72 |
– |
void NullNode<Type>::removePrevious(void) |
73 |
– |
{ |
74 |
– |
NullNode<Type>* ahead = previous->previous; |
75 |
– |
|
76 |
– |
delete previous; |
77 |
– |
|
78 |
– |
previous = ahead; |
79 |
– |
previous->next = this; |
80 |
– |
} |
81 |
– |
|
82 |
– |
template<typename Type> |
83 |
– |
bool NullNode<Type>::contains(const Type& value, NullNode<Type>* end, bool forward) |
84 |
– |
{ |
85 |
– |
NullNode<Type>* another = forward ? next : previous; |
86 |
– |
|
87 |
– |
if (null && end == this) |
88 |
– |
{ |
89 |
– |
return false; |
90 |
– |
} |
91 |
– |
else if (!null && value == this->value) |
92 |
– |
{ |
93 |
– |
return true; |
94 |
– |
} |
95 |
– |
else return another->contains(value, end); |
96 |
– |
} |
97 |
– |
|
98 |
– |
template<typename Type> |
99 |
– |
void NullNode<Type>::remove(const Type& value, NullNode<Type>* end, bool forward) |
100 |
– |
{ |
101 |
– |
NullNode<Type>* another = forward ? next : previous; |
102 |
– |
|
103 |
– |
if (!null && value == this->value) |
104 |
– |
{ |
105 |
– |
next->previous = previous; |
106 |
– |
previous->next = next; |
107 |
– |
|
108 |
– |
delete this; |
109 |
– |
} |
110 |
– |
else if (end != this) another->remove(value, end, forward); |
111 |
– |
} |
112 |
– |
|
113 |
– |
template<typename Type> |
114 |
– |
void NullNode<Type>::accept(Visitor<Type>* visitor, NullNode<Type>* end, bool forward) |
115 |
– |
{ |
116 |
– |
NullNode<Type>* another = forward ? next : previous; |
117 |
– |
|
118 |
– |
if (!null) visitor->visit(value); |
119 |
– |
|
44 |
|
if (end != this) another->accept(visitor, end, forward); |
45 |
|
} |
46 |
|
|