2 ******************************************************************************
3 * Copyright (C) 1999-2010, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 ******************************************************************************
14 #define DEFAULT_CAPACITY 8
17 * Constants for hinting whether a key is an integer
18 * or a pointer. If a hint bit is zero, then the associated
19 * token is assumed to be an integer. This is needed for iSeries
22 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(UVector64
)
24 UVector64::UVector64(UErrorCode
&status
) :
30 _init(DEFAULT_CAPACITY
, status
);
33 UVector64::UVector64(int32_t initialCapacity
, UErrorCode
&status
) :
39 _init(initialCapacity
, status
);
44 void UVector64::_init(int32_t initialCapacity
, UErrorCode
&status
) {
45 // Fix bogus initialCapacity values; avoid malloc(0)
46 if (initialCapacity
< 1) {
47 initialCapacity
= DEFAULT_CAPACITY
;
49 if (maxCapacity
>0 && maxCapacity
<initialCapacity
) {
50 initialCapacity
= maxCapacity
;
52 if (initialCapacity
> (int32_t)(INT32_MAX
/ sizeof(int64_t))) {
53 initialCapacity
= uprv_min(DEFAULT_CAPACITY
, maxCapacity
);
55 elements
= (int64_t *)uprv_malloc(sizeof(int64_t)*initialCapacity
);
57 status
= U_MEMORY_ALLOCATION_ERROR
;
59 capacity
= initialCapacity
;
63 UVector64::~UVector64() {
69 * Assign this object to another (make this a copy of 'other').
71 void UVector64::assign(const UVector64
& other
, UErrorCode
&ec
) {
72 if (ensureCapacity(other
.count
, ec
)) {
74 for (int32_t i
=0; i
<other
.count
; ++i
) {
75 elements
[i
] = other
.elements
[i
];
81 UBool
UVector64::operator==(const UVector64
& other
) {
83 if (count
!= other
.count
) return FALSE
;
84 for (i
=0; i
<count
; ++i
) {
85 if (elements
[i
] != other
.elements
[i
]) {
93 void UVector64::setElementAt(int64_t elem
, int32_t index
) {
94 if (0 <= index
&& index
< count
) {
95 elements
[index
] = elem
;
97 /* else index out of range */
100 void UVector64::insertElementAt(int64_t elem
, int32_t index
, UErrorCode
&status
) {
101 // must have 0 <= index <= count
102 if (0 <= index
&& index
<= count
&& ensureCapacity(count
+ 1, status
)) {
103 for (int32_t i
=count
; i
>index
; --i
) {
104 elements
[i
] = elements
[i
-1];
106 elements
[index
] = elem
;
109 /* else index out of range */
112 void UVector64::removeAllElements(void) {
116 UBool
UVector64::expandCapacity(int32_t minimumCapacity
, UErrorCode
&status
) {
117 if (minimumCapacity
< 0) {
118 status
= U_ILLEGAL_ARGUMENT_ERROR
;
121 if (capacity
>= minimumCapacity
) {
124 if (maxCapacity
>0 && minimumCapacity
>maxCapacity
) {
125 status
= U_BUFFER_OVERFLOW_ERROR
;
128 if (capacity
> (INT32_MAX
- 1) / 2) { // integer overflow check
129 status
= U_ILLEGAL_ARGUMENT_ERROR
;
132 int32_t newCap
= capacity
* 2;
133 if (newCap
< minimumCapacity
) {
134 newCap
= minimumCapacity
;
136 if (maxCapacity
> 0 && newCap
> maxCapacity
) {
137 newCap
= maxCapacity
;
139 if (newCap
> (int32_t)(INT32_MAX
/ sizeof(int64_t))) { // integer overflow check
140 // We keep the original memory contents on bad minimumCapacity/maxCapacity.
141 status
= U_ILLEGAL_ARGUMENT_ERROR
;
144 int64_t* newElems
= (int64_t *)uprv_realloc(elements
, sizeof(int64_t)*newCap
);
145 if (newElems
== NULL
) {
146 // We keep the original contents on the memory failure on realloc.
147 status
= U_MEMORY_ALLOCATION_ERROR
;
155 void UVector64::setMaxCapacity(int32_t limit
) {
156 U_ASSERT(limit
>= 0);
160 if (limit
> (int32_t)(INT32_MAX
/ sizeof(int64_t))) { // integer overflow check for realloc
161 // Something is very wrong, don't realloc, leave capacity and maxCapacity unchanged
165 if (capacity
<= maxCapacity
|| maxCapacity
== 0) {
166 // Current capacity is within the new limit.
170 // New maximum capacity is smaller than the current size.
171 // Realloc the storage to the new, smaller size.
172 int64_t* newElems
= (int64_t *)uprv_realloc(elements
, sizeof(int64_t)*maxCapacity
);
173 if (newElems
== NULL
) {
174 // Realloc to smaller failed.
175 // Just keep what we had. No need to call it a failure.
179 capacity
= maxCapacity
;
180 if (count
> capacity
) {
186 * Change the size of this vector as follows: If newSize is smaller,
187 * then truncate the array, possibly deleting held elements for i >=
188 * newSize. If newSize is larger, grow the array, filling in new
191 void UVector64::setSize(int32_t newSize
) {
196 if (newSize
> count
) {
197 UErrorCode ec
= U_ZERO_ERROR
;
198 if (!ensureCapacity(newSize
, ec
)) {
201 for (i
=count
; i
<newSize
; ++i
) {