My Array Class

The Interface

The following functions and operators are available in my array class.

template <  class Type>
class Array
{
public:
	Array(int = 0,int = ReSize);
	Array(int,int,Type);
	Array(const Array&);
	~Array();
	Type& operator [](int);
	Type operator [](int)const;
	Array& operator = (const Array&);
	Array copy(); //makes a copy that will not allow destruction of original
	int size()const {return data->_size;} // how much memory is allocated for array
	int numberInArray()const {return data->_active;} //how many active items in array
	int operator ==(const Array &)const;
	int operator != (const Array &a)const{return !(*this == a);}
	void operator += (const Array&);
	// concatenates array at end of current array

	// data related functions
	int find(const Type &)const;
	// find returns -1 if not found and subscript if found
	// find uses == for Type so make sure to override ==
	// properly
	int insertItem(const Type &);
	// inserts the item at the end of the list
	int insertItemAt(const Type &,int);
	// inserts at the place indicated by second parameter
	void deleteItem(const Type &);
	// locates item Type and deletes if it is found
	Type deleteItemAt(int);
	// deletes item at the indicated location
	void clearArray();
	// empties the array
protected:
               // the parts only the programmer needs to know
};

The class Array

The class Array is a template class. That means that the user specifies the type of data stored in the class when variables are defined. For a class of objects X, and an array of items of type X is defined by the line:
   Array< X> myXs;
No size need be specified, since arrays defined using this class expand when the need arrises. Array subscripts always start with subscript 0. In the case above the first time something is added to myXs, the array expands by one. If an item is placed in a specific location and that location is beyond the end of the array the array expands to include that location. If we have:
    Array< double> myDouble(10,5);
    mDouble[23] = 1.23;
 
The location 23 is beyond the last location in the array (namely 9) since the array is initized with 10 items (0-9). The array must be increased by memory blocks that are multiples of 5. In this case, if we increase the size of the array by 15, the upper bound of the array will be 24. All of this is performed automatically, so the user will never know that it is happening unless there is not enough memory to allocate the new block.

Classes of functions

There are two distinct types of functions (operators) available in the array class. The first type of function are those used in list manipulation. These are the most frequently used functions since they are data oriented. The second class are standard array functions. These functions are more frequently use when creating a new type of array (descendant of array with special properties).

Standard list manipulation functions.

Array functions are most often used to store and retrieve data. The operations needed are insert delete, find and clearArray.

The Constructors

There are three constructors for the class Array. The first is creates an array of the size specified by the first parameter and allows the array to be resized in blocks of memory that are allocated in multiples of the second parameter. The default is an array with no items in it, that resizes by one. The first example below creates and array of integers with no space, that will be resized by 1. The second creates an array of 10 doubles that will be resized by multiples of 5.
    Array< int> myInts;
    Array< double> myDoubles(10,5);
The second constructor is similar to the first, but allows the user to specify the data contained in all the entries in the array. The following line will create an array of 10 integers all set to 1. If the array is increased in size, the increase will be a multiple of 15 (15, 30, 45, etc).
    Array< int> myInts(10,15,1);
The last constructor is the copy constructor. It creates a new array from an existing array. It is always called by the system when a parameter is passed by value. As in the assigment operator, the new copy contains a counted pointer to the data, not a new block of memory.

The insert functions

The insertItem function simply inserts an item at the end of the list, while the insertItemAt function inserts the item at a given location in the array and moves all items from this position on down on location in the list. Both insert functions return the location of the item in the array. The first item in an array is always at location 0. The insert functions automatically resize the array if more memory is needed.

The delete functions.

There are two delete functions. The DeleteItem function searches for a specific item (and uses find therefore the == operator for the class of objects in the array). If the item is present it deletes that item. The function DeleteItemAt deletes the item at the indicated location in the array. It returns the item that was deleted.

The find function

The find function returns the location of the item sought if it is in the array. This value will range between 0 and one less than the number of items in the array. If the item is not found, find returns -1

The clear function

This function returns the array to its empty condition. It is usefull when you are reading a new file and want this data to replace the old data in the array.

You will notice that there is no method for looping through the array. In object oriented programming, the process of traversing a structure is left to a separate class called an iterator. In some structures (trees), more than one way traversing the structure is available. Each traversal type has its own iterator. To see the simple array iterator described click here.

Underlying Array Functions

The operators "==", "!=" and "[]"

Most of the operators are fairly clear. The equality operators == and != check to see if the data in two arrays is the same. The two [] (subscript) operators return the data a the location indicated. The constant operator will not return a reference so you may not write: a[i] = 5; if the constant operator is called. This will occur if a is a constant (either declared as const or a const parameter). The constant operator will not allow the array to be resized.

The copy operations

There are two copy methods, the assignment operator = and the copy function. The assignment operator just assigns a pointer to the current data to the new array. This is a counted pointer, so when any array goes out of scope the counter is decremented. When the counter hits zero the array memory is freed. The copy function allocates new memory and copies the date into the new array.

The size operators

There are two size functions size and numberInArray. The function size simply tells the size of the block of memory currently allocated. The function numberInArray tells how many items are currently stored in the array. It works in conjunction with the insert, delete and clear functions, but will not work if you insert or delete directly using the subscript operator.