List
Overview
The List class holds a sequence of objects which are identified by a positional index. It is intended for managing groups of objects which have either no natural indexing information or for which the associated numeric identifiers form a densely packed set beginning with the index zero.
The List object may expand or shrink to contain varying numbers of objects. It also supports several “iterator” functions, which are used to successively visit each object in the list.
- Definition
- Set and query elements
count()- Get number of contained objectsallIndices()- Get all indicesmaxIndex()- Get maximum indexinsert()- Place object at specified index locationadd()- Place object in first empty positionappend()- Place object at the end of sequenceget()- Find object at specified locationremove()- Remove object from a specified locationclear()- Remove all objects from the List objectcompact()- Reduce storage space if possibleforEach()- Call a function one time for each objectinitializeIterator()- Prepare to successively visit each objectnextItem()- Return the next object in the sequence
- General functions
getErrorCode()- Return the current error code
Once a List is
instanced, an approximate number of objects to be stored may be defined using
define(). This allocates an internal array for the storage of
object pointers.
As objects are inserted the List class will adjust memory as required.
The actual
size of the allocated storage may be determined by calling inquire().
Each new object is placed in the list by calling insert(),
add(), or append(). The total number of objects
contained in the list may be found by calling count(). The object
at a specified index may be found by calling get(). An object
may be removed by calling remove(). The method clear()
removes all objects from the list. Unused storage at the end of the
internal array may be released by calling compact().
Each object in the list may be processed with an “iterator” construct.
The single method forEach() takes a function as its argument and
calls that function repeatedly, each time with one object from the list.
The pair of methods initializeIterator() and nextItem() are used to
visit each element using a loop. Call the first method before the body of
the loop to prepare the List object for the iteration. Within the loop
body, call nextItem(). It will return a pointer to a new object
each time it is called. It will return a NULL pointer when all the
objects have been processed.
Class Members Descriptions
The currently available List enumerations and functions are described in detail in this section.
-
template<typename
T>
classList Randomly Accessible Sequences.
Public Functions
-
ErrorCode
getErrorCode() Return the current
ErrorCodeof the List object.Returns: ErrorCode- The current error code, orNONEif no error.
-
Status
define(int capacity) Suggest a number of elements to be stored in the List. This is used to allocate the initial array of storage which is used to hold a pointer to each stored object. If the number of inserted elements is greater than this initial estimate, storage space is dynamically expanded to hold the extra objects. This function removes any previously entered objects.
See Also
inquire()- Errors
-
MEMORYis generated if new storage was unable to be allocated.
Parameters: capacity – Estimated number of objects to be held Returns: Status
-
Status
inquire(int *capacity) Find the current length of the internal storage of the List.
See Also
define()Parameters: capacity – [out] Current length of internal storage Returns: Status
-
Status
count(int *itemCount) Get the number of objects which have been inserted or appended, and not yet removed.
Parameters: itemCount – [out] Number of objects held in the List object Returns: Status
-
Status
maxIndex(int *index) Get the maximum index used to store current objects in the List.
See Also
allIndices()Parameters: index – [out] Maximum index Returns: Status
-
Status
allIndices(int indices[]) Get all indices used to store current objects in the List. The number of index values returned will be equal to the number of indices returned by
count().See Also
maxIndex(),count()Parameters: indices – [out] Array of all indices Returns: Status
-
Status
insert(int index, T *item) Place the object at the location identified by index, replacing any object which may have been previously placed there.
See Also
add(),append(),get()- Errors
Parameters: - index – Position in list at which object is to be stored
- item – Pointer to the object
Returns:
-
Status
add(T *item, int *index) Insert an object into the list at an arbitrarily assigned index. The location at which the object was placed will be returned in the output argument index.
Parameters: - item – Pointer to the object
- index – [out] Position in list at which object was stored
Returns:
-
Status
append(T *item) Place an object at the end of the list.
Parameters: item – Pointer to the object Returns: Status
-
Status
get(int index, Pointer<T> &item) Get a pointer to the object located at the position identified by index. Returns a NULL pointer if no object has been placed there, or if a previously inserted object has since been removed from this location.
See Also
insert()Parameters: - index – Location of the desired object
- item – [out] Object found at location index
Returns:
-
Status
remove(int index) Remove any object found at the location identified by index. This may introduce a “gap” in the list, which may later be filled with one of the insertion methods.
Parameters: index – Location to be emptied Returns: Status
-
Status
initializeIterator() Initiate the iteration for sequential access to each item in the list. Each call of
nextItem()will return a new item from the list together with its associated index. After every object has been visited,nextItem()will return a NULL pointer until the iteration is restarted usinginitializeIterator(). Objects are visited starting from position zero, and empty positions in the list are skipped over.See Also
nextItem(),forEach()Returns: Status
-
Status
nextItem(int *index, Pointer<T> &item) Get the next item from the list together with its associated index. Returns a NULL pointer when all objects have been visited.
See Also
initializeIterator(),forEach()Parameters: - index – [out] Index of the next item
- item – [out] Object at the index
Returns:
-
Status
forEach(UnaryFunction *function) Call the supplied function repeatedly, each time using an object from the list as the sole argument to this function. This is useful for deallocating a collection of objects, prior to destroying the List. Objects are visited in numerically increasing order of their position indices. Empty positions in the list are skipped over.
See Also
initializeIterator(),nextItem()Parameters: function – Pointer to function which is to be applied to each object Returns: Status
-
ErrorCode