2 ******************************************************************************
3 * Copyright (C) 1999-2003, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 ******************************************************************************
6 * Date Name Description
7 * 10/22/99 alan Creation.
8 **********************************************************************
16 #define DEFUALT_CAPACITY 8
19 * Constants for hinting whether a key is an integer
20 * or a pointer. If a hint bit is zero, then the associated
21 * token is assumed to be an integer. This is needed for iSeries
24 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(UVector32
)
26 UVector32::UVector32(UErrorCode
&status
) :
31 _init(DEFUALT_CAPACITY
, status
);
34 UVector32::UVector32(int32_t initialCapacity
, UErrorCode
&status
) :
39 _init(initialCapacity
, status
);
44 void UVector32::_init(int32_t initialCapacity
, UErrorCode
&status
) {
45 // Fix bogus initialCapacity values; avoid malloc(0)
46 if (initialCapacity
< 1) {
47 initialCapacity
= DEFUALT_CAPACITY
;
49 elements
= (int32_t *)uprv_malloc(sizeof(int32_t)*initialCapacity
);
51 status
= U_MEMORY_ALLOCATION_ERROR
;
53 capacity
= initialCapacity
;
57 UVector32::~UVector32() {
63 * Assign this object to another (make this a copy of 'other').
65 void UVector32::assign(const UVector32
& other
, UErrorCode
&ec
) {
66 if (ensureCapacity(other
.count
, ec
)) {
68 for (int32_t i
=0; i
<other
.count
; ++i
) {
69 elements
[i
] = other
.elements
[i
];
75 UBool
UVector32::operator==(const UVector32
& other
) {
77 if (count
!= other
.count
) return FALSE
;
78 for (i
=0; i
<count
; ++i
) {
79 if (elements
[i
] != other
.elements
[i
]) {
87 void UVector32::setElementAt(int32_t elem
, int32_t index
) {
88 if (0 <= index
&& index
< count
) {
89 elements
[index
] = elem
;
91 /* else index out of range */
94 void UVector32::insertElementAt(int32_t elem
, int32_t index
, UErrorCode
&status
) {
95 // must have 0 <= index <= count
96 if (0 <= index
&& index
<= count
&& ensureCapacity(count
+ 1, status
)) {
97 for (int32_t i
=count
; i
>index
; --i
) {
98 elements
[i
] = elements
[i
-1];
100 elements
[index
] = elem
;
103 /* else index out of range */
106 UBool
UVector32::containsAll(const UVector32
& other
) const {
107 for (int32_t i
=0; i
<other
.size(); ++i
) {
108 if (indexOf(other
.elements
[i
]) < 0) {
115 UBool
UVector32::containsNone(const UVector32
& other
) const {
116 for (int32_t i
=0; i
<other
.size(); ++i
) {
117 if (indexOf(other
.elements
[i
]) >= 0) {
124 UBool
UVector32::removeAll(const UVector32
& other
) {
125 UBool changed
= FALSE
;
126 for (int32_t i
=0; i
<other
.size(); ++i
) {
127 int32_t j
= indexOf(other
.elements
[i
]);
136 UBool
UVector32::retainAll(const UVector32
& other
) {
137 UBool changed
= FALSE
;
138 for (int32_t j
=size()-1; j
>=0; --j
) {
139 int32_t i
= other
.indexOf(elements
[j
]);
148 void UVector32::removeElementAt(int32_t index
) {
150 for (int32_t i
=index
; i
<count
-1; ++i
) {
151 elements
[i
] = elements
[i
+1];
157 void UVector32::removeAllElements(void) {
161 UBool
UVector32::equals(const UVector32
&other
) const {
164 if (this->count
!= other
.count
) {
167 for (i
=0; i
<count
; i
++) {
168 if (elements
[i
] != other
.elements
[i
]) {
178 int32_t UVector32::indexOf(int32_t key
, int32_t startIndex
) const {
180 for (i
=startIndex
; i
<count
; ++i
) {
181 if (key
== elements
[i
]) {
189 UBool
UVector32::expandCapacity(int32_t minimumCapacity
, UErrorCode
&status
) {
190 if (capacity
>= minimumCapacity
) {
193 int32_t newCap
= capacity
* 2;
194 if (newCap
< minimumCapacity
) {
195 newCap
= minimumCapacity
;
197 int32_t* newElems
= (int32_t *)uprv_malloc(sizeof(int32_t)*newCap
);
199 status
= U_MEMORY_ALLOCATION_ERROR
;
202 uprv_memcpy(newElems
, elements
, sizeof(elements
[0]) * count
);
211 * Change the size of this vector as follows: If newSize is smaller,
212 * then truncate the array, possibly deleting held elements for i >=
213 * newSize. If newSize is larger, grow the array, filling in new
216 void UVector32::setSize(int32_t newSize
) {
221 if (newSize
> count
) {
222 UErrorCode ec
= U_ZERO_ERROR
;
223 if (!ensureCapacity(newSize
, ec
)) {
226 for (i
=count
; i
<newSize
; ++i
) {
237 * Insert the given integer into this vector at its sorted position
238 * as defined by 'compare'. The current elements are assumed to
241 void UVector32::sortedInsert(int32_t tok
, UErrorCode
& ec
) {
242 // Perform a binary search for the location to insert tok at. Tok
243 // will be inserted between two elements a and b such that a <=
244 // tok && tok < b, where there is a 'virtual' elements[-1] always
245 // less than tok and a 'virtual' elements[count] always greater
247 int32_t min
= 0, max
= count
;
249 int32_t probe
= (min
+ max
) / 2;
250 //int8_t c = (*compare)(elements[probe], tok);
252 if (elements
[probe
] > tok
) {
259 if (ensureCapacity(count
+ 1, ec
)) {
260 for (int32_t i
=count
; i
>min
; --i
) {
261 elements
[i
] = elements
[i
-1];