Update contrib.
2 **********************************************************************
3 * Copyright (C) 1999-2004, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
6 * Date Name Description
7 * 10/22/99 alan Creation. This is an internal header.
8 * It should not be exported.
9 **********************************************************************
15 #include "unicode/utypes.h"
16 #include "unicode/uobject.h"
22 * A token comparison function.
23 * @param tok1 A token (object or integer)
24 * @param tok2 A token (object or integer)
25 * @return 0 if the two tokens are equal, -1 if tok1 is < tok2, or
26 * +1 if tok1 is > tok2.
28 typedef int8_t U_CALLCONV USortComparator(UHashTok tok1,
32 * A token assignment function. It may copy an integer, copy
33 * a pointer, or clone a pointer, as appropriate.
34 * @param dst The token to be assigned to
35 * @param src The token to assign from
37 typedef void U_CALLCONV UTokenAssigner(UHashTok *dst,
41 * <p>Ultralightweight C++ implementation of a <tt>void*</tt> vector
42 * that is (mostly) compatible with java.util.Vector.
44 * <p>This is a very simple implementation, written to satisfy an
45 * immediate porting need. As such, it is not completely fleshed out,
46 * and it aims for simplicity and conformity. Nonetheless, it serves
47 * its purpose (porting code from java that uses java.util.Vector)
48 * well, and it could be easily made into a more robust vector class.
50 * <p><b>Design notes</b>
52 * <p>There is index bounds checking, but little is done about it. If
53 * indices are out of bounds, either nothing happens, or zero is
54 * returned. We <em>do</em> avoid indexing off into the weeds.
56 * <p>There is detection of out of memory, but the handling is very
57 * coarse-grained -- similar to UnicodeString's protocol, but even
58 * coarser. The class contains <em>one static flag</em> that is set
59 * when any call to <tt>new</tt> returns zero. This allows the caller
60 * to use several vectors and make just one check at the end to see if
61 * a memory failure occurred. This is more efficient than making a
62 * check after each call on each vector when doing many operations on
63 * multiple vectors. The single static flag works best when memory
64 * failures are infrequent, and when recovery options are limited or
67 * <p>Since we don't have garbage collection, UVector was given the
68 * option to <em>own</em>its contents. To employ this, set a deleter
69 * function. The deleter is called on a void* pointer when that
70 * pointer is released by the vector, either when the vector itself is
71 * destructed, or when a call to setElementAt() overwrites an element,
72 * or when a call to remove() or one of its variants explicitly
73 * removes an element. If no deleter is set, or the deleter is set to
74 * zero, then it is assumed that the caller will delete elements as
77 * <p>In order to implement methods such as contains() and indexOf(),
78 * UVector needs a way to compare objects for equality. To do so, it
79 * uses a comparison frunction, or "comparer." If the comparer is not
80 * set, or is set to zero, then all such methods will act as if the
81 * vector contains no element. That is, indexOf() will always return
82 * -1, contains() will always return FALSE, etc.
86 * <p>Improve the handling of index out of bounds errors.
90 class U_COMMON_API UVector : public UObject {
91 // NOTE: UVector uses the UHashKey (union of void* and int32_t) as
92 // its basic storage type. It uses UKeyComparator as its
93 // comparison function. It uses UObjectDeleter as its deleter
94 // function. These are named for hashtables, but used here as-is
95 // rather than duplicating the type. This allows sharing of
105 UObjectDeleter *deleter;
107 UKeyComparator *comparer;
110 UVector(UErrorCode &status);
112 UVector(int32_t initialCapacity, UErrorCode &status);
114 UVector(UObjectDeleter *d, UKeyComparator *c, UErrorCode &status);
116 UVector(UObjectDeleter *d, UKeyComparator *c, int32_t initialCapacity, UErrorCode &status);
121 * Assign this object to another (make this a copy of 'other').
122 * Use the 'assign' function to assign each element.
124 void assign(const UVector& other, UTokenAssigner *assign, UErrorCode &ec);
127 * Compare this vector with another. They will be considered
128 * equal if they are of the same size and all elements are equal,
129 * as compared using this object's comparer.
131 UBool operator==(const UVector& other);
134 * Equivalent to !operator==()
136 inline UBool operator!=(const UVector& other);
138 //------------------------------------------------------------
139 // java.util.Vector API
140 //------------------------------------------------------------
142 void addElement(void* obj, UErrorCode &status);
144 void addElement(int32_t elem, UErrorCode &status);
146 void setElementAt(void* obj, int32_t index);
148 void setElementAt(int32_t elem, int32_t index);
150 void insertElementAt(void* obj, int32_t index, UErrorCode &status);
152 void insertElementAt(int32_t elem, int32_t index, UErrorCode &status);
154 void* elementAt(int32_t index) const;
156 int32_t elementAti(int32_t index) const;
158 UBool equals(const UVector &other) const;
160 void* firstElement(void) const;
162 void* lastElement(void) const;
164 int32_t lastElementi(void) const;
166 int32_t indexOf(void* obj, int32_t startIndex = 0) const;
168 int32_t indexOf(int32_t obj, int32_t startIndex = 0) const;
170 UBool contains(void* obj) const;
172 UBool contains(int32_t obj) const;
174 UBool containsAll(const UVector& other) const;
176 UBool removeAll(const UVector& other);
178 UBool retainAll(const UVector& other);
180 void removeElementAt(int32_t index);
182 UBool removeElement(void* obj);
184 void removeAllElements();
186 int32_t size(void) const;
188 UBool isEmpty(void) const;
190 UBool ensureCapacity(int32_t minimumCapacity, UErrorCode &status);
193 * Change the size of this vector as follows: If newSize is
194 * smaller, then truncate the array, possibly deleting held
195 * elements for i >= newSize. If newSize is larger, grow the
196 * array, filling in new slows with NULL.
198 void setSize(int32_t newSize);
201 * Fill in the given array with all elements of this vector.
203 void** toArray(void** result) const;
205 //------------------------------------------------------------
207 //------------------------------------------------------------
209 UObjectDeleter *setDeleter(UObjectDeleter *d);
211 UKeyComparator *setComparer(UKeyComparator *c);
213 void* operator[](int32_t index) const;
216 * Removes the element at the given index from this vector and
217 * transfer ownership of it to the caller. After this call, the
218 * caller owns the result and must delete it and the vector entry
219 * at 'index' is removed, shifting all subsequent entries back by
220 * one index and shortening the size of the vector by one. If the
221 * index is out of range or if there is no item at the given index
222 * then 0 is returned and the vector is unchanged.
224 void* orphanElementAt(int32_t index);
227 * Returns true if this vector contains none of the elements
228 * of the given vector.
229 * @param other vector to be checked for containment
230 * @return true if the test condition is met
232 UBool containsNone(const UVector& other) const;
235 * Insert the given object into this vector at its sorted position
236 * as defined by 'compare'. The current elements are assumed to
239 void sortedInsert(void* obj, USortComparator *compare, UErrorCode& ec);
242 * Insert the given integer into this vector at its sorted position
243 * as defined by 'compare'. The current elements are assumed to
246 void sortedInsert(int32_t obj, USortComparator *compare, UErrorCode& ec);
249 * ICU "poor man's RTTI", returns a UClassID for this class.
253 static UClassID U_EXPORT2 getStaticClassID();
256 * ICU "poor man's RTTI", returns a UClassID for the actual class.
260 virtual UClassID getDynamicClassID() const;
263 void _init(int32_t initialCapacity, UErrorCode &status);
265 int32_t indexOf(UHashTok key, int32_t startIndex = 0, int8_t hint = 0) const;
267 void sortedInsert(UHashTok tok, USortComparator *compare, UErrorCode& ec);
270 UVector(const UVector&);
273 UVector& operator=(const UVector&);
279 * <p>Ultralightweight C++ implementation of a <tt>void*</tt> stack
280 * that is (mostly) compatible with java.util.Stack. As in java, this
281 * is merely a paper thin layer around UVector. See the UVector
282 * documentation for further information.
284 * <p><b>Design notes</b>
286 * <p>The element at index <tt>n-1</tt> is (of course) the top of the
289 * <p>The poorly named <tt>empty()</tt> method doesn't empty the
290 * stack; it determines if the stack is empty.
294 class U_COMMON_API UStack : public UVector {
296 UStack(UErrorCode &status);
298 UStack(int32_t initialCapacity, UErrorCode &status);
300 UStack(UObjectDeleter *d, UKeyComparator *c, UErrorCode &status);
302 UStack(UObjectDeleter *d, UKeyComparator *c, int32_t initialCapacity, UErrorCode &status);
306 // It's okay not to have a virtual destructor (in UVector)
307 // because UStack has no special cleanup to do.
309 UBool empty(void) const;
311 void* peek(void) const;
313 int32_t peeki(void) const;
319 void* push(void* obj, UErrorCode &status);
321 int32_t push(int32_t i, UErrorCode &status);
324 If the object o occurs as an item in this stack,
325 this method returns the 1-based distance from the top of the stack.
327 int32_t search(void* obj) const;
330 * ICU "poor man's RTTI", returns a UClassID for this class.
334 static UClassID U_EXPORT2 getStaticClassID();
337 * ICU "poor man's RTTI", returns a UClassID for the actual class.
341 virtual UClassID getDynamicClassID() const;
345 UStack(const UStack&);
348 UStack& operator=(const UStack&);
354 inline int32_t UVector::size(void) const {
358 inline UBool UVector::isEmpty(void) const {
362 inline UBool UVector::contains(void* obj) const {
363 return indexOf(obj) >= 0;
366 inline UBool UVector::contains(int32_t obj) const {
367 return indexOf(obj) >= 0;
370 inline void* UVector::firstElement(void) const {
374 inline void* UVector::lastElement(void) const {
375 return elementAt(count-1);
378 inline int32_t UVector::lastElementi(void) const {
379 return elementAti(count-1);
382 inline void* UVector::operator[](int32_t index) const {
383 return elementAt(index);
386 inline UBool UVector::operator!=(const UVector& other) {
387 return !operator==(other);
392 inline UBool UStack::empty(void) const {
396 inline void* UStack::peek(void) const {
397 return lastElement();
400 inline int32_t UStack::peeki(void) const {
401 return lastElementi();
404 inline void* UStack::push(void* obj, UErrorCode &status) {
405 addElement(obj, status);
409 inline int32_t UStack::push(int32_t i, UErrorCode &status) {
410 addElement(i, status);