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