LCOV - code coverage report
Current view: directory - content/xslt/src/base - txList.cpp (source / functions) Found Hit Coverage
Test: app.info Lines: 175 0 0.0 %
Date: 2012-06-02 Functions: 26 0 0.0 %

       1                 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2                 : /* ***** BEGIN LICENSE BLOCK *****
       3                 :  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
       4                 :  *
       5                 :  * The contents of this file are subject to the Mozilla Public License Version
       6                 :  * 1.1 (the "License"); you may not use this file except in compliance with
       7                 :  * the License. You may obtain a copy of the License at
       8                 :  * http://www.mozilla.org/MPL/
       9                 :  *
      10                 :  * Software distributed under the License is distributed on an "AS IS" basis,
      11                 :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
      12                 :  * for the specific language governing rights and limitations under the
      13                 :  * License.
      14                 :  *
      15                 :  * The Original Code is TransforMiiX XSLT processor code.
      16                 :  *
      17                 :  * The Initial Developer of the Original Code is
      18                 :  * The MITRE Corporation.
      19                 :  * Portions created by the Initial Developer are Copyright (C) 1999
      20                 :  * the Initial Developer. All Rights Reserved.
      21                 :  *
      22                 :  * Contributor(s):
      23                 :  *   Keith Visco <kvisco@ziplink.net> (Original Author)
      24                 :  *
      25                 :  * Alternatively, the contents of this file may be used under the terms of
      26                 :  * either the GNU General Public License Version 2 or later (the "GPL"), or
      27                 :  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
      28                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
      29                 :  * of those above. If you wish to allow use of your version of this file only
      30                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
      31                 :  * use your version of this file under the terms of the MPL, indicate your
      32                 :  * decision by deleting the provisions above and replace them with the notice
      33                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
      34                 :  * the provisions above, a recipient may use your version of this file under
      35                 :  * the terms of any one of the MPL, the GPL or the LGPL.
      36                 :  *
      37                 :  * ***** END LICENSE BLOCK ***** */
      38                 : 
      39                 : #include "txList.h"
      40                 : 
      41                 :   //----------------------------/
      42                 :  //- Implementation of txList -/
      43                 : //----------------------------/
      44                 : 
      45                 : /**
      46                 :  * Default constructor for a txList;
      47                 : **/
      48                 : 
      49               0 : txList::txList() {
      50               0 :    firstItem  = 0;
      51               0 :    lastItem   = 0;
      52               0 :    itemCount  = 0;
      53               0 : } //-- txList;
      54                 : 
      55                 : /**
      56                 :  * txList destructor, cleans up ListItems, but will not delete the Object
      57                 :  * references
      58                 : */
      59               0 : txList::~txList() {
      60               0 :     clear();
      61               0 : } //-- ~txList
      62                 : 
      63               0 : nsresult txList::insert(int index, void* objPtr)
      64                 : {
      65               0 :     if (index >= itemCount) {
      66               0 :         return insertBefore(objPtr, 0);
      67                 :     }
      68                 :     // add inside the list
      69               0 :     ListItem* nextItem = firstItem;
      70               0 :     for (int i = 0; i < index; i++)
      71               0 :         nextItem = nextItem->nextItem;
      72               0 :     return insertBefore(objPtr, nextItem);
      73                 : } //-- insert
      74                 : 
      75               0 : nsresult txList::add(void* objPtr)
      76                 : {
      77               0 :     return insertBefore(objPtr, 0);
      78                 : } //-- add
      79                 : 
      80                 : /**
      81                 :  * Returns the object located at the given index. This may
      82                 :  * be slow or fast depending on the implementation.
      83                 :  * Note:
      84                 :  * Currently this list is implemented via a linked list, so
      85                 :  * this method will be slow (unless the list only has a couple
      86                 :  * members) as it will need traverse the links each time
      87                 :  * @return the object located at the given index
      88                 : **/
      89               0 : void* txList::get(int index) {
      90                 : 
      91               0 :     if (index < 0 || index >= itemCount)
      92               0 :         return 0;
      93                 : 
      94               0 :     int c = 0;
      95               0 :     ListItem* item = firstItem;
      96               0 :     while ((c != index) && item) {
      97               0 :         item = item->nextItem;
      98               0 :         ++c;
      99                 :     }
     100                 : 
     101               0 :     if (item)
     102               0 :         return item->objPtr;
     103               0 :     return 0;
     104                 : } //-- get(int)
     105                 : 
     106               0 : txList::ListItem* txList::getFirstItem() {
     107               0 :     return firstItem;
     108                 : } //-- getFirstItem
     109                 : 
     110               0 : txList::ListItem* txList::getLastItem() {
     111               0 :     return lastItem;
     112                 : } //-- getLastItem
     113                 : 
     114                 : /**
     115                 :  * Returns the number of items in this txList
     116                 : **/
     117               0 : PRInt32 List::getLength() {
     118               0 :    return itemCount;
     119                 : } //-- getLength
     120                 : 
     121                 : 
     122                 : /**
     123                 :  * Inserts the given Object pointer as the item just after refItem.
     124                 :  * If refItem is a null pointer the Object will be inserted at the
     125                 :  * beginning of the txList (ie, insert after nothing).
     126                 :  * This method assumes refItem is a member of this list, and since this
     127                 :  * is a private method, I feel that's a valid assumption
     128                 : **/
     129               0 : nsresult txList::insertAfter(void* objPtr, ListItem* refItem)
     130                 : {
     131                 :     //-- if refItem == null insert at front
     132               0 :     if (!refItem)
     133               0 :         return insertBefore(objPtr, firstItem);
     134               0 :     return insertBefore(objPtr, refItem->nextItem);
     135                 : } //-- insertAfter
     136                 : 
     137                 : /**
     138                 :  * Inserts the given Object pointer as the item just before refItem.
     139                 :  * If refItem is a null pointer the Object will be inserted at the
     140                 :  * end of the txList (ie, insert before nothing).
     141                 :  * This method assumes refItem is a member of this list, and since this
     142                 :  * is a private method, I feel that's a valid assumption
     143                 : **/
     144               0 : nsresult txList::insertBefore(void* objPtr, ListItem* refItem)
     145                 : {
     146               0 :     ListItem* item = new ListItem;
     147               0 :     NS_ENSURE_TRUE(item, NS_ERROR_OUT_OF_MEMORY);
     148                 : 
     149               0 :     item->objPtr = objPtr;
     150               0 :     item->nextItem = 0;
     151               0 :     item->prevItem = 0;
     152                 : 
     153                 :     //-- if refItem == null insert at end
     154               0 :     if (!refItem) {
     155                 :         //-- add to back of list
     156               0 :         if (lastItem) {
     157               0 :             lastItem->nextItem = item;
     158               0 :             item->prevItem = lastItem;
     159                 :         }
     160               0 :         lastItem = item;
     161               0 :         if (!firstItem)
     162               0 :             firstItem = item;
     163                 :     }
     164                 :     else {
     165                 :         //-- insert before given item
     166               0 :         item->nextItem = refItem;
     167               0 :         item->prevItem = refItem->prevItem;
     168               0 :         refItem->prevItem = item;
     169                 : 
     170               0 :         if (item->prevItem)
     171               0 :             item->prevItem->nextItem = item;
     172                 :         else
     173               0 :             firstItem = item;
     174                 :     }
     175                 : 
     176                 :     // increase the item count
     177               0 :     ++itemCount;
     178                 :     
     179               0 :     return NS_OK;
     180                 : } //-- insertBefore
     181                 : 
     182               0 : void* txList::remove(void* objPtr) {
     183               0 :    ListItem* item = firstItem;
     184               0 :    while (item) {
     185               0 :       if (item->objPtr == objPtr) {
     186               0 :          remove(item);
     187                 :          delete item;
     188               0 :          return objPtr;
     189                 :       }
     190               0 :       item = item->nextItem;
     191                 :    }
     192                 :    // not in list
     193               0 :    return 0;
     194                 : } //-- remove
     195                 : 
     196               0 : txList::ListItem* txList::remove(ListItem* item) {
     197                 : 
     198               0 :     if (!item)
     199               0 :         return item;
     200                 : 
     201                 :     //-- adjust the previous item's next pointer
     202               0 :     if (item->prevItem) {
     203               0 :         item->prevItem->nextItem = item->nextItem;
     204                 :     }
     205                 :     //-- adjust the next item's previous pointer
     206               0 :     if (item->nextItem) {
     207               0 :         item->nextItem->prevItem = item->prevItem;
     208                 :     }
     209                 : 
     210                 :     //-- adjust first and last items
     211               0 :     if (item == firstItem)
     212               0 :         firstItem = item->nextItem;
     213               0 :     if (item == lastItem)
     214               0 :         lastItem = item->prevItem;
     215                 : 
     216                 :     //-- decrease Item count
     217               0 :     --itemCount;
     218               0 :     return item;
     219                 : } //-- remove
     220                 : 
     221               0 : void txList::clear()
     222                 : {
     223               0 :     ListItem* item = firstItem;
     224               0 :     while (item) {
     225               0 :         ListItem* tItem = item;
     226               0 :         item = item->nextItem;
     227                 :         delete tItem;
     228                 :     }
     229               0 :     firstItem  = 0;
     230               0 :     lastItem   = 0;
     231               0 :     itemCount  = 0;
     232               0 : }
     233                 : 
     234                 :   //------------------------------------/
     235                 :  //- Implementation of txListIterator -/
     236                 : //------------------------------------/
     237                 : 
     238                 : 
     239                 : /**
     240                 :  * Creates a new txListIterator for the given txList
     241                 :  * @param list, the txList to create an Iterator for
     242                 : **/
     243               0 : txListIterator::txListIterator(txList* list) {
     244               0 :    this->list   = list;
     245               0 :    currentItem  = 0;
     246               0 :    atEndOfList  = false;
     247               0 : } //-- txListIterator
     248                 : 
     249                 : /**
     250                 :  * Adds the Object pointer to the txList pointed to by this txListIterator.
     251                 :  * The Object pointer is inserted as the next item in the txList
     252                 :  * based on the current position within the txList
     253                 :  * @param objPtr the Object pointer to add to the list
     254                 : **/
     255               0 : nsresult txListIterator::addAfter(void* objPtr)
     256                 : {
     257               0 :     if (currentItem || !atEndOfList)
     258               0 :         return list->insertAfter(objPtr, currentItem);
     259               0 :     return list->insertBefore(objPtr, 0);
     260                 : 
     261                 : } //-- addAfter
     262                 : 
     263                 : /**
     264                 :  * Adds the Object pointer to the txList pointed to by this txListIterator.
     265                 :  * The Object pointer is inserted as the previous item in the txList
     266                 :  * based on the current position within the txList
     267                 :  * @param objPtr the Object pointer to add to the list
     268                 : **/
     269               0 : nsresult txListIterator::addBefore(void* objPtr)
     270                 : {
     271               0 :     if (currentItem || atEndOfList)
     272               0 :         return list->insertBefore(objPtr, currentItem);
     273               0 :     return list->insertAfter(objPtr, 0);
     274                 : 
     275                 : } //-- addBefore
     276                 : 
     277                 : /**
     278                 :  * Returns true if a successful call to the next() method can be made
     279                 :  * @return true if a successful call to the next() method can be made,
     280                 :  * otherwise false
     281                 : **/
     282               0 : bool txListIterator::hasNext() {
     283               0 :     bool hasNext = false;
     284               0 :     if (currentItem)
     285               0 :         hasNext = (currentItem->nextItem != 0);
     286               0 :     else if (!atEndOfList)
     287               0 :         hasNext = (list->firstItem != 0);
     288                 : 
     289               0 :     return hasNext;
     290                 : } //-- hasNext
     291                 : 
     292                 : /**
     293                 :  * Returns true if a successful call to the previous() method can be made
     294                 :  * @return true if a successful call to the previous() method can be made,
     295                 :  * otherwise false
     296                 : **/
     297               0 : bool txListIterator::hasPrevious() {
     298               0 :     bool hasPrevious = false;
     299               0 :     if (currentItem)
     300               0 :         hasPrevious = (currentItem->prevItem != 0);
     301               0 :     else if (atEndOfList)
     302               0 :         hasPrevious = (list->lastItem != 0);
     303                 : 
     304               0 :     return hasPrevious;
     305                 : } //-- hasPrevious
     306                 : 
     307                 : /**
     308                 :  * Returns the next Object pointer in the list
     309                 : **/
     310               0 : void* txListIterator::next() {
     311                 : 
     312               0 :     void* obj = 0;
     313               0 :     if (currentItem)
     314               0 :         currentItem = currentItem->nextItem;
     315               0 :     else if (!atEndOfList)
     316               0 :         currentItem = list->firstItem;
     317                 : 
     318               0 :     if (currentItem)
     319               0 :         obj = currentItem->objPtr;
     320                 :     else
     321               0 :         atEndOfList = true;
     322                 : 
     323               0 :     return obj;
     324                 : } //-- next
     325                 : 
     326                 : /**
     327                 :  * Returns the previous Object in the list
     328                 : **/
     329               0 : void* txListIterator::previous() {
     330                 : 
     331               0 :     void* obj = 0;
     332                 : 
     333               0 :     if (currentItem)
     334               0 :         currentItem = currentItem->prevItem;
     335               0 :     else if (atEndOfList)
     336               0 :         currentItem = list->lastItem;
     337                 :     
     338               0 :     if (currentItem)
     339               0 :         obj = currentItem->objPtr;
     340                 : 
     341               0 :     atEndOfList = false;
     342                 : 
     343               0 :     return obj;
     344                 : } //-- previous
     345                 : 
     346                 : /**
     347                 :  * Returns the current Object
     348                 : **/
     349               0 : void* txListIterator::current() {
     350                 : 
     351               0 :     if (currentItem)
     352               0 :         return currentItem->objPtr;
     353                 : 
     354               0 :     return 0;
     355                 : } //-- current
     356                 : 
     357                 : /**
     358                 :  * Moves the specified number of steps
     359                 : **/
     360               0 : void* txListIterator::advance(int i) {
     361                 : 
     362               0 :     void* obj = 0;
     363                 : 
     364               0 :     if (i > 0) {
     365               0 :         if (!currentItem && !atEndOfList) {
     366               0 :             currentItem = list->firstItem;
     367               0 :             --i;
     368                 :         }
     369               0 :         for (; currentItem && i > 0; i--)
     370               0 :             currentItem = currentItem->nextItem;
     371                 :         
     372               0 :         atEndOfList = currentItem == 0;
     373                 :     }
     374               0 :     else if (i < 0) {
     375               0 :         if (!currentItem && atEndOfList) {
     376               0 :             currentItem = list->lastItem;
     377               0 :             ++i;
     378                 :         }
     379               0 :         for (; currentItem && i < 0; i++)
     380               0 :             currentItem = currentItem->prevItem;
     381                 : 
     382               0 :         atEndOfList = false;
     383                 :     }
     384                 : 
     385               0 :     if (currentItem)
     386               0 :         obj = currentItem->objPtr;
     387                 : 
     388               0 :     return obj;
     389                 : } //-- advance
     390                 : 
     391                 : /**
     392                 :  * Removes the Object last returned by the next() or previous() methods;
     393                 :  * @return the removed Object pointer
     394                 : **/
     395               0 : void* txListIterator::remove() {
     396                 : 
     397               0 :     void* obj = 0;
     398               0 :     if (currentItem) {
     399               0 :         obj = currentItem->objPtr;
     400               0 :         txList::ListItem* item = currentItem;
     401               0 :         previous(); //-- make previous item the current item
     402               0 :         list->remove(item);
     403                 :         delete item;
     404                 :     }
     405               0 :     return obj;
     406                 : } //-- remove
     407                 : 
     408                 : /**
     409                 :  * Resets the current location within the txList to the beginning of the txList
     410                 : **/
     411               0 : void txListIterator::reset() {
     412               0 :    atEndOfList = false;
     413               0 :    currentItem = 0;
     414               0 : } //-- reset
     415                 : 
     416                 : /**
     417                 :  * Move the iterator to right after the last element
     418                 : **/
     419               0 : void txListIterator::resetToEnd() {
     420               0 :    atEndOfList = true;
     421               0 :    currentItem = 0;
     422               0 : } //-- moveToEnd

Generated by: LCOV version 1.7