]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/common/uvector.cpp
ICU-400.38.tar.gz
[apple/icu.git] / icuSources / common / uvector.cpp
index 8da159924fcd79ba2ff860f4885e9690c14c7c64..2c9efbc20845c7a667e2a41671da69687897c75b 100644 (file)
@@ -1,6 +1,6 @@
 /*
 ******************************************************************************
-* Copyright (C) 1999-2001, International Business Machines Corporation and   *
+* Copyright (C) 1999-2004, International Business Machines Corporation and   *
 * others. All Rights Reserved.                                               *
 ******************************************************************************
 *   Date        Name        Description
@@ -23,7 +23,7 @@ U_NAMESPACE_BEGIN
 #define HINT_KEY_POINTER   (1)
 #define HINT_KEY_INTEGER   (0)
  
-const char UVector::fgClassID=0;
+UOBJECT_DEFINE_RTTI_IMPLEMENTATION(UVector)
 
 UVector::UVector(UErrorCode &status) :
     count(0),
@@ -66,6 +66,9 @@ UVector::UVector(UObjectDeleter *d, UKeyComparator *c, int32_t initialCapacity,
 }
 
 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;
@@ -90,12 +93,14 @@ UVector::~UVector() {
  */
 void UVector::assign(const UVector& other, UTokenAssigner *assign, UErrorCode &ec) {
     if (ensureCapacity(other.count, ec)) {
-        setSize(other.count);
-        for (int32_t i=0; i<other.count; ++i) {
-            if (elements[i].pointer != 0 && deleter != 0) {
-                (*deleter)(elements[i].pointer);
+        setSize(other.count, ec);
+        if (U_SUCCESS(ec)) {
+            for (int32_t i=0; i<other.count; ++i) {
+                if (elements[i].pointer != 0 && deleter != 0) {
+                    (*deleter)(elements[i].pointer);
+                }
+                (*assign)(&elements[i], &other.elements[i]);
             }
-            (*assign)(&elements[i], &other.elements[i]);
         }
     }
 }
@@ -320,24 +325,21 @@ int32_t UVector::indexOf(UHashTok key, int32_t startIndex, int8_t hint) const {
 }
 
 UBool UVector::ensureCapacity(int32_t minimumCapacity, UErrorCode &status) {
-    if (capacity >= minimumCapacity) {
-        return TRUE;
-    } else {
+    if (capacity < minimumCapacity) {
         int32_t newCap = capacity * 2;
         if (newCap < minimumCapacity) {
             newCap = minimumCapacity;
         }
-        UHashTok* newElems = (UHashTok *)uprv_malloc(sizeof(UHashTok)*newCap);
-        if (newElems == 0) {
+        UHashTok* newElems = (UHashTok *)uprv_realloc(elements, sizeof(UHashTok)*newCap);
+        if (newElems == NULL) {
+            // We keep the original contents on the memory failure on realloc.
             status = U_MEMORY_ALLOCATION_ERROR;
             return FALSE;
         }
-        uprv_memcpy(newElems, elements, sizeof(elements[0]) * count);
-        uprv_free(elements);
         elements = newElems;
         capacity = newCap;
-        return TRUE;
     }
+    return TRUE;
 }
 
 /**
@@ -346,14 +348,13 @@ UBool UVector::ensureCapacity(int32_t minimumCapacity, UErrorCode &status) {
  * newSize.  If newSize is larger, grow the array, filling in new
  * slots with NULL.
  */
-void UVector::setSize(int32_t newSize) {
+void UVector::setSize(int32_t newSize, UErrorCode &status) {
     int32_t i;
     if (newSize < 0) {
         return;
     }
     if (newSize > count) {
-        UErrorCode ec = U_ZERO_ERROR;
-        if (!ensureCapacity(newSize, ec)) {
+        if (!ensureCapacity(newSize, status)) {
             return;
         }
         UHashTok empty;
@@ -465,52 +466,5 @@ void UVector::sortedInsert(UHashTok tok, USortComparator *compare, UErrorCode& e
     }
 }
 
-const char UStack::fgClassID=0;
-
-UStack::UStack(UErrorCode &status) :
-    UVector(status)
-{
-}
-
-UStack::UStack(int32_t initialCapacity, UErrorCode &status) :
-    UVector(initialCapacity, status)
-{
-}
-
-UStack::UStack(UObjectDeleter *d, UKeyComparator *c, UErrorCode &status) :
-    UVector(d, c, status)
-{
-}
-
-UStack::UStack(UObjectDeleter *d, UKeyComparator *c, int32_t initialCapacity, UErrorCode &status) :
-    UVector(d, c, initialCapacity, status)
-{
-}
-
-void* UStack::pop(void) {
-    int32_t n = size() - 1;
-    void* result = 0;
-    if (n >= 0) {
-        result = elementAt(n);
-        removeElementAt(n);
-    }
-    return result;
-}
-
-int32_t UStack::popi(void) {
-    int32_t n = size() - 1;
-    int32_t result = 0;
-    if (n >= 0) {
-        result = elementAti(n);
-        removeElementAt(n);
-    }
-    return result;
-}
-
-int32_t UStack::search(void* obj) const {
-    int32_t i = indexOf(obj);
-    return (i >= 0) ? size() - i : i;
-}
-
 U_NAMESPACE_END