X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/729e4ab9bc6618bc3d8a898e575df7f4019e29ca..ef6cf650f4a75c3f97de06b51fa104f2069b9ea2:/icuSources/common/uvector.cpp diff --git a/icuSources/common/uvector.cpp b/icuSources/common/uvector.cpp index d0ecb449..d8a4283d 100644 --- a/icuSources/common/uvector.cpp +++ b/icuSources/common/uvector.cpp @@ -1,7 +1,7 @@ /* ****************************************************************************** -* Copyright (C) 1999-2010, International Business Machines Corporation and * -* others. All Rights Reserved. * +* Copyright (C) 1999-2013, International Business Machines Corporation and +* others. All Rights Reserved. ****************************************************************************** * Date Name Description * 10/22/99 alan Creation. @@ -11,6 +11,7 @@ #include "uvector.h" #include "cmemory.h" #include "uarrsort.h" +#include "uelement.h" U_NAMESPACE_BEGIN @@ -46,7 +47,7 @@ UVector::UVector(int32_t initialCapacity, UErrorCode &status) : _init(initialCapacity, status); } -UVector::UVector(UObjectDeleter *d, UKeyComparator *c, UErrorCode &status) : +UVector::UVector(UObjectDeleter *d, UElementsAreEqual *c, UErrorCode &status) : count(0), capacity(0), elements(0), @@ -56,7 +57,7 @@ UVector::UVector(UObjectDeleter *d, UKeyComparator *c, UErrorCode &status) : _init(DEFAULT_CAPACITY, status); } -UVector::UVector(UObjectDeleter *d, UKeyComparator *c, int32_t initialCapacity, UErrorCode &status) : +UVector::UVector(UObjectDeleter *d, UElementsAreEqual *c, int32_t initialCapacity, UErrorCode &status) : count(0), capacity(0), elements(0), @@ -71,10 +72,10 @@ void UVector::_init(int32_t initialCapacity, UErrorCode &status) { return; } // Fix bogus initialCapacity values; avoid malloc(0) and integer overflow - if ((initialCapacity < 1) || (initialCapacity > (int32_t)(INT32_MAX / sizeof(UHashTok)))) { + if ((initialCapacity < 1) || (initialCapacity > (int32_t)(INT32_MAX / sizeof(UElement)))) { initialCapacity = DEFAULT_CAPACITY; } - elements = (UHashTok *)uprv_malloc(sizeof(UHashTok)*initialCapacity); + elements = (UElement *)uprv_malloc(sizeof(UElement)*initialCapacity); if (elements == 0) { status = U_MEMORY_ALLOCATION_ERROR; } else { @@ -92,7 +93,7 @@ UVector::~UVector() { * 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) { +void UVector::assign(const UVector& other, UElementAssigner *assign, UErrorCode &ec) { if (ensureCapacity(other.count, ec)) { setSize(other.count, ec); if (U_SUCCESS(ec)) { @@ -272,7 +273,7 @@ UBool UVector::equals(const UVector &other) const { } } } else { - UHashTok key; + UElement key; for (i=0; i (int32_t)(INT32_MAX / sizeof(UHashTok))) { // integer overflow check + if (newCap > (int32_t)(INT32_MAX / sizeof(UElement))) { // integer overflow check // We keep the original memory contents on bad minimumCapacity. status = U_ILLEGAL_ARGUMENT_ERROR; return FALSE; } - UHashTok* newElems = (UHashTok *)uprv_realloc(elements, sizeof(UHashTok)*newCap); + UElement* newElems = (UElement *)uprv_realloc(elements, sizeof(UElement)*newCap); if (newElems == NULL) { // We keep the original contents on the memory failure on realloc or bad minimumCapacity. status = U_MEMORY_ALLOCATION_ERROR; @@ -371,7 +372,7 @@ void UVector::setSize(int32_t newSize, UErrorCode &status) { if (!ensureCapacity(newSize, status)) { return; } - UHashTok empty; + UElement empty; empty.pointer = NULL; empty.integer = 0; for (i=count; i 0) { max = probe; } else { @@ -475,7 +476,7 @@ void UVector::sortedInsert(UHashTok tok, USortComparator *compare, UErrorCode& e for (int32_t i=count; i>min; --i) { elements[i] = elements[i-1]; } - elements[min] = tok; + elements[min] = e; ++count; } } @@ -493,10 +494,10 @@ void UVector::sortedInsert(UHashTok tok, USortComparator *compare, UErrorCode& e */ 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); + UElementComparator *compare = *static_cast(context); + UElement e1 = *static_cast(left); + UElement e2 = *static_cast(right); + int32_t result = (*compare)(e1, e2); return result; } @@ -507,22 +508,22 @@ sortComparator(const void *context, const void *left, const void *right) { */ 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; + const UElement *e1 = static_cast(left); + const UElement *e2 = static_cast(right); + int32_t result = e1->integer < e2->integer? -1 : + e1->integer == e2->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 + * not clear whether UVector's UElementComparator or * UComparator from uprv_sortAray would be more appropriate.) */ void UVector::sorti(UErrorCode &ec) { if (U_SUCCESS(ec)) { - uprv_sortArray(elements, count, sizeof(UHashTok), + uprv_sortArray(elements, count, sizeof(UElement), sortiComparator, NULL, FALSE, &ec); } } @@ -542,12 +543,23 @@ void UVector::sorti(UErrorCode &ec) { * 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) { +void UVector::sort(UElementComparator *compare, UErrorCode &ec) { if (U_SUCCESS(ec)) { - uprv_sortArray(elements, count, sizeof(UHashTok), + uprv_sortArray(elements, count, sizeof(UElement), sortComparator, &compare, FALSE, &ec); } } + +/** + * Stable sort with a user supplied comparator of type UComparator. + */ +void UVector::sortWithUComparator(UComparator *compare, const void *context, UErrorCode &ec) { + if (U_SUCCESS(ec)) { + uprv_sortArray(elements, count, sizeof(UElement), + compare, context, TRUE, &ec); + } +} + U_NAMESPACE_END