ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/ccscs20/NullNode.hpp
Revision: 7
Committed: 2004-05-14T00:13:01-07:00 (21 years, 1 month ago) by Douglas Thrift
File size: 2486 byte(s)
Log Message:
Eraser!

File Contents

# User Rev Content
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 4 template<typename Type>
11 Douglas Thrift 5 class NullNode
12 Douglas Thrift 1 {
13     private:
14 Douglas Thrift 5 Type value;
15     NullNode<Type>* next;
16     NullNode<Type>* previous;
17 Douglas Thrift 1 bool null;
18     public:
19 Douglas Thrift 5 NullNode() : next(NULL), previous(NULL), null(true) {}
20 Douglas Thrift 6 NullNode(const Type& value) : value(value), next(NULL), previous(NULL), null(false) {}
21 Douglas Thrift 5 void setValue(const Type& value) { this->value = value; }
22     const Type& getValue(void) const { return value; }
23     void addNext(const Type& value);
24     void addPrevious(const Type& value);
25     void removeNext(void);
26     void removePrevious(void);
27     void setNext(NullNode<Type>* next) { this->next = next; }
28     void setPrevious(NullNode<Type>* previous) { this->previous = previous; }
29     NullNode<Type>* getNext(void) const { return next; }
30     NullNode<Type>* getPrevious(void) const { return next; }
31 Douglas Thrift 6 bool contains(const Type& value, NullNode<Type>* end, bool forward = true);
32     void remove(const Type& value, NullNode<Type>* end, bool forward = true);
33 Douglas Thrift 1 };
34    
35 Douglas Thrift 3 template<typename Type>
36 Douglas Thrift 5 void NullNode<Type>::addNext(const Type& value)
37     {
38     NullNode<Type>* behind = next;
39    
40     next = new NullNode<Type>(value);
41     next->previous = this;
42     next->next = behind;
43     behind->previous = next;
44     }
45    
46     template<typename Type>
47     void NullNode<Type>::addPrevious(const Type& value)
48     {
49     NullNode<Type>* ahead = previous;
50    
51     previous = new NullNode<Type>(value);
52     previous->next = this;
53     previous->previous = ahead;
54     ahead->next = previous;
55     }
56    
57     template<typename Type>
58     void NullNode<Type>::removeNext(void)
59     {
60     NullNode<Type>* behind = next->next;
61    
62     delete next;
63    
64     next = behind;
65     next->previous = this;
66     }
67    
68     template<typename Type>
69     void NullNode<Type>::removePrevious(void)
70     {
71     NullNode<Type>* ahead = previous->previous;
72    
73     delete previous;
74    
75     previous = ahead;
76     previous->next = this;
77     }
78    
79     template<typename Type>
80 Douglas Thrift 6 bool NullNode<Type>::contains(const Type& value, NullNode<Type>* end, bool forward)
81 Douglas Thrift 3 {
82 Douglas Thrift 6 NullNode<Type>* another = forward ? next : previous;
83 Douglas Thrift 3
84 Douglas Thrift 4 if (null && end == this)
85     {
86     return false;
87     }
88 Douglas Thrift 5 else if (!null && value == this->value)
89 Douglas Thrift 4 {
90     return true;
91     }
92     else return another->contains(value, end);
93 Douglas Thrift 3 }
94    
95 Douglas Thrift 6 template<typename Type>
96     void NullNode<Type>::remove(const Type& value, NullNode<Type>* end, bool forward)
97     {
98     NullNode<Type>* another = forward ? next : previous;
99    
100     if (!null && value == this->value)
101     {
102     next->previous = previous;
103     previous->next = next;
104    
105     delete this;
106     }
107     else if (end != this) another->remove(value, end, forward);
108     }
109    
110 Douglas Thrift 1 #endif // _NullNode_hpp_

Properties

Name Value
svn:eol-style native
svn:keywords Id