/* ****************************************************************************** * Copyright (C) 1999-2003, International Business Machines Corporation and * * others. All Rights Reserved. * ****************************************************************************** * Date Name Description * 10/22/99 alan Creation. ********************************************************************** */ #include "uvectr32.h" #include "cmemory.h" U_NAMESPACE_BEGIN #define DEFUALT_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(UVector32) UVector32::UVector32(UErrorCode &status) : count(0), capacity(0), elements(NULL) { _init(DEFUALT_CAPACITY, status); } UVector32::UVector32(int32_t initialCapacity, UErrorCode &status) : count(0), capacity(0), elements(0) { _init(initialCapacity, status); } void UVector32::_init(int32_t initialCapacity, UErrorCode &status) { // Fix bogus initialCapacity values; avoid malloc(0) if (initialCapacity < 1) { initialCapacity = DEFUALT_CAPACITY; } elements = (int32_t *)uprv_malloc(sizeof(int32_t)*initialCapacity); if (elements == 0) { status = U_MEMORY_ALLOCATION_ERROR; } else { capacity = initialCapacity; } } UVector32::~UVector32() { uprv_free(elements); elements = 0; } /** * Assign this object to another (make this a copy of 'other'). */ void UVector32::assign(const UVector32& other, UErrorCode &ec) { if (ensureCapacity(other.count, ec)) { setSize(other.count); for (int32_t i=0; iindex; --i) { elements[i] = elements[i-1]; } elements[index] = elem; ++count; } /* else index out of range */ } UBool UVector32::containsAll(const UVector32& other) const { for (int32_t i=0; i= 0) { return FALSE; } } return TRUE; } UBool UVector32::removeAll(const UVector32& other) { UBool changed = FALSE; for (int32_t i=0; i= 0) { removeElementAt(j); changed = TRUE; } } return changed; } UBool UVector32::retainAll(const UVector32& other) { UBool changed = FALSE; for (int32_t j=size()-1; j>=0; --j) { int32_t i = other.indexOf(elements[j]); if (i < 0) { removeElementAt(j); changed = TRUE; } } return changed; } void UVector32::removeElementAt(int32_t index) { if (index >= 0) { for (int32_t i=index; icount != other.count) { return FALSE; } for (i=0; i= minimumCapacity) { return TRUE; } else { int32_t newCap = capacity * 2; if (newCap < minimumCapacity) { newCap = minimumCapacity; } int32_t* newElems = (int32_t *)uprv_malloc(sizeof(int32_t)*newCap); if (newElems == 0) { status = U_MEMORY_ALLOCATION_ERROR; return FALSE; } uprv_memcpy(newElems, elements, sizeof(elements[0]) * count); uprv_free(elements); elements = newElems; capacity = newCap; return TRUE; } } /** * 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 UVector32::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 0) { if (elements[probe] > tok) { max = probe; } else { // assert(c <= 0); min = probe + 1; } } if (ensureCapacity(count + 1, ec)) { for (int32_t i=count; i>min; --i) { elements[i] = elements[i-1]; } elements[min] = tok; ++count; } } U_NAMESPACE_END