Simpatico
v1.10
|
A container for pointers to a subset of elements of an associated array.
An ArraySet is a PArray that stores pointers to a subset of the elements of an associated Array container or bare C array. Pointers to the elements of this set are stored in a contiguous sequence, with indices in the range 0, ..., size() - 1. The order in which these pointers are stored is mutable, and generally changes whenever an element is removed.
The append() method appends a pointer to a new element to the end of the sequence and increments the size. The remove() method removes a specified element, then moves the pointer of the last element to the space vacated by the removed element (unless the removed element was the last in the sequence), and decrements the size. The order in which the remaining elements of an ArraySet are stored thus can change whenever an element is removed.
An ArraySet provides O(N) sequential access to all elements of a set, O(1) insertion and deletion, and O(1) access to a randomly chosen element.
Definition at line 46 of file ArraySet.h.
#include <ArraySet.h>
Public Member Functions | |
ArraySet () | |
Constructor. More... | |
virtual | ~ArraySet () |
Destructor. More... | |
void | allocate (const Data *array, int capacity) |
Associate with a C array and allocate required memory. More... | |
void | allocate (const Array< Data > &array) |
Associate with an Array container and allocate required memory. More... | |
Mutators | |
void | append (Data &data) |
Append an element to the set. More... | |
void | remove (const Data &data) |
Remove an element from the set. More... | |
Data & | pop () |
Pop the topmost from the set. More... | |
void | clear () |
Reset to empty state. More... | |
Accessors | |
int | index (const Data &data) const |
Return the current index of an element within the set, if any. More... | |
bool | isAllocated () const |
Return true if the ArraySet is initialized, false otherwise. More... | |
bool | isValid () const |
Return true if the ArraySet is valid, or throw an exception. More... | |
void | dump () const |
Write the internal state of the ArraySet to std::cout. More... | |
![]() | |
virtual | ~PArray () |
Destructor. More... | |
int | capacity () const |
Return allocated size. More... | |
int | size () const |
Return logical size. More... | |
void | begin (PArrayIterator< Data > &iterator) const |
Set a PArrayIterator to the beginning of this PArray. More... | |
void | begin (ConstPArrayIterator< Data > &iterator) const |
Set a ConstPArrayIterator to the beginning of this PArray. More... | |
Data & | operator[] (int i) const |
Mimic C array subscripting. More... | |
Additional Inherited Members | |
![]() | |
PArray () | |
Constructor (protected to prevent instantiation). More... | |
![]() | |
Data ** | ptrs_ |
PArray of of pointers to Data objects. More... | |
int | capacity_ |
Allocated size of ptrs_ array. More... | |
int | size_ |
Logical size (number of elements with initialized data). More... | |
Util::ArraySet< Data >::ArraySet | ( | ) |
Constructor.
Definition at line 187 of file ArraySet.h.
|
virtual |
Destructor.
Definition at line 196 of file ArraySet.h.
References Util::PArray< Data >::capacity_, and Util::PArray< Data >::ptrs_.
void Util::ArraySet< Data >::allocate | ( | const Data * | array, |
int | capacity | ||
) |
Associate with a C array and allocate required memory.
This method associates an ArraySet with a bare C array, and allocates all memory required by the ArraySet.
An ArraySet may only be allocated once. This method throws an Exception if it is called more than once.
array | associated C array of Data objects |
capacity | number of elements in the array |
Definition at line 210 of file ArraySet.h.
References Util::PArray< Data >::capacity(), Util::PArray< Data >::capacity_, Util::PArray< Data >::ptrs_, Util::PArray< Data >::size_, and UTIL_THROW.
Referenced by Util::ArraySet< Data >::allocate().
void Util::ArraySet< Data >::allocate | ( | const Array< Data > & | array | ) |
Associate with an Array container and allocate required memory.
Invokes allocate(&array[0], array.capacity()) internally.
array | associated Array<Data> container |
Definition at line 237 of file ArraySet.h.
References Util::ArraySet< Data >::allocate(), and Util::Array< Data >::capacity().
void Util::ArraySet< Data >::append | ( | Data & | data | ) |
Append an element to the set.
This appends a new element to the end of the sequence. This does not change the order of other elements.
data | array element to be added. |
Definition at line 244 of file ArraySet.h.
References Util::PArray< Data >::capacity_, Util::PArray< Data >::ptrs_, Util::PArray< Data >::size_, and UTIL_THROW.
void Util::ArraySet< Data >::remove | ( | const Data & | data | ) |
Remove an element from the set.
Removal of an element generally changes the order of the remaining elements.
Throws an Exception if data is not in this ArraySet.
data | array element to be added. |
Definition at line 268 of file ArraySet.h.
References Util::PArray< Data >::capacity_, Util::PArray< Data >::ptrs_, Util::PArray< Data >::size_, and UTIL_THROW.
Data & Util::ArraySet< Data >::pop | ( | ) |
Pop the topmost from the set.
Popping the top element does not change the order of the remaining elements.
Definition at line 301 of file ArraySet.h.
References Util::PArray< Data >::ptrs_, Util::PArray< Data >::size_, and UTIL_THROW.
void Util::ArraySet< Data >::clear | ( | ) |
Reset to empty state.
Definition at line 319 of file ArraySet.h.
References Util::PArray< Data >::capacity_, Util::PArray< Data >::ptrs_, and Util::PArray< Data >::size_.
int Util::ArraySet< Data >::index | ( | const Data & | data | ) | const |
Return the current index of an element within the set, if any.
Return the current index of an element within the set, or return a negative value -1 if the element is not in the set.
This method returns the current index of the pointer to object data within this ArraySet, in the range 0 < index < size() -1. The method returns -1 if data is an element of the associated array but is not in the ArraySet.
Throws an exception if data is not in the associated array.
data | array element of interest. |
Definition at line 335 of file ArraySet.h.
References Util::PArray< Data >::capacity_, and UTIL_THROW.
|
inline |
Return true if the ArraySet is initialized, false otherwise.
Definition at line 348 of file ArraySet.h.
References Util::PArray< Data >::ptrs_.
bool Util::ArraySet< Data >::isValid | ( | ) | const |
Return true if the ArraySet is valid, or throw an exception.
Definition at line 355 of file ArraySet.h.
References Util::PArray< Data >::capacity_, Util::PArray< Data >::ptrs_, Util::PArray< Data >::size(), Util::PArray< Data >::size_, and UTIL_THROW.
void Util::ArraySet< Data >::dump | ( | ) | const |
Write the internal state of the ArraySet to std::cout.