SWIList.cpp
上传用户:xqtpzdz
上传日期:2022-05-21
资源大小:1764k
文件大小:9k
源码类别:

xml/soap/webservice

开发平台:

Visual C++

  1. /****************License************************************************
  2.  * Vocalocity OpenVXI
  3.  * Copyright (C) 2004-2005 by Vocalocity, Inc. All Rights Reserved.
  4.  * This program is free software; you can redistribute it and/or
  5.  * modify it under the terms of the GNU General Public License
  6.  * as published by the Free Software Foundation; either version 2
  7.  * of the License, or (at your option) any later version.
  8.  *  
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software
  16.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  17.  * Vocalocity, the Vocalocity logo, and VocalOS are trademarks or 
  18.  * registered trademarks of Vocalocity, Inc. 
  19.  * OpenVXI is a trademark of Scansoft, Inc. and used under license 
  20.  * by Vocalocity.
  21.  ***********************************************************************/
  22. #include "SWIutilInternal.h"
  23. #include <stdlib.h>
  24. #include "SWIList.hpp"
  25. #ifdef REUSE_SWI_CELLS
  26. #include "SWItrdMonitor.hpp"
  27. static SWItrdMonitor SWIListMonitor;
  28. #endif
  29. class SWIListCell
  30. {
  31.  public:
  32.   // Creates a cell with given pointers.
  33.   SWIListCell():
  34.     _item(NULL), _prev(NULL), _next(NULL)
  35.   {}
  36.   SWIListCell(void *item, SWIListCell *prev, SWIListCell *next):
  37.     _item(item), _prev(prev), _next(next)
  38.   {
  39.     prev->_next = this;
  40.     next->_prev = this;
  41.   }
  42. #ifdef REUSE_SWI_CELLS
  43.   // Overloads the new and delete to do memory memanagement of SWIListCell.
  44.   // There is a free list of cell from which cells are taken by
  45.   // new.  delete just pust the cell at the top of the free list.
  46.   void* operator new(size_t);
  47.   void operator delete(void* cell);
  48. #endif
  49.   // Pointer to the list element.
  50.   void* _item;
  51.   // Pointer to the previous cell, null if first cell.
  52.   SWIListCell *_prev;
  53.   // Pointer to the next cell, null if last cell.
  54.   SWIListCell *_next;
  55.  private:
  56. #ifdef REUSE_SWI_CELLS
  57.   // Points to the first free cell.  new and delete take and put
  58.   // SWIListCell objects in this list.
  59.   static SWIListCell* _freeList;
  60. #endif
  61. };
  62. #ifdef REUSE_SWI_CELLS
  63. SWIListCell* SWIListCell::_freeList = NULL;
  64. #define NBCELLS 16
  65. void* SWIListCell::operator new(size_t)
  66. {
  67.   ::SWIListMonitor.lock();
  68.   SWIListCell* pCell = _freeList;
  69.   if (pCell == NULL)
  70.   {
  71.     // Allocates NBCELLS cells.
  72.     pCell  = (SWIListCell*) new char[ NBCELLS * sizeof(SWIListCell) ];
  73.     if (pCell != NULL)
  74.     {
  75.       // Put NBCELLS-1 of them in the free list, and return the last one.
  76.       for (int cell = 1; cell < NBCELLS; cell++)
  77.       {
  78.         pCell->_next = _freeList;
  79.         _freeList = pCell;
  80.         pCell++;
  81.       }
  82.     }
  83.   }
  84.   else
  85.   {
  86.     _freeList = pCell->_next;
  87.   }
  88.   ::SWIListMonitor.unlock();
  89.   return pCell;
  90. }
  91. void SWIListCell::operator delete(void* cell)
  92. {
  93.   ::SWIListMonitor.lock();
  94.   ((SWIListCell*) cell)->_next = _freeList;
  95.   _freeList = (SWIListCell*) cell;
  96.   ::SWIListMonitor.unlock();
  97. }
  98. #endif
  99. SWIList::SWIList()
  100. {
  101.   _head = new SWIListCell();
  102.   _tail = new SWIListCell();
  103.   _head->_prev = _head;
  104.   _head->_next = _tail;
  105.   _tail->_prev = _head;
  106.   _tail->_next = _tail;
  107.   _size = 0;
  108. }
  109. SWIList::SWIList(SWIList const & aList)
  110. {
  111.   _head = new SWIListCell();
  112.   _tail = new SWIListCell();
  113.   _head->_prev = _head;
  114.   _head->_next = _tail;
  115.   _tail->_prev = _head;
  116.   _tail->_next = _tail;
  117.   _size = 0;
  118.   SWIListCell *cell = aList._head->_next;
  119.   while (cell != aList._tail &&
  120.          addLast(cell->_item))
  121.   {
  122.     cell = cell->_next;
  123.   }
  124. }
  125. SWIList::~SWIList()
  126. {
  127.   clear();
  128.   delete _head;
  129.   delete _tail;
  130. }
  131. SWIList& SWIList::operator=(const SWIList& aList)
  132. {
  133.   if (this != &aList)
  134.   {
  135.     clear();
  136.     SWIListCell *cell = aList._head->_next;
  137.     while (cell != aList._tail &&
  138.            addLast(cell->_item))
  139.     {
  140.       cell = cell->_next;
  141.     }
  142.   }
  143.   return *this;
  144. }
  145. void *SWIList::first() const
  146. {
  147.   return _head->_next->_item;
  148. }
  149. void *SWIList::last() const
  150. {
  151.   return _tail->_prev->_item;
  152. }
  153. bool SWIList::addFirst(void* pItem)
  154. {
  155.   if (new SWIListCell(pItem, _head, _head->_next) != NULL)
  156.   {
  157.     _size++;
  158.     return true;
  159.   }
  160.   return false;
  161. }
  162. bool SWIList::addLast(void* pItem)
  163. {
  164.   if (new SWIListCell(pItem, _tail->_prev, _tail) != NULL)
  165.   {
  166.     _size++;
  167.     return true;
  168.   }
  169.   return false;
  170. }
  171. void* SWIList::removeFirst()
  172. {
  173.   if (_size == 0)
  174.   {
  175.     return NULL;
  176.   }
  177.   SWIListCell *first = _head->_next;
  178.   void *item = first->_item;
  179.   remove(first);
  180.   return item;
  181. }
  182. void* SWIList::removeLast()
  183. {
  184.   if (_size == 0)
  185.   {
  186.     return NULL;
  187.    }
  188.   SWIListCell *last = _tail->_prev;
  189.   void *item = last->_item;
  190.   remove(last);
  191.   return item;
  192. }
  193. bool SWIList::remove(void* pItem)
  194. {
  195.   SWIListCell* pRemove = getCell(pItem);
  196.   if (pRemove == NULL)
  197.   {
  198.     return false;
  199.   }
  200.   remove(pRemove);
  201.   return true;
  202. }
  203. bool SWIList::removeAll(void* pItem)
  204. {
  205.   SWIListCell* pNext = NULL;
  206.   bool found = false;
  207.   for (SWIListCell* pCurrent = _head->_next;
  208.        pCurrent != _tail;
  209.        pCurrent = pNext)
  210.   {
  211.     pNext = pCurrent->_next;
  212.     if (pCurrent->_item == pItem)
  213.     {
  214.       remove(pCurrent);
  215.       found = true;
  216.     }
  217.   }
  218.   return found;
  219. }
  220. void SWIList::clear()
  221. {
  222.   SWIListCell *cell = _head->_next;
  223.   while (cell != _tail)
  224.   {
  225.     SWIListCell *tmp = cell;
  226.     cell = cell->_next;
  227.     delete tmp;
  228.   }
  229.   _size = 0;
  230.   _head->_next = _tail;
  231.   _tail->_prev = _head;
  232. }
  233. SWIListCell* SWIList::getCell(void* pItem)
  234. {
  235.   for (SWIListCell* cell = _head->_next;
  236.        cell != _tail;
  237.        cell = cell->_next)
  238.    {
  239.      if (cell->_item == pItem)
  240.      {
  241.        return cell;
  242.      }
  243.    }
  244.    return NULL;
  245. }
  246. void SWIList::remove(SWIListCell* cell)
  247. {
  248.   cell->_prev->_next = cell->_next;
  249.   cell->_next->_prev = cell->_prev;
  250.   delete cell;
  251.   _size--;
  252. }
  253. SWIList::Iterator SWIList::iterator()
  254. {
  255.   Iterator iter(*this, Iterator::BEG);
  256.   return iter;
  257. }
  258. const SWIList::Iterator SWIList::iterator() const
  259. {
  260.   Iterator iter((SWIList &) *this, Iterator::BEG);
  261.   return iter;
  262. }
  263. SWIList::Iterator SWIList::reverseIterator()
  264. {
  265.   Iterator iter(*this, Iterator::END);
  266.   return iter;
  267. }
  268. const SWIList::Iterator SWIList::reverseIterator() const
  269. {
  270.   Iterator iter((SWIList &) *this, Iterator::END);
  271.   return iter;
  272. }
  273. SWIList::Iterator::Iterator(SWIList& listRep,
  274.                             Position pos)
  275. {
  276.   _list = &listRep;
  277.   _cursor = pos == BEG ? listRep._head : listRep._tail;
  278. }
  279. SWIList::Iterator::Iterator(Iterator const& iter)
  280. {
  281.   _list = iter._list;
  282.   _cursor = iter._cursor;
  283. }
  284. SWIList::Iterator::~Iterator()
  285. {}
  286. SWIList::Iterator& SWIList::Iterator::operator=(Iterator const& iter)
  287. {
  288.   if (this != &iter)
  289.   {
  290.     _cursor = iter._cursor;
  291.     _list = iter._list;
  292.   }
  293.   return *this;
  294. }
  295. void SWIList::Iterator::setPos(Position pos)
  296. {
  297.   _cursor = pos == BEG ? _list->_head : _list->_tail;
  298. }
  299. bool SWIList::Iterator::hasNext() const
  300. {
  301.   return _cursor->_next != _list->_tail;
  302. }
  303. bool SWIList::Iterator::hasPrevious() const
  304. {
  305.   return _cursor->_prev != _list->_head;
  306. }
  307. void* SWIList::Iterator::next() const
  308. {
  309.   _cursor = _cursor->_next;
  310.   return _cursor->_item;
  311. }
  312. void* SWIList::Iterator::previous() const
  313. {
  314.   _cursor = _cursor->_prev;
  315.   return _cursor->_item;
  316. }
  317. void * SWIList::Iterator::removeBack()
  318. {
  319.   if (_cursor == _list->_head ||
  320.       _cursor == _list->_tail)
  321.   {
  322.     // either at tail or head of the list.
  323.     return NULL;
  324.   }
  325.   void *item = _cursor->_item;
  326.   _cursor = _cursor->_prev;
  327.   _list->remove(_cursor->_next);
  328.   return item;
  329. }
  330. void * SWIList::Iterator::removeFront()
  331. {
  332.   if (_cursor == _list->_head ||
  333.       _cursor == _list->_tail)
  334.   {
  335.     // either at tail or head of the list.
  336.     return NULL;
  337.   }
  338.   void *item = _cursor->_item;
  339.   _cursor = _cursor->_next;
  340.   _list->remove(_cursor->_prev);
  341.   return item;
  342. }
  343. void * SWIList::Iterator::set(void *item)
  344. {
  345.   if (_cursor == _list->_tail ||
  346.       _cursor == _list->_head)
  347.   {
  348.     // either at tail or head of the list.
  349.     return NULL;
  350.   }
  351.   void *olditem = _cursor->_item;
  352.   _cursor->_item = item;
  353.   return olditem;
  354. }
  355. bool SWIList::Iterator::insertAfter(void *item)
  356. {
  357.   SWIListCell *cell;
  358.   if (_list->_size == 0 || _cursor == _list->_tail)
  359.   {
  360.     cell = new SWIListCell(item, _list->_tail->_prev, _list->_tail);
  361.   }
  362.   else
  363.   {
  364.     cell = new SWIListCell(item, _cursor, _cursor->_next);
  365.   }
  366.   if (cell != NULL)
  367.   {
  368.     _list->_size++;
  369.     return true;
  370.   }
  371.   return false;
  372. }
  373. bool SWIList::Iterator::insertBefore(void *item)
  374. {
  375.   SWIListCell *cell;
  376.   if (_list->_size == 0 || _cursor == _list->_head)
  377.   {
  378.     cell = new SWIListCell(item, _list->_head, _list->_head->_next);
  379.   }
  380.   else
  381.   {
  382.     cell = new SWIListCell(item, _cursor->_prev, _cursor);
  383.   }
  384.   if (cell != NULL)
  385.   {
  386.     _list->_size++;
  387.     return true;
  388.   }
  389.   return false;
  390. }