|
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #include "txList.h" |
|
7 |
|
8 //----------------------------/ |
|
9 //- Implementation of txList -/ |
|
10 //----------------------------/ |
|
11 |
|
12 /** |
|
13 * Default constructor for a txList; |
|
14 **/ |
|
15 |
|
16 txList::txList() { |
|
17 firstItem = 0; |
|
18 lastItem = 0; |
|
19 itemCount = 0; |
|
20 } //-- txList; |
|
21 |
|
22 /** |
|
23 * txList destructor, cleans up ListItems, but will not delete the Object |
|
24 * references |
|
25 */ |
|
26 txList::~txList() { |
|
27 clear(); |
|
28 } //-- ~txList |
|
29 |
|
30 nsresult txList::add(void* objPtr) |
|
31 { |
|
32 return insertBefore(objPtr, 0); |
|
33 } //-- add |
|
34 |
|
35 /** |
|
36 * Returns the number of items in this txList |
|
37 **/ |
|
38 int32_t List::getLength() { |
|
39 return itemCount; |
|
40 } //-- getLength |
|
41 |
|
42 |
|
43 /** |
|
44 * Inserts the given Object pointer as the item just after refItem. |
|
45 * If refItem is a null pointer the Object will be inserted at the |
|
46 * beginning of the txList (ie, insert after nothing). |
|
47 * This method assumes refItem is a member of this list, and since this |
|
48 * is a private method, I feel that's a valid assumption |
|
49 **/ |
|
50 nsresult txList::insertAfter(void* objPtr, ListItem* refItem) |
|
51 { |
|
52 //-- if refItem == null insert at front |
|
53 if (!refItem) |
|
54 return insertBefore(objPtr, firstItem); |
|
55 return insertBefore(objPtr, refItem->nextItem); |
|
56 } //-- insertAfter |
|
57 |
|
58 /** |
|
59 * Inserts the given Object pointer as the item just before refItem. |
|
60 * If refItem is a null pointer the Object will be inserted at the |
|
61 * end of the txList (ie, insert before nothing). |
|
62 * This method assumes refItem is a member of this list, and since this |
|
63 * is a private method, I feel that's a valid assumption |
|
64 **/ |
|
65 nsresult txList::insertBefore(void* objPtr, ListItem* refItem) |
|
66 { |
|
67 ListItem* item = new ListItem; |
|
68 NS_ENSURE_TRUE(item, NS_ERROR_OUT_OF_MEMORY); |
|
69 |
|
70 item->objPtr = objPtr; |
|
71 item->nextItem = 0; |
|
72 item->prevItem = 0; |
|
73 |
|
74 //-- if refItem == null insert at end |
|
75 if (!refItem) { |
|
76 //-- add to back of list |
|
77 if (lastItem) { |
|
78 lastItem->nextItem = item; |
|
79 item->prevItem = lastItem; |
|
80 } |
|
81 lastItem = item; |
|
82 if (!firstItem) |
|
83 firstItem = item; |
|
84 } |
|
85 else { |
|
86 //-- insert before given item |
|
87 item->nextItem = refItem; |
|
88 item->prevItem = refItem->prevItem; |
|
89 refItem->prevItem = item; |
|
90 |
|
91 if (item->prevItem) |
|
92 item->prevItem->nextItem = item; |
|
93 else |
|
94 firstItem = item; |
|
95 } |
|
96 |
|
97 // increase the item count |
|
98 ++itemCount; |
|
99 |
|
100 return NS_OK; |
|
101 } //-- insertBefore |
|
102 |
|
103 txList::ListItem* txList::remove(ListItem* item) { |
|
104 |
|
105 if (!item) |
|
106 return item; |
|
107 |
|
108 //-- adjust the previous item's next pointer |
|
109 if (item->prevItem) { |
|
110 item->prevItem->nextItem = item->nextItem; |
|
111 } |
|
112 //-- adjust the next item's previous pointer |
|
113 if (item->nextItem) { |
|
114 item->nextItem->prevItem = item->prevItem; |
|
115 } |
|
116 |
|
117 //-- adjust first and last items |
|
118 if (item == firstItem) |
|
119 firstItem = item->nextItem; |
|
120 if (item == lastItem) |
|
121 lastItem = item->prevItem; |
|
122 |
|
123 //-- decrease Item count |
|
124 --itemCount; |
|
125 return item; |
|
126 } //-- remove |
|
127 |
|
128 void txList::clear() |
|
129 { |
|
130 ListItem* item = firstItem; |
|
131 while (item) { |
|
132 ListItem* tItem = item; |
|
133 item = item->nextItem; |
|
134 delete tItem; |
|
135 } |
|
136 firstItem = 0; |
|
137 lastItem = 0; |
|
138 itemCount = 0; |
|
139 } |
|
140 |
|
141 //------------------------------------/ |
|
142 //- Implementation of txListIterator -/ |
|
143 //------------------------------------/ |
|
144 |
|
145 |
|
146 /** |
|
147 * Creates a new txListIterator for the given txList |
|
148 * @param list, the txList to create an Iterator for |
|
149 **/ |
|
150 txListIterator::txListIterator(txList* list) { |
|
151 this->list = list; |
|
152 currentItem = 0; |
|
153 atEndOfList = false; |
|
154 } //-- txListIterator |
|
155 |
|
156 /** |
|
157 * Adds the Object pointer to the txList pointed to by this txListIterator. |
|
158 * The Object pointer is inserted as the next item in the txList |
|
159 * based on the current position within the txList |
|
160 * @param objPtr the Object pointer to add to the list |
|
161 **/ |
|
162 nsresult txListIterator::addAfter(void* objPtr) |
|
163 { |
|
164 if (currentItem || !atEndOfList) |
|
165 return list->insertAfter(objPtr, currentItem); |
|
166 return list->insertBefore(objPtr, 0); |
|
167 |
|
168 } //-- addAfter |
|
169 |
|
170 /** |
|
171 * Adds the Object pointer to the txList pointed to by this txListIterator. |
|
172 * The Object pointer is inserted as the previous item in the txList |
|
173 * based on the current position within the txList |
|
174 * @param objPtr the Object pointer to add to the list |
|
175 **/ |
|
176 nsresult txListIterator::addBefore(void* objPtr) |
|
177 { |
|
178 if (currentItem || atEndOfList) |
|
179 return list->insertBefore(objPtr, currentItem); |
|
180 return list->insertAfter(objPtr, 0); |
|
181 |
|
182 } //-- addBefore |
|
183 |
|
184 /** |
|
185 * Returns true if a successful call to the next() method can be made |
|
186 * @return true if a successful call to the next() method can be made, |
|
187 * otherwise false |
|
188 **/ |
|
189 bool txListIterator::hasNext() { |
|
190 bool hasNext = false; |
|
191 if (currentItem) |
|
192 hasNext = (currentItem->nextItem != 0); |
|
193 else if (!atEndOfList) |
|
194 hasNext = (list->firstItem != 0); |
|
195 |
|
196 return hasNext; |
|
197 } //-- hasNext |
|
198 |
|
199 /** |
|
200 * Returns the next Object pointer in the list |
|
201 **/ |
|
202 void* txListIterator::next() { |
|
203 |
|
204 void* obj = 0; |
|
205 if (currentItem) |
|
206 currentItem = currentItem->nextItem; |
|
207 else if (!atEndOfList) |
|
208 currentItem = list->firstItem; |
|
209 |
|
210 if (currentItem) |
|
211 obj = currentItem->objPtr; |
|
212 else |
|
213 atEndOfList = true; |
|
214 |
|
215 return obj; |
|
216 } //-- next |
|
217 |
|
218 /** |
|
219 * Returns the previous Object in the list |
|
220 **/ |
|
221 void* txListIterator::previous() { |
|
222 |
|
223 void* obj = 0; |
|
224 |
|
225 if (currentItem) |
|
226 currentItem = currentItem->prevItem; |
|
227 else if (atEndOfList) |
|
228 currentItem = list->lastItem; |
|
229 |
|
230 if (currentItem) |
|
231 obj = currentItem->objPtr; |
|
232 |
|
233 atEndOfList = false; |
|
234 |
|
235 return obj; |
|
236 } //-- previous |
|
237 |
|
238 /** |
|
239 * Returns the current Object |
|
240 **/ |
|
241 void* txListIterator::current() { |
|
242 |
|
243 if (currentItem) |
|
244 return currentItem->objPtr; |
|
245 |
|
246 return 0; |
|
247 } //-- current |
|
248 |
|
249 /** |
|
250 * Removes the Object last returned by the next() or previous() methods; |
|
251 * @return the removed Object pointer |
|
252 **/ |
|
253 void* txListIterator::remove() { |
|
254 |
|
255 void* obj = 0; |
|
256 if (currentItem) { |
|
257 obj = currentItem->objPtr; |
|
258 txList::ListItem* item = currentItem; |
|
259 previous(); //-- make previous item the current item |
|
260 list->remove(item); |
|
261 delete item; |
|
262 } |
|
263 return obj; |
|
264 } //-- remove |
|
265 |
|
266 /** |
|
267 * Resets the current location within the txList to the beginning of the txList |
|
268 **/ |
|
269 void txListIterator::reset() { |
|
270 atEndOfList = false; |
|
271 currentItem = 0; |
|
272 } //-- reset |
|
273 |
|
274 /** |
|
275 * Move the iterator to right after the last element |
|
276 **/ |
|
277 void txListIterator::resetToEnd() { |
|
278 atEndOfList = true; |
|
279 currentItem = 0; |
|
280 } //-- moveToEnd |