intl/icu/source/common/uvectr32.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /*
michael@0 2 **********************************************************************
michael@0 3 * Copyright (C) 1999-2011, International Business Machines
michael@0 4 * Corporation and others. All Rights Reserved.
michael@0 5 **********************************************************************
michael@0 6 */
michael@0 7
michael@0 8 //
michael@0 9 // UVector32 is a class implementing a vector of 32 bit integers.
michael@0 10 // It is similar to UVector, but holds int32_t values rather than pointers.
michael@0 11 // Most of the code is unchanged from UVector.
michael@0 12 //
michael@0 13
michael@0 14 #ifndef UVECTOR32_H
michael@0 15 #define UVECTOR32_H
michael@0 16
michael@0 17 #include "unicode/utypes.h"
michael@0 18 #include "unicode/uobject.h"
michael@0 19 #include "uhash.h"
michael@0 20 #include "uassert.h"
michael@0 21
michael@0 22 U_NAMESPACE_BEGIN
michael@0 23
michael@0 24
michael@0 25
michael@0 26 /**
michael@0 27 * <p>Ultralightweight C++ implementation of a <tt>void*</tt> vector
michael@0 28 * that is (mostly) compatible with java.util.Vector.
michael@0 29 *
michael@0 30 * <p>This is a very simple implementation, written to satisfy an
michael@0 31 * immediate porting need. As such, it is not completely fleshed out,
michael@0 32 * and it aims for simplicity and conformity. Nonetheless, it serves
michael@0 33 * its purpose (porting code from java that uses java.util.Vector)
michael@0 34 * well, and it could be easily made into a more robust vector class.
michael@0 35 *
michael@0 36 * <p><b>Design notes</b>
michael@0 37 *
michael@0 38 * <p>There is index bounds checking, but little is done about it. If
michael@0 39 * indices are out of bounds, either nothing happens, or zero is
michael@0 40 * returned. We <em>do</em> avoid indexing off into the weeds.
michael@0 41 *
michael@0 42 * <p>There is detection of out of memory, but the handling is very
michael@0 43 * coarse-grained -- similar to UnicodeString's protocol, but even
michael@0 44 * coarser. The class contains <em>one static flag</em> that is set
michael@0 45 * when any call to <tt>new</tt> returns zero. This allows the caller
michael@0 46 * to use several vectors and make just one check at the end to see if
michael@0 47 * a memory failure occurred. This is more efficient than making a
michael@0 48 * check after each call on each vector when doing many operations on
michael@0 49 * multiple vectors. The single static flag works best when memory
michael@0 50 * failures are infrequent, and when recovery options are limited or
michael@0 51 * nonexistent.
michael@0 52 *
michael@0 53 * <p><b>To do</b>
michael@0 54 *
michael@0 55 * <p>Improve the handling of index out of bounds errors.
michael@0 56 *
michael@0 57 * @author Alan Liu
michael@0 58 */
michael@0 59 class U_COMMON_API UVector32 : public UObject {
michael@0 60 private:
michael@0 61 int32_t count;
michael@0 62
michael@0 63 int32_t capacity;
michael@0 64
michael@0 65 int32_t maxCapacity; // Limit beyond which capacity is not permitted to grow.
michael@0 66
michael@0 67 int32_t* elements;
michael@0 68
michael@0 69 public:
michael@0 70 UVector32(UErrorCode &status);
michael@0 71
michael@0 72 UVector32(int32_t initialCapacity, UErrorCode &status);
michael@0 73
michael@0 74 virtual ~UVector32();
michael@0 75
michael@0 76 /**
michael@0 77 * Assign this object to another (make this a copy of 'other').
michael@0 78 * Use the 'assign' function to assign each element.
michael@0 79 */
michael@0 80 void assign(const UVector32& other, UErrorCode &ec);
michael@0 81
michael@0 82 /**
michael@0 83 * Compare this vector with another. They will be considered
michael@0 84 * equal if they are of the same size and all elements are equal,
michael@0 85 * as compared using this object's comparer.
michael@0 86 */
michael@0 87 UBool operator==(const UVector32& other);
michael@0 88
michael@0 89 /**
michael@0 90 * Equivalent to !operator==()
michael@0 91 */
michael@0 92 inline UBool operator!=(const UVector32& other);
michael@0 93
michael@0 94 //------------------------------------------------------------
michael@0 95 // java.util.Vector API
michael@0 96 //------------------------------------------------------------
michael@0 97
michael@0 98 void addElement(int32_t elem, UErrorCode &status);
michael@0 99
michael@0 100 void setElementAt(int32_t elem, int32_t index);
michael@0 101
michael@0 102 void insertElementAt(int32_t elem, int32_t index, UErrorCode &status);
michael@0 103
michael@0 104 int32_t elementAti(int32_t index) const;
michael@0 105
michael@0 106 UBool equals(const UVector32 &other) const;
michael@0 107
michael@0 108 int32_t lastElementi(void) const;
michael@0 109
michael@0 110 int32_t indexOf(int32_t elem, int32_t startIndex = 0) const;
michael@0 111
michael@0 112 UBool contains(int32_t elem) const;
michael@0 113
michael@0 114 UBool containsAll(const UVector32& other) const;
michael@0 115
michael@0 116 UBool removeAll(const UVector32& other);
michael@0 117
michael@0 118 UBool retainAll(const UVector32& other);
michael@0 119
michael@0 120 void removeElementAt(int32_t index);
michael@0 121
michael@0 122 void removeAllElements();
michael@0 123
michael@0 124 int32_t size(void) const;
michael@0 125
michael@0 126 UBool isEmpty(void) const;
michael@0 127
michael@0 128 // Inline. Use this one for speedy size check.
michael@0 129 inline UBool ensureCapacity(int32_t minimumCapacity, UErrorCode &status);
michael@0 130
michael@0 131 // Out-of-line, handles actual growth. Called by ensureCapacity() when necessary.
michael@0 132 UBool expandCapacity(int32_t minimumCapacity, UErrorCode &status);
michael@0 133
michael@0 134 /**
michael@0 135 * Change the size of this vector as follows: If newSize is
michael@0 136 * smaller, then truncate the array, possibly deleting held
michael@0 137 * elements for i >= newSize. If newSize is larger, grow the
michael@0 138 * array, filling in new slows with zero.
michael@0 139 */
michael@0 140 void setSize(int32_t newSize);
michael@0 141
michael@0 142 //------------------------------------------------------------
michael@0 143 // New API
michael@0 144 //------------------------------------------------------------
michael@0 145
michael@0 146 /**
michael@0 147 * Returns true if this vector contains none of the elements
michael@0 148 * of the given vector.
michael@0 149 * @param other vector to be checked for containment
michael@0 150 * @return true if the test condition is met
michael@0 151 */
michael@0 152 UBool containsNone(const UVector32& other) const;
michael@0 153
michael@0 154
michael@0 155 /**
michael@0 156 * Insert the given integer into this vector at its sorted position.
michael@0 157 * The current elements are assumed to be sorted already.
michael@0 158 */
michael@0 159 void sortedInsert(int32_t elem, UErrorCode& ec);
michael@0 160
michael@0 161 /**
michael@0 162 * Returns a pointer to the internal array holding the vector.
michael@0 163 */
michael@0 164 int32_t *getBuffer() const;
michael@0 165
michael@0 166 /**
michael@0 167 * Set the maximum allowed buffer capacity for this vector/stack.
michael@0 168 * Default with no limit set is unlimited, go until malloc() fails.
michael@0 169 * A Limit of zero means unlimited capacity.
michael@0 170 * Units are vector elements (32 bits each), not bytes.
michael@0 171 */
michael@0 172 void setMaxCapacity(int32_t limit);
michael@0 173
michael@0 174 /**
michael@0 175 * ICU "poor man's RTTI", returns a UClassID for this class.
michael@0 176 */
michael@0 177 static UClassID U_EXPORT2 getStaticClassID();
michael@0 178
michael@0 179 /**
michael@0 180 * ICU "poor man's RTTI", returns a UClassID for the actual class.
michael@0 181 */
michael@0 182 virtual UClassID getDynamicClassID() const;
michael@0 183
michael@0 184 private:
michael@0 185 void _init(int32_t initialCapacity, UErrorCode &status);
michael@0 186
michael@0 187 // Disallow
michael@0 188 UVector32(const UVector32&);
michael@0 189
michael@0 190 // Disallow
michael@0 191 UVector32& operator=(const UVector32&);
michael@0 192
michael@0 193
michael@0 194 // API Functions for Stack operations.
michael@0 195 // In the original UVector, these were in a separate derived class, UStack.
michael@0 196 // Here in UVector32, they are all together.
michael@0 197 public:
michael@0 198 UBool empty(void) const; // TODO: redundant, same as empty(). Remove it?
michael@0 199
michael@0 200 int32_t peeki(void) const;
michael@0 201
michael@0 202 int32_t popi(void);
michael@0 203
michael@0 204 int32_t push(int32_t i, UErrorCode &status);
michael@0 205
michael@0 206 int32_t *reserveBlock(int32_t size, UErrorCode &status);
michael@0 207 int32_t *popFrame(int32_t size);
michael@0 208 };
michael@0 209
michael@0 210
michael@0 211 // UVector32 inlines
michael@0 212
michael@0 213 inline UBool UVector32::ensureCapacity(int32_t minimumCapacity, UErrorCode &status) {
michael@0 214 if ((minimumCapacity >= 0) && (capacity >= minimumCapacity)) {
michael@0 215 return TRUE;
michael@0 216 } else {
michael@0 217 return expandCapacity(minimumCapacity, status);
michael@0 218 }
michael@0 219 }
michael@0 220
michael@0 221 inline int32_t UVector32::elementAti(int32_t index) const {
michael@0 222 return (index >= 0 && count > 0 && count - index > 0) ? elements[index] : 0;
michael@0 223 }
michael@0 224
michael@0 225
michael@0 226 inline void UVector32::addElement(int32_t elem, UErrorCode &status) {
michael@0 227 if (ensureCapacity(count + 1, status)) {
michael@0 228 elements[count] = elem;
michael@0 229 count++;
michael@0 230 }
michael@0 231 }
michael@0 232
michael@0 233 inline int32_t *UVector32::reserveBlock(int32_t size, UErrorCode &status) {
michael@0 234 if (ensureCapacity(count+size, status) == FALSE) {
michael@0 235 return NULL;
michael@0 236 }
michael@0 237 int32_t *rp = elements+count;
michael@0 238 count += size;
michael@0 239 return rp;
michael@0 240 }
michael@0 241
michael@0 242 inline int32_t *UVector32::popFrame(int32_t size) {
michael@0 243 U_ASSERT(count >= size);
michael@0 244 count -= size;
michael@0 245 if (count < 0) {
michael@0 246 count = 0;
michael@0 247 }
michael@0 248 return elements+count-size;
michael@0 249 }
michael@0 250
michael@0 251
michael@0 252
michael@0 253 inline int32_t UVector32::size(void) const {
michael@0 254 return count;
michael@0 255 }
michael@0 256
michael@0 257 inline UBool UVector32::isEmpty(void) const {
michael@0 258 return count == 0;
michael@0 259 }
michael@0 260
michael@0 261 inline UBool UVector32::contains(int32_t obj) const {
michael@0 262 return indexOf(obj) >= 0;
michael@0 263 }
michael@0 264
michael@0 265 inline int32_t UVector32::lastElementi(void) const {
michael@0 266 return elementAti(count-1);
michael@0 267 }
michael@0 268
michael@0 269 inline UBool UVector32::operator!=(const UVector32& other) {
michael@0 270 return !operator==(other);
michael@0 271 }
michael@0 272
michael@0 273 inline int32_t *UVector32::getBuffer() const {
michael@0 274 return elements;
michael@0 275 }
michael@0 276
michael@0 277
michael@0 278 // UStack inlines
michael@0 279
michael@0 280 inline UBool UVector32::empty(void) const {
michael@0 281 return isEmpty();
michael@0 282 }
michael@0 283
michael@0 284 inline int32_t UVector32::peeki(void) const {
michael@0 285 return lastElementi();
michael@0 286 }
michael@0 287
michael@0 288 inline int32_t UVector32::push(int32_t i, UErrorCode &status) {
michael@0 289 addElement(i, status);
michael@0 290 return i;
michael@0 291 }
michael@0 292
michael@0 293 inline int32_t UVector32::popi(void) {
michael@0 294 int32_t result = 0;
michael@0 295 if (count > 0) {
michael@0 296 count--;
michael@0 297 result = elements[count];
michael@0 298 }
michael@0 299 return result;
michael@0 300 }
michael@0 301
michael@0 302 U_NAMESPACE_END
michael@0 303
michael@0 304 #endif

mercurial