home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C++ / Snippets / DynamoArray 1.0 / DynamoArray.c++ next >
Encoding:
Text File  |  1994-05-04  |  7.3 KB  |  274 lines  |  [TEXT/KAHL]

  1. /*
  2.     DynamoArray.c++
  3.  
  4.     Version 1.0
  5.     (c)1993, 1994 by Hiep Dam. All Rights Reserved.
  6.     3G Software
  7.     Blah, blah, blah.
  8.     Usage: Public domain. Feel free to exploit it to your own needs; it would
  9.            be nice of you, however, to give me some tiny credits if you decide
  10.            to use this in your application or if the code helps you...
  11.  
  12.     ---------------------------------------------------------------------------
  13.  
  14.     December 21, 1993.
  15.     Last update: May 4, 1994.
  16.  
  17. */
  18.  
  19. //-------------------------------------------------------------
  20.  
  21. #include "DynamoArray.h"
  22.  
  23. //-------------------------------------------------------------
  24.  
  25.  
  26. void DynamoArray::IncreaseSize() {
  27.     // Increase array length/size
  28.     fMaxSize += STD_DYNAMO_LEN;
  29.  
  30.     // Now create newly-sized array
  31.     DynamoArrayType *temp = new DynamoArrayType[fMaxSize];
  32.  
  33.     // Copy items from old array into newly-created array!
  34.     for (short i = 0; i < fCurSize; i++)
  35.         temp[i] = fArray[i];
  36.  
  37.     // Delete original copy.
  38.     delete fArray;
  39.  
  40.     fArray = temp;
  41. } // END IncreaseSize
  42.  
  43. //-------------------------------------------------------------
  44.  
  45. // Copy Constructor
  46.  
  47. DynamoArray::DynamoArray(DynamoArray& sourceArray) {
  48.     if (&sourceArray == this) return;    // Don't copy with references to self
  49.  
  50.     fCurSize = sourceArray.fCurSize;
  51.     fMaxSize = 0;
  52.  
  53.     while (fMaxSize < fCurSize)
  54.         fMaxSize += STD_DYNAMO_LEN;    // Increase it in chunks of STD_DYNAMO_LEN
  55.  
  56.     fArray = new DynamoArrayType[fMaxSize];
  57.  
  58.     BlockMove((Ptr)&sourceArray.fArray[0], (Ptr)&fArray[0],
  59.         sizeof(DynamoArrayType) * fCurSize);
  60. } // END Copy constructor
  61.  
  62. //-------------------------------------------------------------
  63.  
  64. // Assignment operator (overloaded)
  65.  
  66. DynamoArray DynamoArray::operator=(const DynamoArray& sourceArray) {
  67.     // Make a copy, so trash whatever we have currently. Unfortunately
  68.     // we can't make a call to the destructor, so we have to do this manually...
  69.     delete []fArray;
  70.  
  71.     // Now make a new array
  72.     fArray = new DynamoArrayType[fCurSize = sourceArray.fCurSize];
  73.  
  74.     // Do the actual copy...
  75.     BlockMove((Ptr)&sourceArray.fArray[0], (Ptr)&fArray[0],
  76.         sizeof(DynamoArrayType) * fCurSize);
  77.  
  78.     return(*this);    // return a copy of this object
  79. } // END Assignment operator
  80.  
  81. //-------------------------------------------------------------
  82.  
  83. DynamoArrayType *DynamoArray::operator[] (short index) {
  84.     if ((index < 0) || (index >= fCurSize))
  85.         return nil;
  86.     else
  87.         return &fArray[index];
  88. } // END operator[]
  89.         
  90. //-------------------------------------------------------------
  91.  
  92. short DynamoArray::Get(DynamoArrayType& theItem, short index) {
  93.     if ((index < 0) || (index >= fCurSize) || (fArray == nil))
  94.         // Check for correct boundaries...
  95.         return(OUT_OF_BOUNDS);
  96.     else {
  97.         theItem = fArray[index];
  98.         return(index);
  99.     }
  100. } // END GetItem
  101.  
  102. //-------------------------------------------------------------
  103.  
  104. short DynamoArray::RandomGet(DynamoArrayType& theItem) {
  105.     if (fCurSize > 0) {
  106.         // Get a random value.
  107.         short randomVal = GetRandom(0, fCurSize - 1);
  108.         // Now retrieve the item.
  109.         short returnVal = Get(theItem, randomVal);
  110.         
  111.         // Did something go wrong? If no...
  112.         if (returnVal >= 0)
  113.             return(randomVal);
  114.         else
  115.             // Ooops, something went wrong; return error value.
  116.             return(returnVal);
  117.         }
  118.     else
  119.         // Oops, array empty.
  120.         return(OUT_OF_BOUNDS);
  121. } // END RandomGet
  122.  
  123. //-------------------------------------------------------------
  124.  
  125. short DynamoArray::Append(const DynamoArrayType& theItem) {
  126.     if (fArray == nil) {
  127.         // If array empty, create new one.
  128.         fMaxSize = STD_DYNAMO_LEN;
  129.         fCurSize = 1;
  130.         fArray = new DynamoArrayType[fMaxSize];
  131.     }
  132.     else {
  133.         if (fCurSize == fMaxSize)
  134.             // Ooh, we reached the maximum size. Time to enlarge
  135.             // the array.
  136.             IncreaseSize();
  137.         // We're going to add an item, so increment fCurSize
  138.         fCurSize++;
  139.     }
  140.  
  141.     // Put item into the array.
  142.     fArray[fCurSize - 1] = theItem;
  143.     // Return index value of item we appended into array.
  144.     return(fCurSize - 1);
  145. } // END Append
  146.  
  147. //-------------------------------------------------------------
  148.  
  149. short DynamoArray::Insert(const DynamoArrayType& theItem, short index) {
  150.     if ((index < 0) || (index > fCurSize) || (fArray == nil))
  151.         return(OUT_OF_BOUNDS);
  152.     
  153.     if (fCurSize + 1 == fMaxSize)
  154.         // Reached maximum size, so enlarge the array.
  155.         IncreaseSize();
  156.  
  157.     // OK, make room for our new item! Shift everything down from
  158.     // location on to the end of the array.
  159.     for (short i = fCurSize - 1; i >= index; i--)
  160.         fArray[i + 1] = fArray[i];
  161.     
  162.     // Add our new item.
  163.     fArray[index] = theItem;
  164.  
  165.     // We're adding an item, so increment the number of items in array.
  166.     fCurSize++;
  167.     
  168.     return(0);    // Everything OK, so return zero value.
  169. } // END Insert
  170.  
  171. //-------------------------------------------------------------
  172.  
  173. short DynamoArray::Delete(short index) {
  174.     if ((index < 0) || (index >= fCurSize) || (fArray == nil))
  175.         return(OUT_OF_BOUNDS);
  176.     
  177.     // Shift array backwards (down to 0), so we don't lose anything.
  178.     // If user specified last item is the item to be deleted, this loop
  179.     // won't execute. We just decrement fCurSize instead.
  180.     for (short i = index + 1; i < fCurSize; i++) 
  181.         fArray[i - 1] = fArray[i];
  182.  
  183.     fCurSize--;
  184.     return(index);
  185. } // END Delete
  186.  
  187. //-------------------------------------------------------------
  188.  
  189. short DynamoArray::SearchDelete(DynamoArrayType& theItem, short startIndex) {
  190.     short val = OUT_OF_BOUNDS;    // Assume did not find item
  191.  
  192.     // Search for the item by value, not by index.
  193.     val = Search(theItem, startIndex);
  194.     if (val >= 0)
  195.         Delete(val);    // We found it, so delete!
  196.  
  197.     return val;
  198. } // END SearchDelete
  199.  
  200. //-------------------------------------------------------------
  201.  
  202. short DynamoArray::Remove(DynamoArrayType& theItem, short index) {
  203.     if ((index < 0) || (index >= fCurSize) || (fArray == nil))
  204.         return(OUT_OF_BOUNDS);
  205.     else {
  206.         // In previous versions, Remove() just returned the address of the item
  207.         // that was "deleted". Problem was, the address was no longer pointing
  208.         // to the item that was deleted, but pointed to the item replacing
  209.         // the deleted item!!
  210.         if (Get(theItem, index) != OUT_OF_BOUNDS) {
  211.             if (Delete(index) != OUT_OF_BOUNDS) {
  212.                 return(index);
  213.             }
  214.         }
  215.         return(OUT_OF_BOUNDS);
  216.     }
  217. } // END Remove
  218.  
  219. //-------------------------------------------------------------
  220.  
  221. short DynamoArray::RandomRemove(DynamoArrayType& theItem) {
  222.     return Remove(theItem, GetRandom(0, fCurSize - 1));
  223. } // END RandomRemove
  224.  
  225. //-------------------------------------------------------------
  226.  
  227. short DynamoArray::Search(const DynamoArrayType& source, short startIndex) {
  228.     // Assume we haven't found our baby...
  229.     short foundItem = OUT_OF_BOUNDS;
  230.  
  231.     if (startIndex >= fCurSize)
  232.         return(foundItem);
  233.  
  234.     for (short i = startIndex; i < fCurSize; i++) {
  235.         if (fArray[i] == source) {
  236.             // Yeah, found it. Set return value and exit loop.
  237.             foundItem = i;
  238.             break;
  239.         }
  240.     }
  241.     return(foundItem);
  242. } // END Search
  243.  
  244.  
  245. // -----------------------------------------------------------------------------
  246.  
  247. // The following routine was "lifted" from Tony Myles' SpriteWorld Package,
  248. // in GameUtils.c
  249. // Great code in SpriteWorld, by the way.
  250.  
  251.  
  252. //    By:    Tony Myles
  253. //    Date:    9/19/90
  254. //    Copyright: © 1991-93 Tony Myles, All rights reserved worldwide.
  255.  
  256. //    GetRandom
  257. //    generate a random number between min and max inclusive
  258. unsigned short DynamoArray::GetRandom(unsigned short min, unsigned short max)
  259. {
  260.     unsigned short random;
  261.     long range, temp;
  262.  
  263.     random = Random();
  264.     range = (max - min) + 1;
  265.     temp = (random * range) / 65536;
  266.     random = temp + min;
  267.  
  268.     return random;
  269. }
  270.  
  271. // -----------------------------------------------------------------------------
  272.  
  273.  
  274. // END DynamoArray.c++