Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

UVector Class Reference

Ultralightweight C++ implementation of a void* vector that is (mostly) compatible with java.util.Vector. More...

#include <uvector.h>

Inheritance diagram for UVector:

UStack List of all members.

Public Types

typedef void (* Deleter )(void*)
typedef UBool (* Comparer )(void*, void*)

Public Methods

 UVector (int32_t initialCapacity = 8)
 UVector (Deleter d, Comparer c, int32_t initialCapacity = 8)
 ~UVector ()
void addElement (void* obj)
void setElementAt (void* obj, int32_t index)
void insertElementAt (void* obj, int32_t index)
void* elementAt (int32_t index) const
void* firstElement (void) const
void* lastElement (void) const
int32_t indexOf (void* obj, int32_t startIndex = 0) const
UBool contains (void* obj) const
void removeElementAt (int32_t index)
UBool removeElement (void* obj)
void removeAllElements ()
int32_t size (void) const
UBool isEmpty (void) const
UBool ensureCapacity (int32_t minimumCapacity)
Deleter setDeleter (Deleter d)
Comparer setComparer (Comparer c)
void* operator[] (int32_t index) const
void* orphanElementAt (int32_t index)
 Removes the element at the given index from this vector and transfer ownership of it to the caller. More...


Static Public Methods

UBool isOutOfMemory (void)

Detailed Description

Ultralightweight C++ implementation of a void* vector that is (mostly) compatible with java.util.Vector.

This is a very simple implementation, written to satisfy an immediate porting need. As such, it is not completely fleshed out, and it aims for simplicity and conformity. Nonetheless, it serves its purpose (porting code from java that uses java.util.Vector) well, and it could be easily made into a more robust vector class.

Design notes

There is index bounds checking, but little is done about it. If indices are out of bounds, either nothing happens, or zero is returned. We do avoid indexing off into the weeds.

There is detection of out of memory, but the handling is very coarse-grained -- similar to UnicodeString's protocol, but even coarser. The class contains one static flag that is set when any call to new returns zero. This allows the caller to use several vectors and make just one check at the end to see if a memory failure occurred. This is more efficient than making a check after each call on each vector when doing many operations on multiple vectors. The single static flag works best when memory failures are infrequent, and when recovery options are limited or nonexistent.

Since we don't have garbage collection, UVector was given the option to ownits contents. To employ this, set a deleter function. The deleter is called on a void* pointer when that pointer is released by the vector, either when the vector itself is destructed, or when a call to setElementAt() overwrites an element, or when a call to remove() or one of its variants explicitly removes an element. If no deleter is set, or the deleter is set to zero, then it is assumed that the caller will delete elements as needed.

In order to implement methods such as contains() and indexOf(), UVector needs a way to compare objects for equality. To do so, it uses a comparison frunction, or "comparer." If the comparer is not set, or is set to zero, then all such methods will act as if the vector contains no element. That is, indexOf() will always return -1, contains() will always return FALSE, etc.

To do

Improve the handling of index out of bounds errors.

Author(s):
Alan Liu

Definition at line 67 of file uvector.h.


Member Typedef Documentation

typedef UBool (* UVector::Comparer)(void*, void*)
 

typedef void (* UVector::Deleter)(void*)
 


Constructor & Destructor Documentation

UVector::UVector ( int32_t initialCapacity = 8 )
 

UVector::UVector ( Deleter d,
Comparer c,
int32_t initialCapacity = 8 )
 

UVector::~UVector ( )
 


Member Function Documentation

void UVector::addElement ( void * obj )
 

Referenced by UStack::push().

UBool UVector::contains ( void * obj ) const [inline]
 

Definition at line 212 of file uvector.h.

void* UVector::elementAt ( int32_t index ) const
 

Referenced by firstElement(), lastElement(), and operator[]().

UBool UVector::ensureCapacity ( int32_t minimumCapacity )
 

void * UVector::firstElement ( void ) const [inline]
 

Definition at line 216 of file uvector.h.

int32_t UVector::indexOf ( void * obj,
int32_t startIndex = 0 ) const
 

Referenced by contains().

void UVector::insertElementAt ( void * obj,
int32_t index )
 

UBool UVector::isEmpty ( void ) const [inline]
 

Definition at line 208 of file uvector.h.

Referenced by UStack::empty().

UBool UVector::isOutOfMemory ( void ) [static]
 

void * UVector::lastElement ( void ) const [inline]
 

Definition at line 220 of file uvector.h.

Referenced by UStack::peek().

void * UVector::operator[] ( int32_t index ) const [inline]
 

Definition at line 224 of file uvector.h.

void * UVector::orphanElementAt ( int32_t index )
 

Removes the element at the given index from this vector and transfer ownership of it to the caller.

After this call, the caller owns the result and must delete it and the vector entry at 'index' is removed, shifting all subsequent entries back by one index and shortening the size of the vector by one. If the index is out of range or if there is no item at the given index then 0 is returned and the vector is unchanged.

void UVector::removeAllElements ( )
 

UBool UVector::removeElement ( void * obj )
 

void UVector::removeElementAt ( int32_t index )
 

Comparer UVector::setComparer ( Comparer c )
 

Deleter UVector::setDeleter ( Deleter d )
 

void UVector::setElementAt ( void * obj,
int32_t index )
 

int32_t UVector::size ( void ) const [inline]
 

Definition at line 204 of file uvector.h.


The documentation for this class was generated from the following file:
Generated at Tue Dec 5 17:56:35 2000 for ICU by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000