Bug 1885602 - Part 5: Implement navigating to the SUMO help topic from the menu heade...
[gecko.git] / dom / xslt / base / txList.cpp
bloba361490784dc972c4d32aa12c65955962b20401b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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/. */
6 #include "txList.h"
8 //----------------------------/
9 //- Implementation of txList -/
10 //----------------------------/
12 /**
13 * Default constructor for a txList;
14 **/
16 txList::txList() {
17 firstItem = 0;
18 lastItem = 0;
19 itemCount = 0;
20 } //-- txList;
22 /**
23 * txList destructor, cleans up ListItems, but will not delete the Object
24 * references
26 txList::~txList() { clear(); } //-- ~txList
28 void txList::add(void* objPtr) { insertBefore(objPtr, nullptr); } //-- add
30 /**
31 * Returns the number of items in this txList
32 **/
33 int32_t List::getLength() { return itemCount; } //-- getLength
35 /**
36 * Inserts the given Object pointer as the item just after refItem.
37 * If refItem is a null pointer the Object will be inserted at the
38 * beginning of the txList (ie, insert after nothing).
39 * This method assumes refItem is a member of this list, and since this
40 * is a private method, I feel that's a valid assumption
41 **/
42 void txList::insertAfter(void* objPtr, ListItem* refItem) {
43 insertBefore(objPtr, refItem ? refItem->nextItem : firstItem);
44 } //-- insertAfter
46 /**
47 * Inserts the given Object pointer as the item just before refItem.
48 * If refItem is a null pointer the Object will be inserted at the
49 * end of the txList (ie, insert before nothing).
50 * This method assumes refItem is a member of this list, and since this
51 * is a private method, I feel that's a valid assumption
52 **/
53 void txList::insertBefore(void* objPtr, ListItem* refItem) {
54 ListItem* item = new ListItem;
55 item->objPtr = objPtr;
56 item->nextItem = 0;
57 item->prevItem = 0;
59 //-- if refItem == null insert at end
60 if (!refItem) {
61 //-- add to back of list
62 if (lastItem) {
63 lastItem->nextItem = item;
64 item->prevItem = lastItem;
66 lastItem = item;
67 if (!firstItem) firstItem = item;
68 } else {
69 //-- insert before given item
70 item->nextItem = refItem;
71 item->prevItem = refItem->prevItem;
72 refItem->prevItem = item;
74 if (item->prevItem)
75 item->prevItem->nextItem = item;
76 else
77 firstItem = item;
80 // increase the item count
81 ++itemCount;
82 } //-- insertBefore
84 txList::ListItem* txList::remove(ListItem* item) {
85 if (!item) return item;
87 //-- adjust the previous item's next pointer
88 if (item->prevItem) {
89 item->prevItem->nextItem = item->nextItem;
91 //-- adjust the next item's previous pointer
92 if (item->nextItem) {
93 item->nextItem->prevItem = item->prevItem;
96 //-- adjust first and last items
97 if (item == firstItem) firstItem = item->nextItem;
98 if (item == lastItem) lastItem = item->prevItem;
100 //-- decrease Item count
101 --itemCount;
102 return item;
103 } //-- remove
105 void txList::clear() {
106 ListItem* item = firstItem;
107 while (item) {
108 ListItem* tItem = item;
109 item = item->nextItem;
110 delete tItem;
112 firstItem = 0;
113 lastItem = 0;
114 itemCount = 0;
117 //------------------------------------/
118 //- Implementation of txListIterator -/
119 //------------------------------------/
122 * Creates a new txListIterator for the given txList
123 * @param list, the txList to create an Iterator for
125 txListIterator::txListIterator(txList* list) {
126 this->list = list;
127 currentItem = 0;
128 atEndOfList = false;
129 } //-- txListIterator
132 * Adds the Object pointer to the txList pointed to by this txListIterator.
133 * The Object pointer is inserted as the next item in the txList
134 * based on the current position within the txList
135 * @param objPtr the Object pointer to add to the list
137 void txListIterator::addAfter(void* objPtr) {
138 if (currentItem || !atEndOfList) {
139 list->insertAfter(objPtr, currentItem);
140 } else {
141 list->insertBefore(objPtr, nullptr);
143 } //-- addAfter
146 * Adds the Object pointer to the txList pointed to by this txListIterator.
147 * The Object pointer is inserted as the previous item in the txList
148 * based on the current position within the txList
149 * @param objPtr the Object pointer to add to the list
151 void txListIterator::addBefore(void* objPtr) {
152 if (currentItem || atEndOfList) {
153 list->insertBefore(objPtr, currentItem);
154 } else {
155 list->insertAfter(objPtr, nullptr);
157 } //-- addBefore
160 * Returns true if a successful call to the next() method can be made
161 * @return true if a successful call to the next() method can be made,
162 * otherwise false
164 bool txListIterator::hasNext() {
165 bool hasNext = false;
166 if (currentItem)
167 hasNext = (currentItem->nextItem != 0);
168 else if (!atEndOfList)
169 hasNext = (list->firstItem != 0);
171 return hasNext;
172 } //-- hasNext
175 * Returns the next Object pointer in the list
177 void* txListIterator::next() {
178 void* obj = 0;
179 if (currentItem)
180 currentItem = currentItem->nextItem;
181 else if (!atEndOfList)
182 currentItem = list->firstItem;
184 if (currentItem)
185 obj = currentItem->objPtr;
186 else
187 atEndOfList = true;
189 return obj;
190 } //-- next
193 * Returns the previous Object in the list
195 void* txListIterator::previous() {
196 void* obj = 0;
198 if (currentItem)
199 currentItem = currentItem->prevItem;
200 else if (atEndOfList)
201 currentItem = list->lastItem;
203 if (currentItem) obj = currentItem->objPtr;
205 atEndOfList = false;
207 return obj;
208 } //-- previous
211 * Returns the current Object
213 void* txListIterator::current() {
214 if (currentItem) return currentItem->objPtr;
216 return 0;
217 } //-- current
220 * Removes the Object last returned by the next() or previous() methods;
221 * @return the removed Object pointer
223 void* txListIterator::remove() {
224 void* obj = 0;
225 if (currentItem) {
226 obj = currentItem->objPtr;
227 txList::ListItem* item = currentItem;
228 previous(); //-- make previous item the current item
229 list->remove(item);
230 delete item;
232 return obj;
233 } //-- remove
236 * Resets the current location within the txList to the beginning of the txList
238 void txListIterator::reset() {
239 atEndOfList = false;
240 currentItem = 0;
241 } //-- reset
244 * Move the iterator to right after the last element
246 void txListIterator::resetToEnd() {
247 atEndOfList = true;
248 currentItem = 0;
249 } //-- moveToEnd