container::ArrayList Class Reference

A mostly complete array-list implementation of List. More...

#include <arraylist.h>

Inheritance diagram for container::ArrayList:

Inheritance graph
[legend]
Collaboration diagram for container::ArrayList:

Collaboration graph
[legend]

List of all members.

Public Member Functions

virtual ~ArrayList ()
 Free all memory associated with this list.
virtual bool isFirst () const
 Return true if the cursor is pointing to first real node, false otherwise.
virtual bool isLast () const
 Determine whether the list cursor is pointing to the last Entity.
virtual bool isTail () const
 Return true if the cursor is pointing to the list tail, false otherwise.
virtual bool toFirst ()
 Point the cursor to the first Entity in the list.
virtual bool toLast ()
 Point the cursor to the last Entity in the list.
virtual void toTail ()
 Point the cursor to the tail of the list.
virtual bool toNext ()
 Point the cursor to the next Entity in the list.
virtual bool toPrev ()
 Point the cursor to the previous Entity in the list.
virtual EntitycurrentEntity ()
 Return a pointer to the current Entity.
virtual void locateEntity (const Entity &key, int &index, Entity *&entity)
 Locate the first Entity that matches the specified key.
virtual EntityentityAt (int index)
 Return a pointer to the Entity found at the specified, zero-based index.
virtual ostream & renderState (ostream &os) const
 Write the state of an Object for debugging and demonstration.
virtual const InfotypeInfo () const
 Return the instance of Object::Info that describes the class of this object.
virtual Objectclone (bool deepCopy=false) const =0
 A polymorphic copy constructor.

Static Public Attributes

static const Info *const TYPE_INFO = Object::typeInfoFactory("ArrayList")

Protected Member Functions

 ArrayList ()
 Creates an empty list with the default DEFAULT_INITIAL_CAPACITY.
 ArrayList (int initialCapacity, int additionalCapacity)
 Creates an empty list with the specified initialCapacity.
 ArrayList (const ArrayList &pattern)
 Create a shallow copy of the pattern list.
 ArrayList (const ArrayList &pattern, bool deepCopy)
 Create a shallow or deep copy of the pattern list.
void allocateArray ()
void closeGap ()
 Close the gap at the list cursor by shifting Enity objects up in the List.
void closeGap (int index)
 Close the gap at index by shifting Enity objects up in the List.
void openGap (int index)
 Open a new gap at index by shifting Enity objects down in the List.
bool isAtCapacity () const
virtual bool deleteCurrent_impl ()
 Called by a template method.
virtual EntityextractCurrentEntity_impl ()
 Called by a template method.

Protected Attributes

int capacity
 The current capacity of this ArrayList. Reflects the current number of elements allocated for array.
int additionalCapacity
 The number of elements that will be added to capacity when array needs to grow.
Entity ** array
 An array of pointers to Entity objects.
int current
 An index that serves as the List cursor.

Static Protected Attributes

static const int DEFAULT_INITIAL_CAPACITY = 4
 Initial capacity of the underlying array.
static const int DEFAULT_ADDITIONAL_CAPACITY = 2
 The increment that will be added to capacity when the array needs to grow.
static const int BASE_INDEX = 0

Private Member Functions

void copyConstructor (const ArrayList &pattern, bool deepCopy=false)
 Helper method; does actual work of the copy constructors.


Detailed Description

A mostly complete array-list implementation of List.

Derived directly from List, ArrayList is an abstract class that implements most of List using an array of Entity objects. The array grows as needed. Currently it does not contract. It never leaves gaps, i.e., all array elements less than Container::getCount() are populated.


Constructor & Destructor Documentation

container::ArrayList::ArrayList (  )  [protected]

Creates an empty list with the default DEFAULT_INITIAL_CAPACITY.

When the current capacity is exceeded the underlying array will grow by the DEFAULT_ADDITIONAL_CAPACITY.

Here is the call graph for this function:

container::ArrayList::ArrayList ( int  initialCapacity,
int  additionalCapacity 
) [protected]

Creates an empty list with the specified initialCapacity.

When the current capacity is exceeded the underlying array will grow by the specified additionalCapacity.

00067                                                      : capacity(initCapacity),
00068 additionalCapacity(addCapacity), array(NULL) {
00069    current = getCount();
00070    allocateArray();
00071 }

Here is the call graph for this function:

container::ArrayList::ArrayList ( const ArrayList pattern  )  [protected]

Create a shallow copy of the pattern list.

A new underlying array of pointers will be allocated. The new array will point to the existing Entity objects from pattern.

Precondition:
pattern is a valid ArrayList
Postcondition:
this ArrayList is logically equivelent to pattern

this ArrayList and pattern will share the same Entity instances.

00073                                              {
00074    copyConstructor(pattern);
00075 }

Here is the call graph for this function:

container::ArrayList::ArrayList ( const ArrayList pattern,
bool  deepCopy 
) [protected]

Create a shallow or deep copy of the pattern list.

A new underlying array of pointers will be allocated. If deepCopy is false the new array will point to the existing Entity objects from pattern. If deepCopy is true then independent copies of Entity will be created.

Precondition:
pattern is a valid ArrayList
Postcondition:
this ArrayList is logically equivelent to pattern

If deepCopy was true then ArrayList is completely independent from the pattern list

00077                                                             {
00078    copyConstructor(pattern, deepCopy);
00079 }

Here is the call graph for this function:

container::ArrayList::~ArrayList (  )  [virtual]

Free all memory associated with this list.

Calls purgeContents(), so all Entity objects in the list will be deleted.

00107                       {
00108    purgeContents();
00109    delete array;
00110 }

Here is the call graph for this function:


Member Function Documentation

void container::ArrayList::allocateArray (  )  [protected]

00114                               {
00115    Entity ** oldArray = array;
00116 
00117    if (oldArray != NULL) {
00118       capacity += additionalCapacity;
00119    }
00120 
00121    array = new Entity*[capacity];
00122 
00123    // Copying old values
00124    for (int i = 0; i < getCount(); i++) {
00125       array[i] = oldArray[i];
00126    }
00127 
00128    // Initialize the rest to NULL
00129    for (int i = getCount(); i < capacity; i++) {
00130       array[i] = NULL;
00131    }
00132 
00133    if (oldArray != NULL) {
00134       delete [] oldArray;
00135    }
00136 }

Here is the call graph for this function:

Here is the caller graph for this function:

virtual Object* container::ArrayList::clone ( bool  deepCopy = false  )  const [pure virtual]

A polymorphic copy constructor.

Implements container::List.

Implemented in container::OrderedArrayList, and container::SortedArrayList.

void container::ArrayList::closeGap ( int  index  )  [protected]

Close the gap at index by shifting Enity objects up in the List.

00142                                   {
00143    if (isTail()) {
00144       return;
00145    }
00146 
00147    if (current > index) {
00148       current--;
00149    }
00150 
00151    const int lastElementIndex = getCount() - 1;
00152 
00153    for (int i = index; i < lastElementIndex; i++) {
00154       array[i] = array[i + 1];
00155    }
00156 
00157    array[lastElementIndex] = NULL;
00158 }

Here is the call graph for this function:

void container::ArrayList::closeGap (  )  [protected]

Close the gap at the list cursor by shifting Enity objects up in the List.

00138                          {
00139    closeGap(current);
00140 }

Here is the caller graph for this function:

void container::ArrayList::copyConstructor ( const ArrayList pattern,
bool  deepCopy = false 
) [private]

Helper method; does actual work of the copy constructors.

00081                                                                        {
00082    setCount(pattern.getCount());
00083    additionalCapacity = pattern.additionalCapacity;
00084    current = pattern.current;
00085    capacity = pattern.getCount();
00086    if (capacity == 0) {
00087       capacity = DEFAULT_INITIAL_CAPACITY;
00088    }
00089    array = new Entity*[capacity];
00090 
00091    for (int i = 0; i < pattern.getCount(); i++) {
00092       if (deepCopy) {
00093          // point to copies of existing Entities
00094          array[i] = dynamic_cast<Entity*>(pattern.array[i]->clone(deepCopy));
00095       } else {
00096          // point to existing Entities (shallow copy)
00097          array[i] = pattern.array[i];
00098       }
00099    }
00100 
00101    // Initialize the rest to NULL
00102    for (int i = getCount(); i < capacity; i++) {
00103       array[i] = NULL;
00104    }
00105 }

Here is the call graph for this function:

Here is the caller graph for this function:

Entity * container::ArrayList::currentEntity (  )  [virtual]

Return a pointer to the current Entity.

Returns:
A pointer to the current Entity object. Returns NULL if the list was empty or if the cursor was not pointing to an Entity.
Precondition:
The list cursor points to an Entity
Postcondition:
The current Entity remains in the list

The state of the List is unchanged

Implements container::List.

00287                                   {
00288    if (isTail()) return (NULL);
00289    return (array[current]);
00290 }

Here is the call graph for this function:

bool container::ArrayList::deleteCurrent_impl (  )  [protected, virtual]

Called by a template method.

See also:
Details documented in List::deleteCurrent

Implements container::List.

00180                                    {
00181    if (isTail()) return false;
00182 
00183    Entity * entity = array[current];
00184    closeGap();
00185    delete entity;
00186    return true;
00187 }

Here is the call graph for this function:

Entity * container::ArrayList::entityAt ( int  index  )  [virtual]

Return a pointer to the Entity found at the specified, zero-based index.

Returns:
A pointer to the Entity at the specified index
Precondition:
The List must contain at least one Entity object.

A valid index must be specified. For example, if count is 1 then the only valid index is 0.

Postcondition:
The state of the list is unchanged including the cursor position

Implements container::List.

00305                                      {
00306    if ((index >= getCount()) || (index < BASE_INDEX)) {
00307       return NULL;
00308    }
00309 
00310    return array[index];
00311 }

Here is the call graph for this function:

Entity * container::ArrayList::extractCurrentEntity_impl (  )  [protected, virtual]

Called by a template method.

See also:
Details documented in List::extractCurrentEntity

Implements container::List.

00189                                               {
00190    if (isTail()) return NULL;
00191 
00192    Entity * entity = array[current];
00193    closeGap();
00194    return entity;
00195 }

Here is the call graph for this function:

bool container::ArrayList::isAtCapacity (  )  const [inline, protected]

00174                                           {
00175    return capacity == getCount();
00176 }

Here is the call graph for this function:

Here is the caller graph for this function:

bool container::ArrayList::isFirst (  )  const [virtual]

Return true if the cursor is pointing to first real node, false otherwise.

TRUE is only possible if the list is not empty.

Implements container::List.

00227                               {
00228    if (isEmpty()) {
00229       return false;
00230    }
00231 
00232    return current == BASE_INDEX;
00233 }

Here is the call graph for this function:

Here is the caller graph for this function:

bool container::ArrayList::isLast (  )  const [virtual]

Determine whether the list cursor is pointing to the last Entity.

true is only possible if the list is not empty.

Returns:
true if the list cursor is pointing to the last Entity of the list, false otherwise

Implements container::List.

00235                              {
00236    if (isEmpty()) {
00237       return false;
00238    }
00239 
00240    return current == (getCount() - 1);
00241 }

Here is the call graph for this function:

bool container::ArrayList::isTail (  )  const [virtual]

Return true if the cursor is pointing to the list tail, false otherwise.

false is only possible if the list is not empty.

Implements container::List.

00243                              {
00244    return current == getCount();
00245 }

Here is the call graph for this function:

Here is the caller graph for this function:

void container::ArrayList::locateEntity ( const Entity key,
int &  index,
Entity *&  entity 
) [virtual]

Locate the first Entity that matches the specified key.

The position is returned as a zero based index. The first Entity that matches is returned. No further checking of the list is performed.

Returns:
The zero based index of the matching Entity. If a matching Entity is not found List::NO_MATCHING_ENTITY_INDEX_FLAG is returned in index instead.

entity will point to the matching Entity. If a matching Entity is not found, NULL is returned in entity instead.

Precondition:
A matching Entity is in the list
Postcondition:
The state of the list is unchanged including the cursor position

Implements container::List.

Reimplemented in container::SortedArrayList.

00292                                                                            {
00293    for (int i = 0; i < getCount(); i++) {
00294       if (key.compareKeyTo(*array[i]) == 0) {
00295          index = i;
00296          entity = array[i];
00297          return;
00298       }
00299    }
00300 
00301    index = NO_MATCHING_ENTITY_INDEX_FLAG;
00302    entity = NULL;
00303 }

Here is the call graph for this function:

void container::ArrayList::openGap ( int  index  )  [protected]

Open a new gap at index by shifting Enity objects down in the List.

00160                                  {
00161    if (isAtCapacity()) {
00162       allocateArray();
00163    }
00164 
00165    for (int i = getCount(); i > index; i--) {
00166       array[i] = array[i - 1];
00167    }
00168 
00169    if (current >= index) {
00170       current++;
00171    }
00172 }

Here is the call graph for this function:

Here is the caller graph for this function:

ostream & container::ArrayList::renderState ( ostream &  os  )  const [virtual]

Write the state of an Object for debugging and demonstration.

This method must not change the state of an Object; adding or removing debug statements should not change the behavior of a class. The implementation must be robust, e.g., NULL safe, etc. and work without an unrecoverable error for any state, excluding an Object's time of construction and destruction. It is not required for the implementation to be thread safe.

Precondition:
The Object has been fully constructed and is not in the process of destruction
Postcondition:
The state of the Object is unchanged

Reimplemented from container::List.

Reimplemented in container::OrderedArrayList, and container::SortedArrayList.

00199                                                   {
00200    this->List::renderState(os);
00201    os << " capacity(" << capacity << ")";
00202    if (isTail()) {
00203       os << " TAIL<--CURSOR";
00204    }
00205    os << " contents{";
00206 
00207    int first = BASE_INDEX;
00208    for (int i = first; i < getCount(); i++) {
00209       if (i > first) {
00210          os << ", ";
00211       }
00212       os << std::endl << "      [" << i << ']' << array[i];
00213       if (i == current) {
00214          os << " <--CURSOR";
00215       }
00216    }
00217 
00218    return os << "}";
00219 }

Here is the call graph for this function:

bool container::ArrayList::toFirst (  )  [virtual]

Point the cursor to the first Entity in the list.

Returns:
false if the list is empty; otherwise, true. A value of true doesn't imply that the cursor was moved, i.e., it may have already been pointing to the first Entity.
Precondition:
The list is not empty
Postcondition:
The list cursor points to the first Entity in the list

Implements container::List.

00247                         {
00248    if (isEmpty()) {
00249       return false;
00250    }
00251 
00252    current = BASE_INDEX;
00253    return true;
00254 }

Here is the call graph for this function:

bool container::ArrayList::toLast (  )  [virtual]

Point the cursor to the last Entity in the list.

Returns:
false if the list is empty; otherwise, true. A value of true doesn't imply that the cursor was moved, i.e., it may have already been pointing to the last Entity.
Precondition:
The list is not empty
Postcondition:
The list cursor points to the last Entity in the list

Implements container::List.

00256                        {
00257    if (isEmpty()) {
00258       return false;
00259    }
00260 
00261    current = getCount() - 1;
00262    return true;
00263 }

Here is the call graph for this function:

bool container::ArrayList::toNext (  )  [virtual]

Point the cursor to the next Entity in the list.

Returns:
true if the list cursor was moved and points to an Entity

false if the cursor was moved to the tail of list

false if the cursor was already pointing to the tail

Precondition:
The List is not empty

The list cursor is not pointing to the tail

Postcondition:
The list cursor has moved to the next Entity or to the tail if it was pointing to the last Entity

Implements container::List.

00269                        {
00270    if (isTail()) {
00271       return false;
00272    }
00273 
00274    current++;
00275    return !isTail();
00276 }

Here is the call graph for this function:

bool container::ArrayList::toPrev (  )  [virtual]

Point the cursor to the previous Entity in the list.

Calling toPrev() never leaves the cursor pointing to the head of the list.

Returns:
true if the list cursor was moved

false if the cursor is already pointing the first Entity in the list

false if the list is empty

Precondition:
The list is not empty

The list cursor is not pointing to the first Entity in the list

Postcondition:
The list cursor has moved to the previous Entity

Implements container::List.

00278                        {
00279    if (isFirst() || isEmpty()) {
00280       return false;
00281    }
00282 
00283    current--;
00284    return true;
00285 }

Here is the call graph for this function:

void container::ArrayList::toTail (  )  [virtual]

Point the cursor to the tail of the list.

This method should always succeed. A List should always have the concept a tail, i.e., the node or element just beyond the end of the list. No Entity is available at the tail cursor position.

Precondition:
A List has been constructed
Postcondition:
The list cursor points to the tail

Implements container::List.

00265                        {
00266    current = getCount();
00267 }

Here is the call graph for this function:

const Object::Info * container::ArrayList::typeInfo (  )  const [virtual]

Return the instance of Object::Info that describes the class of this object.

Instantiation of Object::Info is controlled by the protected method Object::typeInfoFactory(const string&). Each sub-class of Object should create one and only one instance of Object::Info.

Reimplemented from container::List.

Reimplemented in container::OrderedArrayList, and container::SortedArrayList.

00221                                             {
00222    return TYPE_INFO;
00223 }


Member Data Documentation

The number of elements that will be added to capacity when array needs to grow.

An array of pointers to Entity objects.

const int container::ArrayList::BASE_INDEX = 0 [static, protected]

The current capacity of this ArrayList. Reflects the current number of elements allocated for array.

An index that serves as the List cursor.

Points to the current Entity

const int container::ArrayList::DEFAULT_ADDITIONAL_CAPACITY = 2 [static, protected]

The increment that will be added to capacity when the array needs to grow.

It's set to a small value for testing the dynamic growth behavior.

const int container::ArrayList::DEFAULT_INITIAL_CAPACITY = 4 [static, protected]

Initial capacity of the underlying array.

It's set to a small value for testing the dynamic growth behavior.

const Object::Info *const container::ArrayList::TYPE_INFO = Object::typeInfoFactory("ArrayList") [static]

Reimplemented from container::List.

Reimplemented in container::OrderedArrayList, and container::SortedArrayList.


The documentation for this class was generated from the following files:

Generated on Tue Jun 16 23:12:57 2009 by  doxygen 1.5.9