1999-10-27 16:23:58 +00:00
|
|
|
/*
|
2001-03-21 20:44:20 +00:00
|
|
|
******************************************************************************
|
|
|
|
* Copyright (C) 1999-2001, International Business Machines Corporation and *
|
|
|
|
* others. All Rights Reserved. *
|
|
|
|
******************************************************************************
|
1999-10-27 16:23:58 +00:00
|
|
|
* Date Name Description
|
|
|
|
* 10/22/99 alan Creation.
|
|
|
|
**********************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "uvector.h"
|
|
|
|
#include "cmemory.h"
|
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
2001-11-09 02:04:35 +00:00
|
|
|
#define DEFUALT_CAPACITY 8
|
|
|
|
|
2002-07-17 19:44:00 +00:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
2002-07-18 16:46:30 +00:00
|
|
|
#define HINT_KEY_POINTER (1)
|
|
|
|
#define HINT_KEY_INTEGER (0)
|
2002-07-17 19:44:00 +00:00
|
|
|
|
2002-06-29 00:04:16 +00:00
|
|
|
const char UVector::fgClassID=0;
|
|
|
|
|
2001-11-09 02:04:35 +00:00
|
|
|
UVector::UVector(UErrorCode &status) :
|
1999-10-27 16:23:58 +00:00
|
|
|
count(0),
|
2000-08-14 23:23:20 +00:00
|
|
|
capacity(0),
|
1999-10-27 16:23:58 +00:00
|
|
|
elements(0),
|
|
|
|
deleter(0),
|
2001-11-09 02:04:35 +00:00
|
|
|
comparer(0)
|
|
|
|
{
|
|
|
|
_init(DEFUALT_CAPACITY, status);
|
|
|
|
}
|
1999-10-27 16:23:58 +00:00
|
|
|
|
2001-11-09 02:04:35 +00:00
|
|
|
UVector::UVector(int32_t initialCapacity, UErrorCode &status) :
|
|
|
|
count(0),
|
|
|
|
capacity(0),
|
|
|
|
elements(0),
|
|
|
|
deleter(0),
|
|
|
|
comparer(0)
|
|
|
|
{
|
2001-08-23 01:06:08 +00:00
|
|
|
_init(initialCapacity, status);
|
1999-10-27 16:23:58 +00:00
|
|
|
}
|
|
|
|
|
2002-07-12 00:23:52 +00:00
|
|
|
UVector::UVector(UObjectDeleter *d, UKeyComparator *c, UErrorCode &status) :
|
2001-11-09 02:04:35 +00:00
|
|
|
count(0),
|
|
|
|
capacity(0),
|
|
|
|
elements(0),
|
|
|
|
deleter(d),
|
|
|
|
comparer(c)
|
|
|
|
{
|
|
|
|
_init(DEFUALT_CAPACITY, status);
|
|
|
|
}
|
|
|
|
|
2002-07-12 00:23:52 +00:00
|
|
|
UVector::UVector(UObjectDeleter *d, UKeyComparator *c, int32_t initialCapacity, UErrorCode &status) :
|
1999-10-27 16:23:58 +00:00
|
|
|
count(0),
|
2000-08-14 23:23:20 +00:00
|
|
|
capacity(0),
|
1999-10-27 16:23:58 +00:00
|
|
|
elements(0),
|
|
|
|
deleter(d),
|
2001-11-09 02:04:35 +00:00
|
|
|
comparer(c)
|
|
|
|
{
|
2001-08-23 01:06:08 +00:00
|
|
|
_init(initialCapacity, status);
|
1999-10-27 16:23:58 +00:00
|
|
|
}
|
|
|
|
|
2001-08-23 01:06:08 +00:00
|
|
|
void UVector::_init(int32_t initialCapacity, UErrorCode &status) {
|
2002-03-18 21:53:11 +00:00
|
|
|
// Fix bogus initialCapacity values; avoid malloc(0)
|
|
|
|
if (initialCapacity < 1) {
|
|
|
|
initialCapacity = DEFUALT_CAPACITY;
|
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
elements = (UHashTok *)uprv_malloc(sizeof(UHashTok)*initialCapacity);
|
1999-10-27 16:23:58 +00:00
|
|
|
if (elements == 0) {
|
2001-08-23 01:06:08 +00:00
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
1999-10-27 16:23:58 +00:00
|
|
|
} else {
|
|
|
|
capacity = initialCapacity;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UVector::~UVector() {
|
|
|
|
removeAllElements();
|
2002-02-28 01:42:40 +00:00
|
|
|
uprv_free(elements);
|
1999-10-27 16:23:58 +00:00
|
|
|
elements = 0;
|
|
|
|
}
|
|
|
|
|
2002-04-26 06:30:37 +00:00
|
|
|
/**
|
|
|
|
* Assign this object to another (make this a copy of 'other').
|
|
|
|
* Use the 'assign' function to assign each element.
|
|
|
|
*/
|
2002-07-23 23:12:03 +00:00
|
|
|
void UVector::assign(const UVector& other, UTokenAssigner *assign, UErrorCode &ec) {
|
2002-04-26 06:30:37 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
(*assign)(elements[i], other.elements[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This only does something sensible if this object has a non-null comparer
|
|
|
|
UBool UVector::operator==(const UVector& other) {
|
|
|
|
int32_t i;
|
|
|
|
if (count != other.count) return FALSE;
|
|
|
|
if (comparer != NULL) {
|
|
|
|
// Compare using this object's comparer
|
|
|
|
for (i=0; i<count; ++i) {
|
|
|
|
if (!(*comparer)(elements[i], other.elements[i])) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2001-08-23 01:06:08 +00:00
|
|
|
void UVector::addElement(void* obj, UErrorCode &status) {
|
|
|
|
if (ensureCapacity(count + 1, status)) {
|
2001-07-31 18:10:53 +00:00
|
|
|
elements[count++].pointer = obj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-08-23 01:06:08 +00:00
|
|
|
void UVector::addElement(int32_t elem, UErrorCode &status) {
|
|
|
|
if (ensureCapacity(count + 1, status)) {
|
2002-06-25 17:23:07 +00:00
|
|
|
elements[count].pointer = NULL; // Pointers may be bigger than ints.
|
|
|
|
elements[count].integer = elem;
|
|
|
|
count++;
|
1999-10-27 16:23:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UVector::setElementAt(void* obj, int32_t index) {
|
|
|
|
if (0 <= index && index < count) {
|
2001-07-31 18:10:53 +00:00
|
|
|
if (elements[index].pointer != 0 && deleter != 0) {
|
|
|
|
(*deleter)(elements[index].pointer);
|
1999-10-27 16:23:58 +00:00
|
|
|
}
|
2001-07-31 18:10:53 +00:00
|
|
|
elements[index].pointer = obj;
|
|
|
|
}
|
|
|
|
/* else index out of range */
|
|
|
|
}
|
|
|
|
|
|
|
|
void UVector::setElementAt(int32_t elem, int32_t index) {
|
|
|
|
if (0 <= index && index < count) {
|
|
|
|
if (elements[index].pointer != 0 && deleter != 0) {
|
2002-06-25 17:23:07 +00:00
|
|
|
// TODO: this should be an error. mixing up ints and pointers.
|
2001-07-31 18:10:53 +00:00
|
|
|
(*deleter)(elements[index].pointer);
|
|
|
|
}
|
2002-06-25 17:23:07 +00:00
|
|
|
elements[index].pointer = NULL;
|
2001-07-31 18:10:53 +00:00
|
|
|
elements[index].integer = elem;
|
1999-10-27 16:23:58 +00:00
|
|
|
}
|
|
|
|
/* else index out of range */
|
|
|
|
}
|
|
|
|
|
2001-08-23 01:06:08 +00:00
|
|
|
void UVector::insertElementAt(void* obj, int32_t index, UErrorCode &status) {
|
1999-10-27 16:23:58 +00:00
|
|
|
// must have 0 <= index <= count
|
2001-08-23 01:06:08 +00:00
|
|
|
if (0 <= index && index <= count && ensureCapacity(count + 1, status)) {
|
1999-10-27 16:23:58 +00:00
|
|
|
for (int32_t i=count; i>index; --i) {
|
|
|
|
elements[i] = elements[i-1];
|
|
|
|
}
|
2001-07-31 18:10:53 +00:00
|
|
|
elements[index].pointer = obj;
|
2002-02-28 01:42:40 +00:00
|
|
|
++count;
|
1999-10-27 16:23:58 +00:00
|
|
|
}
|
|
|
|
/* else index out of range */
|
|
|
|
}
|
|
|
|
|
2002-07-22 22:02:08 +00:00
|
|
|
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 */
|
|
|
|
}
|
|
|
|
|
1999-10-27 16:23:58 +00:00
|
|
|
void* UVector::elementAt(int32_t index) const {
|
2001-07-31 18:10:53 +00:00
|
|
|
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;
|
1999-10-27 16:23:58 +00:00
|
|
|
}
|
|
|
|
|
2002-04-26 06:30:37 +00:00
|
|
|
UBool UVector::containsAll(const UVector& other) const {
|
|
|
|
for (int32_t i=0; i<other.size(); ++i) {
|
|
|
|
if (indexOf(other.elements[i]) < 0) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
UBool UVector::containsNone(const UVector& other) const {
|
|
|
|
for (int32_t i=0; i<other.size(); ++i) {
|
|
|
|
if (indexOf(other.elements[i]) >= 0) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
UBool UVector::removeAll(const UVector& other) {
|
|
|
|
UBool changed = FALSE;
|
|
|
|
for (int32_t i=0; i<other.size(); ++i) {
|
|
|
|
int32_t j = indexOf(other.elements[i]);
|
|
|
|
if (j >= 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;
|
|
|
|
}
|
|
|
|
|
1999-10-27 16:23:58 +00:00
|
|
|
void UVector::removeElementAt(int32_t index) {
|
1999-11-16 17:13:02 +00:00
|
|
|
void* e = orphanElementAt(index);
|
|
|
|
if (e != 0 && deleter != 0) {
|
|
|
|
(*deleter)(e);
|
1999-10-27 16:23:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool UVector::removeElement(void* obj) {
|
1999-10-27 16:23:58 +00:00
|
|
|
int32_t i = indexOf(obj);
|
|
|
|
if (i >= 0) {
|
|
|
|
removeElementAt(i);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
1999-12-22 22:57:04 +00:00
|
|
|
void UVector::removeAllElements(void) {
|
1999-10-27 16:23:58 +00:00
|
|
|
if (deleter != 0) {
|
|
|
|
for (int32_t i=0; i<count; ++i) {
|
2001-07-31 18:10:53 +00:00
|
|
|
if (elements[i].pointer != 0) {
|
|
|
|
(*deleter)(elements[i].pointer);
|
1999-10-27 16:23:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
count = 0;
|
|
|
|
}
|
|
|
|
|
2002-06-25 17:23:07 +00:00
|
|
|
UBool UVector::equals(const UVector &other) const {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (this->count != other.count) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (comparer == 0) {
|
|
|
|
for (i=0; i<count; i++) {
|
|
|
|
if (elements[i].pointer != other.elements[i].pointer) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
UHashTok key;
|
|
|
|
for (i=0; i<count; i++) {
|
|
|
|
key.pointer = &other.elements[i];
|
|
|
|
if (!(*comparer)(key, elements[i])) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
1999-10-27 16:23:58 +00:00
|
|
|
int32_t UVector::indexOf(void* obj, int32_t startIndex) const {
|
2002-04-26 06:30:37 +00:00
|
|
|
UHashTok key;
|
|
|
|
key.pointer = obj;
|
2002-07-17 19:44:00 +00:00
|
|
|
return indexOf(key, startIndex, HINT_KEY_POINTER);
|
1999-10-27 16:23:58 +00:00
|
|
|
}
|
|
|
|
|
2002-03-29 01:56:09 +00:00
|
|
|
int32_t UVector::indexOf(int32_t obj, int32_t startIndex) const {
|
2002-04-26 06:30:37 +00:00
|
|
|
UHashTok key;
|
|
|
|
key.integer = obj;
|
2002-07-17 19:44:00 +00:00
|
|
|
return indexOf(key, startIndex, HINT_KEY_INTEGER);
|
2002-04-26 06:30:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This only works if this object has a non-null comparer
|
2002-07-17 19:44:00 +00:00
|
|
|
int32_t UVector::indexOf(UHashTok key, int32_t startIndex, int8_t hint) const {
|
2002-04-26 06:30:37 +00:00
|
|
|
int32_t i;
|
2002-03-29 01:56:09 +00:00
|
|
|
if (comparer != 0) {
|
2002-04-26 06:30:37 +00:00
|
|
|
for (i=startIndex; i<count; ++i) {
|
2002-03-29 01:56:09 +00:00
|
|
|
if ((*comparer)(key, elements[i])) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
2002-06-25 17:23:07 +00:00
|
|
|
} else {
|
|
|
|
for (i=startIndex; i<count; ++i) {
|
2002-07-17 19:44:00 +00:00
|
|
|
/* Pointers are not always the same size as ints so to perform
|
|
|
|
* a valid comparision we need to know whether we are being
|
|
|
|
* provided an int or a pointer. */
|
2002-07-18 16:46:30 +00:00
|
|
|
if (hint & HINT_KEY_POINTER) {
|
|
|
|
if (key.pointer == elements[i].pointer) {
|
2002-07-17 19:44:00 +00:00
|
|
|
return i;
|
2002-07-18 16:46:30 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (key.integer == elements[i].integer) {
|
|
|
|
return i;
|
|
|
|
}
|
2002-06-25 17:23:07 +00:00
|
|
|
}
|
|
|
|
}
|
2002-03-29 01:56:09 +00:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-08-23 01:06:08 +00:00
|
|
|
UBool UVector::ensureCapacity(int32_t minimumCapacity, UErrorCode &status) {
|
1999-10-27 16:23:58 +00:00
|
|
|
if (capacity >= minimumCapacity) {
|
|
|
|
return TRUE;
|
|
|
|
} else {
|
|
|
|
int32_t newCap = capacity * 2;
|
2001-10-30 19:44:11 +00:00
|
|
|
if (newCap < minimumCapacity) {
|
|
|
|
newCap = minimumCapacity;
|
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
UHashTok* newElems = (UHashTok *)uprv_malloc(sizeof(UHashTok)*newCap);
|
1999-10-27 16:23:58 +00:00
|
|
|
if (newElems == 0) {
|
2001-08-23 01:06:08 +00:00
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
1999-10-27 16:23:58 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2001-07-31 18:10:53 +00:00
|
|
|
uprv_memcpy(newElems, elements, sizeof(elements[0]) * count);
|
2002-03-01 23:26:35 +00:00
|
|
|
uprv_free(elements);
|
1999-10-27 16:23:58 +00:00
|
|
|
elements = newElems;
|
|
|
|
capacity = newCap;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-10-30 19:44:11 +00:00
|
|
|
/**
|
|
|
|
* 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
|
2002-02-07 01:07:55 +00:00
|
|
|
* slots with NULL.
|
2001-10-30 19:44:11 +00:00
|
|
|
*/
|
|
|
|
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) {
|
|
|
|
elements[i] = empty;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Most efficient to count down */
|
|
|
|
for (i=count-1; 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<count; ++i) {
|
|
|
|
*a++ = elements[i].pointer;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2002-07-12 00:23:52 +00:00
|
|
|
UObjectDeleter *UVector::setDeleter(UObjectDeleter *d) {
|
|
|
|
UObjectDeleter *old = deleter;
|
1999-10-27 16:23:58 +00:00
|
|
|
deleter = d;
|
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
2002-07-12 00:23:52 +00:00
|
|
|
UKeyComparator *UVector::setComparer(UKeyComparator *d) {
|
|
|
|
UKeyComparator *old = comparer;
|
1999-10-27 16:23:58 +00:00
|
|
|
comparer = d;
|
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
1999-11-16 17:13:02 +00:00
|
|
|
/**
|
|
|
|
* Removes the element at the given index from this vector and
|
|
|
|
* transfer ownership of it to the caller. After this call, the
|
|
|
|
* caller owns the result and must delete it and the vector entry
|
|
|
|
* at 'index' is removed, shifting all subsequent entries back by
|
|
|
|
* one index and shortening the size of the vector by one. If the
|
|
|
|
* index is out of range or if there is no item at the given index
|
|
|
|
* then 0 is returned and the vector is unchanged.
|
|
|
|
*/
|
|
|
|
void* UVector::orphanElementAt(int32_t index) {
|
|
|
|
void* e = 0;
|
|
|
|
if (0 <= index && index < count) {
|
2001-07-31 18:10:53 +00:00
|
|
|
e = elements[index].pointer;
|
2000-06-15 22:32:39 +00:00
|
|
|
for (int32_t i=index; i<count-1; ++i) {
|
1999-11-16 17:13:02 +00:00
|
|
|
elements[i] = elements[i+1];
|
|
|
|
}
|
2000-08-11 21:24:17 +00:00
|
|
|
--count;
|
1999-11-16 17:13:02 +00:00
|
|
|
}
|
|
|
|
/* else index out of range */
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2002-04-26 06:30:37 +00:00
|
|
|
/**
|
|
|
|
* Insert the given object into this vector at its sorted position
|
|
|
|
* as defined by 'compare'. The current elements are assumed to
|
|
|
|
* be sorted already.
|
|
|
|
*/
|
2002-07-23 23:12:03 +00:00
|
|
|
void UVector::sortedInsert(void* obj, USortComparator *compare, UErrorCode& ec) {
|
2002-04-26 06:30:37 +00:00
|
|
|
UHashTok tok;
|
|
|
|
tok.pointer = obj;
|
|
|
|
sortedInsert(tok, compare, ec);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert the given integer into this vector at its sorted position
|
|
|
|
* as defined by 'compare'. The current elements are assumed to
|
|
|
|
* be sorted already.
|
|
|
|
*/
|
2002-07-23 23:12:03 +00:00
|
|
|
void UVector::sortedInsert(int32_t obj, USortComparator *compare, UErrorCode& ec) {
|
2002-04-26 06:30:37 +00:00
|
|
|
UHashTok tok;
|
|
|
|
tok.integer = obj;
|
|
|
|
sortedInsert(tok, compare, ec);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ASSUME elements[] IS CURRENTLY SORTED
|
2002-07-23 23:12:03 +00:00
|
|
|
void UVector::sortedInsert(UHashTok tok, USortComparator *compare, UErrorCode& ec) {
|
2002-04-26 06:30:37 +00:00
|
|
|
// Perform a binary search for the location to insert tok at. Tok
|
|
|
|
// will be inserted between two elements a and b such that a <=
|
|
|
|
// tok && tok < b, where there is a 'virtual' elements[-1] always
|
|
|
|
// less than tok and a 'virtual' elements[count] always greater
|
|
|
|
// than tok.
|
|
|
|
int32_t min = 0, max = count;
|
|
|
|
while (min != max) {
|
|
|
|
int32_t probe = (min + max) / 2;
|
|
|
|
int8_t c = (*compare)(elements[probe], tok);
|
|
|
|
if (c > 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-29 00:04:16 +00:00
|
|
|
const char UStack::fgClassID=0;
|
|
|
|
|
2001-11-09 02:04:35 +00:00
|
|
|
UStack::UStack(UErrorCode &status) :
|
|
|
|
UVector(status)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
UStack::UStack(int32_t initialCapacity, UErrorCode &status) :
|
|
|
|
UVector(initialCapacity, status)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2002-07-12 00:23:52 +00:00
|
|
|
UStack::UStack(UObjectDeleter *d, UKeyComparator *c, UErrorCode &status) :
|
2001-11-09 02:04:35 +00:00
|
|
|
UVector(d, c, status)
|
|
|
|
{
|
1999-10-27 16:23:58 +00:00
|
|
|
}
|
|
|
|
|
2002-07-12 00:23:52 +00:00
|
|
|
UStack::UStack(UObjectDeleter *d, UKeyComparator *c, int32_t initialCapacity, UErrorCode &status) :
|
2001-11-09 02:04:35 +00:00
|
|
|
UVector(d, c, initialCapacity, status)
|
|
|
|
{
|
1999-10-27 16:23:58 +00:00
|
|
|
}
|
|
|
|
|
1999-12-22 22:57:04 +00:00
|
|
|
void* UStack::pop(void) {
|
2001-07-31 18:10:53 +00:00
|
|
|
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);
|
1999-10-27 16:23:58 +00:00
|
|
|
}
|
2001-07-31 18:10:53 +00:00
|
|
|
return result;
|
1999-10-27 16:23:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t UStack::search(void* obj) const {
|
|
|
|
int32_t i = indexOf(obj);
|
|
|
|
return (i >= 0) ? size() - i : i;
|
|
|
|
}
|
2001-10-08 23:26:58 +00:00
|
|
|
|
|
|
|
U_NAMESPACE_END
|
|
|
|
|