home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ocl150a.zip / OCL / Source / OOrderedCollection.cpp < prev    next >
C/C++ Source or Header  |  1996-08-12  |  4KB  |  179 lines

  1. // OCL - OS/2 Class Library
  2. // (c) Cubus 1995
  3. // All Rights Reserved
  4. // OOrderedCollection.cpp
  5.  
  6. /*
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions
  9.  * are met:
  10.  * 1. Redistributions of source code must retain the above copyright
  11.  *    notice, this list of conditions and the following disclaimer.
  12.  * 2. Neither the name Cubus nor the name Team OCL may be used to
  13.  *    endorse or promote products derived from this software
  14.  *    without specific prior written permission.
  15.  * 3. See OCL.INF for a detailed copyright notice.
  16.  *
  17.  *              THIS SOFTWARE IS PROVIDED ``AS IS'' AND
  18.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  21.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27.  * SUCH DAMAGE.
  28.  */
  29.  
  30.  
  31. // $Header: W:/Projects/OCL/Source/rcs/OOrderedCollection.cpp 1.50 1996/08/11 23:49:25 B.STEIN Release $
  32.  
  33. #define __OCL_SOURCE__
  34.  
  35. #define OINCL_OSTRING
  36. #define OINCL_BASE
  37.  
  38. #include <ocl.hpp>
  39. #include <OOrderedCollection.hpp>
  40.  
  41. OOrderedCollection::OOrderedCollection(BOOL copyElements) 
  42.   : OCollection(copyElements),
  43.     collectionSem((PSZ)NULL),
  44.     sortAscending(TRUE), 
  45.     collectionCursor(0) 
  46.   {}
  47.  
  48. OOrderedCollection::~OOrderedCollection()
  49. {
  50.  collectionSem.requestMuxSem();
  51.  collectionSem.releaseMuxSem();
  52. }
  53.  
  54. PSZ OOrderedCollection::isOfType() const
  55.  return("OOrderedCollection"); 
  56. }
  57.  
  58. void OOrderedCollection::setAscending(BOOL ascending)
  59. {
  60.  sortAscending = ascending;
  61. }
  62.  
  63. void OOrderedCollection::addSorted(PVOID elem)
  64. {
  65.  if (!first) {
  66.    addFirst(elem);
  67.    return; }
  68.  
  69.  if (sortAscending)
  70.    addSortAscending(elem);
  71.  else
  72.    addSortDescending(elem);
  73. }
  74.  
  75. void OOrderedCollection::addSortAscending(PVOID elem)
  76. {
  77.  ULONG   step = 2, i;
  78.  PPVITEM tmp;
  79.  
  80.  if (_isLess(elem, first->item))
  81.   {
  82.    addFirst(elem);
  83.    return; 
  84.   }
  85.  
  86.  if (_isLess(last->item, elem))
  87.   {
  88.    addLast(elem);
  89.    return; 
  90.   }
  91.  
  92.  collectionSem.requestMuxSem();
  93.  collectionCursor = items/step;
  94.  
  95.  tmp = _getNextListItem(_getItem(collectionCursor));
  96.  
  97.  while(TRUE)
  98.   {
  99.    step = 2*step;
  100.    collectionCursor = items/step;
  101.    if (collectionCursor > 2)
  102.      {
  103.       if (_isLess(elem, tmp->item))
  104.         {
  105.          for(i = 0; i < collectionCursor; i+=2)
  106.             tmp = tmp->prev->prev; 
  107.         }
  108.       else 
  109.         {
  110.          for(i = 0; i < collectionCursor; i+=2)
  111.             tmp = tmp->next->next; 
  112.         }
  113.      }
  114.    else break;
  115.  }
  116.  
  117.  while (_isLess(elem, tmp->item))
  118.     tmp = tmp->prev;
  119.  while(_isLess(tmp->item, elem))
  120.     tmp = tmp->next;
  121.  addAfter(tmp->prev, elem);
  122.  collectionSem.releaseMuxSem();
  123. }
  124.  
  125. void OOrderedCollection::addSortDescending(PVOID elem)
  126. {
  127.  ULONG    step = 2, i;
  128.  PPVITEM  tmp;
  129.  
  130.  if (_isLess(first->item, elem))
  131.   {
  132.    addFirst(elem);
  133.    return; 
  134.   }
  135.  
  136.  if (_isLess(elem, last->item))
  137.   {
  138.    addLast(elem);
  139.    return; 
  140.   }
  141.  
  142.  collectionSem.requestMuxSem();
  143.  collectionCursor = items/step;
  144.  
  145.  tmp = _getNextListItem(_getItem(collectionCursor));
  146.  
  147.  while(TRUE)
  148.   {
  149.    step = 2*step;
  150.    collectionCursor = items/step;
  151.    if (collectionCursor > 2)
  152.      {
  153.       if (_isLess(tmp->item, elem))
  154.         {
  155.          for(i = 0; i < collectionCursor; i+=2)
  156.             tmp = tmp->prev->prev; 
  157.         }
  158.       else 
  159.         {
  160.          for(i = 0; i < collectionCursor; i+=2)
  161.             tmp = tmp->next->next; 
  162.         }
  163.      }
  164.    else break;
  165.  }
  166.  
  167.  while (_isLess(tmp->item, elem))
  168.     tmp = tmp->prev;
  169.  while(_isLess(elem, tmp->item))
  170.     tmp = tmp->next;
  171.  addAfter(tmp->prev, elem);
  172.  collectionCursor = 0;
  173.  collectionSem.releaseMuxSem();
  174. }
  175.  
  176.  
  177. // end of source
  178.