1 |
Douglas Thrift |
13 |
// Node |
2 |
|
|
// |
3 |
|
|
// Douglas Thrift |
4 |
|
|
// |
5 |
|
|
// $Id$ |
6 |
|
|
|
7 |
|
|
#ifndef _Node_hpp_ |
8 |
|
|
#define _Node_hpp_ |
9 |
|
|
|
10 |
|
|
#include "Visitor.hpp" |
11 |
|
|
|
12 |
|
|
template<typename Type> |
13 |
|
|
class Node |
14 |
|
|
{ |
15 |
|
|
private: |
16 |
|
|
Type value; |
17 |
|
|
protected: |
18 |
|
|
Node<Type>* next; |
19 |
|
|
Node<Type>* previous; |
20 |
|
|
public: |
21 |
|
|
Node() {} |
22 |
|
|
Node(const Type& value) : value(value) {} |
23 |
|
|
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(Node<Type>* next) { this->next = next; } |
30 |
|
|
void setPrevious(Node<Type>* previous) { this->previous = previous; } |
31 |
|
|
Node<Type>* getNext(void) const { return next; } |
32 |
|
|
Node<Type>* getPrevious(void) const { return previous; } |
33 |
|
|
virtual bool contains(const Type& value, Node<Type>* end, bool forward); |
34 |
|
|
virtual void remove(const Type& value, Node<Type>* end, bool forward); |
35 |
|
|
virtual void accept(Visitor<Type>* visitor, Node<Type>* end, bool forward); |
36 |
|
|
}; |
37 |
|
|
|
38 |
|
|
template<typename Type> |
39 |
|
|
void Node<Type>::addNext(const Type& value) |
40 |
|
|
{ |
41 |
|
|
Node<Type>* behind = next; |
42 |
|
|
|
43 |
|
|
next = new Node<Type>(value); |
44 |
|
|
next->previous = this; |
45 |
|
|
next->next = behind; |
46 |
|
|
behind->previous = next; |
47 |
|
|
} |
48 |
|
|
|
49 |
|
|
template<typename Type> |
50 |
|
|
void Node<Type>::addPrevious(const Type& value) |
51 |
|
|
{ |
52 |
|
|
Node<Type>* ahead = previous; |
53 |
|
|
|
54 |
|
|
previous = new Node<Type>(value); |
55 |
|
|
previous->next = this; |
56 |
|
|
previous->previous = ahead; |
57 |
|
|
ahead->next = previous; |
58 |
|
|
} |
59 |
|
|
|
60 |
|
|
template<typename Type> |
61 |
|
|
void Node<Type>::removeNext(void) |
62 |
|
|
{ |
63 |
|
|
Node<Type>* behind = next->next; |
64 |
|
|
|
65 |
|
|
delete next; |
66 |
|
|
|
67 |
|
|
next = behind; |
68 |
|
|
next->previous = this; |
69 |
|
|
} |
70 |
|
|
|
71 |
|
|
template<typename Type> |
72 |
|
|
void Node<Type>::removePrevious(void) |
73 |
|
|
{ |
74 |
|
|
Node<Type>* ahead = previous->previous; |
75 |
|
|
|
76 |
|
|
delete previous; |
77 |
|
|
|
78 |
|
|
previous = ahead; |
79 |
|
|
previous->next = this; |
80 |
|
|
} |
81 |
|
|
|
82 |
|
|
template<typename Type> |
83 |
|
|
bool Node<Type>::contains(const Type& value, Node<Type>* end, bool forward) |
84 |
|
|
{ |
85 |
|
|
Node<Type>* another = forward ? next : previous; |
86 |
|
|
|
87 |
|
|
if (value == this->value) return true; else return another->contains(value, end, forward); |
88 |
|
|
} |
89 |
|
|
|
90 |
|
|
template<typename Type> |
91 |
|
|
void Node<Type>::remove(const Type& value, Node<Type>* end, bool forward) |
92 |
|
|
{ |
93 |
|
|
Node<Type>* another = forward ? next : previous; |
94 |
|
|
|
95 |
|
|
if (value == this->value) |
96 |
|
|
{ |
97 |
|
|
next->previous = previous; |
98 |
|
|
previous->next = next; |
99 |
|
|
|
100 |
|
|
delete this; |
101 |
|
|
} |
102 |
|
|
else another->remove(value, end, forward); |
103 |
|
|
} |
104 |
|
|
|
105 |
|
|
template<typename Type> |
106 |
|
|
void Node<Type>::accept(Visitor<Type>* visitor, Node<Type>* end, bool forward) |
107 |
|
|
{ |
108 |
|
|
Node<Type>* another = forward ? next : previous; |
109 |
|
|
|
110 |
|
|
visitor->visit(value); |
111 |
|
|
another->accept(visitor, end, forward); |
112 |
|
|
} |
113 |
|
|
|
114 |
|
|
#endif // _Node_hpp_ |