#include <sortedarraylist.h>
Public Member Functions | |
SortedArrayList () | |
SortedArrayList (const SortedArrayList &pattern) | |
virtual | ~SortedArrayList () |
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 |
A polymorphic copy constructor. | |
Static Public Attributes | |
static const Info *const | TYPE_INFO |
Protected Member Functions | |
virtual bool | insertSorted_impl (Entity *entity) |
Called by a template method. | |
virtual void | locateEntity (const Entity &key, int &index, Entity *&entity) |
Uses a binary search to locate the Entity object with the given key. | |
virtual bool | add_impl (Entity *entity) |
Called by a template method. | |
Private Member Functions | |
bool | binarySearch (const Entity &key, int &index) const |
Locate the Entity with the matching key using a binary search algorithm. |
container::SortedArrayList::SortedArrayList | ( | const SortedArrayList & | pattern | ) |
bool container::SortedArrayList::add_impl | ( | Entity * | entity | ) | [protected, virtual] |
Called by a template method.
Implements container::List.
00104 { 00105 return insertSorted_impl(entity); 00106 }
bool container::SortedArrayList::binarySearch | ( | const Entity & | key, | |
int & | index | |||
) | const [private] |
Locate the Entity with the matching key using a binary search algorithm.
index will point to the Entity if found, otherwise index will point to where the key belongs if the corresponding Entity object were to be inserted
00056 { 00057 int tIndex = BASE_INDEX; 00058 int bIndex = getCount() - 1; 00059 index = BASE_INDEX; // default to first element for empty list 00060 00061 for (int range = bIndex - tIndex; range >= 0; range = bIndex - tIndex) { 00062 index = (range / 2) + tIndex; 00063 00064 int delta = key.compareKeyTo(*array[index]); 00065 if (delta > 0) { 00066 tIndex = ++index; 00067 } else if (delta < 0) { 00068 bIndex = index - 1; 00069 } else { 00070 return true; 00071 } 00072 } 00073 00074 return false; 00075 }
Object * container::SortedArrayList::clone | ( | bool | deepCopy = false |
) | const [virtual] |
bool container::SortedArrayList::insertSorted_impl | ( | Entity * | entity | ) | [protected, virtual] |
Called by a template method.
Implements container::SortedList.
00079 { 00080 int targetIndex = 0; 00081 if (binarySearch(*entity, targetIndex)) { 00082 return false; 00083 } 00084 openGap(targetIndex); 00085 array[targetIndex] = entity; 00086 return true; 00087 }
void container::SortedArrayList::locateEntity | ( | const Entity & | key, | |
int & | index, | |||
Entity *& | entity | |||
) | [protected, virtual] |
Uses a binary search to locate the Entity object with the given key.
Overrides the implementation in ArrayList. Uses a binary search for optimization.
Reimplemented from container::ArrayList.
00091 { 00092 int i = 0; 00093 if (binarySearch(key, i)) { 00094 index = i; 00095 entity = array[i]; 00096 } else { 00097 index = NO_MATCHING_ENTITY_INDEX_FLAG; 00098 entity = NULL; 00099 } 00100 }
ostream & container::SortedArrayList::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::ArrayList.
00110 { 00111 return this->ArrayList::renderState(os); 00112 }
const Object::Info * container::SortedArrayList::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::ArrayList.
00114 { 00115 return TYPE_INFO; 00116 }
const Object::Info *const container::SortedArrayList::TYPE_INFO [static] |