PObject
class.
More...
#include <lists.h>
Inheritance diagram for PAbstractSortedList:
Public Types | |
typedef PSortedListElement | Element |
Public Member Functions | |
Construction | |
PAbstractSortedList () | |
Create a new, empty, sorted list. | |
Overrides from class PObject | |
virtual Comparison | Compare (const PObject &obj) const |
Get the relative rank of the two lists. | |
Overrides from class PContainer | |
virtual PBoolean | SetSize (PINDEX newSize) |
This function is meaningless for lists. | |
Overrides from class PCollection | |
virtual PINDEX | Append (PObject *obj) |
Add a new object to the collection. | |
virtual PINDEX | Insert (const PObject &before, PObject *obj) |
Add a new object to the collection. | |
virtual PINDEX | InsertAt (PINDEX index, PObject *obj) |
Add a new object to the collection. | |
virtual PBoolean | Remove (const PObject *obj) |
Remove the object from the collection. | |
virtual PObject * | RemoveAt (PINDEX index) |
Remove the object at the specified ordinal index from the collection. | |
virtual void | RemoveAll () |
Remove all of the elements in the collection. | |
virtual PBoolean | SetAt (PINDEX index, PObject *val) |
This method simply returns false as the list order is mantained by the class. | |
virtual PObject * | GetAt (PINDEX index) const |
Get the object at the specified ordinal position. | |
virtual PINDEX | GetObjectsIndex (const PObject *obj) const |
Search the collection for the specific instance of the object. | |
virtual PINDEX | GetObjectsIndex (const PObject *obj, PSortedListElement *&lastElement) const |
virtual PINDEX | GetValuesIndex (const PObject &obj) const |
Search the collection for the specified value of the object. | |
Protected Member Functions | |
void | RemoveElement (Element *node) |
void | LeftRotate (Element *node) |
void | RightRotate (Element *node) |
void | DeleteSubTrees (Element *node, PBoolean deleteObject) |
PINDEX | ValueSelect (const Element *node, const PObject &obj, const Element **lastElement) const |
Protected Attributes | |
PSortedListInfo * | info |
PObject
class.
It is implemeted as a Red-Black binary tree to maintain the objects in rank order. Note that this requires that the PObject::Compare()
function be fully implemented oin objects contained in the collection.
The implementation of a sorted list allows fast inserting and deleting as well as random access of objects in the collection. As the objects are being kept sorted, "fast" is a relative term. All operations take o(lg n) unless a particular object is repeatedly accessed.
The class remembers the last accessed element. This state information is used to optimise access by the "virtual array" model of collections. If repeated access via ordinal index is made there is little overhead. All other access incurs a minimum overhead, but not insignificant.
The PAbstractSortedList class would very rarely be descended from directly by the user. The PDECLARE_LIST
and PLIST
macros would normally be used to create descendent classes. They will instantiate the template based on PSortedList
or directly declare and define the class (using inline functions) if templates are not being used.
The PSortedList
class or PDECLARE_SORTED_LIST
macro will define the correctly typed operators for subscript access (operator[]).
PAbstractSortedList::PAbstractSortedList | ( | ) |
Create a new, empty, sorted list.
Note that by default, objects placed into the list will be deleted when removed or when all references to the list are destroyed.
virtual PINDEX PAbstractSortedList::Append | ( | PObject * | obj | ) | [virtual] |
Add a new object to the collection.
The object is always placed in the correct ordinal position in the list. It is not placed at the "end".
Implements PCollection.
virtual Comparison PAbstractSortedList::Compare | ( | const PObject & | obj | ) | const [virtual] |
Get the relative rank of the two lists.
The following algorithm is employed for the comparison:
EqualTo
if the two lists are identical in length and each objects values, not pointer, are equal.LessThan
if the instances object value at an ordinal position is less than the corresponding objects value in the obj
parameters list.obj
parameters list length.
GreaterThan
if the instances object value at an ordinal position is greater than the corresponding objects value in the obj
parameters list.obj
parameters list length.
EqualTo
for same, LessThan
for obj
logically less than the object and GreaterThan
for obj
logically greater than the object. Reimplemented from PObject.
virtual PObject* PAbstractSortedList::GetAt | ( | PINDEX | index | ) | const [virtual] |
Get the object at the specified ordinal position.
If the index was greater than the size of the collection then NULL is returned.
Implements PCollection.
virtual PINDEX PAbstractSortedList::GetObjectsIndex | ( | const PObject * | obj, | |
PSortedListElement *& | lastElement | |||
) | const [virtual] |
virtual PINDEX PAbstractSortedList::GetObjectsIndex | ( | const PObject * | obj | ) | const [virtual] |
Search the collection for the specific instance of the object.
The object pointers are compared, not the values. A binary search is employed to locate the entry.
Note that that will require value comparisons to be made to find the equivalent entry and then a final check is made with the pointers to see if they are the same instance.
Implements PCollection.
virtual PINDEX PAbstractSortedList::GetValuesIndex | ( | const PObject & | obj | ) | const [virtual] |
Search the collection for the specified value of the object.
The object values are compared, not the pointers. So the objects in the collection must correctly implement the PObject::Compare()
function. A binary search is employed to locate the entry.
Implements PCollection.
Add a new object to the collection.
The object is always placed in the correct ordinal position in the list. It is not placed at the specified position. The before
parameter is ignored.
Implements PCollection.
virtual PINDEX PAbstractSortedList::InsertAt | ( | PINDEX | index, | |
PObject * | obj | |||
) | [virtual] |
Add a new object to the collection.
The object is always placed in the correct ordinal position in the list. It is not placed at the specified position. The index
parameter is ignored.
Implements PCollection.
void PAbstractSortedList::LeftRotate | ( | Element * | node | ) | [protected] |
Remove the object from the collection.
If the AllowDeleteObjects option is set then the object is also deleted.
Note that the comparison for searching for the object in collection is made by pointer, not by value. Thus the parameter must point to the same instance of the object that is in the collection.
Implements PCollection.
virtual void PAbstractSortedList::RemoveAll | ( | ) | [virtual] |
Remove all of the elements in the collection.
This operates by continually calling RemoveAt()
until there are no objects left.
The objects are removed from the last, at index (GetSize()-1)
toward the first at index zero.
Reimplemented from PCollection.
virtual PObject* PAbstractSortedList::RemoveAt | ( | PINDEX | index | ) | [virtual] |
Remove the object at the specified ordinal index from the collection.
If the AllowDeleteObjects option is set then the object is also deleted.
Note if the index is beyond the size of the collection then the function will assert.
Implements PCollection.
void PAbstractSortedList::RemoveElement | ( | Element * | node | ) | [protected] |
void PAbstractSortedList::RightRotate | ( | Element * | node | ) | [protected] |
This method simply returns false as the list order is mantained by the class.
Kept to mimic PAbstractList
interface.
Implements PCollection.
virtual PBoolean PAbstractSortedList::SetSize | ( | PINDEX | newSize | ) | [virtual] |
This function is meaningless for lists.
The size of the collection is determined by the addition and removal of objects. The size cannot be set in any other way.
Implements PContainer.
PINDEX PAbstractSortedList::ValueSelect | ( | const Element * | node, | |
const PObject & | obj, | |||
const Element ** | lastElement | |||
) | const [protected] |
PSortedListInfo* PAbstractSortedList::info [protected] |