LArray is a PowerPlant class that is used for implementing an ordered collection of fixed-size items. The first item is at index value 1. The index value 0 is used to indicate a nonexistent item.
Methods :
The methods in this class are:
Data Members:
The data members in this class are:
Operation:
Index values are signed, 32-bit integers. When specifying an item, you pass a pointer to the item data as a parameter. The array stores a copy of the data, or returns a copy of the data to you.
You can insert, remove, get a value, assign a value, swap, move
items, and get data about the array. PowerPlant defines the constants
index_First and index_Last so that you can easily insert items at the beginning or end of
the array.
Source files:
See Also:
Purpose:
The constructors create the object with the passed-in parameters.
Access:
Prototypes:
LArray();
LArray( const LArray &inOriginal );
These constructors create an array with space pre-allocated for the specified number of items of the specified size. If nItemCount is not specified, then the array will be empty after creation.
LArray( USInt32 inItemCount,
USInt32 inItemSize,
LComparator *inComparator = nil,
Boolean inKeepSorted = false );
LArray( USInt32 inItemSize,
LComparator *inComparator = nil,
Boolean inKeepSorted = false );
LArray( USInt32 inItemSize,Parameters:
Handle inItemsHandle,
LComparator *inComparator = nil,
Boolean inIsSorted = false,
Boolean inKeepSorted = false );
The parameters for this constructor are:
Purpose:
The destructor destroys the array.
Access:
Prototype:
virtual ~LArray();
Purpose:
Add one item to array. If the array is not sorted, add the item to the end of the array.
Access:
Prototype:
virtual ArrayIndexT AddItem( const void *inItem,Parameters:
USInt32 inItemSize );
This method takes the following parameters:
Return:
Returns the index point at which the item was inserted.
Remarks:
For unsorted Arrays, this function is a faster version of InsertItemsAt() since it doesn't have to bother with checking/adjusting the count and insertion index.
Purpose:
This method is a wrapper call to InternalAdjustAllocation().
Access:
Prototype:
virtual void AdjustAllocation(Parameters:
USInt32 inExtraItems,
USInt32 inExtraData );
This method takes the following parameters:
| The size of the item in bytes. This has a default parameter value of zero and is ignored. |
Return:
Remarks:
For fixed-size items, the number of items determines the amount of data stored.
Purpose:
This method is called internally when the number of bytes used by items in the array changes.
Access:
Prototype:
virtual void AdjustStorage(Parameters:
SInt32 inDeltaItems,
SInt32 inDeltaData );
This method takes the following parameters:
| Ignored, since the number of items determines the amount of data stored for fixed-size item arrays. |
Return:
Remarks:
If the current allocation is too small, this implementation sets the internal allocation size to:
current_alloc + max(current_alloc, delta_bytes)
For small adjustments (adding less bytes than what's already allocated), this doubles the allocation.
For large adjustments (adding more bytes than what's already allocated), this increases the allocation by the number of bytes added.
Purpose:
Assign the same value to items in the array starting at the specified index.
Access:
Prototype:
virtual ArrayIndexT AssignItemsAt(Parameters:
USInt32 inCount,
ArrayIndexT inAtIndex,
const void *inValue,
USInt32 inItemSize)
This method takes the following parameters:
| The starting index. | ||
| A pointer to the item data. The array makes and stores a copy of the item data. | ||
| The size of the array item. |
Return:
Returns index of first "assigned" item. This may be different
from inAtIndex if the array is sorted. Returns LArray::index_Bad if inAtIndex is out of range.
Remarks:
Does nothing if inAtIndex is out of range.
Purpose:
Associate an iterator with an array.
Access:
Prototype:
void AttachIterator(Parameters:
LArrayIterator *inIterator ) const;
This method takes the following parameter:
Return:
Purpose:
This method returns the index of the specified item using a binary search. It assumes that the array is sorted.
Access:
Prototype:
ArrayIndexT BinarySearch(Parameters:
const void *inItem,
USInt32 inItemSize) const;
This method takes the following parameters:
| A pointer to the item to search for. | ||
| The size of the array item. |
Return:
The index at which the item was found. If not found, the value
returned will be index_Bad.
Purpose:
Return the index of the item with the specified key, using a binary search. It assumes that the array is sorted.
Access:
Prototype:
ArrayIndexT BinarySearchByKey(Parameters:
const void *inKey) const;
This method takes the following parameter:
| A pointer to the key to search for. |
Return:
The index at which the item was found. If not found, the value
returned will be index_Bad.
Purpose:
Creates a deep copy by duplicating the items in the Array. However, if the items in the Array are pointers to other objects, those other objects aren't duplicated.
Access:
Prototype:
void CopyArray( const LArray &inOriginal );Parameters:
This method takes the following parameter:
| A reference to the original object to copy. |
Return:
Remarks:
Purpose:
Destroy the internal data an array.
Access:
Prototype:
void DestroyArray();Parameters:
Return:
Purpose:
Remove the association of an iterator from an array.
Access:
Prototype:
void DetachIterator(Parameters:
LArrayIterator *inIterator) const;
This method takes the following parameter:
| A pointer to the iterator. |
Return:
Purpose:
Returns the index of the specified item within the array.
Access:
Prototype:
virtual ArrayIndexT FetchIndexOf(Parameters:
const void *inItem,
USInt32 inItemSize)
This method takes the following parameters:
| A pointer to the item to search for. | ||
| The size of the array item. |
Return:
Returns index_Bad if the item is not in the array.
Purpose:
Return the index of the item with the specified Key value.
Access:
Prototype:
virtual ArrayIndexT FetchIndexOfKey(Parameters:
const void *inKey)
This method takes the following parameter:
| A pointer to the key to search for. |
Return:
Returns the index of the item.
Purpose:
Return the index at which the specified item would be inserted.
Access:
Prototype:
virtual ArrayIndexT FetchInsertIndexOf(Parameters:
const void *inItem,
USInt32 inItemSize)
This method takes the following parameters:
| A pointer to the item to search for. | ||
| The size of the array item. |
Return:
Returns index_Last if the Array is not sorted or if the item is nil.
Purpose:
Return the index at which an item with the specified Key would be inserted.
Access:
Prototype:
virtual ArrayIndexT FetchInsertIndexOfKey(Parameters:
const void *inKey)
This method takes the following parameter:
| A pointer to the key to search for. |
Return:
Returns index_Last if the Array is not sorted or if the item is nil.
Purpose:
Pass back the item at the specified index.
Access:
Prototype:
virtual Boolean FetchItemAt(
ArrayIndexT inAtIndex,
void *outItem,
USInt32 &ioItemSize ) const;
virtual Boolean FetchItemAt(Parameters:
ArrayIndexT inAtIndex,
void *outItem ) const;
This method takes the following parameters:
| The index to retrieve from. | ||
| The pointer to the retrieved item. Caller must make sure that this points a buffer large enough to hold the item data. | ||
| The size of the item. |
Return:
Returns true if an item exists at inIndex (and sets outItem). Returns false if inIndex is out of range (and leaves outItem unchanged).
Purpose:
This method returns the value of the mComparator data member.
Access:
Prototype:
LComparator* GetComparator() const;Parameters:
Return:
A pointer to the LComparator.
Purpose:
This method returns the value of the mItemCount data member.
Access:
Prototype:
USInt32 GetCount() const;Parameters:
Return:
USInt32 indicating the value of mItemCount.
Purpose:
Returns a pointer to the start of an item's data within the internal storage Handle.
Access:
Prototype:
virtual void* GetItemPtr(Parameters:
ArrayIndexT inAtIndex) const;
This method takes the following parameter:
| The index to retrieve from. |
Return:
A pointer to the start of the item data.
Remarks:
WARNING: The return pointer references information inside a relocatable block. This pointer will become invalid if the Handle block moves. Call Lock() and then Unlock() where necessary.
WARNING: For sorted arrays, be careful when changing the data using the pointer. If your changes alter the sorting order, call InvalidateSort() so that the array's internal flags correctly reflect the sorting state. Then call Sort() afterwards if you still want the array to be sorted.
Purpose:
This method returns the value of the mItemSize data member.
Access:
Prototype:
virtual USInt32 GetItemSize(ArrayIndexT inIndex) const;Parameters:
This method takes the following parameter:
| The index to retrieve the size of. |
Return:
The value of mItemSize in a USInt32.
Purpose:
Return Handle used to store data for array items.
Access:
Prototype:
Handle GetItemsHandle() const;Parameters:
Return:
The handle to the array items.
Purpose:
This method returns the total size in bytes of the items in the array between (inclusive) the specified start and end indices.
Access:
Prototype:
virtual USInt32 GrabItemRangeSize(Parameters:
ArrayIndexT inStartIndex,
ArrayIndexT inEndIndex) const;
This method takes the following parameters:
| The starting index. | ||
| The ending index. |
Return:
A size in bytes stored in a USInt32.
Purpose:
This data member returns the value of the mItemSize data member.
Access:
Prototype:
virtual USInt32 GrabItemSize(Parameters:
ArrayIndexT inIndex ) const;
This method takes the following parameter:
| The index (unused). |
Return:
USInt32 containing the value of the mItemSize member.
Purpose:
This is an internal method which initializes the data members for an array.
Access:
Prototype:
void InitArray(Parameters:
USInt32 inItemSize,
LComparator *inComparator,
Boolean inIsSorted,
Boolean inKeepSorted);
This method takes the following parameters:
| The size of the items for the array. | ||
| The pointer to the comparator object. | ||
| A value indicating whether the array is sorted or not. | ||
| A value indicating whether to keep the array sorted or not. |
Return:
Purpose:
Insert items at the specified position in an array.
Access:
Prototype:
virtual ArrayIndexT InsertItemsAt(Parameters:
USInt32 inCount,
ArrayIndexT inAtIndex,
const void *inItem,
USInt32 inItemSize);
This method takes the following parameters:
| The number of items to insert. | ||
| The index to begin inserting at. | ||
| A pointer to the item to insert. | ||
| The size of the item in bytes. |
Return:
The index at which items were inserted. This can differ from the input value of inAtIndex as described in the Remarks.
Remarks:
All items are set to the same value, as specified by inItem. inItem
may be nil, in which case the data for the inserted items is unspecified
(but space is allocated).
inAtIndex is adjusted if necessary:
> to sorted position if array is kept sorted > to after last item if inAtIndex is too big > to 1 if inAtIndex is too small
Purpose:
Called internally to change the size of the storage used.
Access:
Prototype:
virtual void InternalAdjustAllocation(Parameters:
USInt32 inItemAllocation,
USInt32 inDataAllocation );
This method takes the following parameter:
| The the number of items to allocate space for. | ||
| The the amount of data to allocate space for. |
Remarks:
Fixed-size item Array only stores data, so inItemAllocation is ignored.
Purpose:
Set value of destination item to that of the source item.
Access:
Prototype:
virtual void InternalCopyItem(Parameters:
ArrayIndexT inSourceIndex,
ArrayIndexT inDestIndex);
This method takes the following parameters:
| The source index. | ||
| The destination index. |
Return:
Purpose:
Move an item from one position to another in an array. The net result is the same as removing the item and inserting at a new position.
Access:
Prototype:
virtual void InternalMoveItem(Parameters:
ArrayIndexT inFromIndex,
ArrayIndexT inToIndex,
void *inBuffer);
This method takes the following parameters:
| A pointer to the data buffer. | ||
| The source index. | ||
| The destination index. |
Return:
Purpose:
Swap the values of the Items at the specified indices. This is an internal method for this class.
Access:
Prototype:
virtual void InternalSwapItems(Parameters:
ArrayIndexT inIndexA,
ArrayIndexT inIndexB,
void *inBuffer);
This method takes the following parameters:
| A pointer to the data buffer. | ||
| The first index. | ||
| The second index. |
Return:
Purpose:
This method sets the mIsSorted data member to false.
Access:
Prototype:
void InvalidateSort();Parameters:
Return:
Purpose:
This method returns the value of the mKeepSorted data member.
Access:
Prototype:
Boolean IsKeptSorted() const;Parameters:
Return:
Boolean true or false indicating the value of mKeepSorted.
Purpose:
This method returns the value of the mIsSorted data member.
Access:
Prototype:
Boolean IsSorted() const;Parameters:
Return:
Boolean true or false indicating the value of mIsSorted.
Purpose:
Notify Iterators associated with an array that items have been inserted.
Access:
Prototype:
void ItemsInserted(Parameters:
USInt32 inCount,
ArrayIndexT inAtIndex);
This method takes the following parameters:
| The number of items that have been inserted. | ||
| The index of the insertion. |
Return:
Purpose:
Notify Iterators associated with an Array that items have been removed
Access:
Prototype:
void ItemsRemoved(Parameters:
USInt32 inCount,
ArrayIndexT inAtIndex);
This method takes the following parameters:
| The number of items that have been removed. | ||
| The index of the removal. |
Return:
Purpose:
Return the index of the specified item, searching linearly from the start of the array.
Access:
Prototype:
ArrayIndexT LinearSearch(Parameters:
const void *inItem,
USInt32 inItemSize) const;
This method takes the following parameters:
| The size of the item to search for. | ||
| The pointer to the item to search for. |
Return:
The index at which the item is located, index_Bad if not found.
Purpose:
Return the index of the item with the specified key, searching linearly from the start of the array.
Access:
Prototype:
ArrayIndexT LinearSearchByKey(Parameters:
const void *inKey) const;
This method takes the following parameter:
| A pointer to the key to search for. |
Return:
The index at which the item is located, index_Bad if not found.
Purpose:
Lock the Handle that stores the data for the items in the array.
Access:
Prototype:
void Lock() const;Parameters:
Return:
Remarks:
This class maintains a lock count, so each call to Lock() should be balanced by a corresponding call to Unlock().
Purpose:
Move an item from one position to another in an array. The net result is the same as removing the item and inserting at a new position.
Access:
Prototype:
virtual void MoveItem(Parameters:
ArrayIndexT inFromIndex,
ArrayIndexT inToIndex );
This method takes the following parameters:
| The source index. | ||
| The destination index. |
Return:
Remarks:
Does nothing if either index is out of range or if the array is kept sorted (since moving could invalidate the sort).
Purpose:
Pass back the data for the item at the specified index. This is used internally to read item data.
Access:
Prototype:
virtual void PeekItem(Parameters:
ArrayIndexT inAtIndex,
void *outItem ) const;
This method takes the following parameters:
| The array index to peek at. | ||
| The pointer to the item to return data for. |
Return:
Purpose:
Store data for the item at the specified index.
Access:
Prototype:
virtual void PokeItem(Parameters:
ArrayIndexT inAtIndex,
const void *inItem,
USInt32 inItemSize );
This method takes the following parameters:
| The array index to poke at. | ||
| The pointer to the item to store data for. | ||
| The size of the data block to store |
Return:
Purpose:
Access:
Prototype:
virtual void Remove(Parameters:
const void *inItem,
USInt32 inItemSize);
This method takes the following parameters:
| The pointer to the item to remove data for. | ||
| The size of the data block to remove. |
Return:
Purpose:
Remove items from an array starting at a specified position.
Access:
Prototype:
virtual void RemoveItemsAt(Parameters:
USInt32 inCount,
ArrayIndexT inAtIndex);
This method takes the following parameters:
| The index to start removing at. | ||
| The size of the data block to remove. |
Return:
Remarks:
Does nothing if inAtIndex is out of range. Checks if inCount would remove items past the end of the array, and adjusts it
accordingly to remove the items from inAtIndex to the end of the array. That means you can pass a large number
to remove the items from inAtIndex to the end of the array.
Purpose:
Specify the comparator for items in an array.
Access:
Prototype:
void SetComparator(Parameters:
LComparator *inComparator,
Boolean inTakeOwnership);
This method takes the following parameters:
| The comparator to use. | ||
| Indicates the value to set for mOwnsComparator. |
Return:
Purpose:
Specify whether to keep an array sorted when items change.
Access:
Prototype:
void SetKeepSorted( Boolean inKeepSorted );Parameters:
This method takes the following parameter:
| Whether to keep the array sorted or not. |
Return:
Purpose:
is an internal method that moves items within the Handle used
for internal storage. It moves items in the range inStartIndex to inEndIndex (inclusive).
Access:
Prototype:
virtual void ShiftItems(Parameters:
ArrayIndexT inStartIndex,
ArrayIndexT inEndIndex,
SInt32 inIndexShift,
SInt32 inDataShift);
This method takes the following parameters:
| The index to start shifting at. | ||
| The index to end shifting at. | ||
| The amount to shift. | ||
| This is unused. |
Return:
Purpose:
Access:
Prototype:
virtual void Sort();Parameters:
Return:
Purpose:
This is an internal method that stores values within the internal storage Handle. Items all have the same value, and space must already have been allocated for them.
Access:
Prototype:
virtual void StoreNewItems(Parameters:
USInt32 inCount,
ArrayIndexT inAtIndex,
const void *inItem,
USInt32 inItemSize);
This method takes the following parameters:
| The number of items. | ||
| The index to start storing at. | ||
| The item to store. | ||
| The size of the item to store. |
Return:
Purpose:
Swap the values of the Items at the specified indices. This method does nothing if either index is out of range or if array is kept sorted (since swapping could invalidate the sort).
Access:
Prototype:
virtual void SwapItems(Parameters:
ArrayIndexT inIndexA,
ArrayIndexT inIndexB);
This method takes the following parameters:
| The first index. | ||
| The second index. |
Return:
Purpose:
Unlock the Handle that stores the data for the items in the array.
This class maintains a lock count, so each call to Lock() should be balanced by a corresponding call to Unlock().
Access:
Prototype:
void Unlock() const;Parameters:
Return:
Purpose:
Indicate whether an index is valid (between 1 and the number of items) for the array.
Access:
Prototype:
Boolean ValidIndex(Parameters:
ArrayIndexT &ioIndex ) const;
This method takes the following parameter:
The reference to the index to check. If ioIndex is the special flag index_Last, the index's value is changed to the actual index of the last
item. |
Return:
Boolean indicating whether the index is a valid one, false if it is invalid.
Purpose:
Disposes array's existing data and copies data of the specified array. See comments for CopyArray() for detailed information about how the copy is done.
Access:
Prototype:
LArray& operator=(Parameters:
const LArray &inArray);
This method takes the following parameter:
| The reference to the array. |
Return:
Purpose:
This data member stores the comparator for the array.
Access:
Prototype:
LComparator *mComparator;
Purpose:
Access:
Prototype:
USInt32 mDataAllocated;
Purpose:
Stores the amount of space to allocate for storage.
Access:
Prototype:
USInt32 mDataStored;
Purpose:
Indicates whether the array is supposed to be sorted.
Access:
Prototype:
Boolean mIsSorted;
Purpose:
The number of items in the array.
Access:
Prototype:
USInt32 mItemCount;
Purpose:
The handle to the array items.
Access:
Prototype:
Handle mItemsH;
Purpose:
Access:
Prototype:
USInt32 mItemSize;
Purpose:
The head for the iterator for the array.
Access:
Prototype:
mutable LArrayIterator *mIteratorListHead;
Purpose:
Indicates whether the array is to be kept sorted.
Access:
Prototype:
Boolean mKeepSorted;
Purpose:
A reference counter to count the number of times the array is locked (decremented by Unlock()).
Access:
Prototype:
mutable USInt32 mLockCount;
Purpose:
This indicates whether the array owns the comparator.
Access:
Prototype:
Boolean mOwnsComparator;