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