1 |
Douglas Thrift |
1 |
// Null Node |
2 |
|
|
// |
3 |
|
|
// Douglas Thrift |
4 |
|
|
// |
5 |
|
|
// $Id$ |
6 |
|
|
|
7 |
|
|
#ifndef _NullNode_hpp_ |
8 |
|
|
#define _NullNode_hpp_ |
9 |
|
|
|
10 |
Douglas Thrift |
10 |
#include "Visitor.hpp" |
11 |
|
|
|
12 |
Douglas Thrift |
4 |
template<typename Type> |
13 |
Douglas Thrift |
5 |
class NullNode |
14 |
Douglas Thrift |
1 |
{ |
15 |
|
|
private: |
16 |
Douglas Thrift |
5 |
Type value; |
17 |
|
|
NullNode<Type>* next; |
18 |
|
|
NullNode<Type>* previous; |
19 |
Douglas Thrift |
1 |
bool null; |
20 |
|
|
public: |
21 |
Douglas Thrift |
8 |
NullNode() : null(true) {} |
22 |
|
|
NullNode(const Type& value) : value(value), null(false) {} |
23 |
Douglas Thrift |
5 |
void setValue(const Type& value) { this->value = value; } |
24 |
|
|
const Type& getValue(void) const { return value; } |
25 |
|
|
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 |
Douglas Thrift |
6 |
bool contains(const Type& value, NullNode<Type>* end, bool forward = true); |
34 |
|
|
void remove(const Type& value, NullNode<Type>* end, bool forward = true); |
35 |
Douglas Thrift |
10 |
void accept(Visitor<Type>* visitor, NullNode<Type>* end, bool forward = true); |
36 |
Douglas Thrift |
1 |
}; |
37 |
|
|
|
38 |
Douglas Thrift |
3 |
template<typename Type> |
39 |
Douglas Thrift |
5 |
void NullNode<Type>::addNext(const Type& value) |
40 |
|
|
{ |
41 |
|
|
NullNode<Type>* behind = next; |
42 |
|
|
|
43 |
|
|
next = new NullNode<Type>(value); |
44 |
|
|
next->previous = this; |
45 |
|
|
next->next = behind; |
46 |
|
|
behind->previous = next; |
47 |
|
|
} |
48 |
|
|
|
49 |
|
|
template<typename Type> |
50 |
|
|
void NullNode<Type>::addPrevious(const Type& value) |
51 |
|
|
{ |
52 |
|
|
NullNode<Type>* ahead = previous; |
53 |
|
|
|
54 |
|
|
previous = new NullNode<Type>(value); |
55 |
|
|
previous->next = this; |
56 |
|
|
previous->previous = ahead; |
57 |
|
|
ahead->next = previous; |
58 |
|
|
} |
59 |
|
|
|
60 |
|
|
template<typename Type> |
61 |
|
|
void NullNode<Type>::removeNext(void) |
62 |
|
|
{ |
63 |
|
|
NullNode<Type>* behind = next->next; |
64 |
|
|
|
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 |
Douglas Thrift |
6 |
bool NullNode<Type>::contains(const Type& value, NullNode<Type>* end, bool forward) |
84 |
Douglas Thrift |
3 |
{ |
85 |
Douglas Thrift |
6 |
NullNode<Type>* another = forward ? next : previous; |
86 |
Douglas Thrift |
3 |
|
87 |
Douglas Thrift |
4 |
if (null && end == this) |
88 |
|
|
{ |
89 |
|
|
return false; |
90 |
|
|
} |
91 |
Douglas Thrift |
5 |
else if (!null && value == this->value) |
92 |
Douglas Thrift |
4 |
{ |
93 |
|
|
return true; |
94 |
|
|
} |
95 |
|
|
else return another->contains(value, end); |
96 |
Douglas Thrift |
3 |
} |
97 |
|
|
|
98 |
Douglas Thrift |
6 |
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 |
Douglas Thrift |
10 |
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 |
|
|
|
120 |
|
|
if (end != this) another->accept(visitor, end, forward); |
121 |
|
|
} |
122 |
|
|
|
123 |
Douglas Thrift |
1 |
#endif // _NullNode_hpp_ |