global items used:
member variables:
INT32
allocSize_: the number of additional elements that are allocated if
the array is expanded.
SINT32
*array_: a pointer to the beginning of the sorted array. Every occupied
array entry contains a pointer to a nodeID.
INT32
current_: contains the index of the currently accessed entry. That
may also be a free entry (in that case, the current pointer is said to
be invalid).
INT32
firstFree_: contains the index of the first free entry in the array.
INT32
size_: the current size of the array.
member functions:
CNodeIdList
(INT32 aSize = ArrayAllocationSize): The data is stored in an array,
so aSize is the number of array entries that are allocated or
freed whenever the array is expanded or shrinked. The ctor allocates an
array of storage objects (type SINT32) with that number of entries. allocSize
stores the aSize value.
virtual
~CNodeIdList (): destructor
Deletes the array that is created in the ctor.
void
AllocateMemory(): Allocates a new array that is allocSize
elements bigger than the current array. The data is copied to the new array,
and the old array is discarded.
INT32
AllocationSize() const: Returns the additional size used for expansion
or contraction of the array.
EBool
BeginOfList () const: Returns True if the current pointer,
which always contains the index of the data that was accessed last, is
at the beginning of the list, False if it is not at the beginning or if
the array is empty.
SINT32
GetCurrent() const: Returns the value of the element pointed to by
current. If current is not valid, an assert fails. Note
that the value is at the same time the key, so this function has the same
result as GetCurrentID().
SINT32
GetCurrentID() const: Returns the key of the element pointed to by
current. If current is not valid, an assert fails. Note
that the key is at the same time the value, so this function has the same
result as GetCurrent().
EBool
CurrentIsValid () const: The function returns True if current
contains the index of an occupied slot, False if it does not.
EBool
Empty() const: Returns True if the array is empty, False if objects
are stored.
EBool
EndOfList () const: Returns True if the current pointer is
at the end of the list, False if it is not at the end or if the array is
empty.
EBool
Find (SINT32 index): Searches for an entry with the given key. If
the list is not empty, it first checks if current is valid. If
it is, the element current points to is checked. If the key does
not match the search key, it checks the neighbours of current.
If their keys do not match either, or if current was invalid in
the first place, binary search is used to find the element. If it is found,
the function returns True and current contains the array index
of the element. If it is not found, False is returned and current
contains the index where the element should be inserted (if the element
is to be appended, current is now invalid!).
void
FreeMemory(): Allocates a new array that is allocSize elements
smaller than the current array (if that would result in a non-positive
size, an assert fails). The data is copied to the new array, and the old
array is discarded. All data must fit into the new array. If that is not
the case, an assert will fail.
SINT32
GetFirst (): Returns the first nodeID in the list, or the global constant
NoAccount if the list is empty. current
is set to the first element.
SINT32
GetLast (): Returns the first nodeID in the list, or NoAccount
if the list is empty. current is set to the last element.
SINT32
GetNext (): If current is valid, ie. if it contains the index
of an array entry that is occupied (data is stored there), the function
returns the nodeID of the next data entry in the array. It returns NoAccount
if the array is empty, if the end of the array is reached or if current
does not point to an occupied slot. If this next element exists, current
is incremented accordingly.
EBool
Insert (SINT32 index): If an entry with the ID index exists,
False is returned. Else, index is inserted into the (sorted) list
and the function returns True.
void
InsertAtCurrent (SINT32 index): Inserts the new data at the array
position specified by current. Shifts the rest of the array one
element to the right. If current is not valid or if the array
does not contain any free elements, an assert fails. If the array is full
after the new data is inserted, a bigger array is allocated (using
AllocateMemory()).
INT32
Occupied() const: Returns the number of array elements that are currently
in use.
EBool
Remove (SINT32 index): If no entry index exists, False is
returned. Else, the entry is removed and True is returned.
void
RemoveAtCurrent (): The element pointed to by current is
freed, ie. all elements after the current one are shifted one place to
the left, overwriting the current slot. If current is no valid
index, an assert fails. If more than allocSize+1 elements are
unused, a smaller array is allocated using FreeMemory().
INT32
Size() const: Returns the current size of the array (ie. the number
of array elements).
void
UpdateArray(): Allocates a new array (the new size is taken from the
private member size), and copies the occupied part of the old
array to the new one. The old array is deleted, and the internal array
pointer is set to the new array. Note that the pointer is set to the new
array *after* the old array has been deleted, so concurrent access should
not be allowed. The new array must be big enough to hold the occupied part
of the old array, otherwise an assert will fail.