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