uvectr32.cpp revision ac04d0bbe12b3ef54518635711412f178cb4d16
1ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/* 2ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru****************************************************************************** 3ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru* Copyright (C) 1999-2003, International Business Machines Corporation and * 4ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru* others. All Rights Reserved. * 5ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru****************************************************************************** 6ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru* Date Name Description 7ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru* 10/22/99 alan Creation. 8ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru********************************************************************** 9ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*/ 10ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 11ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "uvectr32.h" 12ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "cmemory.h" 13ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 14ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_NAMESPACE_BEGIN 15ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 16ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define DEFUALT_CAPACITY 8 17ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 18ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/* 19ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Constants for hinting whether a key is an integer 20ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * or a pointer. If a hint bit is zero, then the associated 21ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * token is assumed to be an integer. This is needed for iSeries 22ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 23ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 24ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruUOBJECT_DEFINE_RTTI_IMPLEMENTATION(UVector32) 25ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 26ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruUVector32::UVector32(UErrorCode &status) : 27ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru count(0), 28ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru capacity(0), 29ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru elements(NULL) 30ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{ 31ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru _init(DEFUALT_CAPACITY, status); 32ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 33ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 34ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruUVector32::UVector32(int32_t initialCapacity, UErrorCode &status) : 35ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru count(0), 36ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru capacity(0), 37ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru elements(0) 38ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{ 39ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru _init(initialCapacity, status); 40ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 41ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 42ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 43ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 44ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid UVector32::_init(int32_t initialCapacity, UErrorCode &status) { 45ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Fix bogus initialCapacity values; avoid malloc(0) 46ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (initialCapacity < 1) { 47ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru initialCapacity = DEFUALT_CAPACITY; 48ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 49ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru elements = (int32_t *)uprv_malloc(sizeof(int32_t)*initialCapacity); 50ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (elements == 0) { 51ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru status = U_MEMORY_ALLOCATION_ERROR; 52ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 53ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru capacity = initialCapacity; 54ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 55ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 56ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 57ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruUVector32::~UVector32() { 58ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru uprv_free(elements); 59ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru elements = 0; 60ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 61ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 62ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/** 63ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Assign this object to another (make this a copy of 'other'). 64ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 65ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid UVector32::assign(const UVector32& other, UErrorCode &ec) { 66ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (ensureCapacity(other.count, ec)) { 67ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru setSize(other.count); 68ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru for (int32_t i=0; i<other.count; ++i) { 69ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru elements[i] = other.elements[i]; 70ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 71ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 72ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 73ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 74ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 75ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruUBool UVector32::operator==(const UVector32& other) { 76ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t i; 77ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (count != other.count) return FALSE; 78ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru for (i=0; i<count; ++i) { 79ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (elements[i] != other.elements[i]) { 80ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return FALSE; 81ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 82ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 83ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return TRUE; 84ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 85ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 86ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 87ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid UVector32::setElementAt(int32_t elem, int32_t index) { 88ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (0 <= index && index < count) { 89ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru elements[index] = elem; 90ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 91ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* else index out of range */ 92ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 93ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 94ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid UVector32::insertElementAt(int32_t elem, int32_t index, UErrorCode &status) { 95ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // must have 0 <= index <= count 96ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (0 <= index && index <= count && ensureCapacity(count + 1, status)) { 97ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru for (int32_t i=count; i>index; --i) { 98ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru elements[i] = elements[i-1]; 99ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 100ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru elements[index] = elem; 101ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ++count; 102ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 103ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* else index out of range */ 104ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 105ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 106ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruUBool UVector32::containsAll(const UVector32& other) const { 107ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru for (int32_t i=0; i<other.size(); ++i) { 108ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (indexOf(other.elements[i]) < 0) { 109ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return FALSE; 110ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 111ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 112ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return TRUE; 113ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 114ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 115ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruUBool UVector32::containsNone(const UVector32& other) const { 116ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru for (int32_t i=0; i<other.size(); ++i) { 117ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (indexOf(other.elements[i]) >= 0) { 118ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return FALSE; 119ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 120ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 121ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return TRUE; 122ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 123ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 124ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruUBool UVector32::removeAll(const UVector32& other) { 125ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UBool changed = FALSE; 126ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru for (int32_t i=0; i<other.size(); ++i) { 127ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t j = indexOf(other.elements[i]); 128ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (j >= 0) { 129ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru removeElementAt(j); 130ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru changed = TRUE; 131ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 132ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 133ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return changed; 134ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 135ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 136ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruUBool UVector32::retainAll(const UVector32& other) { 137ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UBool changed = FALSE; 138ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru for (int32_t j=size()-1; j>=0; --j) { 139ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t i = other.indexOf(elements[j]); 140ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (i < 0) { 141ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru removeElementAt(j); 142ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru changed = TRUE; 143ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 144ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 145ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return changed; 146ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 147ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 148ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid UVector32::removeElementAt(int32_t index) { 149ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (index >= 0) { 150ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru for (int32_t i=index; i<count-1; ++i) { 151ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru elements[i] = elements[i+1]; 152ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 153ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru --count; 154ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 155ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 156ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 157ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid UVector32::removeAllElements(void) { 158ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru count = 0; 159ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 160ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 161ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruUBool UVector32::equals(const UVector32 &other) const { 162ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int i; 163ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 164ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (this->count != other.count) { 165ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return FALSE; 166ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 167ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru for (i=0; i<count; i++) { 168ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (elements[i] != other.elements[i]) { 169ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return FALSE; 170ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 171ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 172ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return TRUE; 173ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 174ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 175ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 176ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 177ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 178ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruint32_t UVector32::indexOf(int32_t key, int32_t startIndex) const { 179ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t i; 180ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru for (i=startIndex; i<count; ++i) { 181ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (key == elements[i]) { 182ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return i; 183ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 184ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 185ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return -1; 186ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 187ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 188ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 189ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruUBool UVector32::expandCapacity(int32_t minimumCapacity, UErrorCode &status) { 190ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (capacity >= minimumCapacity) { 191ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return TRUE; 192ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 193ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t newCap = capacity * 2; 194ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (newCap < minimumCapacity) { 195ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru newCap = minimumCapacity; 196ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 197ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t* newElems = (int32_t *)uprv_malloc(sizeof(int32_t)*newCap); 198ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (newElems == 0) { 199ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru status = U_MEMORY_ALLOCATION_ERROR; 200ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return FALSE; 201ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 202ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru uprv_memcpy(newElems, elements, sizeof(elements[0]) * count); 203ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru uprv_free(elements); 204ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru elements = newElems; 205ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru capacity = newCap; 206ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return TRUE; 207ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 208ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 209ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 210ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/** 211ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Change the size of this vector as follows: If newSize is smaller, 212ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * then truncate the array, possibly deleting held elements for i >= 213ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * newSize. If newSize is larger, grow the array, filling in new 214ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * slots with NULL. 215ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 216ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid UVector32::setSize(int32_t newSize) { 217ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t i; 218ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (newSize < 0) { 219ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return; 220ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 221ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (newSize > count) { 222ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UErrorCode ec = U_ZERO_ERROR; 223ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (!ensureCapacity(newSize, ec)) { 224ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return; 225ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 226ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru for (i=count; i<newSize; ++i) { 227ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru elements[i] = 0; 228ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 229ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 230ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru count = newSize; 231ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 232ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 233ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 234ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 235ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 236ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/** 237ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Insert the given integer into this vector at its sorted position 238ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * as defined by 'compare'. The current elements are assumed to 239ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * be sorted already. 240ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 241ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid UVector32::sortedInsert(int32_t tok, UErrorCode& ec) { 242ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Perform a binary search for the location to insert tok at. Tok 243ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // will be inserted between two elements a and b such that a <= 244ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // tok && tok < b, where there is a 'virtual' elements[-1] always 245ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // less than tok and a 'virtual' elements[count] always greater 246ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // than tok. 247ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t min = 0, max = count; 248ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru while (min != max) { 249ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t probe = (min + max) / 2; 250ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru //int8_t c = (*compare)(elements[probe], tok); 251ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru //if (c > 0) { 252ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (elements[probe] > tok) { 253ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru max = probe; 254ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 255ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // assert(c <= 0); 256ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru min = probe + 1; 257ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 258ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 259ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (ensureCapacity(count + 1, ec)) { 260ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru for (int32_t i=count; i>min; --i) { 261ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru elements[i] = elements[i-1]; 262ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 263ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru elements[min] = tok; 264ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ++count; 265ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 266ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 267ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 268ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 269ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 270ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 271ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 272ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_NAMESPACE_END 273ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 274