/* ****************************************************************************** * Copyright (C) 1999-2009, International Business Machines Corporation and * * others. All Rights Reserved. * ****************************************************************************** * Date Name Description * 10/22/99 alan Creation. ********************************************************************** */ #include "uvector.h" #include "cmemory.h" #include "uarrsort.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 */ #define HINT_KEY_POINTER (1) #define HINT_KEY_INTEGER (0) UOBJECT_DEFINE_RTTI_IMPLEMENTATION(UVector) UVector::UVector(UErrorCode &status) : count(0), capacity(0), elements(0), deleter(0), comparer(0) { _init(DEFUALT_CAPACITY, status); } UVector::UVector(int32_t initialCapacity, UErrorCode &status) : count(0), capacity(0), elements(0), deleter(0), comparer(0) { _init(initialCapacity, status); } UVector::UVector(UObjectDeleter *d, UKeyComparator *c, UErrorCode &status) : count(0), capacity(0), elements(0), deleter(d), comparer(c) { _init(DEFUALT_CAPACITY, status); } UVector::UVector(UObjectDeleter *d, UKeyComparator *c, int32_t initialCapacity, UErrorCode &status) : count(0), capacity(0), elements(0), deleter(d), comparer(c) { _init(initialCapacity, status); } void UVector::_init(int32_t initialCapacity, UErrorCode &status) { if (U_FAILURE(status)) { return; } // Fix bogus initialCapacity values; avoid malloc(0) if (initialCapacity < 1) { initialCapacity = DEFUALT_CAPACITY; } elements = (UHashTok *)uprv_malloc(sizeof(UHashTok)*initialCapacity); if (elements == 0) { status = U_MEMORY_ALLOCATION_ERROR; } else { capacity = initialCapacity; } } UVector::~UVector() { removeAllElements(); uprv_free(elements); elements = 0; } /** * Assign this object to another (make this a copy of 'other'). * Use the 'assign' function to assign each element. */ void UVector::assign(const UVector& other, UTokenAssigner *assign, UErrorCode &ec) { if (ensureCapacity(other.count, ec)) { setSize(other.count, ec); if (U_SUCCESS(ec)) { for (int32_t i=0; iindex; --i) { elements[i] = elements[i-1]; } elements[index].pointer = obj; ++count; } /* else index out of range */ } void UVector::insertElementAt(int32_t elem, int32_t index, UErrorCode &status) { // must have 0 <= index <= count if (0 <= index && index <= count && ensureCapacity(count + 1, status)) { for (int32_t i=count; i>index; --i) { elements[i] = elements[i-1]; } elements[index].pointer = NULL; elements[index].integer = elem; ++count; } /* else index out of range */ } void* UVector::elementAt(int32_t index) const { return (0 <= index && index < count) ? elements[index].pointer : 0; } int32_t UVector::elementAti(int32_t index) const { return (0 <= index && index < count) ? elements[index].integer : 0; } UBool UVector::containsAll(const UVector& other) const { for (int32_t i=0; i= 0) { return FALSE; } } return TRUE; } UBool UVector::removeAll(const UVector& other) { UBool changed = FALSE; for (int32_t i=0; i= 0) { removeElementAt(j); changed = TRUE; } } return changed; } UBool UVector::retainAll(const UVector& 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 UVector::removeElementAt(int32_t index) { void* e = orphanElementAt(index); if (e != 0 && deleter != 0) { (*deleter)(e); } } UBool UVector::removeElement(void* obj) { int32_t i = indexOf(obj); if (i >= 0) { removeElementAt(i); return TRUE; } return FALSE; } void UVector::removeAllElements(void) { if (deleter != 0) { for (int32_t i=0; icount != other.count) { return FALSE; } if (comparer == 0) { for (i=0; i= * newSize. If newSize is larger, grow the array, filling in new * slots with NULL. */ void UVector::setSize(int32_t newSize, UErrorCode &status) { int32_t i; if (newSize < 0) { return; } if (newSize > count) { if (!ensureCapacity(newSize, status)) { return; } UHashTok empty; empty.pointer = NULL; empty.integer = 0; for (i=count; i=newSize; --i) { removeElementAt(i); } } count = newSize; } /** * Fill in the given array with all elements of this vector. */ void** UVector::toArray(void** result) const { void** a = result; for (int i=0; i 0) { 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; } } /** * Array sort comparator function. * Used from UVector::sort() * Conforms to function signature required for uprv_sortArray(). * This function is essentially just a wrapper, to make a * UVector style comparator function usable with uprv_sortArray(). * * The context pointer to this function is a pointer back * (with some extra indirection) to the user supplied comparator. * */ static int32_t U_CALLCONV sortComparator(const void *context, const void *left, const void *right) { USortComparator *compare = *static_cast(context); UHashTok tok1 = *static_cast(left); UHashTok tok2 = *static_cast(right); int32_t result = (*compare)(tok1, tok2); return result; } /** * Array sort comparison function for use from UVector::sorti() * Compares int32_t vector elements. */ static int32_t U_CALLCONV sortiComparator(const void * /*context */, const void *left, const void *right) { const UHashTok *tok1 = static_cast(left); const UHashTok *tok2 = static_cast(right); int32_t result = tok1->integer < tok2->integer? -1 : tok1->integer == tok2->integer? 0 : 1; return result; } /** * Sort the vector, assuming it constains ints. * (A more general sort would take a comparison function, but it's * not clear whether UVector's USortComparator or * UComparator from uprv_sortAray would be more appropriate.) */ void UVector::sorti(UErrorCode &ec) { if (U_SUCCESS(ec)) { uprv_sortArray(elements, count, sizeof(UHashTok), sortiComparator, NULL, FALSE, &ec); } } /** * Sort with a user supplied comparator. * * The comparator function handling is confusing because the function type * for UVector (as defined for sortedInsert()) is different from the signature * required by uprv_sortArray(). This is handled by passing the * the UVector sort function pointer via the context pointer to a * sortArray() comparator function, which can then call back to * the original user functtion. * * An additional twist is that it's not safe to pass a pointer-to-function * as a (void *) data pointer, so instead we pass a (data) pointer to a * pointer-to-function variable. */ void UVector::sort(USortComparator *compare, UErrorCode &ec) { if (U_SUCCESS(ec)) { uprv_sortArray(elements, count, sizeof(UHashTok), sortComparator, &compare, FALSE, &ec); } } U_NAMESPACE_END