#include <arraylist.h>
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 Entity * | currentEntity () |
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 Entity * | entityAt (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 Info * | typeInfo () const |
Return the instance of Object::Info that describes the class of this object. | |
virtual Object * | clone (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 Entity * | extractCurrentEntity_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. |
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.
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.
00061 : capacity(DEFAULT_INITIAL_CAPACITY), 00062 additionalCapacity(DEFAULT_ADDITIONAL_CAPACITY), array(NULL) { 00063 current = getCount(); 00064 allocateArray(); 00065 }
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 }
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.
this ArrayList and pattern will share the same Entity instances.
00073 { 00074 copyConstructor(pattern); 00075 }
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.
If deepCopy was true then ArrayList is completely independent from the pattern list
00077 { 00078 copyConstructor(pattern, deepCopy); 00079 }
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 }
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 }
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 }
void container::ArrayList::closeGap | ( | ) | [protected] |
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 }
Entity * container::ArrayList::currentEntity | ( | ) | [virtual] |
Return a pointer to the current Entity.
Implements container::List.
bool container::ArrayList::deleteCurrent_impl | ( | ) | [protected, virtual] |
Called by a template method.
Implements container::List.
00180 { 00181 if (isTail()) return false; 00182 00183 Entity * entity = array[current]; 00184 closeGap(); 00185 delete entity; 00186 return true; 00187 }
Entity * container::ArrayList::entityAt | ( | int | index | ) | [virtual] |
Return a pointer to the Entity found at the specified, zero-based index.
A valid index must be specified. For example, if count is 1 then the only valid index is 0.
Implements container::List.
00305 { 00306 if ((index >= getCount()) || (index < BASE_INDEX)) { 00307 return NULL; 00308 } 00309 00310 return array[index]; 00311 }
Entity * container::ArrayList::extractCurrentEntity_impl | ( | ) | [protected, virtual] |
Called by a template method.
Implements container::List.
00189 { 00190 if (isTail()) return NULL; 00191 00192 Entity * entity = array[current]; 00193 closeGap(); 00194 return entity; 00195 }
bool container::ArrayList::isAtCapacity | ( | ) | const [inline, protected] |
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 }
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.
Implements container::List.
00235 { 00236 if (isEmpty()) { 00237 return false; 00238 } 00239 00240 return current == (getCount() - 1); 00241 }
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.
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.
entity will point to the matching Entity. If a matching Entity is not found, NULL is returned in entity instead.
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 }
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 }
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.
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 }
bool container::ArrayList::toFirst | ( | ) | [virtual] |
Point the cursor 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 }
bool container::ArrayList::toLast | ( | ) | [virtual] |
Point the cursor 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 }
bool container::ArrayList::toNext | ( | ) | [virtual] |
Point the cursor to the next Entity in the list.
false if the cursor was moved to the tail of list
false if the cursor was already pointing to the tail
The list cursor is not pointing to the tail
Implements container::List.
00269 { 00270 if (isTail()) { 00271 return false; 00272 } 00273 00274 current++; 00275 return !isTail(); 00276 }
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.
false if the cursor is already pointing the first Entity in the list
false if the list is empty
The list cursor is not pointing to the first Entity in the list
Implements container::List.
00278 { 00279 if (isFirst() || isEmpty()) { 00280 return false; 00281 } 00282 00283 current--; 00284 return true; 00285 }
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.
Implements container::List.
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 }
int container::ArrayList::additionalCapacity [protected] |
Entity** container::ArrayList::array [protected] |
An array of pointers to Entity objects.
const int container::ArrayList::BASE_INDEX = 0 [static, protected] |
int container::ArrayList::capacity [protected] |
int container::ArrayList::current [protected] |
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.