]>
git.saurik.com Git - apple/icu.git/blob - icuSources/common/uvector.h
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
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 **********************************************************************
17 #include "unicode/utypes.h"
18 #include "unicode/uobject.h"
26 * <p>Ultralightweight C++ implementation of a <tt>void*</tt> vector
27 * that is (mostly) compatible with java.util.Vector.
29 * <p>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.
35 * <p><b>Design notes</b>
37 * <p>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 <em>do</em> avoid indexing off into the weeds.
41 * <p>There is detection of out of memory, but the handling is very
42 * coarse-grained -- similar to UnicodeString's protocol, but even
43 * coarser. The class contains <em>one static flag</em> that is set
44 * when any call to <tt>new</tt> returns zero. This allows the caller
45 * to use several vectors and make just one check at the end to see if
46 * a memory failure occurred. This is more efficient than making a
47 * check after each call on each vector when doing many operations on
48 * multiple vectors. The single static flag works best when memory
49 * failures are infrequent, and when recovery options are limited or
52 * <p>Since we don't have garbage collection, UVector was given the
53 * option to <em>own</em>its contents. To employ this, set a deleter
54 * function. The deleter is called on a void* pointer when that
55 * pointer is released by the vector, either when the vector itself is
56 * destructed, or when a call to setElementAt() overwrites an element,
57 * or when a call to remove() or one of its variants explicitly
58 * removes an element. If no deleter is set, or the deleter is set to
59 * zero, then it is assumed that the caller will delete elements as
62 * <p>In order to implement methods such as contains() and indexOf(),
63 * UVector needs a way to compare objects for equality. To do so, it
64 * uses a comparison function, or "comparer." If the comparer is not
65 * set, or is set to zero, then all such methods will act as if the
66 * vector contains no element. That is, indexOf() will always return
67 * -1, contains() will always return FALSE, etc.
71 * <p>Improve the handling of index out of bounds errors.
75 class U_COMMON_API UVector
: public UObject
{
76 // NOTE: UVector uses the UHashKey (union of void* and int32_t) as
77 // its basic storage type. It uses UElementsAreEqual as its
78 // comparison function. It uses UObjectDeleter as its deleter
79 // function. These are named for hashtables, but used here as-is
80 // rather than duplicating the type. This allows sharing of
90 UObjectDeleter
*deleter
;
92 UElementsAreEqual
*comparer
;
95 UVector(UErrorCode
&status
);
97 UVector(int32_t initialCapacity
, UErrorCode
&status
);
99 UVector(UObjectDeleter
*d
, UElementsAreEqual
*c
, UErrorCode
&status
);
101 UVector(UObjectDeleter
*d
, UElementsAreEqual
*c
, int32_t initialCapacity
, UErrorCode
&status
);
106 * Assign this object to another (make this a copy of 'other').
107 * Use the 'assign' function to assign each element.
109 void assign(const UVector
& other
, UElementAssigner
*assign
, UErrorCode
&ec
);
112 * Compare this vector with another. They will be considered
113 * equal if they are of the same size and all elements are equal,
114 * as compared using this object's comparer.
116 UBool
operator==(const UVector
& other
);
119 * Equivalent to !operator==()
121 inline UBool
operator!=(const UVector
& other
);
123 //------------------------------------------------------------
124 // java.util.Vector API
125 //------------------------------------------------------------
127 void addElement(void* obj
, UErrorCode
&status
);
129 void addElement(int32_t elem
, UErrorCode
&status
);
131 void setElementAt(void* obj
, int32_t index
);
133 void setElementAt(int32_t elem
, int32_t index
);
135 void insertElementAt(void* obj
, int32_t index
, UErrorCode
&status
);
137 void insertElementAt(int32_t elem
, int32_t index
, UErrorCode
&status
);
139 void* elementAt(int32_t index
) const;
141 int32_t elementAti(int32_t index
) const;
143 UBool
equals(const UVector
&other
) const;
145 inline void* firstElement(void) const;
147 inline void* lastElement(void) const;
149 inline int32_t lastElementi(void) const;
151 int32_t indexOf(void* obj
, int32_t startIndex
= 0) const;
153 int32_t indexOf(int32_t obj
, int32_t startIndex
= 0) const;
155 inline UBool
contains(void* obj
) const;
157 inline UBool
contains(int32_t obj
) const;
159 UBool
containsAll(const UVector
& other
) const;
161 UBool
removeAll(const UVector
& other
);
163 UBool
retainAll(const UVector
& other
);
165 void removeElementAt(int32_t index
);
167 UBool
removeElement(void* obj
);
169 void removeAllElements();
171 inline int32_t size(void) const;
173 inline UBool
isEmpty(void) const;
175 UBool
ensureCapacity(int32_t minimumCapacity
, UErrorCode
&status
);
178 * Change the size of this vector as follows: If newSize is
179 * smaller, then truncate the array, possibly deleting held
180 * elements for i >= newSize. If newSize is larger, grow the
181 * array, filling in new slots with NULL.
183 void setSize(int32_t newSize
, UErrorCode
&status
);
186 * Fill in the given array with all elements of this vector.
188 void** toArray(void** result
) const;
190 //------------------------------------------------------------
192 //------------------------------------------------------------
194 UObjectDeleter
*setDeleter(UObjectDeleter
*d
);
196 UElementsAreEqual
*setComparer(UElementsAreEqual
*c
);
198 inline void* operator[](int32_t index
) const;
201 * Removes the element at the given index from this vector and
202 * transfer ownership of it to the caller. After this call, the
203 * caller owns the result and must delete it and the vector entry
204 * at 'index' is removed, shifting all subsequent entries back by
205 * one index and shortening the size of the vector by one. If the
206 * index is out of range or if there is no item at the given index
207 * then 0 is returned and the vector is unchanged.
209 void* orphanElementAt(int32_t index
);
212 * Returns true if this vector contains none of the elements
213 * of the given vector.
214 * @param other vector to be checked for containment
215 * @return true if the test condition is met
217 UBool
containsNone(const UVector
& other
) const;
220 * Insert the given object into this vector at its sorted position
221 * as defined by 'compare'. The current elements are assumed to
224 void sortedInsert(void* obj
, UElementComparator
*compare
, UErrorCode
& ec
);
227 * Insert the given integer into this vector at its sorted position
228 * as defined by 'compare'. The current elements are assumed to
231 void sortedInsert(int32_t obj
, UElementComparator
*compare
, UErrorCode
& ec
);
234 * Sort the contents of the vector, assuming that the contents of the
235 * vector are of type int32_t.
237 void sorti(UErrorCode
&ec
);
240 * Sort the contents of this vector, using a caller-supplied function
241 * to do the comparisons. (It's confusing that
242 * UVector's UElementComparator function is different from the
243 * UComparator function type defined in uarrsort.h)
245 void sort(UElementComparator
*compare
, UErrorCode
&ec
);
248 * Stable sort the contents of this vector using a caller-supplied function
249 * of type UComparator to do the comparison. Provides more flexibility
250 * than UVector::sort() because an additional user parameter can be passed to
251 * the comparison function.
253 void sortWithUComparator(UComparator
*compare
, const void *context
, UErrorCode
&ec
);
256 * ICU "poor man's RTTI", returns a UClassID for this class.
258 static UClassID U_EXPORT2
getStaticClassID();
261 * ICU "poor man's RTTI", returns a UClassID for the actual class.
263 virtual UClassID
getDynamicClassID() const;
266 void _init(int32_t initialCapacity
, UErrorCode
&status
);
268 int32_t indexOf(UElement key
, int32_t startIndex
= 0, int8_t hint
= 0) const;
270 void sortedInsert(UElement e
, UElementComparator
*compare
, UErrorCode
& ec
);
273 UVector(const UVector
&);
276 UVector
& operator=(const UVector
&);
282 * <p>Ultralightweight C++ implementation of a <tt>void*</tt> stack
283 * that is (mostly) compatible with java.util.Stack. As in java, this
284 * is merely a paper thin layer around UVector. See the UVector
285 * documentation for further information.
287 * <p><b>Design notes</b>
289 * <p>The element at index <tt>n-1</tt> is (of course) the top of the
292 * <p>The poorly named <tt>empty()</tt> method doesn't empty the
293 * stack; it determines if the stack is empty.
297 class U_COMMON_API UStack
: public UVector
{
299 UStack(UErrorCode
&status
);
301 UStack(int32_t initialCapacity
, UErrorCode
&status
);
303 UStack(UObjectDeleter
*d
, UElementsAreEqual
*c
, UErrorCode
&status
);
305 UStack(UObjectDeleter
*d
, UElementsAreEqual
*c
, int32_t initialCapacity
, UErrorCode
&status
);
309 // It's okay not to have a virtual destructor (in UVector)
310 // because UStack has no special cleanup to do.
312 inline UBool
empty(void) const;
314 inline void* peek(void) const;
316 inline int32_t peeki(void) const;
322 inline void* push(void* obj
, UErrorCode
&status
);
324 inline int32_t push(int32_t i
, UErrorCode
&status
);
327 If the object o occurs as an item in this stack,
328 this method returns the 1-based distance from the top of the stack.
330 int32_t search(void* obj
) const;
333 * ICU "poor man's RTTI", returns a UClassID for this class.
335 static UClassID U_EXPORT2
getStaticClassID();
338 * ICU "poor man's RTTI", returns a UClassID for the actual class.
340 virtual UClassID
getDynamicClassID() const;
344 UStack(const UStack
&);
347 UStack
& operator=(const UStack
&);
353 inline int32_t UVector::size(void) const {
357 inline UBool
UVector::isEmpty(void) const {
361 inline UBool
UVector::contains(void* obj
) const {
362 return indexOf(obj
) >= 0;
365 inline UBool
UVector::contains(int32_t obj
) const {
366 return indexOf(obj
) >= 0;
369 inline void* UVector::firstElement(void) const {
373 inline void* UVector::lastElement(void) const {
374 return elementAt(count
-1);
377 inline int32_t UVector::lastElementi(void) const {
378 return elementAti(count
-1);
381 inline void* UVector::operator[](int32_t index
) const {
382 return elementAt(index
);
385 inline UBool
UVector::operator!=(const UVector
& other
) {
386 return !operator==(other
);
391 inline UBool
UStack::empty(void) const {
395 inline void* UStack::peek(void) const {
396 return lastElement();
399 inline int32_t UStack::peeki(void) const {
400 return lastElementi();
403 inline void* UStack::push(void* obj
, UErrorCode
&status
) {
404 addElement(obj
, status
);
408 inline int32_t UStack::push(int32_t i
, UErrorCode
&status
) {
409 addElement(i
, status
);