// This only does something sensible if this object has a non-null comparer bool UVector::operator==(const UVector& other) const {
U_ASSERT(comparer != nullptr); if (count != other.count) returnfalse; if (comparer != nullptr) { // Compare using this object's comparer for (int32_t i=0; i<count; ++i) { if (!(*comparer)(elements[i], other.elements[i])) { returnfalse;
}
}
} return true;
}
void UVector::adoptElement(void* obj, UErrorCode &status) {
U_ASSERT(deleter != nullptr); if (ensureCapacity(count + 1, status)) {
elements[count++].pointer = obj;
} else {
(*deleter)(obj);
}
} void UVector::addElement(int32_t elem, UErrorCode &status) {
U_ASSERT(deleter == nullptr); // Usage error. Mixing up ints and pointers. if (ensureCapacity(count + 1, status)) {
elements[count].pointer = nullptr; // Pointers may be bigger than ints.
elements[count].integer = elem;
count++;
}
}
void UVector::setElementAt(void* obj, int32_t index) { if (0 <= index && index < count) { if (elements[index].pointer != nullptr && deleter != nullptr) {
(*deleter)(elements[index].pointer);
}
elements[index].pointer = obj;
} else { /* index out of range */ if (deleter != nullptr) {
(*deleter)(obj);
}
}
}
void UVector::setElementAt(int32_t elem, int32_t index) {
U_ASSERT(deleter == nullptr); // Usage error. Mixing up ints and pointers. if (0 <= index && index < count) {
elements[index].pointer = nullptr;
elements[index].integer = elem;
} /* else index out of range */
}
void UVector::insertElementAt(void* obj, int32_t index, UErrorCode &status) { if (ensureCapacity(count + 1, status)) { if (0 <= index && index <= count) { for (int32_t i=count; i>index; --i) {
elements[i] = elements[i-1];
}
elements[index].pointer = obj;
++count;
} else { /* index out of range */
status = U_ILLEGAL_ARGUMENT_ERROR;
}
} if (U_FAILURE(status) && deleter != nullptr) {
(*deleter)(obj);
}
}
void UVector::insertElementAt(int32_t elem, int32_t index, UErrorCode &status) {
U_ASSERT(deleter == nullptr); // Usage error. Mixing up ints and pointers. // must have 0 <= index <= count if (ensureCapacity(count + 1, status)) { if (0 <= index && index <= count) { for (int32_t i=count; i>index; --i) {
elements[i] = elements[i-1];
}
elements[index].pointer = nullptr;
elements[index].integer = elem;
++count;
} else { /* index out of range */
status = U_ILLEGAL_ARGUMENT_ERROR;
}
}
}
void* UVector::elementAt(int32_t index) const { return (0 <= index && index < count) ? elements[index].pointer : nullptr;
}
int32_t UVector::elementAti(int32_t index) const { return (0 <= index && index < count) ? elements[index].integer : 0;
}
int32_t UVector::indexOf(UElement key, int32_t startIndex, int8_t hint) const { if (comparer != nullptr) { for (int32_t i=startIndex; i<count; ++i) { if ((*comparer)(key, elements[i])) { return i;
}
}
} else { for (int32_t i=startIndex; i<count; ++i) { /* Pointers are not always the same size as ints so to perform *avalidcomparisonweneedtoknowwhetherwearebeing
* provided an int or a pointer. */ if (hint & HINT_KEY_POINTER) { if (key.pointer == elements[i].pointer) { return i;
}
} else { if (key.integer == elements[i].integer) { return i;
}
}
}
} return -1;
}
UBool UVector::ensureCapacity(int32_t minimumCapacity, UErrorCode &status) { if (U_FAILURE(status)) { returnfalse;
} if (minimumCapacity < 0) {
status = U_ILLEGAL_ARGUMENT_ERROR; returnfalse;
} if (capacity < minimumCapacity) { if (capacity > (INT32_MAX - 1) / 2) { // integer overflow check
status = U_ILLEGAL_ARGUMENT_ERROR; returnfalse;
}
int32_t newCap = capacity * 2; if (newCap < minimumCapacity) {
newCap = minimumCapacity;
} if (newCap > static_cast<int32_t>(INT32_MAX / sizeof(UElement))) { // integer overflow check // We keep the original memory contents on bad minimumCapacity.
status = U_ILLEGAL_ARGUMENT_ERROR; returnfalse;
}
UElement* newElems = static_cast<UElement*>(uprv_realloc(elements, sizeof(UElement) * newCap)); if (newElems == nullptr) { // We keep the original contents on the memory failure on realloc or bad minimumCapacity.
status = U_MEMORY_ALLOCATION_ERROR; returnfalse;
}
elements = newElems;
capacity = newCap;
} return true;
}
/** *Changethesizeofthisvectorasfollows:IfnewSizeissmaller, *thentruncatethearray,possiblydeletingheldelementsfori>= *newSize.IfnewSizeislarger,growthearray,fillinginnew *slotswithnullptr.
*/ void UVector::setSize(int32_t newSize, UErrorCode &status) { if (!ensureCapacity(newSize, status)) { return;
} if (newSize > count) {
UElement empty;
empty.pointer = nullptr;
empty.integer = 0; for (int32_t i=count; i<newSize; ++i) {
elements[i] = empty;
}
} else { /* Most efficient to count down */ for (int32_t i=count-1; i>=newSize; --i) {
removeElementAt(i);
}
}
count = newSize;
}
// ASSUME elements[] IS CURRENTLY SORTED void UVector::sortedInsert(UElement e, UElementComparator *compare, UErrorCode& ec) { // 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. if (!ensureCapacity(count + 1, ec)) { if (deleter != nullptr) {
(*deleter)(e.pointer);
} return;
}
int32_t min = 0, max = count; while (min != max) {
int32_t probe = (min + max) / 2;
int32_t c = (*compare)(elements[probe], e); if (c > 0) {
max = probe;
} else { // assert(c <= 0);
min = probe + 1;
}
} for (int32_t i=count; i>min; --i) {
elements[i] = elements[i-1];
}
elements[min] = e;
++count;
}
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.3Bemerkung:
(Wie Sie bei der Firma Beratungs- und Dienstleistungen beauftragen können 2026-08-26)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.