2 ******************************************************************************
3 * Copyright (C) 1999-2015, 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 (U_FAILURE(status
)) {
120 if (minimumCapacity
< 0) {
121 status
= U_ILLEGAL_ARGUMENT_ERROR
;
124 if (capacity
>= minimumCapacity
) {
127 if (maxCapacity
>0 && minimumCapacity
>maxCapacity
) {
128 status
= U_BUFFER_OVERFLOW_ERROR
;
131 if (capacity
> (INT32_MAX
- 1) / 2) { // integer overflow check
132 status
= U_ILLEGAL_ARGUMENT_ERROR
;
135 int32_t newCap
= capacity
* 2;
136 if (newCap
< minimumCapacity
) {
137 newCap
= minimumCapacity
;
139 if (maxCapacity
> 0 && newCap
> maxCapacity
) {
140 newCap
= maxCapacity
;
142 if (newCap
> (int32_t)(INT32_MAX
/ sizeof(int64_t))) { // integer overflow check
143 // We keep the original memory contents on bad minimumCapacity/maxCapacity.
144 status
= U_ILLEGAL_ARGUMENT_ERROR
;
147 int64_t* newElems
= (int64_t *)uprv_realloc(elements
, sizeof(int64_t)*newCap
);
148 if (newElems
== NULL
) {
149 // We keep the original contents on the memory failure on realloc.
150 status
= U_MEMORY_ALLOCATION_ERROR
;
158 void UVector64::setMaxCapacity(int32_t limit
) {
159 U_ASSERT(limit
>= 0);
163 if (limit
> (int32_t)(INT32_MAX
/ sizeof(int64_t))) { // integer overflow check for realloc
164 // Something is very wrong, don't realloc, leave capacity and maxCapacity unchanged
168 if (capacity
<= maxCapacity
|| maxCapacity
== 0) {
169 // Current capacity is within the new limit.
173 // New maximum capacity is smaller than the current size.
174 // Realloc the storage to the new, smaller size.
175 int64_t* newElems
= (int64_t *)uprv_realloc(elements
, sizeof(int64_t)*maxCapacity
);
176 if (newElems
== NULL
) {
177 // Realloc to smaller failed.
178 // Just keep what we had. No need to call it a failure.
182 capacity
= maxCapacity
;
183 if (count
> capacity
) {
189 * Change the size of this vector as follows: If newSize is smaller,
190 * then truncate the array, possibly deleting held elements for i >=
191 * newSize. If newSize is larger, grow the array, filling in new
194 void UVector64::setSize(int32_t newSize
) {
199 if (newSize
> count
) {
200 UErrorCode ec
= U_ZERO_ERROR
;
201 if (!ensureCapacity(newSize
, ec
)) {
204 for (i
=count
; i
<newSize
; ++i
) {