Main Page | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members | File Members

legoPArr.H

Go to the documentation of this file.
00001 /**********************************************************************
00002 *  File:            legoPArr.H
00003 *
00004 *  Designer:        Willie Glover
00005 *
00006 *  Description:     Header file for the LEGO array class of pointers.
00007 *                   This implementation uses templates.
00008 *
00009 *                   The legoPArr class is used to construct a set of
00010 *                   pointers to legoOp's and legoRegion's.
00011 *
00012 * 3/18/97 WAH:      Changed Search to return index value.
00013 * 4/9/97 WAH:       Added IsOwner method.
00014 * 1/6/98 WAH:       Removed annoying PRECONDITION/CHECK macros.
00015 * 2/18/98 WAH:      Made destructor virtual.
00016 * 3/20/98 WAH:      Added forward declarations of legoPSet, legoPArrItr.
00017 **********************************************************************/
00018 
00019 #ifndef LEGOPARR_H
00020 #define LEGOPARR_H
00021 
00022 //#ifndef LEGO_HEADER
00023 //#include <lego.H>
00024 //#endif
00025 
00026 #include "globals.H"
00027 #include "legoErr.H"
00028 
00029 //#ifndef LEGOMORE_H
00030 //#include "legoMore.H"
00031 //#endif
00032 
00033 #ifndef __STRING_H
00034 #include <string.h>
00035 #endif
00036 
00037 #ifndef __STDLIB_H
00038 #include <stdlib.h>
00039 #endif
00040 
00041 #ifndef MAX_UNSIGNED
00042 #define MAX_UNSIGNED ((unsigned) -1)
00043 #endif
00044 
00045 template <class T> class legoPSet;
00046 template <class T> class legoPArrItr;
00047 
00048 /*---------------------- Class Definition -----------------------*/
00049 
00050 //----------------------------------------------------------------
00051 //
00052 // The following class maintains an array of T*'s in contiguous
00053 // memory.  The size of the array will be dynamically adjusted
00054 // depending on how many items are stored in it.  The class T must
00055 // have a valid default constructor and copy semantics, as well as
00056 // valid operator == semantics.
00057 //
00058 // For the sake of efficiency, the array size will be initialized to
00059 // 4 and whenever resizing is performed, the size will increase by
00060 // 4.  This will reduce the number of calls to "ResizeArray" when
00061 // items are added to the array.  The cost in unused storage will
00062 // be 12 or fewer bytes for each active array.
00063 //
00064 //----------------------------------------------------------------
00065 
00066 template <class T> class legoPArr {
00067 
00068 protected:
00069 
00070    T           *data;               // Pointer to the array of data
00071    unsigned    _size,_delta;        // Size and increment values
00072    unsigned    count;               // Number of items in array
00073    int         shouldDelete;        // Flags if elements should be deleted
00074 
00075       // Resize the array to a new size
00076    void Expand(unsigned newSize);
00077    void Shrink();
00078 
00079       // Member to compute the actual size
00080    unsigned ComputeSize(unsigned sz)
00081    {
00082      if ((_delta != 0) && (sz % _delta != 0))
00083        return (((sz + _delta) / _delta) * _delta);
00084      else
00085        return sz;
00086    };
00087 
00088 public:
00089    friend legoPSet<T>;
00090 
00091       // Default constructor
00092    legoPArr();
00093 
00094       // Constructor
00095    legoPArr(unsigned size,unsigned delta = 4);
00096 
00097       // Destructor: caller is responsible for destroying T data
00098    virtual ~legoPArr()
00099    {
00100      //     fprintf(stderr,"Deleting LEGOPARR %p\n",this);
00101      PEmpty();
00102    }
00103 
00104       // Copy constructors
00105    legoPArr(const legoPArr<T>& a,unsigned delta = LARGE_INTVAL);
00106    const legoPArr<T>& operator = (const legoPArr<T>& a);
00107 
00108       // Comparison methods for arrays
00109    int  operator == (const legoPArr<T>& a);
00110    int  operator != (const legoPArr<T>& a)
00111    { return !(*this == a); };
00112 
00113       // Indexing operators
00114    T& operator [] (unsigned index)
00115    {
00116      if (data == NULL)
00117        LegoFatal ("legoPArr operator []", "Container %x is uninitialized.",
00118                   this);
00119      if (index >= count)
00120        LegoFatal ("legoPArr operator []", "Index given is %d, but container "
00121                   "%x only has %d items.", index, this, count);
00122      //     PRECONDITION(data != NULL && index < count);
00123      return data[index];
00124    };
00125 
00126    const T& operator [] (unsigned index) const
00127    {
00128      if (data == NULL)
00129        LegoFatal ("legoPArr operator []", "Container %x is uninitialized.",
00130                   this);
00131      if (index < count)
00132        LegoFatal ("legoPArr operator []", "Index given is %d, but container "
00133                   "%x only has %d items.", index, this, count);
00134      // PRECONDITION(data != NULL && index < count);
00135      return data[index];
00136    };
00137 
00138       // Methods to add elements to the array
00139    unsigned AddItem(T item);
00140    void Insert(T item, unsigned index, unsigned count = 1);
00141 
00142       // Method to replace an element in the array
00143    void Replace(T item, unsigned index);
00144 
00145       // Method to destroy an element in the array
00146    void DestroyItem(unsigned index);
00147 
00148       // Method to search for an element in the array
00149    virtual  unsigned Search(T item, unsigned first, unsigned last,
00150                             int direction = +1) const;
00151    unsigned Search(T item, int direction = +1) const
00152    {
00153       if ( count == 0 ) { return MAX_UNSIGNED; }
00154       return Search(item, 0, count-1, direction); 
00155    };
00156 
00157       // Update the number of elements in the array (empties it)
00158    void SetCount(unsigned newCount);
00159 
00160       // Return the number of items in the array
00161    unsigned GetCount() const      { return count; };
00162 
00163       // Returns true if the array is empty
00164    int IsEmpty() const     { return count == 0; };
00165 
00166       // Returns true if the array is full
00167    int IsFull() const
00168    { return (_delta == 0) && (count == _size); };
00169 
00170       // Return the size/delta for the array
00171    unsigned GetSize() const  { return _size; };
00172    unsigned GetDelta() const { return _delta; };
00173 
00174       // Modify the delta value for the array
00175    void SetDelta(unsigned delta);
00176 
00177       // Resize the array to a new size (array is emptied of data)
00178    void SetSize(unsigned newSize);
00179 
00180       // Empties the array of all data
00181    void Empty();
00182 
00183       // Empties the array of all data with possible data destruction
00184    void PEmpty();
00185 
00186       // Returns true if the array is valid
00187    int  Valid() const { return (data != NULL); };
00188 
00189       // Returns true if the array owns its own data.
00190    int IsOwner() const { return shouldDelete; }
00191 
00192 private:
00193 
00194    friend  class legoPArrItr<T>;
00195 };
00196 
00197 //-------------------------------------------------------------------
00198 //
00199 // The following classes are used to iterate through the elements
00200 // in the array.  Generally you can simply do normal array indexing
00201 // rather than using an iterator.
00202 //
00203 //-------------------------------------------------------------------
00204 
00205 template <class T> class legoPArrItr {
00206 
00207 protected:
00208 
00209         const legoPArr<T>        *a;
00210         unsigned                        cursor,lower,upper;
00211 
00212 public:
00213 
00214       // Default constructor
00215    legoPArrItr()
00216    { a = NULL; cursor = lower = upper = 0; };
00217 
00218       // Constructor given an array reference
00219    legoPArrItr(const legoPArr<T>& arr)
00220    { a = &arr; Restart(0,arr.GetCount()); };
00221 
00222       // Constructor given an array reference and range
00223    legoPArrItr(const legoPArr<T>& arr, unsigned start, unsigned stop)
00224    { a = &arr; Restart(start,stop); };
00225 
00226       // Initialize an array iterator from an array
00227    const legoPArrItr<T>& operator = (const legoPArr<T>& arr)
00228    { a = &arr; Restart(0,arr.GetCount());
00229      return *this;
00230    };
00231 
00232       // Initialize an array iterator from another iterator
00233    const legoPArrItr<T>& operator = (const legoPArrItr<T>& i)
00234    { a = i.a; Restart(i.lower,i.upper);
00235      return *this;
00236    };
00237 
00238       // Overloaded cast to an integer
00239    operator int () { return cursor < upper; };
00240 
00241       // Convert the iterator to the corresponding node
00242    T node()
00243    {
00244      if (cursor >= upper)
00245        LegoFatal ("LegoPArrItr::node", "Cursor out of range.");
00246      //     PRECONDITION(cursor < upper);
00247      return (*a)[cursor];
00248    };
00249 
00250       // Pre-increment operator
00251    T operator ++ ()
00252    { if (++cursor < upper) return (*a)[cursor];
00253      else                  return (*a)[upper-1];
00254    };
00255 
00256       // Post-increment operator
00257    T operator ++ (int)
00258    { if (cursor < upper)   return (*a)[cursor++];
00259      else                  return (*a)[upper-1];
00260    };
00261 
00262       // Method to restart the array iterator
00263    void Restart()  { Restart(lower,upper); };
00264 
00265       // Method to restart with a new range
00266    void Restart(unsigned start,unsigned stop)
00267    {  cursor = lower = start; upper = stop; };
00268 };
00269 
00270 /*---------------------------- Implementation -----------------------------*/
00271 
00272 // 1/6/98 WAH: I tried moving all of these into legoPArr.C, but the compiler
00273 //             doesn't seem to generate the actual functions correctly
00274 //             (in particular, PEmpty and Search). If this is a compiler
00275 //             bug, and someone fixes it, these should be shoved off ASAP.
00276 
00277 template <class T> unsigned legoPArr<T>::AddItem(T item)
00278 /*-----------------------------------------------------------------------
00279 * Function:             legoPArr<T>::AddItem
00280 * Parameters:   item    - Item to add to the array
00281 * Description:  Resizes the array if the count exceeds the size of the
00282 *               array, and copies the item into the last position.
00283 * Return:       1 if successful, 0 if unsuccessful
00284 ------------------------------------------------------------------------*/
00285 {
00286     //      fprintf(stderr,"In legoPArr::AddItem(T)\n");
00287         if (!Valid())
00288           LegoFatal ("legoPArr::AddItem", "Container %x is uninitialized.",
00289                      this);
00290         //      CHECK(Valid());
00291         if (count == _size) Expand(count+1);
00292         if (count >= _size)
00293           LegoFatal ("legoPArr::AddItem", "Expansion of container %x "
00294                      "failed!", this);
00295         //      CHECK(count < _size);
00296         if (Valid()) {
00297            data[count++] = item;
00298            return 1;
00299         }
00300         else { return 0; }
00301 }
00302 
00303 template <class T> void legoPArr<T>::SetCount(unsigned newCount)
00304 /*------------------------------------------------------------------------
00305 * Function:             legoPArr<T>::SetCount
00306 * Parameters:   newCount        - New count for the array
00307 * Description:  Sets the count value for the array, expanding the size
00308 *               if necessary. The array will be filled with the default
00309 *               value.
00310 ------------------------------------------------------------------------*/
00311 {
00312         SetSize(newCount);
00313         count = newCount;
00314 }
00315 
00316 template <class T>
00317 void legoPArr<T>::Replace(T item, unsigned index)
00318 /*------------------------------------------------------------------------
00319 * Function:             legoPArr<T>::Replace
00320 * Parameters:   item    - Item to replace in the array
00321 *               index   - Index of the item in the array to replace
00322 * Description:  Replaces the item in the array with the new item. The index
00323 *               MUST fall within the current bounds of the array.
00324 ------------------------------------------------------------------------*/
00325 {
00326         if (!Valid())
00327           LegoFatal ("legoPArr::AddItem", "Container %x is uninitialized.",
00328                      this);
00329         //      CHECK(Valid());
00330         if (index >= count)
00331           LegoFatal ("legoPArr::Replace", "Index given is %d, but container "
00332                   "%x only has %d items.", index, this, count);
00333         //      PRECONDITION(index < count);
00334         if (shouldDelete)
00335                 delete data[index];
00336         data[index] = item;
00337 }
00338 
00339 template <class T> legoPArr<T>::legoPArr()
00340         : _size(4), _delta(4), count(0)
00341 /*------------------------------------------------------------------------
00342 * Function:             legoPArr<T>::legoPArr
00343 * Description:  Default constructor for the array template. This creates
00344 *               an empty array of size 4, with an increment of 4.
00345 ------------------------------------------------------------------------*/
00346 {
00347   //  fprintf(stderr,"Creating LEGOPARR %p\n",this);
00348         shouldDelete = true;
00349         data = new T[_size];
00350         //      CHECK(data != NULL);
00351 }
00352 
00353 template <class T> legoPArr<T>::legoPArr(unsigned size,unsigned delta)
00354         : _delta(delta), count(0)
00355 /*------------------------------------------------------------------------
00356 * Function:             legoPArr<T>::legoPArr
00357 * Parameters:   size    - Initial size of the array
00358 *               delta   - Initial increment value for the array
00359 * Description:  Constructor for the array template, given the size of the
00360 *               array to create, and the expansion ratio. If the increment
00361 *               is zero, the array is of a fixed size and will not be
00362 *               expanded.
00363 *               The array is initially empty containing no valid data.
00364 ------------------------------------------------------------------------*/
00365 {
00366   //  fprintf(stderr,"Creating LEGOPARR %p\n",this);
00367         shouldDelete = true;
00368         _size = ComputeSize(size);
00369         data = new T[_size];
00370         //      CHECK(data != NULL);
00371 }
00372 
00373 template <class T> legoPArr<T>::legoPArr(const legoPArr<T>& a,
00374                                          unsigned delta = LARGE_INTVAL)
00375         : count(a.count)
00376 /*------------------------------------------------------------------------
00377 * Function:             legoPArr<T>::legoPArr
00378 * Parameters:   a       - legoPArr to copy
00379 * Description:  Copy constructor for arrays. Allocate space for new array,
00380 *               and copy each item individually using memcpy.
00381 ------------------------------------------------------------------------*/
00382 {
00383   //  fprintf(stderr,"Creating LEGOPARR %p\n",this);
00384         shouldDelete = false;
00385         if (!(a.Valid()))
00386           LegoFatal ("legoPArr::legoPArr [copy constructor]",
00387                      "Original container %x is uninitialized.", a);
00388         //      CHECK(a.Valid());
00389         _delta = (delta == LARGE_INTVAL) ? a._delta : delta;
00390         _size = ComputeSize(a._size);
00391         data = new T[_size];
00392         if (Valid())
00393                 memcpy(data,a.data,count * sizeof(T));
00394 }
00395 
00396 template <class T> const legoPArr<T>& legoPArr<T>::operator = (const legoPArr<T>& a)
00397 /*------------------------------------------------------------------------
00398 * Function:             legoPArr<T>::operator =
00399 * Parameters:   a       - Array to copy
00400 * Description:  Assignment operator for array's. Allocates space for the
00401 *               new array and copies the data with a fast memcpy call.
00402 *               Since only pointers are copied,  the original owns data.
00403 ------------------------------------------------------------------------*/
00404 {
00405    // Check to make sure both instances are valid and that the assignment
00406    //    is not to itself.
00407   //  fprintf(stderr,"Assigning LEGOPARR %p\n",this);
00408         if (!Valid())
00409           LegoFatal ("LegoPArr operator =", "Container %x (dest.) is "
00410                      "uninitialized.", this);
00411         if (!(a.Valid()))
00412           LegoFatal ("LegoPArr operator =", "Source container is "
00413                      "uninitialized.");
00414         //      CHECK(Valid() && a.Valid());
00415         if (data != a.data) {
00416                 PEmpty();
00417                 shouldDelete = false;
00418                 _size = a._size;
00419                 _delta = a._delta;
00420                 count = a.count;
00421                 delete [] data;
00422                 data = new T[_size];
00423                 if (Valid())
00424                         memcpy(data,a.data,count * sizeof(T));
00425                 }
00426         return *this;
00427 }
00428 
00429 template <class T> int legoPArr<T>::operator == (const legoPArr<T>& a)
00430 /*------------------------------------------------------------------------
00431 * Function:             legoPArr<T>::operator ==
00432 * Parameters:   a       - Array to compare to this array
00433 * Returns:              True if all array elements are equal, false if not.
00434 *                       Returns false unless items in same order.
00435 *                       True = 1, False = 0
00436 ------------------------------------------------------------------------*/
00437 {
00438         if (!Valid())
00439           LegoFatal ("LegoPArr operator ==", "Container %x (first) is "
00440                      "uninitialized.", this);
00441         if (!(a.Valid()))
00442           LegoFatal ("LegoPArr operator ==", "Second container is "
00443                      "uninitialized.");
00444         //      CHECK(Valid() && a.Valid());
00445         if (count != a.count)
00446                 return 0;
00447         T *p = data, *pa = a.data;
00448         for (unsigned i = 0; i < count; i++)
00449                 if (!(*p++ == *pa++))
00450                         return 0;
00451         return 1;
00452 }
00453 
00454 template <class T> void legoPArr<T>::Insert(T item,
00455                                             unsigned index, unsigned aCount)
00456 /*------------------------------------------------------------------------
00457 * Function:             legoPArr<T>::Insert
00458 * Parameters:   item    - Item to insert into the array
00459 *               index   - Index to insert the item in front of
00460 *               aCount  - Count of elements to insert
00461 * Description:  Inserts the specified item into the array at the index'th
00462 *               position. All the elements from [index,count] are moved
00463 *               up one position to make room. The index must be in the
00464 *               range [0,count], and if the value is count it is simply
00465 *               tacked onto the end of the array.
00466 ------------------------------------------------------------------------*/
00467 {
00468    if (!Valid())
00469      LegoFatal ("legoPArr::Insert", "Container %x is uninitialized.",
00470                 this);
00471   //   CHECK(Valid());
00472 
00473    if (index > count)
00474      LegoFatal ("legoPArr::Insert", "Index given is %d, but container "
00475                 "%x only has %d items.", index, this, count);
00476    //   PRECONDITION(index <= count);
00477    if (count+aCount > _size)       Expand(count+aCount);
00478 
00479    // Move the items up one position in the array to make room, and insert
00480 
00481    if (Valid()) {
00482       memmove(&data[index+aCount],&data[index],(count-index) * sizeof(T));
00483       for (unsigned i = 0; i < aCount; i++)
00484          data[index+i] = item;
00485       count += aCount;
00486    }
00487 }
00488 
00489 template <class T> unsigned legoPArr<T>::Search(T item,
00490                    unsigned first, unsigned last, int direction) const
00491 /*------------------------------------------------------------------------
00492 * Function:             legoPArr<T>::Search
00493 * Parameters:   item      - Item to search for
00494 *               first     - Index of first element to search
00495 *               last      - Index of last element to search
00496 *               direction - End to search from (+1 = start, -1 = end)
00497 * Returns:      index of item if it is in the array, MAX_UNSIGNED if not found
00498 * Description:  Performs a simple linear search for the item from the
00499 *               specified end of the array. Compares pointers.
00500 * DANGER DANGER! If the set contains 2^32 items, a search for the last
00501 * item will return not found. Look out buddy.
00502 ------------------------------------------------------------------------*/
00503 {
00504    if (!Valid())
00505      LegoFatal ("legoPArr::Search", "Container %x is uninitialized.",
00506                 this);
00507    //   CHECK(Valid());
00508 
00509    if (count <= first || count <= last)
00510      LegoFatal ("legoPArr::Search", "Search indices given are %d to %d, but "
00511                 "container %x only has %d items.", first, last, this, count);
00512    //   PRECONDITION((first < count) && (last < count));
00513 
00514    if (direction != -1 && direction != +1)
00515      {
00516        LegoNonFatal ("legoPArr::Search", "Direction specified was %d, should "
00517                      "be either +1 or -1. Defaulting to forward search.",
00518                      direction);
00519        direction = +1;
00520      }
00521    //   PRECONDITION(direction == +1 || direction == -1);
00522 
00523    if (direction == +1) {
00524       T *p = &data[first];
00525       for (unsigned i = first; i <= last; i++) {
00526 //          fprintf(stderr,"item=%p count=%d\n",item,count);
00527           if (*(p++) == item)  return i;
00528       }
00529    }
00530    else {
00531       T *p = &data[last];
00532       for (unsigned i = last; i >= first; i--)
00533          { if (*(p--) == item) return i; }
00534    }
00535    //   fprintf(stderr,"In legoPArr<T>::Search(,,,) return 0\n");
00536    return MAX_UNSIGNED;
00537 }
00538 /*------------------------------------------------------------------------
00539  * The following method can be modified to search for a particular
00540  * field within the item instance of T.  It derefernces the T* so that
00541  * the data rather than the pointer is compared.
00542  -----------------------------------------------------------------------*/
00543 //{
00544 //        CHECK(Valid());
00545 //        PRECONDITION(first < count && last < count);
00546 //        PRECONDITION(direction == +1 || direction == -1);
00547 //        if (direction == +1) { T **p = &data[first];
00548 //                               for (unsigned i = first; i <= last; i++)
00549 //                                   if (*(*p++) == *item) return 1; }
00550 //        else {                 T **p = &data[last];
00551 //                               for (unsigned i = last; i >= first; i--)
00552 //                                   if (*(*p--) == *item) return 1; }
00553 //        return 0;
00554 //}
00555 
00556 template <class T> void legoPArr<T>::Expand(unsigned newSize)
00557 /*------------------------------------------------------------------------
00558 * Function:             legoPArr<T>::Expand
00559 * Parameters:   newSize - New size of the array
00560 * Description:  Expands the array to be a multiple of 'delta' that includes
00561 *               the specified newSize for the array. The array data is
00562 *               re-allocated and copied to the resized array.
00563 *
00564 *               Note that 'count' must contain the actual number of elements
00565 *               in the array.
00566 ------------------------------------------------------------------------*/
00567 {
00568    if (!Valid())
00569      LegoFatal ("legoPArr::Expand", "Container %x is uninitialized.",
00570                 this);
00571    //   CHECK(Valid());
00572 
00573    if (_delta == 0)  // not resizeable
00574      return;
00575    //   PRECONDITION(_delta != 0);
00576    if (newSize < count)
00577      {
00578        LegoNonFatal ("legoPArr::Expand", "New size %d is smaller than "
00579                      "current count %d of container %x. Skipping expansion.",
00580                      newSize, count, this);
00581        return;
00582      }
00583    //   PRECONDITION(newSize >= count);
00584 
00585    T *temp = new T[_size = ComputeSize(newSize)];
00586 
00587       // Copy the data from old array to the new array.
00588 
00589    if (temp != NULL)
00590      {
00591        memcpy(temp,data,count * sizeof(T));
00592        delete[] data;
00593      } // end if
00594    else
00595      {
00596        LegoNonFatal ("legoPArr::Expand", "Expansion failed.");
00597        temp = data;
00598      } // end else
00599    data = temp;
00600 }
00601 
00602 template <class T> void legoPArr<T>::Shrink()
00603 /*------------------------------------------------------------------------
00604 * Function:             legoPArr<T>::Shrink
00605 * Description:  Shrinks the allocated space for the array, if the
00606 *               threshold point has been reached.
00607 ------------------------------------------------------------------------*/
00608 {
00609    if (!Valid())
00610      LegoFatal ("legoPArr::Shrink", "Container %x is uninitialized.",
00611                 this);
00612    //   CHECK(Valid());
00613 
00614    if (_delta == 0)                // Array is not resizeable when _delta == 0
00615            return;
00616 
00617    // Only shrink the array when the amount of free space gets smaller
00618    // than half of the delta value, and it is at least delta in size
00619 
00620    if ((_size - count) > (_delta + _delta/2) && (_size > _delta)) {
00621       T *temp = new T[_size = ComputeSize(_size - _delta)];
00622       if (temp != NULL)
00623         {
00624           memcpy(temp, data, count * sizeof(T));
00625           delete [] data;
00626         } // end if
00627       else
00628         {
00629           LegoNonFatal("legoPArr::Shrink", "Shrinking failed.");
00630           temp = data;
00631         } // end else
00632       data = temp;
00633    } // end if
00634 }
00635 
00636 template <class T> void legoPArr<T>::SetDelta(unsigned delta)
00637 /*------------------------------------------------------------------------
00638 * Function:             legoPArr<T>::SetDelta
00639 * Parameters:   delta   - New delta value for the array
00640 * Description:  Sets the delta value for the array, expanding or
00641 *               shrinking the array as need be.
00642 ------------------------------------------------------------------------*/
00643 {
00644         if (delta >= _delta) {
00645                 _delta = delta;
00646                 Expand(_size);
00647                 }
00648         else {
00649                 _delta = delta;
00650                 Shrink();
00651                 }
00652 }
00653 
00654 template <class T> void legoPArr<T>::SetSize(unsigned newSize)
00655 /*------------------------------------------------------------------------
00656 * Function:             legoPArr<T>::SetSize
00657 * Parameters:   newSize - New size for the array
00658 * Description:  Resizes the array to the new size. If the array is non-
00659 *               resizeable, we bomb out. Note that the array will be empty
00660 *               after this operation.
00661 ------------------------------------------------------------------------*/
00662 {
00663   if (_delta == 0) return;  // not resizeable
00664   //    PRECONDITION(_delta != 0);
00665         Empty();
00666         Expand(newSize);
00667 }
00668 
00669 template <class T> void legoPArr<T>::Empty()
00670 /*------------------------------------------------------------------------
00671 * Function:             legoPArr<T>::Empty
00672 * Description:  Empties the array of all elements.
00673 ------------------------------------------------------------------------*/
00674 {
00675         count = 0;
00676         Shrink();
00677 }
00678 
00679 template <class T> void legoPArr<T>::PEmpty()
00680 /*------------------------------------------------------------------------
00681 * Function:             legoPArr<T>::PEmpty
00682 * Description:  Destroys the array elements and their data if
00683 *               shouldDelete is true (default).
00684 -------------------------------------------------------------------------*/
00685 {
00686         if (shouldDelete) {
00687                 for (unsigned i = 0; i < count; i++)
00688                         delete data[i];
00689                 }
00690         Empty();
00691 }
00692 
00693 template <class T> void legoPArr<T>::DestroyItem(unsigned index)
00694 /*------------------------------------------------------------------------
00695 * Function:             legoPArr<T>::DestroyItem
00696 * Parameters:   index   - Index of element to remove
00697 * Description:  Removes an indexed element from the array, by copying all
00698 *               the data down one position. The index must be in the
00699 *               range [0,count). If shouldDelete is true, the element is
00700 *               deleted, otherwise just the ptr is removed.
00701 ------------------------------------------------------------------------*/
00702 {
00703         if (!Valid())
00704           LegoFatal ("legoPArr::DestroyItem", "Container %x is uninitialized.",
00705                      this);
00706         //      CHECK(Valid());
00707         if (index >= count)
00708           LegoFatal ("legoPArr::DestroyItem", "Index given is %d, but "
00709                      "container %x only has %d items.", index, this, count);
00710         //      PRECONDITION(index < count);
00711 
00712         // Move the items down one position, and shrink the allocated memory
00713 
00714         if (shouldDelete)
00715                 delete data[index];
00716         count--;
00717         memmove(&data[index],&data[index+1],(count-index) * sizeof(T));
00718         Shrink();
00719 }
00720 
00721 #endif  // LEGOPARR_H

Generated on Mon Jul 21 20:27:41 2003 for TINKER LEGO DOC by doxygen 1.3.2