2 ******************************************************************************
3 * Copyright (C) 1999-2013, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ******************************************************************************
6 * Date Name Description
7 * 10/22/99 alan Creation.
8 **********************************************************************
18 #define DEFAULT_CAPACITY 8
21 * Constants for hinting whether a key is an integer
22 * or a pointer. If a hint bit is zero, then the associated
23 * token is assumed to be an integer. This is needed for iSeries
25 #define HINT_KEY_POINTER (1)
26 #define HINT_KEY_INTEGER (0)
28 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(UVector
)
30 UVector::UVector(UErrorCode
&status
) :
37 _init(DEFAULT_CAPACITY
, status
);
40 UVector::UVector(int32_t initialCapacity
, UErrorCode
&status
) :
47 _init(initialCapacity
, status
);
50 UVector::UVector(UObjectDeleter
*d
, UElementsAreEqual
*c
, UErrorCode
&status
) :
57 _init(DEFAULT_CAPACITY
, status
);
60 UVector::UVector(UObjectDeleter
*d
, UElementsAreEqual
*c
, int32_t initialCapacity
, UErrorCode
&status
) :
67 _init(initialCapacity
, status
);
70 void UVector::_init(int32_t initialCapacity
, UErrorCode
&status
) {
71 if (U_FAILURE(status
)) {
74 // Fix bogus initialCapacity values; avoid malloc(0) and integer overflow
75 if ((initialCapacity
< 1) || (initialCapacity
> (int32_t)(INT32_MAX
/ sizeof(UElement
)))) {
76 initialCapacity
= DEFAULT_CAPACITY
;
78 elements
= (UElement
*)uprv_malloc(sizeof(UElement
)*initialCapacity
);
80 status
= U_MEMORY_ALLOCATION_ERROR
;
82 capacity
= initialCapacity
;
93 * Assign this object to another (make this a copy of 'other').
94 * Use the 'assign' function to assign each element.
96 void UVector::assign(const UVector
& other
, UElementAssigner
*assign
, UErrorCode
&ec
) {
97 if (ensureCapacity(other
.count
, ec
)) {
98 setSize(other
.count
, ec
);
100 for (int32_t i
=0; i
<other
.count
; ++i
) {
101 if (elements
[i
].pointer
!= 0 && deleter
!= 0) {
102 (*deleter
)(elements
[i
].pointer
);
104 (*assign
)(&elements
[i
], &other
.elements
[i
]);
110 // This only does something sensible if this object has a non-null comparer
111 UBool
UVector::operator==(const UVector
& other
) {
113 if (count
!= other
.count
) return FALSE
;
114 if (comparer
!= NULL
) {
115 // Compare using this object's comparer
116 for (i
=0; i
<count
; ++i
) {
117 if (!(*comparer
)(elements
[i
], other
.elements
[i
])) {
125 void UVector::addElement(void* obj
, UErrorCode
&status
) {
126 if (ensureCapacity(count
+ 1, status
)) {
127 elements
[count
++].pointer
= obj
;
131 void UVector::addElement(int32_t elem
, UErrorCode
&status
) {
132 if (ensureCapacity(count
+ 1, status
)) {
133 elements
[count
].pointer
= NULL
; // Pointers may be bigger than ints.
134 elements
[count
].integer
= elem
;
139 void UVector::setElementAt(void* obj
, int32_t index
) {
140 if (0 <= index
&& index
< count
) {
141 if (elements
[index
].pointer
!= 0 && deleter
!= 0) {
142 (*deleter
)(elements
[index
].pointer
);
144 elements
[index
].pointer
= obj
;
146 /* else index out of range */
149 void UVector::setElementAt(int32_t elem
, int32_t index
) {
150 if (0 <= index
&& index
< count
) {
151 if (elements
[index
].pointer
!= 0 && deleter
!= 0) {
152 // TODO: this should be an error. mixing up ints and pointers.
153 (*deleter
)(elements
[index
].pointer
);
155 elements
[index
].pointer
= NULL
;
156 elements
[index
].integer
= elem
;
158 /* else index out of range */
161 void UVector::insertElementAt(void* obj
, int32_t index
, UErrorCode
&status
) {
162 // must have 0 <= index <= count
163 if (0 <= index
&& index
<= count
&& ensureCapacity(count
+ 1, status
)) {
164 for (int32_t i
=count
; i
>index
; --i
) {
165 elements
[i
] = elements
[i
-1];
167 elements
[index
].pointer
= obj
;
170 /* else index out of range */
173 void UVector::insertElementAt(int32_t elem
, int32_t index
, UErrorCode
&status
) {
174 // must have 0 <= index <= count
175 if (0 <= index
&& index
<= count
&& ensureCapacity(count
+ 1, status
)) {
176 for (int32_t i
=count
; i
>index
; --i
) {
177 elements
[i
] = elements
[i
-1];
179 elements
[index
].pointer
= NULL
;
180 elements
[index
].integer
= elem
;
183 /* else index out of range */
186 void* UVector::elementAt(int32_t index
) const {
187 return (0 <= index
&& index
< count
) ? elements
[index
].pointer
: 0;
190 int32_t UVector::elementAti(int32_t index
) const {
191 return (0 <= index
&& index
< count
) ? elements
[index
].integer
: 0;
194 UBool
UVector::containsAll(const UVector
& other
) const {
195 for (int32_t i
=0; i
<other
.size(); ++i
) {
196 if (indexOf(other
.elements
[i
]) < 0) {
203 UBool
UVector::containsNone(const UVector
& other
) const {
204 for (int32_t i
=0; i
<other
.size(); ++i
) {
205 if (indexOf(other
.elements
[i
]) >= 0) {
212 UBool
UVector::removeAll(const UVector
& other
) {
213 UBool changed
= FALSE
;
214 for (int32_t i
=0; i
<other
.size(); ++i
) {
215 int32_t j
= indexOf(other
.elements
[i
]);
224 UBool
UVector::retainAll(const UVector
& other
) {
225 UBool changed
= FALSE
;
226 for (int32_t j
=size()-1; j
>=0; --j
) {
227 int32_t i
= other
.indexOf(elements
[j
]);
236 void UVector::removeElementAt(int32_t index
) {
237 void* e
= orphanElementAt(index
);
238 if (e
!= 0 && deleter
!= 0) {
243 UBool
UVector::removeElement(void* obj
) {
244 int32_t i
= indexOf(obj
);
252 void UVector::removeAllElements(void) {
254 for (int32_t i
=0; i
<count
; ++i
) {
255 if (elements
[i
].pointer
!= 0) {
256 (*deleter
)(elements
[i
].pointer
);
263 UBool
UVector::equals(const UVector
&other
) const {
266 if (this->count
!= other
.count
) {
270 for (i
=0; i
<count
; i
++) {
271 if (elements
[i
].pointer
!= other
.elements
[i
].pointer
) {
277 for (i
=0; i
<count
; i
++) {
278 key
.pointer
= &other
.elements
[i
];
279 if (!(*comparer
)(key
, elements
[i
])) {
289 int32_t UVector::indexOf(void* obj
, int32_t startIndex
) const {
292 return indexOf(key
, startIndex
, HINT_KEY_POINTER
);
295 int32_t UVector::indexOf(int32_t obj
, int32_t startIndex
) const {
298 return indexOf(key
, startIndex
, HINT_KEY_INTEGER
);
301 // This only works if this object has a non-null comparer
302 int32_t UVector::indexOf(UElement key
, int32_t startIndex
, int8_t hint
) const {
305 for (i
=startIndex
; i
<count
; ++i
) {
306 if ((*comparer
)(key
, elements
[i
])) {
311 for (i
=startIndex
; i
<count
; ++i
) {
312 /* Pointers are not always the same size as ints so to perform
313 * a valid comparision we need to know whether we are being
314 * provided an int or a pointer. */
315 if (hint
& HINT_KEY_POINTER
) {
316 if (key
.pointer
== elements
[i
].pointer
) {
320 if (key
.integer
== elements
[i
].integer
) {
329 UBool
UVector::ensureCapacity(int32_t minimumCapacity
, UErrorCode
&status
) {
330 if (minimumCapacity
< 0) {
331 status
= U_ILLEGAL_ARGUMENT_ERROR
;
334 if (capacity
< minimumCapacity
) {
335 if (capacity
> (INT32_MAX
- 1) / 2) { // integer overflow check
336 status
= U_ILLEGAL_ARGUMENT_ERROR
;
339 int32_t newCap
= capacity
* 2;
340 if (newCap
< minimumCapacity
) {
341 newCap
= minimumCapacity
;
343 if (newCap
> (int32_t)(INT32_MAX
/ sizeof(UElement
))) { // integer overflow check
344 // We keep the original memory contents on bad minimumCapacity.
345 status
= U_ILLEGAL_ARGUMENT_ERROR
;
348 UElement
* newElems
= (UElement
*)uprv_realloc(elements
, sizeof(UElement
)*newCap
);
349 if (newElems
== NULL
) {
350 // We keep the original contents on the memory failure on realloc or bad minimumCapacity.
351 status
= U_MEMORY_ALLOCATION_ERROR
;
361 * Change the size of this vector as follows: If newSize is smaller,
362 * then truncate the array, possibly deleting held elements for i >=
363 * newSize. If newSize is larger, grow the array, filling in new
366 void UVector::setSize(int32_t newSize
, UErrorCode
&status
) {
371 if (newSize
> count
) {
372 if (!ensureCapacity(newSize
, status
)) {
376 empty
.pointer
= NULL
;
378 for (i
=count
; i
<newSize
; ++i
) {
382 /* Most efficient to count down */
383 for (i
=count
-1; i
>=newSize
; --i
) {
391 * Fill in the given array with all elements of this vector.
393 void** UVector::toArray(void** result
) const {
395 for (int i
=0; i
<count
; ++i
) {
396 *a
++ = elements
[i
].pointer
;
401 UObjectDeleter
*UVector::setDeleter(UObjectDeleter
*d
) {
402 UObjectDeleter
*old
= deleter
;
407 UElementsAreEqual
*UVector::setComparer(UElementsAreEqual
*d
) {
408 UElementsAreEqual
*old
= comparer
;
414 * Removes the element at the given index from this vector and
415 * transfer ownership of it to the caller. After this call, the
416 * caller owns the result and must delete it and the vector entry
417 * at 'index' is removed, shifting all subsequent entries back by
418 * one index and shortening the size of the vector by one. If the
419 * index is out of range or if there is no item at the given index
420 * then 0 is returned and the vector is unchanged.
422 void* UVector::orphanElementAt(int32_t index
) {
424 if (0 <= index
&& index
< count
) {
425 e
= elements
[index
].pointer
;
426 for (int32_t i
=index
; i
<count
-1; ++i
) {
427 elements
[i
] = elements
[i
+1];
431 /* else index out of range */
436 * Insert the given object into this vector at its sorted position
437 * as defined by 'compare'. The current elements are assumed to
440 void UVector::sortedInsert(void* obj
, UElementComparator
*compare
, UErrorCode
& ec
) {
443 sortedInsert(e
, compare
, ec
);
447 * Insert the given integer into this vector at its sorted position
448 * as defined by 'compare'. The current elements are assumed to
451 void UVector::sortedInsert(int32_t obj
, UElementComparator
*compare
, UErrorCode
& ec
) {
454 sortedInsert(e
, compare
, ec
);
457 // ASSUME elements[] IS CURRENTLY SORTED
458 void UVector::sortedInsert(UElement e
, UElementComparator
*compare
, UErrorCode
& ec
) {
459 // Perform a binary search for the location to insert tok at. Tok
460 // will be inserted between two elements a and b such that a <=
461 // tok && tok < b, where there is a 'virtual' elements[-1] always
462 // less than tok and a 'virtual' elements[count] always greater
464 int32_t min
= 0, max
= count
;
466 int32_t probe
= (min
+ max
) / 2;
467 int8_t c
= (*compare
)(elements
[probe
], e
);
475 if (ensureCapacity(count
+ 1, ec
)) {
476 for (int32_t i
=count
; i
>min
; --i
) {
477 elements
[i
] = elements
[i
-1];
485 * Array sort comparator function.
486 * Used from UVector::sort()
487 * Conforms to function signature required for uprv_sortArray().
488 * This function is essentially just a wrapper, to make a
489 * UVector style comparator function usable with uprv_sortArray().
491 * The context pointer to this function is a pointer back
492 * (with some extra indirection) to the user supplied comparator.
495 static int32_t U_CALLCONV
496 sortComparator(const void *context
, const void *left
, const void *right
) {
497 UElementComparator
*compare
= *static_cast<UElementComparator
* const *>(context
);
498 UElement e1
= *static_cast<const UElement
*>(left
);
499 UElement e2
= *static_cast<const UElement
*>(right
);
500 int32_t result
= (*compare
)(e1
, e2
);
506 * Array sort comparison function for use from UVector::sorti()
507 * Compares int32_t vector elements.
509 static int32_t U_CALLCONV
510 sortiComparator(const void * /*context */, const void *left
, const void *right
) {
511 const UElement
*e1
= static_cast<const UElement
*>(left
);
512 const UElement
*e2
= static_cast<const UElement
*>(right
);
513 int32_t result
= e1
->integer
< e2
->integer
? -1 :
514 e1
->integer
== e2
->integer
? 0 : 1;
519 * Sort the vector, assuming it constains ints.
520 * (A more general sort would take a comparison function, but it's
521 * not clear whether UVector's UElementComparator or
522 * UComparator from uprv_sortAray would be more appropriate.)
524 void UVector::sorti(UErrorCode
&ec
) {
526 uprv_sortArray(elements
, count
, sizeof(UElement
),
527 sortiComparator
, NULL
, FALSE
, &ec
);
533 * Sort with a user supplied comparator.
535 * The comparator function handling is confusing because the function type
536 * for UVector (as defined for sortedInsert()) is different from the signature
537 * required by uprv_sortArray(). This is handled by passing the
538 * the UVector sort function pointer via the context pointer to a
539 * sortArray() comparator function, which can then call back to
540 * the original user functtion.
542 * An additional twist is that it's not safe to pass a pointer-to-function
543 * as a (void *) data pointer, so instead we pass a (data) pointer to a
544 * pointer-to-function variable.
546 void UVector::sort(UElementComparator
*compare
, UErrorCode
&ec
) {
548 uprv_sortArray(elements
, count
, sizeof(UElement
),
549 sortComparator
, &compare
, FALSE
, &ec
);
555 * Stable sort with a user supplied comparator of type UComparator.
557 void UVector::sortWithUComparator(UComparator
*compare
, const void *context
, UErrorCode
&ec
) {
559 uprv_sortArray(elements
, count
, sizeof(UElement
),
560 compare
, context
, TRUE
, &ec
);