2
**********************************************************************
3
* Copyright (C) 1999-2001, International Business Machines
4
* Corporation and others. All Rights Reserved.
5
**********************************************************************
6
* Date Name Description
7
* 10/22/99 alan Creation. This is an internal header.
8
* It should not be exported.
9
**********************************************************************
15
#include "unicode/utypes.h"
21
* <p>Ultralightweight C++ implementation of a <tt>void*</tt> vector
22
* that is (mostly) compatible with java.util.Vector.
24
* <p>This is a very simple implementation, written to satisfy an
25
* immediate porting need. As such, it is not completely fleshed out,
26
* and it aims for simplicity and conformity. Nonetheless, it serves
27
* its purpose (porting code from java that uses java.util.Vector)
28
* well, and it could be easily made into a more robust vector class.
30
* <p><b>Design notes</b>
32
* <p>There is index bounds checking, but little is done about it. If
33
* indices are out of bounds, either nothing happens, or zero is
34
* returned. We <em>do</em> avoid indexing off into the weeds.
36
* <p>There is detection of out of memory, but the handling is very
37
* coarse-grained -- similar to UnicodeString's protocol, but even
38
* coarser. The class contains <em>one static flag</em> that is set
39
* when any call to <tt>new</tt> returns zero. This allows the caller
40
* to use several vectors and make just one check at the end to see if
41
* a memory failure occurred. This is more efficient than making a
42
* check after each call on each vector when doing many operations on
43
* multiple vectors. The single static flag works best when memory
44
* failures are infrequent, and when recovery options are limited or
47
* <p>Since we don't have garbage collection, UVector was given the
48
* option to <em>own</em>its contents. To employ this, set a deleter
49
* function. The deleter is called on a void* pointer when that
50
* pointer is released by the vector, either when the vector itself is
51
* destructed, or when a call to setElementAt() overwrites an element,
52
* or when a call to remove() or one of its variants explicitly
53
* removes an element. If no deleter is set, or the deleter is set to
54
* zero, then it is assumed that the caller will delete elements as
57
* <p>In order to implement methods such as contains() and indexOf(),
58
* UVector needs a way to compare objects for equality. To do so, it
59
* uses a comparison frunction, or "comparer." If the comparer is not
60
* set, or is set to zero, then all such methods will act as if the
61
* vector contains no element. That is, indexOf() will always return
62
* -1, contains() will always return FALSE, etc.
66
* <p>Improve the handling of index out of bounds errors.
70
class U_COMMON_API UVector {
71
// NOTE: UVector uses the UHashKey (union of void* and int32_t) as
72
// its basic storage type. It uses UKeyComparator as its
73
// comparison function. It uses UObjectDeleter as its deleter
74
// function. These are named for hashtables, but used here as-is
75
// rather than duplicating the type. This allows sharing of
85
UObjectDeleter deleter;
87
UKeyComparator comparer;
90
UVector(UErrorCode &status);
92
UVector(int32_t initialCapacity, UErrorCode &status);
94
UVector(UObjectDeleter d, UKeyComparator c, UErrorCode &status);
96
UVector(UObjectDeleter d, UKeyComparator c, int32_t initialCapacity, UErrorCode &status);
99
//------------------------------------------------------------
100
// java.util.Vector API
101
//------------------------------------------------------------
103
void addElement(void* obj, UErrorCode &status);
105
void addElement(int32_t elem, UErrorCode &status);
107
void setElementAt(void* obj, int32_t index);
109
void setElementAt(int32_t elem, int32_t index);
111
void insertElementAt(void* obj, int32_t index, UErrorCode &status);
113
void* elementAt(int32_t index) const;
115
int32_t elementAti(int32_t index) const;
117
void* firstElement(void) const;
119
void* lastElement(void) const;
121
int32_t indexOf(void* obj, int32_t startIndex = 0) const;
123
UBool contains(void* obj) const;
125
void removeElementAt(int32_t index);
127
UBool removeElement(void* obj);
129
void removeAllElements();
131
int32_t size(void) const;
133
UBool isEmpty(void) const;
135
UBool ensureCapacity(int32_t minimumCapacity, UErrorCode &status);
138
* Change the size of this vector as follows: If newSize is
139
* smaller, then truncate the array, possibly deleting held
140
* elements for i >= newSize. If newSize is larger, grow the
141
* array, filling in new slows with NULL.
143
void setSize(int32_t newSize);
146
* Fill in the given array with all elements of this vector.
148
void** toArray(void** result) const;
150
//------------------------------------------------------------
152
//------------------------------------------------------------
154
UObjectDeleter setDeleter(UObjectDeleter d);
156
UKeyComparator setComparer(UKeyComparator c);
158
void* operator[](int32_t index) const;
161
* Removes the element at the given index from this vector and
162
* transfer ownership of it to the caller. After this call, the
163
* caller owns the result and must delete it and the vector entry
164
* at 'index' is removed, shifting all subsequent entries back by
165
* one index and shortening the size of the vector by one. If the
166
* index is out of range or if there is no item at the given index
167
* then 0 is returned and the vector is unchanged.
169
void* orphanElementAt(int32_t index);
172
void _init(int32_t initialCapacity, UErrorCode &status);
175
UVector(const UVector&);
178
UVector& operator=(const UVector&);
183
* <p>Ultralightweight C++ implementation of a <tt>void*</tt> stack
184
* that is (mostly) compatible with java.util.Stack. As in java, this
185
* is merely a paper thin layer around UVector. See the UVector
186
* documentation for further information.
188
* <p><b>Design notes</b>
190
* <p>The element at index <tt>n-1</tt> is (of course) the top of the
193
* <p>The poorly named <tt>empty()</tt> method doesn't empty the
194
* stack; it determines if the stack is empty.
198
class U_COMMON_API UStack : public UVector {
200
UStack(UErrorCode &status);
202
UStack(int32_t initialCapacity, UErrorCode &status);
204
UStack(UObjectDeleter d, UKeyComparator c, UErrorCode &status);
206
UStack(UObjectDeleter d, UKeyComparator c, int32_t initialCapacity, UErrorCode &status);
208
// It's okay not to have a virtual destructor (in UVector)
209
// because UStack has no special cleanup to do.
211
UBool empty(void) const;
213
void* peek(void) const;
219
void* push(void* obj, UErrorCode &status);
221
int32_t push(int32_t i, UErrorCode &status);
223
int32_t search(void* obj) const;
227
UStack(const UStack&);
230
UStack& operator=(const UStack&);
236
inline int32_t UVector::size(void) const {
240
inline UBool UVector::isEmpty(void) const {
244
inline UBool UVector::contains(void* obj) const {
245
return indexOf(obj) >= 0;
248
inline void* UVector::firstElement(void) const {
252
inline void* UVector::lastElement(void) const {
253
return elementAt(count-1);
256
inline void* UVector::operator[](int32_t index) const {
257
return elementAt(index);
262
inline UBool UStack::empty(void) const {
266
inline void* UStack::peek(void) const {
267
return lastElement();
270
inline void* UStack::push(void* obj, UErrorCode &status) {
271
addElement(obj, status);
275
inline int32_t UStack::push(int32_t i, UErrorCode &status) {
276
addElement(i, status);