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