dom/xslt/base/txList.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/xslt/base/txList.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,280 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "txList.h"
    1.10 +
    1.11 +  //----------------------------/
    1.12 + //- Implementation of txList -/
    1.13 +//----------------------------/
    1.14 +
    1.15 +/**
    1.16 + * Default constructor for a txList;
    1.17 +**/
    1.18 +
    1.19 +txList::txList() {
    1.20 +   firstItem  = 0;
    1.21 +   lastItem   = 0;
    1.22 +   itemCount  = 0;
    1.23 +} //-- txList;
    1.24 +
    1.25 +/**
    1.26 + * txList destructor, cleans up ListItems, but will not delete the Object
    1.27 + * references
    1.28 +*/
    1.29 +txList::~txList() {
    1.30 +    clear();
    1.31 +} //-- ~txList
    1.32 +
    1.33 +nsresult txList::add(void* objPtr)
    1.34 +{
    1.35 +    return insertBefore(objPtr, 0);
    1.36 +} //-- add
    1.37 +
    1.38 +/**
    1.39 + * Returns the number of items in this txList
    1.40 +**/
    1.41 +int32_t List::getLength() {
    1.42 +   return itemCount;
    1.43 +} //-- getLength
    1.44 +
    1.45 +
    1.46 +/**
    1.47 + * Inserts the given Object pointer as the item just after refItem.
    1.48 + * If refItem is a null pointer the Object will be inserted at the
    1.49 + * beginning of the txList (ie, insert after nothing).
    1.50 + * This method assumes refItem is a member of this list, and since this
    1.51 + * is a private method, I feel that's a valid assumption
    1.52 +**/
    1.53 +nsresult txList::insertAfter(void* objPtr, ListItem* refItem)
    1.54 +{
    1.55 +    //-- if refItem == null insert at front
    1.56 +    if (!refItem)
    1.57 +        return insertBefore(objPtr, firstItem);
    1.58 +    return insertBefore(objPtr, refItem->nextItem);
    1.59 +} //-- insertAfter
    1.60 +
    1.61 +/**
    1.62 + * Inserts the given Object pointer as the item just before refItem.
    1.63 + * If refItem is a null pointer the Object will be inserted at the
    1.64 + * end of the txList (ie, insert before nothing).
    1.65 + * This method assumes refItem is a member of this list, and since this
    1.66 + * is a private method, I feel that's a valid assumption
    1.67 +**/
    1.68 +nsresult txList::insertBefore(void* objPtr, ListItem* refItem)
    1.69 +{
    1.70 +    ListItem* item = new ListItem;
    1.71 +    NS_ENSURE_TRUE(item, NS_ERROR_OUT_OF_MEMORY);
    1.72 +
    1.73 +    item->objPtr = objPtr;
    1.74 +    item->nextItem = 0;
    1.75 +    item->prevItem = 0;
    1.76 +
    1.77 +    //-- if refItem == null insert at end
    1.78 +    if (!refItem) {
    1.79 +        //-- add to back of list
    1.80 +        if (lastItem) {
    1.81 +            lastItem->nextItem = item;
    1.82 +            item->prevItem = lastItem;
    1.83 +        }
    1.84 +        lastItem = item;
    1.85 +        if (!firstItem)
    1.86 +            firstItem = item;
    1.87 +    }
    1.88 +    else {
    1.89 +        //-- insert before given item
    1.90 +        item->nextItem = refItem;
    1.91 +        item->prevItem = refItem->prevItem;
    1.92 +        refItem->prevItem = item;
    1.93 +
    1.94 +        if (item->prevItem)
    1.95 +            item->prevItem->nextItem = item;
    1.96 +        else
    1.97 +            firstItem = item;
    1.98 +    }
    1.99 +
   1.100 +    // increase the item count
   1.101 +    ++itemCount;
   1.102 +    
   1.103 +    return NS_OK;
   1.104 +} //-- insertBefore
   1.105 +
   1.106 +txList::ListItem* txList::remove(ListItem* item) {
   1.107 +
   1.108 +    if (!item)
   1.109 +        return item;
   1.110 +
   1.111 +    //-- adjust the previous item's next pointer
   1.112 +    if (item->prevItem) {
   1.113 +        item->prevItem->nextItem = item->nextItem;
   1.114 +    }
   1.115 +    //-- adjust the next item's previous pointer
   1.116 +    if (item->nextItem) {
   1.117 +        item->nextItem->prevItem = item->prevItem;
   1.118 +    }
   1.119 +
   1.120 +    //-- adjust first and last items
   1.121 +    if (item == firstItem)
   1.122 +        firstItem = item->nextItem;
   1.123 +    if (item == lastItem)
   1.124 +        lastItem = item->prevItem;
   1.125 +
   1.126 +    //-- decrease Item count
   1.127 +    --itemCount;
   1.128 +    return item;
   1.129 +} //-- remove
   1.130 +
   1.131 +void txList::clear()
   1.132 +{
   1.133 +    ListItem* item = firstItem;
   1.134 +    while (item) {
   1.135 +        ListItem* tItem = item;
   1.136 +        item = item->nextItem;
   1.137 +        delete tItem;
   1.138 +    }
   1.139 +    firstItem  = 0;
   1.140 +    lastItem   = 0;
   1.141 +    itemCount  = 0;
   1.142 +}
   1.143 +
   1.144 +  //------------------------------------/
   1.145 + //- Implementation of txListIterator -/
   1.146 +//------------------------------------/
   1.147 +
   1.148 +
   1.149 +/**
   1.150 + * Creates a new txListIterator for the given txList
   1.151 + * @param list, the txList to create an Iterator for
   1.152 +**/
   1.153 +txListIterator::txListIterator(txList* list) {
   1.154 +   this->list   = list;
   1.155 +   currentItem  = 0;
   1.156 +   atEndOfList  = false;
   1.157 +} //-- txListIterator
   1.158 +
   1.159 +/**
   1.160 + * Adds the Object pointer to the txList pointed to by this txListIterator.
   1.161 + * The Object pointer is inserted as the next item in the txList
   1.162 + * based on the current position within the txList
   1.163 + * @param objPtr the Object pointer to add to the list
   1.164 +**/
   1.165 +nsresult txListIterator::addAfter(void* objPtr)
   1.166 +{
   1.167 +    if (currentItem || !atEndOfList)
   1.168 +        return list->insertAfter(objPtr, currentItem);
   1.169 +    return list->insertBefore(objPtr, 0);
   1.170 +
   1.171 +} //-- addAfter
   1.172 +
   1.173 +/**
   1.174 + * Adds the Object pointer to the txList pointed to by this txListIterator.
   1.175 + * The Object pointer is inserted as the previous item in the txList
   1.176 + * based on the current position within the txList
   1.177 + * @param objPtr the Object pointer to add to the list
   1.178 +**/
   1.179 +nsresult txListIterator::addBefore(void* objPtr)
   1.180 +{
   1.181 +    if (currentItem || atEndOfList)
   1.182 +        return list->insertBefore(objPtr, currentItem);
   1.183 +    return list->insertAfter(objPtr, 0);
   1.184 +
   1.185 +} //-- addBefore
   1.186 +
   1.187 +/**
   1.188 + * Returns true if a successful call to the next() method can be made
   1.189 + * @return true if a successful call to the next() method can be made,
   1.190 + * otherwise false
   1.191 +**/
   1.192 +bool txListIterator::hasNext() {
   1.193 +    bool hasNext = false;
   1.194 +    if (currentItem)
   1.195 +        hasNext = (currentItem->nextItem != 0);
   1.196 +    else if (!atEndOfList)
   1.197 +        hasNext = (list->firstItem != 0);
   1.198 +
   1.199 +    return hasNext;
   1.200 +} //-- hasNext
   1.201 +
   1.202 +/**
   1.203 + * Returns the next Object pointer in the list
   1.204 +**/
   1.205 +void* txListIterator::next() {
   1.206 +
   1.207 +    void* obj = 0;
   1.208 +    if (currentItem)
   1.209 +        currentItem = currentItem->nextItem;
   1.210 +    else if (!atEndOfList)
   1.211 +        currentItem = list->firstItem;
   1.212 +
   1.213 +    if (currentItem)
   1.214 +        obj = currentItem->objPtr;
   1.215 +    else
   1.216 +        atEndOfList = true;
   1.217 +
   1.218 +    return obj;
   1.219 +} //-- next
   1.220 +
   1.221 +/**
   1.222 + * Returns the previous Object in the list
   1.223 +**/
   1.224 +void* txListIterator::previous() {
   1.225 +
   1.226 +    void* obj = 0;
   1.227 +
   1.228 +    if (currentItem)
   1.229 +        currentItem = currentItem->prevItem;
   1.230 +    else if (atEndOfList)
   1.231 +        currentItem = list->lastItem;
   1.232 +    
   1.233 +    if (currentItem)
   1.234 +        obj = currentItem->objPtr;
   1.235 +
   1.236 +    atEndOfList = false;
   1.237 +
   1.238 +    return obj;
   1.239 +} //-- previous
   1.240 +
   1.241 +/**
   1.242 + * Returns the current Object
   1.243 +**/
   1.244 +void* txListIterator::current() {
   1.245 +
   1.246 +    if (currentItem)
   1.247 +        return currentItem->objPtr;
   1.248 +
   1.249 +    return 0;
   1.250 +} //-- current
   1.251 +
   1.252 +/**
   1.253 + * Removes the Object last returned by the next() or previous() methods;
   1.254 + * @return the removed Object pointer
   1.255 +**/
   1.256 +void* txListIterator::remove() {
   1.257 +
   1.258 +    void* obj = 0;
   1.259 +    if (currentItem) {
   1.260 +        obj = currentItem->objPtr;
   1.261 +        txList::ListItem* item = currentItem;
   1.262 +        previous(); //-- make previous item the current item
   1.263 +        list->remove(item);
   1.264 +        delete item;
   1.265 +    }
   1.266 +    return obj;
   1.267 +} //-- remove
   1.268 +
   1.269 +/**
   1.270 + * Resets the current location within the txList to the beginning of the txList
   1.271 +**/
   1.272 +void txListIterator::reset() {
   1.273 +   atEndOfList = false;
   1.274 +   currentItem = 0;
   1.275 +} //-- reset
   1.276 +
   1.277 +/**
   1.278 + * Move the iterator to right after the last element
   1.279 +**/
   1.280 +void txListIterator::resetToEnd() {
   1.281 +   atEndOfList = true;
   1.282 +   currentItem = 0;
   1.283 +} //-- moveToEnd

mercurial