/* ****************************************************************************** * Copyright (C) 1999-2001, International Business Machines Corporation and * * others. All Rights Reserved. * ****************************************************************************** * Date Name Description * 10/22/99 alan Creation. ********************************************************************** */ #include "uvector.h" #include "cmemory.h" U_NAMESPACE_BEGIN #define DEFUALT_CAPACITY 8 const char UVector::fgClassID=0; 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) { // 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); for (int32_t i=0; iindex; --i) { elements[i] = elements[i-1]; } elements[index].pointer = obj; ++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= minimumCapacity) { return TRUE; } else { int32_t newCap = capacity * 2; if (newCap < minimumCapacity) { newCap = minimumCapacity; } UHashTok* newElems = (UHashTok *)uprv_malloc(sizeof(UHashTok)*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 UVector::setSize(int32_t newSize) { int32_t i; if (newSize < 0) { return; } if (newSize > count) { UErrorCode ec = U_ZERO_ERROR; if (!ensureCapacity(newSize, ec)) { 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; } } 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