michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "txList.h" michael@0: michael@0: //----------------------------/ michael@0: //- Implementation of txList -/ michael@0: //----------------------------/ michael@0: michael@0: /** michael@0: * Default constructor for a txList; michael@0: **/ michael@0: michael@0: txList::txList() { michael@0: firstItem = 0; michael@0: lastItem = 0; michael@0: itemCount = 0; michael@0: } //-- txList; michael@0: michael@0: /** michael@0: * txList destructor, cleans up ListItems, but will not delete the Object michael@0: * references michael@0: */ michael@0: txList::~txList() { michael@0: clear(); michael@0: } //-- ~txList michael@0: michael@0: nsresult txList::add(void* objPtr) michael@0: { michael@0: return insertBefore(objPtr, 0); michael@0: } //-- add michael@0: michael@0: /** michael@0: * Returns the number of items in this txList michael@0: **/ michael@0: int32_t List::getLength() { michael@0: return itemCount; michael@0: } //-- getLength michael@0: michael@0: michael@0: /** michael@0: * Inserts the given Object pointer as the item just after refItem. michael@0: * If refItem is a null pointer the Object will be inserted at the michael@0: * beginning of the txList (ie, insert after nothing). michael@0: * This method assumes refItem is a member of this list, and since this michael@0: * is a private method, I feel that's a valid assumption michael@0: **/ michael@0: nsresult txList::insertAfter(void* objPtr, ListItem* refItem) michael@0: { michael@0: //-- if refItem == null insert at front michael@0: if (!refItem) michael@0: return insertBefore(objPtr, firstItem); michael@0: return insertBefore(objPtr, refItem->nextItem); michael@0: } //-- insertAfter michael@0: michael@0: /** michael@0: * Inserts the given Object pointer as the item just before refItem. michael@0: * If refItem is a null pointer the Object will be inserted at the michael@0: * end of the txList (ie, insert before nothing). michael@0: * This method assumes refItem is a member of this list, and since this michael@0: * is a private method, I feel that's a valid assumption michael@0: **/ michael@0: nsresult txList::insertBefore(void* objPtr, ListItem* refItem) michael@0: { michael@0: ListItem* item = new ListItem; michael@0: NS_ENSURE_TRUE(item, NS_ERROR_OUT_OF_MEMORY); michael@0: michael@0: item->objPtr = objPtr; michael@0: item->nextItem = 0; michael@0: item->prevItem = 0; michael@0: michael@0: //-- if refItem == null insert at end michael@0: if (!refItem) { michael@0: //-- add to back of list michael@0: if (lastItem) { michael@0: lastItem->nextItem = item; michael@0: item->prevItem = lastItem; michael@0: } michael@0: lastItem = item; michael@0: if (!firstItem) michael@0: firstItem = item; michael@0: } michael@0: else { michael@0: //-- insert before given item michael@0: item->nextItem = refItem; michael@0: item->prevItem = refItem->prevItem; michael@0: refItem->prevItem = item; michael@0: michael@0: if (item->prevItem) michael@0: item->prevItem->nextItem = item; michael@0: else michael@0: firstItem = item; michael@0: } michael@0: michael@0: // increase the item count michael@0: ++itemCount; michael@0: michael@0: return NS_OK; michael@0: } //-- insertBefore michael@0: michael@0: txList::ListItem* txList::remove(ListItem* item) { michael@0: michael@0: if (!item) michael@0: return item; michael@0: michael@0: //-- adjust the previous item's next pointer michael@0: if (item->prevItem) { michael@0: item->prevItem->nextItem = item->nextItem; michael@0: } michael@0: //-- adjust the next item's previous pointer michael@0: if (item->nextItem) { michael@0: item->nextItem->prevItem = item->prevItem; michael@0: } michael@0: michael@0: //-- adjust first and last items michael@0: if (item == firstItem) michael@0: firstItem = item->nextItem; michael@0: if (item == lastItem) michael@0: lastItem = item->prevItem; michael@0: michael@0: //-- decrease Item count michael@0: --itemCount; michael@0: return item; michael@0: } //-- remove michael@0: michael@0: void txList::clear() michael@0: { michael@0: ListItem* item = firstItem; michael@0: while (item) { michael@0: ListItem* tItem = item; michael@0: item = item->nextItem; michael@0: delete tItem; michael@0: } michael@0: firstItem = 0; michael@0: lastItem = 0; michael@0: itemCount = 0; michael@0: } michael@0: michael@0: //------------------------------------/ michael@0: //- Implementation of txListIterator -/ michael@0: //------------------------------------/ michael@0: michael@0: michael@0: /** michael@0: * Creates a new txListIterator for the given txList michael@0: * @param list, the txList to create an Iterator for michael@0: **/ michael@0: txListIterator::txListIterator(txList* list) { michael@0: this->list = list; michael@0: currentItem = 0; michael@0: atEndOfList = false; michael@0: } //-- txListIterator michael@0: michael@0: /** michael@0: * Adds the Object pointer to the txList pointed to by this txListIterator. michael@0: * The Object pointer is inserted as the next item in the txList michael@0: * based on the current position within the txList michael@0: * @param objPtr the Object pointer to add to the list michael@0: **/ michael@0: nsresult txListIterator::addAfter(void* objPtr) michael@0: { michael@0: if (currentItem || !atEndOfList) michael@0: return list->insertAfter(objPtr, currentItem); michael@0: return list->insertBefore(objPtr, 0); michael@0: michael@0: } //-- addAfter michael@0: michael@0: /** michael@0: * Adds the Object pointer to the txList pointed to by this txListIterator. michael@0: * The Object pointer is inserted as the previous item in the txList michael@0: * based on the current position within the txList michael@0: * @param objPtr the Object pointer to add to the list michael@0: **/ michael@0: nsresult txListIterator::addBefore(void* objPtr) michael@0: { michael@0: if (currentItem || atEndOfList) michael@0: return list->insertBefore(objPtr, currentItem); michael@0: return list->insertAfter(objPtr, 0); michael@0: michael@0: } //-- addBefore michael@0: michael@0: /** michael@0: * Returns true if a successful call to the next() method can be made michael@0: * @return true if a successful call to the next() method can be made, michael@0: * otherwise false michael@0: **/ michael@0: bool txListIterator::hasNext() { michael@0: bool hasNext = false; michael@0: if (currentItem) michael@0: hasNext = (currentItem->nextItem != 0); michael@0: else if (!atEndOfList) michael@0: hasNext = (list->firstItem != 0); michael@0: michael@0: return hasNext; michael@0: } //-- hasNext michael@0: michael@0: /** michael@0: * Returns the next Object pointer in the list michael@0: **/ michael@0: void* txListIterator::next() { michael@0: michael@0: void* obj = 0; michael@0: if (currentItem) michael@0: currentItem = currentItem->nextItem; michael@0: else if (!atEndOfList) michael@0: currentItem = list->firstItem; michael@0: michael@0: if (currentItem) michael@0: obj = currentItem->objPtr; michael@0: else michael@0: atEndOfList = true; michael@0: michael@0: return obj; michael@0: } //-- next michael@0: michael@0: /** michael@0: * Returns the previous Object in the list michael@0: **/ michael@0: void* txListIterator::previous() { michael@0: michael@0: void* obj = 0; michael@0: michael@0: if (currentItem) michael@0: currentItem = currentItem->prevItem; michael@0: else if (atEndOfList) michael@0: currentItem = list->lastItem; michael@0: michael@0: if (currentItem) michael@0: obj = currentItem->objPtr; michael@0: michael@0: atEndOfList = false; michael@0: michael@0: return obj; michael@0: } //-- previous michael@0: michael@0: /** michael@0: * Returns the current Object michael@0: **/ michael@0: void* txListIterator::current() { michael@0: michael@0: if (currentItem) michael@0: return currentItem->objPtr; michael@0: michael@0: return 0; michael@0: } //-- current michael@0: michael@0: /** michael@0: * Removes the Object last returned by the next() or previous() methods; michael@0: * @return the removed Object pointer michael@0: **/ michael@0: void* txListIterator::remove() { michael@0: michael@0: void* obj = 0; michael@0: if (currentItem) { michael@0: obj = currentItem->objPtr; michael@0: txList::ListItem* item = currentItem; michael@0: previous(); //-- make previous item the current item michael@0: list->remove(item); michael@0: delete item; michael@0: } michael@0: return obj; michael@0: } //-- remove michael@0: michael@0: /** michael@0: * Resets the current location within the txList to the beginning of the txList michael@0: **/ michael@0: void txListIterator::reset() { michael@0: atEndOfList = false; michael@0: currentItem = 0; michael@0: } //-- reset michael@0: michael@0: /** michael@0: * Move the iterator to right after the last element michael@0: **/ michael@0: void txListIterator::resetToEnd() { michael@0: atEndOfList = true; michael@0: currentItem = 0; michael@0: } //-- moveToEnd