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