Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* |
michael@0 | 2 | ********************************************************************** |
michael@0 | 3 | * Copyright (C) 1999-2013, International Business Machines |
michael@0 | 4 | * Corporation and others. All Rights Reserved. |
michael@0 | 5 | ********************************************************************** |
michael@0 | 6 | * Date Name Description |
michael@0 | 7 | * 10/22/99 alan Creation. This is an internal header. |
michael@0 | 8 | * It should not be exported. |
michael@0 | 9 | ********************************************************************** |
michael@0 | 10 | */ |
michael@0 | 11 | |
michael@0 | 12 | #ifndef UVECTOR_H |
michael@0 | 13 | #define UVECTOR_H |
michael@0 | 14 | |
michael@0 | 15 | #include "unicode/utypes.h" |
michael@0 | 16 | #include "unicode/uobject.h" |
michael@0 | 17 | #include "cmemory.h" |
michael@0 | 18 | #include "uarrsort.h" |
michael@0 | 19 | #include "uelement.h" |
michael@0 | 20 | |
michael@0 | 21 | U_NAMESPACE_BEGIN |
michael@0 | 22 | |
michael@0 | 23 | /** |
michael@0 | 24 | * <p>Ultralightweight C++ implementation of a <tt>void*</tt> vector |
michael@0 | 25 | * that is (mostly) compatible with java.util.Vector. |
michael@0 | 26 | * |
michael@0 | 27 | * <p>This is a very simple implementation, written to satisfy an |
michael@0 | 28 | * immediate porting need. As such, it is not completely fleshed out, |
michael@0 | 29 | * and it aims for simplicity and conformity. Nonetheless, it serves |
michael@0 | 30 | * its purpose (porting code from java that uses java.util.Vector) |
michael@0 | 31 | * well, and it could be easily made into a more robust vector class. |
michael@0 | 32 | * |
michael@0 | 33 | * <p><b>Design notes</b> |
michael@0 | 34 | * |
michael@0 | 35 | * <p>There is index bounds checking, but little is done about it. If |
michael@0 | 36 | * indices are out of bounds, either nothing happens, or zero is |
michael@0 | 37 | * returned. We <em>do</em> avoid indexing off into the weeds. |
michael@0 | 38 | * |
michael@0 | 39 | * <p>There is detection of out of memory, but the handling is very |
michael@0 | 40 | * coarse-grained -- similar to UnicodeString's protocol, but even |
michael@0 | 41 | * coarser. The class contains <em>one static flag</em> that is set |
michael@0 | 42 | * when any call to <tt>new</tt> returns zero. This allows the caller |
michael@0 | 43 | * to use several vectors and make just one check at the end to see if |
michael@0 | 44 | * a memory failure occurred. This is more efficient than making a |
michael@0 | 45 | * check after each call on each vector when doing many operations on |
michael@0 | 46 | * multiple vectors. The single static flag works best when memory |
michael@0 | 47 | * failures are infrequent, and when recovery options are limited or |
michael@0 | 48 | * nonexistent. |
michael@0 | 49 | * |
michael@0 | 50 | * <p>Since we don't have garbage collection, UVector was given the |
michael@0 | 51 | * option to <em>own</em>its contents. To employ this, set a deleter |
michael@0 | 52 | * function. The deleter is called on a void* pointer when that |
michael@0 | 53 | * pointer is released by the vector, either when the vector itself is |
michael@0 | 54 | * destructed, or when a call to setElementAt() overwrites an element, |
michael@0 | 55 | * or when a call to remove() or one of its variants explicitly |
michael@0 | 56 | * removes an element. If no deleter is set, or the deleter is set to |
michael@0 | 57 | * zero, then it is assumed that the caller will delete elements as |
michael@0 | 58 | * needed. |
michael@0 | 59 | * |
michael@0 | 60 | * <p>In order to implement methods such as contains() and indexOf(), |
michael@0 | 61 | * UVector needs a way to compare objects for equality. To do so, it |
michael@0 | 62 | * uses a comparison frunction, or "comparer." If the comparer is not |
michael@0 | 63 | * set, or is set to zero, then all such methods will act as if the |
michael@0 | 64 | * vector contains no element. That is, indexOf() will always return |
michael@0 | 65 | * -1, contains() will always return FALSE, etc. |
michael@0 | 66 | * |
michael@0 | 67 | * <p><b>To do</b> |
michael@0 | 68 | * |
michael@0 | 69 | * <p>Improve the handling of index out of bounds errors. |
michael@0 | 70 | * |
michael@0 | 71 | * @author Alan Liu |
michael@0 | 72 | */ |
michael@0 | 73 | class U_COMMON_API UVector : public UObject { |
michael@0 | 74 | // NOTE: UVector uses the UHashKey (union of void* and int32_t) as |
michael@0 | 75 | // its basic storage type. It uses UElementsAreEqual as its |
michael@0 | 76 | // comparison function. It uses UObjectDeleter as its deleter |
michael@0 | 77 | // function. These are named for hashtables, but used here as-is |
michael@0 | 78 | // rather than duplicating the type. This allows sharing of |
michael@0 | 79 | // support functions. |
michael@0 | 80 | |
michael@0 | 81 | private: |
michael@0 | 82 | int32_t count; |
michael@0 | 83 | |
michael@0 | 84 | int32_t capacity; |
michael@0 | 85 | |
michael@0 | 86 | UElement* elements; |
michael@0 | 87 | |
michael@0 | 88 | UObjectDeleter *deleter; |
michael@0 | 89 | |
michael@0 | 90 | UElementsAreEqual *comparer; |
michael@0 | 91 | |
michael@0 | 92 | public: |
michael@0 | 93 | UVector(UErrorCode &status); |
michael@0 | 94 | |
michael@0 | 95 | UVector(int32_t initialCapacity, UErrorCode &status); |
michael@0 | 96 | |
michael@0 | 97 | UVector(UObjectDeleter *d, UElementsAreEqual *c, UErrorCode &status); |
michael@0 | 98 | |
michael@0 | 99 | UVector(UObjectDeleter *d, UElementsAreEqual *c, int32_t initialCapacity, UErrorCode &status); |
michael@0 | 100 | |
michael@0 | 101 | virtual ~UVector(); |
michael@0 | 102 | |
michael@0 | 103 | /** |
michael@0 | 104 | * Assign this object to another (make this a copy of 'other'). |
michael@0 | 105 | * Use the 'assign' function to assign each element. |
michael@0 | 106 | */ |
michael@0 | 107 | void assign(const UVector& other, UElementAssigner *assign, UErrorCode &ec); |
michael@0 | 108 | |
michael@0 | 109 | /** |
michael@0 | 110 | * Compare this vector with another. They will be considered |
michael@0 | 111 | * equal if they are of the same size and all elements are equal, |
michael@0 | 112 | * as compared using this object's comparer. |
michael@0 | 113 | */ |
michael@0 | 114 | UBool operator==(const UVector& other); |
michael@0 | 115 | |
michael@0 | 116 | /** |
michael@0 | 117 | * Equivalent to !operator==() |
michael@0 | 118 | */ |
michael@0 | 119 | inline UBool operator!=(const UVector& other); |
michael@0 | 120 | |
michael@0 | 121 | //------------------------------------------------------------ |
michael@0 | 122 | // java.util.Vector API |
michael@0 | 123 | //------------------------------------------------------------ |
michael@0 | 124 | |
michael@0 | 125 | void addElement(void* obj, UErrorCode &status); |
michael@0 | 126 | |
michael@0 | 127 | void addElement(int32_t elem, UErrorCode &status); |
michael@0 | 128 | |
michael@0 | 129 | void setElementAt(void* obj, int32_t index); |
michael@0 | 130 | |
michael@0 | 131 | void setElementAt(int32_t elem, int32_t index); |
michael@0 | 132 | |
michael@0 | 133 | void insertElementAt(void* obj, int32_t index, UErrorCode &status); |
michael@0 | 134 | |
michael@0 | 135 | void insertElementAt(int32_t elem, int32_t index, UErrorCode &status); |
michael@0 | 136 | |
michael@0 | 137 | void* elementAt(int32_t index) const; |
michael@0 | 138 | |
michael@0 | 139 | int32_t elementAti(int32_t index) const; |
michael@0 | 140 | |
michael@0 | 141 | UBool equals(const UVector &other) const; |
michael@0 | 142 | |
michael@0 | 143 | void* firstElement(void) const; |
michael@0 | 144 | |
michael@0 | 145 | void* lastElement(void) const; |
michael@0 | 146 | |
michael@0 | 147 | int32_t lastElementi(void) const; |
michael@0 | 148 | |
michael@0 | 149 | int32_t indexOf(void* obj, int32_t startIndex = 0) const; |
michael@0 | 150 | |
michael@0 | 151 | int32_t indexOf(int32_t obj, int32_t startIndex = 0) const; |
michael@0 | 152 | |
michael@0 | 153 | UBool contains(void* obj) const; |
michael@0 | 154 | |
michael@0 | 155 | UBool contains(int32_t obj) const; |
michael@0 | 156 | |
michael@0 | 157 | UBool containsAll(const UVector& other) const; |
michael@0 | 158 | |
michael@0 | 159 | UBool removeAll(const UVector& other); |
michael@0 | 160 | |
michael@0 | 161 | UBool retainAll(const UVector& other); |
michael@0 | 162 | |
michael@0 | 163 | void removeElementAt(int32_t index); |
michael@0 | 164 | |
michael@0 | 165 | UBool removeElement(void* obj); |
michael@0 | 166 | |
michael@0 | 167 | void removeAllElements(); |
michael@0 | 168 | |
michael@0 | 169 | int32_t size(void) const; |
michael@0 | 170 | |
michael@0 | 171 | UBool isEmpty(void) const; |
michael@0 | 172 | |
michael@0 | 173 | UBool ensureCapacity(int32_t minimumCapacity, UErrorCode &status); |
michael@0 | 174 | |
michael@0 | 175 | /** |
michael@0 | 176 | * Change the size of this vector as follows: If newSize is |
michael@0 | 177 | * smaller, then truncate the array, possibly deleting held |
michael@0 | 178 | * elements for i >= newSize. If newSize is larger, grow the |
michael@0 | 179 | * array, filling in new slots with NULL. |
michael@0 | 180 | */ |
michael@0 | 181 | void setSize(int32_t newSize, UErrorCode &status); |
michael@0 | 182 | |
michael@0 | 183 | /** |
michael@0 | 184 | * Fill in the given array with all elements of this vector. |
michael@0 | 185 | */ |
michael@0 | 186 | void** toArray(void** result) const; |
michael@0 | 187 | |
michael@0 | 188 | //------------------------------------------------------------ |
michael@0 | 189 | // New API |
michael@0 | 190 | //------------------------------------------------------------ |
michael@0 | 191 | |
michael@0 | 192 | UObjectDeleter *setDeleter(UObjectDeleter *d); |
michael@0 | 193 | |
michael@0 | 194 | UElementsAreEqual *setComparer(UElementsAreEqual *c); |
michael@0 | 195 | |
michael@0 | 196 | void* operator[](int32_t index) const; |
michael@0 | 197 | |
michael@0 | 198 | /** |
michael@0 | 199 | * Removes the element at the given index from this vector and |
michael@0 | 200 | * transfer ownership of it to the caller. After this call, the |
michael@0 | 201 | * caller owns the result and must delete it and the vector entry |
michael@0 | 202 | * at 'index' is removed, shifting all subsequent entries back by |
michael@0 | 203 | * one index and shortening the size of the vector by one. If the |
michael@0 | 204 | * index is out of range or if there is no item at the given index |
michael@0 | 205 | * then 0 is returned and the vector is unchanged. |
michael@0 | 206 | */ |
michael@0 | 207 | void* orphanElementAt(int32_t index); |
michael@0 | 208 | |
michael@0 | 209 | /** |
michael@0 | 210 | * Returns true if this vector contains none of the elements |
michael@0 | 211 | * of the given vector. |
michael@0 | 212 | * @param other vector to be checked for containment |
michael@0 | 213 | * @return true if the test condition is met |
michael@0 | 214 | */ |
michael@0 | 215 | UBool containsNone(const UVector& other) const; |
michael@0 | 216 | |
michael@0 | 217 | /** |
michael@0 | 218 | * Insert the given object into this vector at its sorted position |
michael@0 | 219 | * as defined by 'compare'. The current elements are assumed to |
michael@0 | 220 | * be sorted already. |
michael@0 | 221 | */ |
michael@0 | 222 | void sortedInsert(void* obj, UElementComparator *compare, UErrorCode& ec); |
michael@0 | 223 | |
michael@0 | 224 | /** |
michael@0 | 225 | * Insert the given integer into this vector at its sorted position |
michael@0 | 226 | * as defined by 'compare'. The current elements are assumed to |
michael@0 | 227 | * be sorted already. |
michael@0 | 228 | */ |
michael@0 | 229 | void sortedInsert(int32_t obj, UElementComparator *compare, UErrorCode& ec); |
michael@0 | 230 | |
michael@0 | 231 | /** |
michael@0 | 232 | * Sort the contents of the vector, assuming that the contents of the |
michael@0 | 233 | * vector are of type int32_t. |
michael@0 | 234 | */ |
michael@0 | 235 | void sorti(UErrorCode &ec); |
michael@0 | 236 | |
michael@0 | 237 | /** |
michael@0 | 238 | * Sort the contents of this vector, using a caller-supplied function |
michael@0 | 239 | * to do the comparisons. (It's confusing that |
michael@0 | 240 | * UVector's UElementComparator function is different from the |
michael@0 | 241 | * UComparator function type defined in uarrsort.h) |
michael@0 | 242 | */ |
michael@0 | 243 | void sort(UElementComparator *compare, UErrorCode &ec); |
michael@0 | 244 | |
michael@0 | 245 | /** |
michael@0 | 246 | * Stable sort the contents of this vector using a caller-supplied function |
michael@0 | 247 | * of type UComparator to do the comparison. Provides more flexibility |
michael@0 | 248 | * than UVector::sort() because an additional user parameter can be passed to |
michael@0 | 249 | * the comparison function. |
michael@0 | 250 | */ |
michael@0 | 251 | void sortWithUComparator(UComparator *compare, const void *context, UErrorCode &ec); |
michael@0 | 252 | |
michael@0 | 253 | /** |
michael@0 | 254 | * ICU "poor man's RTTI", returns a UClassID for this class. |
michael@0 | 255 | */ |
michael@0 | 256 | static UClassID U_EXPORT2 getStaticClassID(); |
michael@0 | 257 | |
michael@0 | 258 | /** |
michael@0 | 259 | * ICU "poor man's RTTI", returns a UClassID for the actual class. |
michael@0 | 260 | */ |
michael@0 | 261 | virtual UClassID getDynamicClassID() const; |
michael@0 | 262 | |
michael@0 | 263 | private: |
michael@0 | 264 | void _init(int32_t initialCapacity, UErrorCode &status); |
michael@0 | 265 | |
michael@0 | 266 | int32_t indexOf(UElement key, int32_t startIndex = 0, int8_t hint = 0) const; |
michael@0 | 267 | |
michael@0 | 268 | void sortedInsert(UElement e, UElementComparator *compare, UErrorCode& ec); |
michael@0 | 269 | |
michael@0 | 270 | // Disallow |
michael@0 | 271 | UVector(const UVector&); |
michael@0 | 272 | |
michael@0 | 273 | // Disallow |
michael@0 | 274 | UVector& operator=(const UVector&); |
michael@0 | 275 | |
michael@0 | 276 | }; |
michael@0 | 277 | |
michael@0 | 278 | |
michael@0 | 279 | /** |
michael@0 | 280 | * <p>Ultralightweight C++ implementation of a <tt>void*</tt> stack |
michael@0 | 281 | * that is (mostly) compatible with java.util.Stack. As in java, this |
michael@0 | 282 | * is merely a paper thin layer around UVector. See the UVector |
michael@0 | 283 | * documentation for further information. |
michael@0 | 284 | * |
michael@0 | 285 | * <p><b>Design notes</b> |
michael@0 | 286 | * |
michael@0 | 287 | * <p>The element at index <tt>n-1</tt> is (of course) the top of the |
michael@0 | 288 | * stack. |
michael@0 | 289 | * |
michael@0 | 290 | * <p>The poorly named <tt>empty()</tt> method doesn't empty the |
michael@0 | 291 | * stack; it determines if the stack is empty. |
michael@0 | 292 | * |
michael@0 | 293 | * @author Alan Liu |
michael@0 | 294 | */ |
michael@0 | 295 | class U_COMMON_API UStack : public UVector { |
michael@0 | 296 | public: |
michael@0 | 297 | UStack(UErrorCode &status); |
michael@0 | 298 | |
michael@0 | 299 | UStack(int32_t initialCapacity, UErrorCode &status); |
michael@0 | 300 | |
michael@0 | 301 | UStack(UObjectDeleter *d, UElementsAreEqual *c, UErrorCode &status); |
michael@0 | 302 | |
michael@0 | 303 | UStack(UObjectDeleter *d, UElementsAreEqual *c, int32_t initialCapacity, UErrorCode &status); |
michael@0 | 304 | |
michael@0 | 305 | virtual ~UStack(); |
michael@0 | 306 | |
michael@0 | 307 | // It's okay not to have a virtual destructor (in UVector) |
michael@0 | 308 | // because UStack has no special cleanup to do. |
michael@0 | 309 | |
michael@0 | 310 | UBool empty(void) const; |
michael@0 | 311 | |
michael@0 | 312 | void* peek(void) const; |
michael@0 | 313 | |
michael@0 | 314 | int32_t peeki(void) const; |
michael@0 | 315 | |
michael@0 | 316 | void* pop(void); |
michael@0 | 317 | |
michael@0 | 318 | int32_t popi(void); |
michael@0 | 319 | |
michael@0 | 320 | void* push(void* obj, UErrorCode &status); |
michael@0 | 321 | |
michael@0 | 322 | int32_t push(int32_t i, UErrorCode &status); |
michael@0 | 323 | |
michael@0 | 324 | /* |
michael@0 | 325 | If the object o occurs as an item in this stack, |
michael@0 | 326 | this method returns the 1-based distance from the top of the stack. |
michael@0 | 327 | */ |
michael@0 | 328 | int32_t search(void* obj) const; |
michael@0 | 329 | |
michael@0 | 330 | /** |
michael@0 | 331 | * ICU "poor man's RTTI", returns a UClassID for this class. |
michael@0 | 332 | */ |
michael@0 | 333 | static UClassID U_EXPORT2 getStaticClassID(); |
michael@0 | 334 | |
michael@0 | 335 | /** |
michael@0 | 336 | * ICU "poor man's RTTI", returns a UClassID for the actual class. |
michael@0 | 337 | */ |
michael@0 | 338 | virtual UClassID getDynamicClassID() const; |
michael@0 | 339 | |
michael@0 | 340 | private: |
michael@0 | 341 | // Disallow |
michael@0 | 342 | UStack(const UStack&); |
michael@0 | 343 | |
michael@0 | 344 | // Disallow |
michael@0 | 345 | UStack& operator=(const UStack&); |
michael@0 | 346 | }; |
michael@0 | 347 | |
michael@0 | 348 | |
michael@0 | 349 | // UVector inlines |
michael@0 | 350 | |
michael@0 | 351 | inline int32_t UVector::size(void) const { |
michael@0 | 352 | return count; |
michael@0 | 353 | } |
michael@0 | 354 | |
michael@0 | 355 | inline UBool UVector::isEmpty(void) const { |
michael@0 | 356 | return count == 0; |
michael@0 | 357 | } |
michael@0 | 358 | |
michael@0 | 359 | inline UBool UVector::contains(void* obj) const { |
michael@0 | 360 | return indexOf(obj) >= 0; |
michael@0 | 361 | } |
michael@0 | 362 | |
michael@0 | 363 | inline UBool UVector::contains(int32_t obj) const { |
michael@0 | 364 | return indexOf(obj) >= 0; |
michael@0 | 365 | } |
michael@0 | 366 | |
michael@0 | 367 | inline void* UVector::firstElement(void) const { |
michael@0 | 368 | return elementAt(0); |
michael@0 | 369 | } |
michael@0 | 370 | |
michael@0 | 371 | inline void* UVector::lastElement(void) const { |
michael@0 | 372 | return elementAt(count-1); |
michael@0 | 373 | } |
michael@0 | 374 | |
michael@0 | 375 | inline int32_t UVector::lastElementi(void) const { |
michael@0 | 376 | return elementAti(count-1); |
michael@0 | 377 | } |
michael@0 | 378 | |
michael@0 | 379 | inline void* UVector::operator[](int32_t index) const { |
michael@0 | 380 | return elementAt(index); |
michael@0 | 381 | } |
michael@0 | 382 | |
michael@0 | 383 | inline UBool UVector::operator!=(const UVector& other) { |
michael@0 | 384 | return !operator==(other); |
michael@0 | 385 | } |
michael@0 | 386 | |
michael@0 | 387 | // UStack inlines |
michael@0 | 388 | |
michael@0 | 389 | inline UBool UStack::empty(void) const { |
michael@0 | 390 | return isEmpty(); |
michael@0 | 391 | } |
michael@0 | 392 | |
michael@0 | 393 | inline void* UStack::peek(void) const { |
michael@0 | 394 | return lastElement(); |
michael@0 | 395 | } |
michael@0 | 396 | |
michael@0 | 397 | inline int32_t UStack::peeki(void) const { |
michael@0 | 398 | return lastElementi(); |
michael@0 | 399 | } |
michael@0 | 400 | |
michael@0 | 401 | inline void* UStack::push(void* obj, UErrorCode &status) { |
michael@0 | 402 | addElement(obj, status); |
michael@0 | 403 | return obj; |
michael@0 | 404 | } |
michael@0 | 405 | |
michael@0 | 406 | inline int32_t UStack::push(int32_t i, UErrorCode &status) { |
michael@0 | 407 | addElement(i, status); |
michael@0 | 408 | return i; |
michael@0 | 409 | } |
michael@0 | 410 | |
michael@0 | 411 | U_NAMESPACE_END |
michael@0 | 412 | |
michael@0 | 413 | #endif |