txList.cpp (6345B)
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/. */ 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() { clear(); } //-- ~txList 27 28 void txList::add(void* objPtr) { insertBefore(objPtr, nullptr); } //-- add 29 30 /** 31 * Returns the number of items in this txList 32 **/ 33 int32_t List::getLength() { return itemCount; } //-- getLength 34 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 45 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; 58 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; 65 } 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; 73 74 if (item->prevItem) 75 item->prevItem->nextItem = item; 76 else 77 firstItem = item; 78 } 79 80 // increase the item count 81 ++itemCount; 82 } //-- insertBefore 83 84 txList::ListItem* txList::remove(ListItem* item) { 85 if (!item) return item; 86 87 //-- adjust the previous item's next pointer 88 if (item->prevItem) { 89 item->prevItem->nextItem = item->nextItem; 90 } 91 //-- adjust the next item's previous pointer 92 if (item->nextItem) { 93 item->nextItem->prevItem = item->prevItem; 94 } 95 96 //-- adjust first and last items 97 if (item == firstItem) firstItem = item->nextItem; 98 if (item == lastItem) lastItem = item->prevItem; 99 100 //-- decrease Item count 101 --itemCount; 102 return item; 103 } //-- remove 104 105 void txList::clear() { 106 ListItem* item = firstItem; 107 while (item) { 108 ListItem* tItem = item; 109 item = item->nextItem; 110 delete tItem; 111 } 112 firstItem = 0; 113 lastItem = 0; 114 itemCount = 0; 115 } 116 117 //------------------------------------/ 118 //- Implementation of txListIterator -/ 119 //------------------------------------/ 120 121 /** 122 * Creates a new txListIterator for the given txList 123 * @param list, the txList to create an Iterator for 124 **/ 125 txListIterator::txListIterator(txList* list) { 126 this->list = list; 127 currentItem = 0; 128 atEndOfList = false; 129 } //-- txListIterator 130 131 /** 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 136 **/ 137 void txListIterator::addAfter(void* objPtr) { 138 if (currentItem || !atEndOfList) { 139 list->insertAfter(objPtr, currentItem); 140 } else { 141 list->insertBefore(objPtr, nullptr); 142 } 143 } //-- addAfter 144 145 /** 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 150 **/ 151 void txListIterator::addBefore(void* objPtr) { 152 if (currentItem || atEndOfList) { 153 list->insertBefore(objPtr, currentItem); 154 } else { 155 list->insertAfter(objPtr, nullptr); 156 } 157 } //-- addBefore 158 159 /** 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 163 **/ 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); 170 171 return hasNext; 172 } //-- hasNext 173 174 /** 175 * Returns the next Object pointer in the list 176 **/ 177 void* txListIterator::next() { 178 void* obj = 0; 179 if (currentItem) 180 currentItem = currentItem->nextItem; 181 else if (!atEndOfList) 182 currentItem = list->firstItem; 183 184 if (currentItem) 185 obj = currentItem->objPtr; 186 else 187 atEndOfList = true; 188 189 return obj; 190 } //-- next 191 192 /** 193 * Returns the previous Object in the list 194 **/ 195 void* txListIterator::previous() { 196 void* obj = 0; 197 198 if (currentItem) 199 currentItem = currentItem->prevItem; 200 else if (atEndOfList) 201 currentItem = list->lastItem; 202 203 if (currentItem) obj = currentItem->objPtr; 204 205 atEndOfList = false; 206 207 return obj; 208 } //-- previous 209 210 /** 211 * Returns the current Object 212 **/ 213 void* txListIterator::current() { 214 if (currentItem) return currentItem->objPtr; 215 216 return 0; 217 } //-- current 218 219 /** 220 * Removes the Object last returned by the next() or previous() methods; 221 * @return the removed Object pointer 222 **/ 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; 231 } 232 return obj; 233 } //-- remove 234 235 /** 236 * Resets the current location within the txList to the beginning of the txList 237 **/ 238 void txListIterator::reset() { 239 atEndOfList = false; 240 currentItem = 0; 241 } //-- reset 242 243 /** 244 * Move the iterator to right after the last element 245 **/ 246 void txListIterator::resetToEnd() { 247 atEndOfList = true; 248 currentItem = 0; 249 } //-- moveToEnd