Logo Search packages:      
Sourcecode: icu version File versions  Download package

UVector Class Reference

#include <uvector.h>

Inheritance diagram for UVector:

UObject UMemory UStack

List of all members.

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.

Alan Liu

Definition at line 90 of file uvector.h.

Public Member Functions

void addElement (int32_t elem, UErrorCode &status)
void addElement (void *obj, UErrorCode &status)
void assign (const UVector &other, UTokenAssigner *assign, UErrorCode &ec)
UBool contains (int32_t obj) const
UBool contains (void *obj) const
UBool containsAll (const UVector &other) const
UBool containsNone (const UVector &other) const
void * elementAt (int32_t index) const
int32_t elementAti (int32_t index) const
UBool ensureCapacity (int32_t minimumCapacity, UErrorCode &status)
UBool equals (const UVector &other) const
void * firstElement (void) const
virtual UClassID getDynamicClassID () const
int32_t indexOf (int32_t obj, int32_t startIndex=0) const
int32_t indexOf (void *obj, int32_t startIndex=0) const
void insertElementAt (int32_t elem, int32_t index, UErrorCode &status)
void insertElementAt (void *obj, int32_t index, UErrorCode &status)
UBool isEmpty (void) const
void * lastElement (void) const
int32_t lastElementi (void) const
UBool operator!= (const UVector &other)
UBool operator== (const UVector &other)
void * operator[] (int32_t index) const
void * orphanElementAt (int32_t index)
UBool removeAll (const UVector &other)
void removeAllElements ()
UBool removeElement (void *obj)
void removeElementAt (int32_t index)
UBool retainAll (const UVector &other)
UKeyComparator * setComparer (UKeyComparator *c)
UObjectDeleter * setDeleter (UObjectDeleter *d)
void setElementAt (int32_t elem, int32_t index)
void setElementAt (void *obj, int32_t index)
void setSize (int32_t newSize, UErrorCode &status)
int32_t size (void) const
void sort (USortComparator *compare, UErrorCode &ec)
void sortedInsert (int32_t obj, USortComparator *compare, UErrorCode &ec)
void sortedInsert (void *obj, USortComparator *compare, UErrorCode &ec)
void sorti (UErrorCode &ec)
void ** toArray (void **result) const
 UVector (UObjectDeleter *d, UKeyComparator *c, int32_t initialCapacity, UErrorCode &status)
 UVector (UObjectDeleter *d, UKeyComparator *c, UErrorCode &status)
 UVector (int32_t initialCapacity, UErrorCode &status)
 UVector (UErrorCode &status)

Static Public Member Functions

static UClassID U_EXPORT2 getStaticClassID ()
static void U_EXPORT2 operator delete (void *, void *) U_NO_THROW
static void U_EXPORT2 operator delete (void *p) U_NO_THROW
static void U_EXPORT2 operator delete[] (void *p) U_NO_THROW
static void *U_EXPORT2 operator new (size_t, void *ptr) U_NO_THROW
static void *U_EXPORT2 operator new (size_t size) U_NO_THROW
static void *U_EXPORT2 operator new[] (size_t size) U_NO_THROW

Private Member Functions

void _init (int32_t initialCapacity, UErrorCode &status)
int32_t indexOf (UHashTok key, int32_t startIndex=0, int8_t hint=0) const
UVectoroperator= (const UVector &)
void sortedInsert (UHashTok tok, USortComparator *compare, UErrorCode &ec)
 UVector (const UVector &)

Private Attributes

int32_t capacity
UKeyComparator * comparer
int32_t count
UObjectDeleter * deleter

The documentation for this class was generated from the following files:

Generated by  Doxygen 1.6.0   Back to index