home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tlx501.zip / SRC / VPARRAY.CPP < prev    next >
C/C++ Source or Header  |  1996-07-08  |  10KB  |  288 lines

  1. /****************************************************************************
  2.     $Id: vparray.cpp 501.0 1995/03/07 12:26:26 RON Exp $
  3.  
  4.     Copyright (c) 1991-95 Tarma Software Research. All rights reserved.
  5.  
  6.     Project:    Tarma Library for C++ V5.0
  7.     Author:    Ron van der Wal
  8.  
  9.     Implementation of class TLVPArray. Class TLVPArray is largely a wrapper
  10.     around its TLVPVector data member 'mVector', who provides most of the
  11.     services. TLVPArray adds an index base.
  12.  
  13.     $Log: vparray.cpp $
  14.     Revision 501.0  1995/03/07 12:26:26  RON
  15.     Updated for TLX 5.01
  16.     Revision 1.7  1995/01/31 16:30:32  RON
  17.     Update for release 012
  18.     Added partial support for SunPro C++ compiler
  19.     Revision 1.6  1995/01/06  15:58:49  ron
  20.     Corrected Revision keyword
  21.  
  22.     Revision 1.5  1994/11/16  15:45:49  ron
  23.     Added module info; rearranged #include directives
  24.  
  25.     Revision 1.4  1994/09/28  14:47:26  ron
  26.     Removed Macintosh-style #include references
  27.  
  28.     Revision 1.3  1994/09/27  20:23:25  ron
  29.     Changed path separator from / to \
  30.  
  31.     Revision 1.2  1994/09/26  15:50:55  ron
  32.     Renamed SetSize() to Resize()
  33.  
  34.     Revision 1.1  1994/08/16  18:13:21  ron
  35.     Initial revision
  36.  
  37. ****************************************************************************/
  38.  
  39. #include <tlx\501\_build.h>
  40.  
  41. TLX_MODULE_INFO("$Revision: 501.0 $");
  42.  
  43. #include <tlx\501\except.h>        // Exception handling
  44. #include <tlx\501\vparrays.h>    // Class declaration
  45.  
  46. /*-------------------------------------------------------------------------*/
  47.     TLVPArray::TLVPArray(size_t aSize)
  48.  
  49. /*  Constructor that creates 0-based array of specified size. Also doubles
  50.     as default constructor, building an empty array.
  51. ---------------------------------------------------------------------------*/
  52. : mVector(aSize), mBase(0)
  53. {
  54.     TLX_ASSERT(Size() == aSize);
  55. }
  56.  
  57. /*-------------------------------------------------------------------------*/
  58.     TLVPArray::TLVPArray(index_t aFrom, index_t aTo)
  59.  
  60. /*  Constructor creating an array with specified lower and upper bounds.
  61. ---------------------------------------------------------------------------*/
  62. : mVector(aTo - aFrom + 1), mBase(aFrom)
  63. {
  64.     // TLVector has been constructed already, but we check a few assumptions
  65.     // here anyway. (We chose not to check them on beforehand so as save on
  66.     // runtime. After all, we can trust the programmer, can't we...?)
  67.  
  68.     TLX_ASSERT(aFrom <= aTo);
  69.     TLX_ASSERT((uint32)Size() == (uint32)((long)aTo - (long)aFrom + 1L));
  70. }
  71.  
  72. /*-------------------------------------------------------------------------*/
  73.     TLVPArray::TLVPArray(void *aPtr)
  74.  
  75. /*  Constructor creating a 0-based, single element array with just one
  76.     pointer in it.
  77. ---------------------------------------------------------------------------*/
  78. : mVector(aPtr), mBase(0)
  79. {
  80.     TLX_ASSERT(Size() == 1);
  81. }
  82.  
  83. /*-------------------------------------------------------------------------*/
  84.     TLVPArray::TLVPArray(void **aVector, size_t aSize)
  85.  
  86. /*  Constructor creating a 0-based array that is a copy of the C-style
  87.     vector of the given size.
  88. ---------------------------------------------------------------------------*/
  89. : mVector(aVector, aSize), mBase(0)
  90. {
  91.     TLX_ASSERT(Size() == aSize);
  92. }
  93.  
  94. /*-------------------------------------------------------------------------*/
  95.     TLVPArray::TLVPArray(const TLVPArray &aVector)
  96.  
  97. /*  Copy constructor. Not really necessary (compiler generated version
  98.     would work equally well, since mVector's copy constructor does the
  99.     right thing), but can be used for extra checks and to set breakpoints.
  100. ---------------------------------------------------------------------------*/
  101. : mVector(aVector.mVector), mBase(aVector.mBase)
  102. {
  103.     TLX_ASSERT(Size() == aVector.Size());
  104.     TLX_ASSERT(Mini() == aVector.Mini());
  105. }
  106.  
  107. /*-------------------------------------------------------------------------*/
  108.     TLVPArray::~TLVPArray()
  109.  
  110. /*  Destructor. Not really necessary (compiler generated version would work
  111.     equally well, since mVector's destructor does the right thing), but can
  112.     be used for extra checks and for breakpoints.
  113. */
  114. {
  115. }
  116.  
  117. /*-------------------------------------------------------------------------*/
  118.     void TLVPArray::ExpandBy(size_t aDelta)
  119.  
  120. /*  Increases the size of the array by a given amount.
  121. ---------------------------------------------------------------------------*/
  122. {
  123.     // Make sure that all elements remain accessible. The index type
  124.     // is an integer, so we can use INT_MAX to determine its range.
  125.  
  126.     TLX_ASSERT(aDelta <= ((uint32)INT_MAX - (uint32)Size()));
  127.     mVector.ExpandBy(aDelta);
  128. }
  129.  
  130. /*-------------------------------------------------------------------------*/
  131.     index_t TLVPArray::Maxi() const
  132.  
  133. /*  Returns the maximum valid index for the array. If the array is empty,
  134.     it returns Mini() - 1.
  135. ---------------------------------------------------------------------------*/
  136. {
  137.     // If the array is empty and the index base is INT_MIN, we get in trouble
  138.     // because Mini() - 1 will then wrap around to INT_MAX on most
  139.     // platforms.
  140.  
  141.     TLX_ASSERT(Size() > 0 || Mini() > INT_MIN);
  142.     return Mini() + Size() - 1;
  143. }
  144.  
  145. /*-------------------------------------------------------------------------*/
  146.     index_t TLVPArray::IndexOf(const void *aPtr) const
  147.  
  148. /*  Finds the array index of a specified value. The function performs a
  149.     linear search of pointer values. If the pointer does not appear in the
  150.     array, Maxi() + 1 is returned.
  151. ---------------------------------------------------------------------------*/
  152. {
  153.     return mVector.IndexOf(aPtr) + mBase;
  154. }
  155.  
  156. /*-------------------------------------------------------------------------*/
  157.     bool TLVPArray::IsValidIndex(index_t aIndex) const
  158.  
  159. /*  Tests the validity of a given index.
  160. ---------------------------------------------------------------------------*/
  161. {
  162.     return aIndex >= Mini() && aIndex <= Maxi();
  163. }
  164.  
  165. /*-------------------------------------------------------------------------*/
  166.     TLVPArray &TLVPArray::operator =(const TLVPArray &aVector)
  167.  
  168. /*  Overloaded assignment operator for class TLVPArray. Not really
  169.     necessary (compiler generated version would work as well, because
  170.     mVector's assignment operator does the right thing), but useful
  171.     for extra checks and breakpoints.
  172. ---------------------------------------------------------------------------*/
  173. {
  174.     if (this != &aVector)
  175.     {
  176.     mVector = aVector.mVector;
  177.     mBase = aVector.mBase;
  178.  
  179.     TLX_ASSERT(Size() == aVector.Size());
  180.     }
  181.     return *this;
  182. }
  183.  
  184. /*-------------------------------------------------------------------------*/
  185.     TLVPArray &TLVPArray::operator =(void *aPtr)
  186.  
  187. /*  Overloading of the assignment operator for class TLVPArray that allows
  188.     assignment of a single (void *) to the array. The index base is left
  189.     unchanged.
  190. ---------------------------------------------------------------------------*/
  191. {
  192.     mVector = aPtr;
  193.  
  194.     TLX_ASSERT(Size() == 1);
  195.     TLX_ASSERT(mVector.PeekAt(0) == aPtr);
  196.  
  197.     return *this;
  198. }
  199.  
  200. /*-------------------------------------------------------------------------*/
  201.     void *&TLVPArray::operator [](index_t aIndex)
  202.  
  203. /*  Returns a referenceto the aIndex-th element in the array. If the index
  204.     is invalid, a TLXIndex exception is thrown.
  205. ---------------------------------------------------------------------------*/
  206. {
  207.     if (!IsValidIndex(aIndex))
  208.     THROW(TLXIndex(LOCUS, aIndex));
  209.  
  210.     return PeekAt(aIndex);
  211. }
  212.  
  213. /*-------------------------------------------------------------------------*/
  214.     void *TLVPArray::operator [](index_t aIndex) const
  215.  
  216. /*  Returns the value of the aIndex-th element in the array. If the index
  217.     is invalid, a TLXIndex exception is thrown.
  218. ---------------------------------------------------------------------------*/
  219. {
  220.     if (!IsValidIndex(aIndex))
  221.     THROW(TLXIndex(LOCUS, aIndex));
  222.  
  223.     return PeekAt(aIndex);
  224. }
  225.  
  226. /*-------------------------------------------------------------------------*/
  227.     void *&TLVPArray::PeekAt(index_t aIndex)
  228.  
  229. /*  Returns a reference to the aIndex-th element in the array. There is
  230.     no range checking.
  231. ---------------------------------------------------------------------------*/
  232. {
  233.     TLX_ASSERT(aIndex >= Mini());
  234.     TLX_ASSERT(aIndex <= Maxi());
  235.  
  236.     return mVector.PeekAt(aIndex - mBase);
  237. }
  238.  
  239. /*-------------------------------------------------------------------------*/
  240.     void *TLVPArray::PeekAt(index_t aIndex) const
  241.  
  242. /*  Returns the value of the element at the aIndex-th position. There is no
  243.     range checking.
  244. ---------------------------------------------------------------------------*/
  245. {
  246.     TLX_ASSERT(aIndex >= Mini());
  247.     TLX_ASSERT(aIndex <= Maxi());
  248.  
  249.     return mVector.PeekAt(aIndex - mBase);
  250. }
  251.  
  252. /*-------------------------------------------------------------------------*/
  253.     void TLVPArray::SetMini(index_t aBase)
  254.  
  255. /*  Sets the base index of the array.
  256. ---------------------------------------------------------------------------*/
  257. {
  258.     // Make sure that all elements remain accessible. The index type
  259.     // is an integer, so we can use INT_MAX to determine its range.
  260.  
  261.     TLX_ASSERT((uint32)aBase <= ((uint32)INT_MAX - (uint32)Size()));
  262.     mBase = aBase;
  263. }
  264.  
  265. /*-------------------------------------------------------------------------*/
  266.     void TLVPArray::Resize(size_t aSize)
  267.  
  268. /*  Changes the size of the array to a given value.
  269. ---------------------------------------------------------------------------*/
  270. {
  271.     // Make sure that all elements remain accessible. The index type
  272.     // is an integer, so we can use INT_MAX to determine its range.
  273.  
  274.     TLX_ASSERT(aSize <= ((uint32)INT_MAX - (uint32)Mini()));
  275.     mVector.Resize(aSize);
  276. }
  277.  
  278. /*-------------------------------------------------------------------------*/
  279.     void TLVPArray::ShrinkBy(size_t aDelta)
  280.  
  281. /*  Reduces the size of the array by a given amount.
  282. ---------------------------------------------------------------------------*/
  283. {
  284.     TLX_ASSERT(aDelta <= Size());
  285.     mVector.ShrinkBy(aDelta);
  286. }
  287.  
  288.