/* ****************************************************************************** * Copyright (C) 1999-2010, International Business Machines Corporation and * * others. All Rights Reserved. * ****************************************************************************** */ #include "uvectr64.h" #include "cmemory.h" U_NAMESPACE_BEGIN #define DEFAULT_CAPACITY 8 /* * Constants for hinting whether a key is an integer * or a pointer. If a hint bit is zero, then the associated * token is assumed to be an integer. This is needed for iSeries */ UOBJECT_DEFINE_RTTI_IMPLEMENTATION(UVector64) UVector64::UVector64(UErrorCode &status) : count(0), capacity(0), maxCapacity(0), elements(NULL) { _init(DEFAULT_CAPACITY, status); } UVector64::UVector64(int32_t initialCapacity, UErrorCode &status) : count(0), capacity(0), maxCapacity(0), elements(0) { _init(initialCapacity, status); } void UVector64::_init(int32_t initialCapacity, UErrorCode &status) { // Fix bogus initialCapacity values; avoid malloc(0) if (initialCapacity < 1) { initialCapacity = DEFAULT_CAPACITY; } if (maxCapacity>0 && maxCapacityindex; --i) { elements[i] = elements[i-1]; } elements[index] = elem; ++count; } /* else index out of range */ } void UVector64::removeAllElements(void) { count = 0; } UBool UVector64::expandCapacity(int32_t minimumCapacity, UErrorCode &status) { if (capacity >= minimumCapacity) { return TRUE; } if (maxCapacity>0 && minimumCapacity>maxCapacity) { status = U_BUFFER_OVERFLOW_ERROR; return FALSE; } int32_t newCap = capacity * 2; if (newCap < minimumCapacity) { newCap = minimumCapacity; } if (maxCapacity > 0 && newCap > maxCapacity) { newCap = maxCapacity; } int64_t* newElems = (int64_t *)uprv_realloc(elements, sizeof(int64_t)*newCap); if (newElems == NULL) { // We keep the original contents on the memory failure on realloc. status = U_MEMORY_ALLOCATION_ERROR; return FALSE; } elements = newElems; capacity = newCap; return TRUE; } void UVector64::setMaxCapacity(int32_t limit) { U_ASSERT(limit >= 0); maxCapacity = limit; if (maxCapacity < 0) { maxCapacity = 0; } if (capacity <= maxCapacity || maxCapacity == 0) { // Current capacity is within the new limit. return; } // New maximum capacity is smaller than the current size. // Realloc the storage to the new, smaller size. int64_t* newElems = (int64_t *)uprv_realloc(elements, sizeof(int64_t)*maxCapacity); if (newElems == NULL) { // Realloc to smaller failed. // Just keep what we had. No need to call it a failure. return; } elements = newElems; capacity = maxCapacity; if (count > capacity) { count = capacity; } } /** * Change the size of this vector as follows: If newSize is smaller, * then truncate the array, possibly deleting held elements for i >= * newSize. If newSize is larger, grow the array, filling in new * slots with NULL. */ void UVector64::setSize(int32_t newSize) { int32_t i; if (newSize < 0) { return; } if (newSize > count) { UErrorCode ec = U_ZERO_ERROR; if (!ensureCapacity(newSize, ec)) { return; } for (i=count; i