Important: The information in this document is obsolete and should not be used for new development.
Overview
A list is a data structure that stores a variable number of items of similar types. MacApp provides classes for creating and manipulating lists, allowing your application to use lists without having to implement the low-level details. MacApp's list classes and methods are shown in Figure 25-1.Figure 25-1 List classes and methods
MacApp's list classes use a dynamic array model. Each item in a list has a unique index--you can access the items in a list by index or by standard list-traversal methods. The
TSortedList
class and its descendants can store only references to items that descend from theTObject
class. Other MacApp-defined list classes, such asTHandleList
, store generic references that can be used to deal with a variety of developer-defined data types.Iteration is a general-purpose mechanism for traversing the items in a list. MacApp provides the
CIterator
class and many specialized subclasses that operate on various types of lists. Iteration classes can traverse the items in a list without knowing the exact details of the data structures involved.Figure 25-2 shows MacApp's iteration classes and methods.
Figure 25-2 Iteration classes and methods
Working With Lists of Objects
MacApp'sTSortedList
andTList
classes provide extensive capabilities for working with lists of objects that descend fromTObject
. TheTSortedList
class includes methods and fields for processing an ordered list in which each element is a reference to an object. TheTList
class is a subclass ofTSortedList
that doesn't sort the objects.TList
is used widely by MacApp, and it can be a valuable tool in your application as well.
- Note
- This section describes the
TSortedList
andTList
classes, which refer to objects. TheTSortedHandleList
andTHandleList
provide virtually identical operations on handles.Creating a List
MacApp provides theNewList
routine to create and initialize a newTList
object. The convenience routineNewAllocatedList
lets you create a newTList
object and at the same time specify the number of elements for which storage should be allocated. Each of these routines callsFailure
if the list cannot be allocated.Adding an Object to a List
You add an object to aTList
with a method that inserts a reference to the specified object into the list. The insertion method callsFailure
if an attempt is made to insert the reference at an index that is out of range.The following insertion methods are available:
MacApp's iteration mechanism, described beginning on page 576, allows you to iterate over a list correctly, even as items are added to the list.
AtPut
- The
AtPut
method replaces the object reference at the specified position with a reference to the new object, without freeing the old object.Insert
- For the
TList
class, theInsert
method inserts the object reference at the end of the list. For theTSortedList
class, the parent class ofTList
,Insert
adds the object reference in sorted order, based on the list'sCompare
method.InsertBefore
- The
InsertBefore
method inserts the object reference before the reference at the specified index. If the index is equal to 1, the object reference is inserted at the head of the list.InsertFirst
- The
InsertFirst
method inserts the object reference at the front of the list and makes its index equal to 1.InsertLast
- The
InsertLast
method inserts the object reference at the end of the list.Deleting an Object From a List
MacApp's list classes provide a number of methods for deleting an item from a list--not all are available in every list class:
MacApp's iteration mechanism, described beginning on page 576, allows list iteration to proceed correctly, even as items are deleted from the list.
AtDelete
- The
AtDelete
method deletes the object reference at the specified index position.Delete
- The
Delete
method deletes the first reference to the object in the list. The object itself is not freed.Delete
does nothing if the object is not found in the list.DeleteAll
- The
DeleteAll
method deletes every element from the list, but does not free any of the objects.- IMPORTANT
- These methods delete a reference from the list--they do not free any memory occupied by the referred-to item itself. See the section "Freeing a List" (page 576) for more information on freeing a list and the items it references.
Finding an Object in a List
TheTList
class provides a number of methods for finding an object in a list. You can find an object that is the same as another object, or find a specific object by its position in the list.The following methods are available:
At
- The
At
method returns the object reference at the specified index.First
- The
First
method returns the first object reference in the list. It returnsNULL
if the list is empty.Last
- The
Last
method returns the last object reference in the list. It returnsNULL
if the list is empty.GetEqualItemNo
- The
GetEqualItemNo
method uses the object'sCompareObject
method to test for equality. This allows object equality to be determined by the object. An "equal" object may be found, even though the object being looked for is not, in fact, identical to the one found.GetIdentityItemNo
- The
GetIdentityItemNo
method uses theoperator==
function to compare items in the list to the item being searched for. Theoperator==
function is overloaded inTObject
, where it calls theIsEqual
method, which does nothing inTObject
. As a result,GetIdentityItemNo
does nothing unless you override theIsEqual
method.- IMPORTANT
- To use the
GetIdentityItemNo
method, you must override theIsEqual
method with a version that can identify equality between the items stored in your list.Freeing a List
TheTDynamicArray::Free
method frees the memory occupied by a list itself but does not free the items referred to. You can call theFreeAll
method to free each of the objects in a list of object references (TSortedList
and descendants) or each of the handles in a list of handle references (TSortedHandleList
and descendants). The list itself becomes empty but is not freed. You can call theFreeList
method to free each object in the list and then free the list as well.Iteration
Iteration allows you to traverse an array or list and perform some operation on each element. MacApp provides theCIterator
class and several specialized subclasses for this purpose. For example, theCArrayIterator
class iterates through an array of indexed items, thecMenuIterator
class iterates through the menu handles in a menu list, theCBehaviorIterator
class iterates through the behaviors that are associated with an event handler, and theCSubViewIterator
class iterates through a list of subviews belonging to a view.Each iterator class is defined for use with a specific kind of list (for example,
CObjectIterator
works withTSortedList
) and can iterate correctly over the items in the list. This allows iteration to continue correctly, even as items are added to or deleted from a list. Iteration can be performed with both sorted and nonsorted lists and can be performed in either the forward or backward direction.The most important base class for iterators is the
CArrayIterator
class. Like several of its subclasses, theCArrayIterator
class provides multiple constructor methods for your convenience. The constructor methods ofCArrayIterator
call the initialization method,IArrayIterator
, which sets the iterator's fields, including the following:
The
fCurrentIndex
- The
fCurrentIndex
field specifies the current index during iteration.fDynamicArray
- The
fDynamicArray
field refers to the dynamic array for the list that is iterated through.fHighBound
- The
fHighBound
field is the upper bound for forward iteration through the list.fIterateForward
- The
fIterateForward
field is set toTRUE
for forward iteration through the list and toFALSE
for backward iteration.fLowBound
- The
fLowBound
field is the lower bound for backward iteration through the list.CArrayIterator
class also provides methods for inserting and deleting items in the list during iteration so that the iteration process is not invalidated by the change to the list. It includes the following methods:
DeleteElementAt
- The
DeleteElementAt
method adjusts the iteration index to account for the deleted element.InsertElementBefore
- The
InsertElementBefore
method adjusts the iteration index to account for the added element.