intl/icu/source/common/uvectr32.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 ******************************************************************************
michael@0 3 * Copyright (C) 1999-2010, International Business Machines Corporation and *
michael@0 4 * others. All Rights Reserved. *
michael@0 5 ******************************************************************************
michael@0 6 * Date Name Description
michael@0 7 * 10/22/99 alan Creation.
michael@0 8 **********************************************************************
michael@0 9 */
michael@0 10
michael@0 11 #include "uvectr32.h"
michael@0 12 #include "cmemory.h"
michael@0 13 #include "putilimp.h"
michael@0 14
michael@0 15 U_NAMESPACE_BEGIN
michael@0 16
michael@0 17 #define DEFAULT_CAPACITY 8
michael@0 18
michael@0 19 /*
michael@0 20 * Constants for hinting whether a key is an integer
michael@0 21 * or a pointer. If a hint bit is zero, then the associated
michael@0 22 * token is assumed to be an integer. This is needed for iSeries
michael@0 23 */
michael@0 24
michael@0 25 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(UVector32)
michael@0 26
michael@0 27 UVector32::UVector32(UErrorCode &status) :
michael@0 28 count(0),
michael@0 29 capacity(0),
michael@0 30 maxCapacity(0),
michael@0 31 elements(NULL)
michael@0 32 {
michael@0 33 _init(DEFAULT_CAPACITY, status);
michael@0 34 }
michael@0 35
michael@0 36 UVector32::UVector32(int32_t initialCapacity, UErrorCode &status) :
michael@0 37 count(0),
michael@0 38 capacity(0),
michael@0 39 maxCapacity(0),
michael@0 40 elements(0)
michael@0 41 {
michael@0 42 _init(initialCapacity, status);
michael@0 43 }
michael@0 44
michael@0 45
michael@0 46
michael@0 47 void UVector32::_init(int32_t initialCapacity, UErrorCode &status) {
michael@0 48 // Fix bogus initialCapacity values; avoid malloc(0)
michael@0 49 if (initialCapacity < 1) {
michael@0 50 initialCapacity = DEFAULT_CAPACITY;
michael@0 51 }
michael@0 52 if (maxCapacity>0 && maxCapacity<initialCapacity) {
michael@0 53 initialCapacity = maxCapacity;
michael@0 54 }
michael@0 55 if (initialCapacity > (int32_t)(INT32_MAX / sizeof(int32_t))) {
michael@0 56 initialCapacity = uprv_min(DEFAULT_CAPACITY, maxCapacity);
michael@0 57 }
michael@0 58 elements = (int32_t *)uprv_malloc(sizeof(int32_t)*initialCapacity);
michael@0 59 if (elements == 0) {
michael@0 60 status = U_MEMORY_ALLOCATION_ERROR;
michael@0 61 } else {
michael@0 62 capacity = initialCapacity;
michael@0 63 }
michael@0 64 }
michael@0 65
michael@0 66 UVector32::~UVector32() {
michael@0 67 uprv_free(elements);
michael@0 68 elements = 0;
michael@0 69 }
michael@0 70
michael@0 71 /**
michael@0 72 * Assign this object to another (make this a copy of 'other').
michael@0 73 */
michael@0 74 void UVector32::assign(const UVector32& other, UErrorCode &ec) {
michael@0 75 if (ensureCapacity(other.count, ec)) {
michael@0 76 setSize(other.count);
michael@0 77 for (int32_t i=0; i<other.count; ++i) {
michael@0 78 elements[i] = other.elements[i];
michael@0 79 }
michael@0 80 }
michael@0 81 }
michael@0 82
michael@0 83
michael@0 84 UBool UVector32::operator==(const UVector32& other) {
michael@0 85 int32_t i;
michael@0 86 if (count != other.count) return FALSE;
michael@0 87 for (i=0; i<count; ++i) {
michael@0 88 if (elements[i] != other.elements[i]) {
michael@0 89 return FALSE;
michael@0 90 }
michael@0 91 }
michael@0 92 return TRUE;
michael@0 93 }
michael@0 94
michael@0 95
michael@0 96 void UVector32::setElementAt(int32_t elem, int32_t index) {
michael@0 97 if (0 <= index && index < count) {
michael@0 98 elements[index] = elem;
michael@0 99 }
michael@0 100 /* else index out of range */
michael@0 101 }
michael@0 102
michael@0 103 void UVector32::insertElementAt(int32_t elem, int32_t index, UErrorCode &status) {
michael@0 104 // must have 0 <= index <= count
michael@0 105 if (0 <= index && index <= count && ensureCapacity(count + 1, status)) {
michael@0 106 for (int32_t i=count; i>index; --i) {
michael@0 107 elements[i] = elements[i-1];
michael@0 108 }
michael@0 109 elements[index] = elem;
michael@0 110 ++count;
michael@0 111 }
michael@0 112 /* else index out of range */
michael@0 113 }
michael@0 114
michael@0 115 UBool UVector32::containsAll(const UVector32& other) const {
michael@0 116 for (int32_t i=0; i<other.size(); ++i) {
michael@0 117 if (indexOf(other.elements[i]) < 0) {
michael@0 118 return FALSE;
michael@0 119 }
michael@0 120 }
michael@0 121 return TRUE;
michael@0 122 }
michael@0 123
michael@0 124 UBool UVector32::containsNone(const UVector32& other) const {
michael@0 125 for (int32_t i=0; i<other.size(); ++i) {
michael@0 126 if (indexOf(other.elements[i]) >= 0) {
michael@0 127 return FALSE;
michael@0 128 }
michael@0 129 }
michael@0 130 return TRUE;
michael@0 131 }
michael@0 132
michael@0 133 UBool UVector32::removeAll(const UVector32& other) {
michael@0 134 UBool changed = FALSE;
michael@0 135 for (int32_t i=0; i<other.size(); ++i) {
michael@0 136 int32_t j = indexOf(other.elements[i]);
michael@0 137 if (j >= 0) {
michael@0 138 removeElementAt(j);
michael@0 139 changed = TRUE;
michael@0 140 }
michael@0 141 }
michael@0 142 return changed;
michael@0 143 }
michael@0 144
michael@0 145 UBool UVector32::retainAll(const UVector32& other) {
michael@0 146 UBool changed = FALSE;
michael@0 147 for (int32_t j=size()-1; j>=0; --j) {
michael@0 148 int32_t i = other.indexOf(elements[j]);
michael@0 149 if (i < 0) {
michael@0 150 removeElementAt(j);
michael@0 151 changed = TRUE;
michael@0 152 }
michael@0 153 }
michael@0 154 return changed;
michael@0 155 }
michael@0 156
michael@0 157 void UVector32::removeElementAt(int32_t index) {
michael@0 158 if (index >= 0) {
michael@0 159 for (int32_t i=index; i<count-1; ++i) {
michael@0 160 elements[i] = elements[i+1];
michael@0 161 }
michael@0 162 --count;
michael@0 163 }
michael@0 164 }
michael@0 165
michael@0 166 void UVector32::removeAllElements(void) {
michael@0 167 count = 0;
michael@0 168 }
michael@0 169
michael@0 170 UBool UVector32::equals(const UVector32 &other) const {
michael@0 171 int i;
michael@0 172
michael@0 173 if (this->count != other.count) {
michael@0 174 return FALSE;
michael@0 175 }
michael@0 176 for (i=0; i<count; i++) {
michael@0 177 if (elements[i] != other.elements[i]) {
michael@0 178 return FALSE;
michael@0 179 }
michael@0 180 }
michael@0 181 return TRUE;
michael@0 182 }
michael@0 183
michael@0 184
michael@0 185
michael@0 186
michael@0 187 int32_t UVector32::indexOf(int32_t key, int32_t startIndex) const {
michael@0 188 int32_t i;
michael@0 189 for (i=startIndex; i<count; ++i) {
michael@0 190 if (key == elements[i]) {
michael@0 191 return i;
michael@0 192 }
michael@0 193 }
michael@0 194 return -1;
michael@0 195 }
michael@0 196
michael@0 197
michael@0 198 UBool UVector32::expandCapacity(int32_t minimumCapacity, UErrorCode &status) {
michael@0 199 if (minimumCapacity < 0) {
michael@0 200 status = U_ILLEGAL_ARGUMENT_ERROR;
michael@0 201 return FALSE;
michael@0 202 }
michael@0 203 if (capacity >= minimumCapacity) {
michael@0 204 return TRUE;
michael@0 205 }
michael@0 206 if (maxCapacity>0 && minimumCapacity>maxCapacity) {
michael@0 207 status = U_BUFFER_OVERFLOW_ERROR;
michael@0 208 return FALSE;
michael@0 209 }
michael@0 210 if (capacity > (INT32_MAX - 1) / 2) { // integer overflow check
michael@0 211 status = U_ILLEGAL_ARGUMENT_ERROR;
michael@0 212 return FALSE;
michael@0 213 }
michael@0 214 int32_t newCap = capacity * 2;
michael@0 215 if (newCap < minimumCapacity) {
michael@0 216 newCap = minimumCapacity;
michael@0 217 }
michael@0 218 if (maxCapacity > 0 && newCap > maxCapacity) {
michael@0 219 newCap = maxCapacity;
michael@0 220 }
michael@0 221 if (newCap > (int32_t)(INT32_MAX / sizeof(int32_t))) { // integer overflow check
michael@0 222 // We keep the original memory contents on bad minimumCapacity/maxCapacity.
michael@0 223 status = U_ILLEGAL_ARGUMENT_ERROR;
michael@0 224 return FALSE;
michael@0 225 }
michael@0 226 int32_t* newElems = (int32_t *)uprv_realloc(elements, sizeof(int32_t)*newCap);
michael@0 227 if (newElems == NULL) {
michael@0 228 // We keep the original contents on the memory failure on realloc.
michael@0 229 status = U_MEMORY_ALLOCATION_ERROR;
michael@0 230 return FALSE;
michael@0 231 }
michael@0 232 elements = newElems;
michael@0 233 capacity = newCap;
michael@0 234 return TRUE;
michael@0 235 }
michael@0 236
michael@0 237 void UVector32::setMaxCapacity(int32_t limit) {
michael@0 238 U_ASSERT(limit >= 0);
michael@0 239 if (limit < 0) {
michael@0 240 limit = 0;
michael@0 241 }
michael@0 242 if (limit > (int32_t)(INT32_MAX / sizeof(int32_t))) { // integer overflow check for realloc
michael@0 243 // Something is very wrong, don't realloc, leave capacity and maxCapacity unchanged
michael@0 244 return;
michael@0 245 }
michael@0 246 maxCapacity = limit;
michael@0 247 if (capacity <= maxCapacity || maxCapacity == 0) {
michael@0 248 // Current capacity is within the new limit.
michael@0 249 return;
michael@0 250 }
michael@0 251
michael@0 252 // New maximum capacity is smaller than the current size.
michael@0 253 // Realloc the storage to the new, smaller size.
michael@0 254 int32_t* newElems = (int32_t *)uprv_realloc(elements, sizeof(int32_t)*maxCapacity);
michael@0 255 if (newElems == NULL) {
michael@0 256 // Realloc to smaller failed.
michael@0 257 // Just keep what we had. No need to call it a failure.
michael@0 258 return;
michael@0 259 }
michael@0 260 elements = newElems;
michael@0 261 capacity = maxCapacity;
michael@0 262 if (count > capacity) {
michael@0 263 count = capacity;
michael@0 264 }
michael@0 265 }
michael@0 266
michael@0 267 /**
michael@0 268 * Change the size of this vector as follows: If newSize is smaller,
michael@0 269 * then truncate the array, possibly deleting held elements for i >=
michael@0 270 * newSize. If newSize is larger, grow the array, filling in new
michael@0 271 * slots with NULL.
michael@0 272 */
michael@0 273 void UVector32::setSize(int32_t newSize) {
michael@0 274 int32_t i;
michael@0 275 if (newSize < 0) {
michael@0 276 return;
michael@0 277 }
michael@0 278 if (newSize > count) {
michael@0 279 UErrorCode ec = U_ZERO_ERROR;
michael@0 280 if (!ensureCapacity(newSize, ec)) {
michael@0 281 return;
michael@0 282 }
michael@0 283 for (i=count; i<newSize; ++i) {
michael@0 284 elements[i] = 0;
michael@0 285 }
michael@0 286 }
michael@0 287 count = newSize;
michael@0 288 }
michael@0 289
michael@0 290
michael@0 291
michael@0 292
michael@0 293 /**
michael@0 294 * Insert the given integer into this vector at its sorted position
michael@0 295 * as defined by 'compare'. The current elements are assumed to
michael@0 296 * be sorted already.
michael@0 297 */
michael@0 298 void UVector32::sortedInsert(int32_t tok, UErrorCode& ec) {
michael@0 299 // Perform a binary search for the location to insert tok at. Tok
michael@0 300 // will be inserted between two elements a and b such that a <=
michael@0 301 // tok && tok < b, where there is a 'virtual' elements[-1] always
michael@0 302 // less than tok and a 'virtual' elements[count] always greater
michael@0 303 // than tok.
michael@0 304 int32_t min = 0, max = count;
michael@0 305 while (min != max) {
michael@0 306 int32_t probe = (min + max) / 2;
michael@0 307 //int8_t c = (*compare)(elements[probe], tok);
michael@0 308 //if (c > 0) {
michael@0 309 if (elements[probe] > tok) {
michael@0 310 max = probe;
michael@0 311 } else {
michael@0 312 // assert(c <= 0);
michael@0 313 min = probe + 1;
michael@0 314 }
michael@0 315 }
michael@0 316 if (ensureCapacity(count + 1, ec)) {
michael@0 317 for (int32_t i=count; i>min; --i) {
michael@0 318 elements[i] = elements[i-1];
michael@0 319 }
michael@0 320 elements[min] = tok;
michael@0 321 ++count;
michael@0 322 }
michael@0 323 }
michael@0 324
michael@0 325
michael@0 326
michael@0 327
michael@0 328
michael@0 329 U_NAMESPACE_END
michael@0 330

mercurial