home *** CD-ROM | disk | FTP | other *** search
- #ifndef CLASS_DYNAMOARRAY_H_
- #define CLASS_DYNAMOARRAY_H_
-
- /*
- Class_DynamoArray.h
-
- Version 2.0
- (c)1993, 1994, 1995 by Hiep Dam. All Rights Reserved.
- From The Witches' Brew
-
- ---------------------------------------------------------------------------
-
- "Dynamic" arrays implemented as a C++ template class. Pretty self-explanatory.
-
- Reason for this class? Well, I needed to maintain a dynamic list of items
- that were to be randomly chosen, one item at a time. However, once that
- item was chosen, it was NOT to be chosen again. The best way to do
- this was via a dynamic array: getting the item and then deleting it from
- the array and then shrinking the array by 1.
-
- Note however that in the implementation I don't actually "shrink" the array;
- I just decrement the array size counter. The array is allocated via _new.
- You might change this implementation if you have large arrays and memory
- usage is critical for you.
-
- This is also a good sample to work from if you're new to C++ and OOP.
-
- ---------------------------------------------------------------------------
-
- Created: December 21, 1993.
- Just for your information, I spent 5 hours one night developing this
- class (from midnight to 5 in the morning. I must be crazy).
-
- Version History:
- 12/24/93
- Added [] operator and fixed minor bug in destructor.
-
- 4/1/94
- Added copy constructor, assignment constructor
-
- 4/21/95
- Updated copy constructor so it would
- delete old array after copying...
-
- 4/22/95
- Updated assignment constructor; added Preallocate() routine
-
- 4/30/95
- Made changes so the code is platform-independent (ANSI C++);
- however, I have included a #define for Macintosh platforms
- for speedier operations.
-
- 11/22/95
- Removed #define for Macintosh platforms - having this second
- set of code made maintaining the class more of a chore than it
- needed to be. True ANSI C++ compatibility except for the
- Random() routine.
- Changed code so the class is now a *template* class for
- true multiple data type support. Yeah!
- */
-
-
- // ---------------------------------------------------------------------------
-
- enum {
- kDynamoArrayOutOfBoundsErr = -1 // Error code returned by the methods
- };
-
- // ---------------------------------------------------------------------------
-
- /*
- IMPORTANT:
- -----------
- When using arrays, it is always tricky dealing with indices.
- Remember that arrays start with 0, as in anArray[0]. This is different
- from Pascal, where the first item starts at 1, as in anArray[1]. So in C and
- C++ the correct boundaries are anArray[0] to anArray[x - 1], where x is the
- number of items in the array. In Pascal this would be anArray[1] to
- anArray[x].
-
- To clarify things, I use two terms: whole values and index values.
- Whole values range from 1 to the number of items, and index values
- range from 0 to the number of items - 1. So there. No reason for anyone
- to get confused anymore, is there?
-
- Remember: index => 0 to n-1
- whole => 1 to n
- */
-
-
- template <class DynamoArrayType>
- class DynamoArray {
- public:
- DynamoArray();
- DynamoArray(short size);
- ~DynamoArray();
-
- // Copy constructor
- DynamoArray(DynamoArray& sourceArray);
-
- // Assignment operator
- DynamoArray operator=(const DynamoArray& sourceArray);
-
- /*
- [] operator
- Bracket operator; pass a valid INDEX value (0 to fCurSize - 1).
- (Note: fCurSize is the size of the array).
- */
- DynamoArrayType *operator[] (short index);
-
- /*
- Preallocate.
- Increases the size of the array to the specified length.
- Note: old contents of array are destroyed and not saved.
-
- You may call this routine *before* adding any data to the
- array if you know beforehand the approximate size of the
- array (otherwise the array might get deleted and reallocated
- to make room if the array is too small; calling this
- prevents multiple deletions & allocations as you're
- adding items to the array). Mostly used in conjunction
- with the default constructor, which has no size arguments...
- */
- void Preallocate(short size);
-
- /*
- SizeOf.
- Get how many items are stored in array.
- Returns a WHOLE value, from 1 to x.
- */
- virtual short SizeOf() { return fCurSize; }
-
- /*
- Get.
- Specify an INDEX number from 0 to fCurSize - 1. Returns
- kDynamoArrayOutOfBoundsErr if the array is empty;
- else the INDEX number of the item chosen.
- */
- virtual short Get(DynamoArrayType& theItem, short index);
-
- /*
- RandomGet.
- Get a random item from the array. The item is NOT deleted from
- the array. Returns the INDEX value of the item randomly chosen,
- kDynamoArrayOutOfBoundsErr (-1) if array is empty.
- */
- virtual short RandomGet(DynamoArrayType& theItem);
-
- /*
- Append.
- Inserts the specified item at the end of the array. Returns the
- INDEX value of the inserted item.
- */
- virtual short Append(const DynamoArrayType& theItem);
-
- /*
- Insert.
- Inserts the specified item into the array at the specified
- INDEX location. Valid locations are from 0 to fCurSize, not
- just 0 to fCurSize - 1!
- Using fCurSize as the location is essentially the same as Append().
-
- Returns kDynamoArrayOutOfBoundsErr if error occured and
- unable to insert item; if able to insert item, returns
- the INDEX item was inserted into.
- */
- virtual short Insert(const DynamoArrayType& theItem, short index);
-
- /*
- Delete.
- Delete the specified item at array INDEX.
- Specify an item number from 0 to fCurSize - 1.
- If the array is empty, Delete() returns kDynamoArrayOutOfBoundsErr;
- else the INDEX value of the item deleted.
- */
- virtual short Delete(short index);
-
- /*
- SearchDelete.
- Remove the specified item in the array, if that item
- exists. Basically a call to Search(item, x) and then calling Delete(x).
- Returns kDynamoArrayOutOfBoundsErr if item not in array,
- else the INDEX of the item deleted (from 0 to fCurSize - 1).
- */
- virtual short SearchDelete(DynamoArrayType& theItem, short startIndex = 0);
-
- /*
- Remove.
- Get the specified item from the array, and then delete that item.
- Almost the same as calling Get(x) and then calling Delete(x).
- However, you must pass to Remove the variable that is to hold the
- deleted item. Item specified should be from 0 to fCurSize - 1.
- Returns kDynamoArrayOutOfBoundsErr if the array is empty, else
- the array INDEX of the item removed.
- */
- virtual short Remove(DynamoArrayType& theItem, short index);
-
- /*
- RandomRemove.
- Same as Remove(), except item removed is randomly chosen.
- */
- virtual short RandomRemove(DynamoArrayType& theItem);
-
- /*
- Search.
- Looks for specified item in the array.
- Returns kDynamoArrayOutOfBoundsErr if not found, else INDEX of item.
- */
- virtual short Search(const DynamoArrayType& source, short startIndex = 0);
-
-
- protected:
- short fMaxSize; // Real length of array (it's maximum size).
- short fCurSize; // Number of items in array currently.
- DynamoArrayType *fArray; // The array itself (actually pointer to the array)
-
- unsigned short GetRandom(unsigned short min, unsigned short max);
- void IncreaseSize();
- }; // END DynamoArray
-
- // END Class_DynamoArray.h
-
- #endif // CLASS_DYNAMOARRAY_H_