2
**********************************************************************
3
* Copyright (C) 1999-2010, International Business Machines
4
* Corporation and others. All Rights Reserved.
5
**********************************************************************
9
// UVector32 is a class implementing a vector of 32 bit integers.
10
// It is similar to UVector, but holds int32_t values rather than pointers.
11
// Most of the code is unchanged from UVector.
17
#include "unicode/utypes.h"
18
#include "unicode/uobject.h"
27
* <p>Ultralightweight C++ implementation of a <tt>void*</tt> vector
28
* that is (mostly) compatible with java.util.Vector.
30
* <p>This is a very simple implementation, written to satisfy an
31
* immediate porting need. As such, it is not completely fleshed out,
32
* and it aims for simplicity and conformity. Nonetheless, it serves
33
* its purpose (porting code from java that uses java.util.Vector)
34
* well, and it could be easily made into a more robust vector class.
36
* <p><b>Design notes</b>
38
* <p>There is index bounds checking, but little is done about it. If
39
* indices are out of bounds, either nothing happens, or zero is
40
* returned. We <em>do</em> avoid indexing off into the weeds.
42
* <p>There is detection of out of memory, but the handling is very
43
* coarse-grained -- similar to UnicodeString's protocol, but even
44
* coarser. The class contains <em>one static flag</em> that is set
45
* when any call to <tt>new</tt> returns zero. This allows the caller
46
* to use several vectors and make just one check at the end to see if
47
* a memory failure occurred. This is more efficient than making a
48
* check after each call on each vector when doing many operations on
49
* multiple vectors. The single static flag works best when memory
50
* failures are infrequent, and when recovery options are limited or
55
* <p>Improve the handling of index out of bounds errors.
59
class U_COMMON_API UVector32 : public UObject {
65
int32_t maxCapacity; // Limit beyond which capacity is not permitted to grow.
70
UVector32(UErrorCode &status);
72
UVector32(int32_t initialCapacity, UErrorCode &status);
77
* Assign this object to another (make this a copy of 'other').
78
* Use the 'assign' function to assign each element.
80
void assign(const UVector32& other, UErrorCode &ec);
83
* Compare this vector with another. They will be considered
84
* equal if they are of the same size and all elements are equal,
85
* as compared using this object's comparer.
87
UBool operator==(const UVector32& other);
90
* Equivalent to !operator==()
92
inline UBool operator!=(const UVector32& other);
94
//------------------------------------------------------------
95
// java.util.Vector API
96
//------------------------------------------------------------
98
void addElement(int32_t elem, UErrorCode &status);
100
void setElementAt(int32_t elem, int32_t index);
102
void insertElementAt(int32_t elem, int32_t index, UErrorCode &status);
104
int32_t elementAti(int32_t index) const;
106
UBool equals(const UVector32 &other) const;
108
int32_t lastElementi(void) const;
110
int32_t indexOf(int32_t elem, int32_t startIndex = 0) const;
112
UBool contains(int32_t elem) const;
114
UBool containsAll(const UVector32& other) const;
116
UBool removeAll(const UVector32& other);
118
UBool retainAll(const UVector32& other);
120
void removeElementAt(int32_t index);
122
void removeAllElements();
124
int32_t size(void) const;
126
UBool isEmpty(void) const;
128
// Inline. Use this one for speedy size check.
129
inline UBool ensureCapacity(int32_t minimumCapacity, UErrorCode &status);
131
// Out-of-line, handles actual growth. Called by ensureCapacity() when necessary.
132
UBool expandCapacity(int32_t minimumCapacity, UErrorCode &status);
135
* Change the size of this vector as follows: If newSize is
136
* smaller, then truncate the array, possibly deleting held
137
* elements for i >= newSize. If newSize is larger, grow the
138
* array, filling in new slows with zero.
140
void setSize(int32_t newSize);
142
//------------------------------------------------------------
144
//------------------------------------------------------------
147
* Returns true if this vector contains none of the elements
148
* of the given vector.
149
* @param other vector to be checked for containment
150
* @return true if the test condition is met
152
UBool containsNone(const UVector32& other) const;
156
* Insert the given integer into this vector at its sorted position.
157
* The current elements are assumed to be sorted already.
159
void sortedInsert(int32_t elem, UErrorCode& ec);
162
* Returns a pointer to the internal array holding the vector.
164
int32_t *getBuffer() const;
167
* Set the maximum allowed buffer capacity for this vector/stack.
168
* Default with no limit set is unlimited, go until malloc() fails.
169
* A Limit of zero means unlimited capacity.
170
* Units are vector elements (32 bits each), not bytes.
172
void setMaxCapacity(int32_t limit);
175
* ICU "poor man's RTTI", returns a UClassID for this class.
177
static UClassID U_EXPORT2 getStaticClassID();
180
* ICU "poor man's RTTI", returns a UClassID for the actual class.
182
virtual UClassID getDynamicClassID() const;
185
void _init(int32_t initialCapacity, UErrorCode &status);
188
UVector32(const UVector32&);
191
UVector32& operator=(const UVector32&);
194
// API Functions for Stack operations.
195
// In the original UVector, these were in a separate derived class, UStack.
196
// Here in UVector32, they are all together.
198
UBool empty(void) const; // TODO: redundant, same as empty(). Remove it?
200
int32_t peeki(void) const;
204
int32_t push(int32_t i, UErrorCode &status);
206
int32_t *reserveBlock(int32_t size, UErrorCode &status);
207
int32_t *popFrame(int32_t size);
213
inline UBool UVector32::ensureCapacity(int32_t minimumCapacity, UErrorCode &status) {
214
if ((minimumCapacity >= 0) && (capacity >= minimumCapacity)) {
217
return expandCapacity(minimumCapacity, status);
221
inline int32_t UVector32::elementAti(int32_t index) const {
222
return (0 <= index && index < count) ? elements[index] : 0;
226
inline void UVector32::addElement(int32_t elem, UErrorCode &status) {
227
if (ensureCapacity(count + 1, status)) {
228
elements[count] = elem;
233
inline int32_t *UVector32::reserveBlock(int32_t size, UErrorCode &status) {
234
if (ensureCapacity(count+size, status) == FALSE) {
237
int32_t *rp = elements+count;
242
inline int32_t *UVector32::popFrame(int32_t size) {
243
U_ASSERT(count >= size);
248
return elements+count-size;
253
inline int32_t UVector32::size(void) const {
257
inline UBool UVector32::isEmpty(void) const {
261
inline UBool UVector32::contains(int32_t obj) const {
262
return indexOf(obj) >= 0;
265
inline int32_t UVector32::lastElementi(void) const {
266
return elementAti(count-1);
269
inline UBool UVector32::operator!=(const UVector32& other) {
270
return !operator==(other);
273
inline int32_t *UVector32::getBuffer() const {
280
inline UBool UVector32::empty(void) const {
284
inline int32_t UVector32::peeki(void) const {
285
return lastElementi();
288
inline int32_t UVector32::push(int32_t i, UErrorCode &status) {
289
addElement(i, status);
293
inline int32_t UVector32::popi(void) {
297
result = elements[count];