3 karl 1.4 // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
4 // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
5 // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
|
13 tony 1.1 //
14 // Permission is hereby granted, free of charge, to any person obtaining a copy
15 // of this software and associated documentation files (the "Software"), to
16 // deal in the Software without restriction, including without limitation the
17 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
18 // sell copies of the Software, and to permit persons to whom the Software is
19 // furnished to do so, subject to the following conditions:
20 //
21 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
22 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
23 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
24 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
25 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
26 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
27 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 //==============================================================================
31 //
32 // Author: Dong Xiang, EMC Corporation (xiang_dong@emc.com)
33 //
34 tony 1.1 // Modified By:
35 //
36 //%/////////////////////////////////////////////////////////////////////////////
37
38 #include "List.h"
39
40 #include <Pegasus/Common/Exception.h>
41
42
43 PEGASUS_NAMESPACE_BEGIN
44
45 /////////////////////////////////////////////////////////////////////////////
46 // ListNode
47 /////////////////////////////////////////////////////////////////////////////
48 class ListNode
49 {
50 public:
51 ListNode(void* element);
52 ~ListNode();
53
54 ListNode* getNext() const;
55 tony 1.1 void setNext(ListNode* next);
56
57 ListNode* getPrevious() const;
58 void setPrevious(ListNode* previous);
59
60 void* getElement() const;
61
62 private:
63 void* _element;
64 ListNode* _next;
65 ListNode* _previous;
66 };
67
68 ListNode::ListNode(void* element)
69 :_element(element),_next(NULL),_previous(NULL)
70 {
71 }
72 ListNode::~ListNode()
73 {
74 }
75
76 tony 1.1 ListNode* ListNode::getNext() const
77 {
78 return _next;
79 }
80 void ListNode::setNext(ListNode* next)
81 {
82 _next = next;
83 }
84
85 ListNode* ListNode::getPrevious() const
86 {
87 return _previous;
88 }
89 void ListNode::setPrevious(ListNode* previous)
90 {
91 _previous = previous;
92 }
93
94 void* ListNode::getElement() const
95 {
96 return _element;
97 tony 1.1 }
98
99 /////////////////////////////////////////////////////////////////////////////
100 // PtrListIterator
101 /////////////////////////////////////////////////////////////////////////////
102 class PtrListIterator : public Iterator
103 {
104 public:
105 PtrListIterator(ListNode* first);
106 ~PtrListIterator();
107
108 Boolean hasNext();
109 void* next();
110 void remove();
111
112 private:
113 ListNode* _lead;
114 };
115
116 PtrListIterator::PtrListIterator(ListNode* lead)
117 :_lead(lead)
118 tony 1.1 {
119 }
120 PtrListIterator::~PtrListIterator()
121 {
122 }
123 Boolean PtrListIterator::hasNext()
124 {
125 return (_lead!=NULL)?true:false;
126 }
127 void* PtrListIterator::next()
128 {
129 if(_lead==NULL)
130 throw IndexOutOfBoundsException();
131
132 void* element = _lead->getElement();
133 _lead = _lead->getNext();
134
135 return element;
136 }
137 void PtrListIterator::remove()
138 {
139 tony 1.1 throw Exception("Not Supported feature");
140 }
141
142
143 /////////////////////////////////////////////////////////////////////////////
144 // PtrListRep
145 /////////////////////////////////////////////////////////////////////////////
146 class PtrListRep
147 {
148 public:
149 PtrListRep();
150 ~PtrListRep();
151
152 void add(void* element);
153 void remove(void* element);
154
155 Iterator* iterator();
156
157 private:
158 ListNode* _first;
159 ListNode* _last;
160 tony 1.1 };
161
162 PtrListRep::PtrListRep()
163 :_first(NULL),_last(NULL)
164 {
165 }
166 PtrListRep::~PtrListRep()
167 {
168 // reemove all the nodes
169 while(_last!=NULL)
170 {
171 ListNode* n = _last->getPrevious();
172 delete _last;
173 _last = n;
174 }
175 }
176 void PtrListRep::add(void* element)
177 {
178 ListNode* n = new ListNode(element);
179
180 if(_last==NULL)
181 tony 1.1 { // this is the very first one
182 _first = _last = n;
183 }
184 else { // append to the end
185 _last->setNext(n);
186 n->setPrevious(_last);
187 _last = n;
188 }
189 }
190 void PtrListRep::remove(void* element)
191 {
192 if(element!=NULL && _first!=NULL)
193 {
194 for(ListNode* n=_first; n!=NULL; n=n->getNext())
195 {
196 void* el = n->getElement();
197 if(el==element)
198 { // remove the node
199 ListNode* prev = n->getPrevious();
200 ListNode* next = n->getNext();
201
202 tony 1.1 if(prev!=NULL)
203 prev->setNext(next);
204 else // the node is the very first
205 _first = next;
206
207 if(next!=NULL)
208 next->setPrevious(prev);
209 else // the node is the last
210 _last = prev;
211
212 delete n;
213 break;
214 }
215 }
216 }
217 }
218 Iterator* PtrListRep::iterator()
219 {
220 return new PtrListIterator(_first);
221 }
222
223 tony 1.1 /////////////////////////////////////////////////////////////////////////////
224 // List
225 /////////////////////////////////////////////////////////////////////////////
226 PtrList::PtrList()
227 :_rep(new PtrListRep())
228 {
229 }
230 PtrList::~PtrList()
231 {
232 if(_rep!=NULL)
|