1ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/*
2ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru******************************************************************************
327f654740f2a26ad62a5c155af9199af9e69b889claireho* Copyright (C) 1999-2010, 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"
1327f654740f2a26ad62a5c155af9199af9e69b889claireho#include "putilimp.h"
14ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
15ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_NAMESPACE_BEGIN
16ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
1727f654740f2a26ad62a5c155af9199af9e69b889claireho#define DEFAULT_CAPACITY 8
18ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
19ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/*
20ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Constants for hinting whether a key is an integer
21ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * or a pointer.  If a hint bit is zero, then the associated
22ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * token is assumed to be an integer. This is needed for iSeries
23ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */
24ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
25ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruUOBJECT_DEFINE_RTTI_IMPLEMENTATION(UVector32)
26ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
27ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruUVector32::UVector32(UErrorCode &status) :
28ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    count(0),
29ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    capacity(0),
3085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    maxCapacity(0),
31ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    elements(NULL)
32ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{
3327f654740f2a26ad62a5c155af9199af9e69b889claireho    _init(DEFAULT_CAPACITY, status);
34ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
35ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
36ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruUVector32::UVector32(int32_t initialCapacity, UErrorCode &status) :
37ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    count(0),
38ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    capacity(0),
3985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    maxCapacity(0),
40ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    elements(0)
41ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{
42ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    _init(initialCapacity, status);
43ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
44ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
45ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
46ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
47ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid UVector32::_init(int32_t initialCapacity, UErrorCode &status) {
48ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    // Fix bogus initialCapacity values; avoid malloc(0)
49ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if (initialCapacity < 1) {
5027f654740f2a26ad62a5c155af9199af9e69b889claireho        initialCapacity = DEFAULT_CAPACITY;
51ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
5285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    if (maxCapacity>0 && maxCapacity<initialCapacity) {
5385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        initialCapacity = maxCapacity;
5485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    }
5527f654740f2a26ad62a5c155af9199af9e69b889claireho    if (initialCapacity > (int32_t)(INT32_MAX / sizeof(int32_t))) {
5627f654740f2a26ad62a5c155af9199af9e69b889claireho        initialCapacity = uprv_min(DEFAULT_CAPACITY, maxCapacity);
5727f654740f2a26ad62a5c155af9199af9e69b889claireho    }
58ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    elements = (int32_t *)uprv_malloc(sizeof(int32_t)*initialCapacity);
59ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if (elements == 0) {
60ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        status = U_MEMORY_ALLOCATION_ERROR;
61ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    } else {
62ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        capacity = initialCapacity;
63ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
64ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
65ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
66ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruUVector32::~UVector32() {
67ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    uprv_free(elements);
68ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    elements = 0;
69ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
70ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
71ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/**
72ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Assign this object to another (make this a copy of 'other').
73ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */
74ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid UVector32::assign(const UVector32& other, UErrorCode &ec) {
75ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if (ensureCapacity(other.count, ec)) {
76ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        setSize(other.count);
77ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        for (int32_t i=0; i<other.count; ++i) {
78ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            elements[i] = other.elements[i];
79ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        }
80ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
81ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
82ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
83ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
84ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruUBool UVector32::operator==(const UVector32& other) {
85ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int32_t i;
86ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if (count != other.count) return FALSE;
87ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    for (i=0; i<count; ++i) {
88ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        if (elements[i] != other.elements[i]) {
89ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            return FALSE;
90ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        }
91ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
92ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return TRUE;
93ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
94ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
95ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
96ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid UVector32::setElementAt(int32_t elem, int32_t index) {
97ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if (0 <= index && index < count) {
98ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        elements[index] = elem;
99ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
100ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    /* else index out of range */
101ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
102ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
103ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid UVector32::insertElementAt(int32_t elem, int32_t index, UErrorCode &status) {
104ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    // must have 0 <= index <= count
105ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if (0 <= index && index <= count && ensureCapacity(count + 1, status)) {
106ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        for (int32_t i=count; i>index; --i) {
107ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            elements[i] = elements[i-1];
108ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        }
109ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        elements[index] = elem;
110ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        ++count;
111ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
112ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    /* else index out of range */
113ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
114ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
115ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruUBool UVector32::containsAll(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::containsNone(const UVector32& other) const {
125ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    for (int32_t i=0; i<other.size(); ++i) {
126ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        if (indexOf(other.elements[i]) >= 0) {
127ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            return FALSE;
128ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        }
129ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
130ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return TRUE;
131ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
132ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
133ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruUBool UVector32::removeAll(const UVector32& other) {
134ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    UBool changed = FALSE;
135ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    for (int32_t i=0; i<other.size(); ++i) {
136ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        int32_t j = indexOf(other.elements[i]);
137ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        if (j >= 0) {
138ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            removeElementAt(j);
139ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            changed = TRUE;
140ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        }
141ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
142ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return changed;
143ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
144ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
145ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruUBool UVector32::retainAll(const UVector32& other) {
146ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    UBool changed = FALSE;
147ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    for (int32_t j=size()-1; j>=0; --j) {
148ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        int32_t i = other.indexOf(elements[j]);
149ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        if (i < 0) {
150ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            removeElementAt(j);
151ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            changed = TRUE;
152ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        }
153ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
154ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return changed;
155ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
156ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
157ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid UVector32::removeElementAt(int32_t index) {
158ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if (index >= 0) {
159ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        for (int32_t i=index; i<count-1; ++i) {
160ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            elements[i] = elements[i+1];
161ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        }
162ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        --count;
163ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
164ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
165ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
166ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid UVector32::removeAllElements(void) {
167ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    count = 0;
168ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
169ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
170ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruUBool   UVector32::equals(const UVector32 &other) const {
171ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int      i;
172ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
173ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if (this->count != other.count) {
174ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return FALSE;
175ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
176ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    for (i=0; i<count; i++) {
177ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        if (elements[i] != other.elements[i]) {
178ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            return FALSE;
179ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        }
180ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
181ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return TRUE;
182ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
183ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
184ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
185ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
186ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
187ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruint32_t UVector32::indexOf(int32_t key, int32_t startIndex) const {
188ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int32_t i;
189ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    for (i=startIndex; i<count; ++i) {
190ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        if (key == elements[i]) {
191ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            return i;
192ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        }
193ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
194ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return -1;
195ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
196ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
197ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
198ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruUBool UVector32::expandCapacity(int32_t minimumCapacity, UErrorCode &status) {
19927f654740f2a26ad62a5c155af9199af9e69b889claireho    if (minimumCapacity < 0) {
20027f654740f2a26ad62a5c155af9199af9e69b889claireho        status = U_ILLEGAL_ARGUMENT_ERROR;
20127f654740f2a26ad62a5c155af9199af9e69b889claireho        return FALSE;
20227f654740f2a26ad62a5c155af9199af9e69b889claireho    }
203ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if (capacity >= minimumCapacity) {
204ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return TRUE;
20585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    }
20685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    if (maxCapacity>0 && minimumCapacity>maxCapacity) {
20785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        status = U_BUFFER_OVERFLOW_ERROR;
20885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        return FALSE;
20985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    }
21027f654740f2a26ad62a5c155af9199af9e69b889claireho    if (capacity > (INT32_MAX - 1) / 2) {  // integer overflow check
21127f654740f2a26ad62a5c155af9199af9e69b889claireho        status = U_ILLEGAL_ARGUMENT_ERROR;
21227f654740f2a26ad62a5c155af9199af9e69b889claireho        return FALSE;
21327f654740f2a26ad62a5c155af9199af9e69b889claireho    }
21485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    int32_t newCap = capacity * 2;
21585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    if (newCap < minimumCapacity) {
21685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        newCap = minimumCapacity;
21785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    }
21885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    if (maxCapacity > 0 && newCap > maxCapacity) {
21985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        newCap = maxCapacity;
22085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    }
22127f654740f2a26ad62a5c155af9199af9e69b889claireho    if (newCap > (int32_t)(INT32_MAX / sizeof(int32_t))) {  // integer overflow check
22227f654740f2a26ad62a5c155af9199af9e69b889claireho        // We keep the original memory contents on bad minimumCapacity/maxCapacity.
22327f654740f2a26ad62a5c155af9199af9e69b889claireho        status = U_ILLEGAL_ARGUMENT_ERROR;
22427f654740f2a26ad62a5c155af9199af9e69b889claireho        return FALSE;
22527f654740f2a26ad62a5c155af9199af9e69b889claireho    }
22685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    int32_t* newElems = (int32_t *)uprv_realloc(elements, sizeof(int32_t)*newCap);
22785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    if (newElems == NULL) {
22885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        // We keep the original contents on the memory failure on realloc.
22985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        status = U_MEMORY_ALLOCATION_ERROR;
23085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        return FALSE;
23185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    }
23285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    elements = newElems;
23385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    capacity = newCap;
23485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    return TRUE;
23585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho}
23685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
23785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hovoid UVector32::setMaxCapacity(int32_t limit) {
23885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    U_ASSERT(limit >= 0);
23927f654740f2a26ad62a5c155af9199af9e69b889claireho    if (limit < 0) {
24027f654740f2a26ad62a5c155af9199af9e69b889claireho        limit = 0;
24185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    }
24227f654740f2a26ad62a5c155af9199af9e69b889claireho    if (limit > (int32_t)(INT32_MAX / sizeof(int32_t))) {  // integer overflow check for realloc
24327f654740f2a26ad62a5c155af9199af9e69b889claireho        //  Something is very wrong, don't realloc, leave capacity and maxCapacity unchanged
24427f654740f2a26ad62a5c155af9199af9e69b889claireho        return;
24527f654740f2a26ad62a5c155af9199af9e69b889claireho    }
24627f654740f2a26ad62a5c155af9199af9e69b889claireho    maxCapacity = limit;
24785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    if (capacity <= maxCapacity || maxCapacity == 0) {
24885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        // Current capacity is within the new limit.
24985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        return;
25085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    }
25185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
25285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // New maximum capacity is smaller than the current size.
25385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // Realloc the storage to the new, smaller size.
25485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    int32_t* newElems = (int32_t *)uprv_realloc(elements, sizeof(int32_t)*maxCapacity);
25585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    if (newElems == NULL) {
25685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        // Realloc to smaller failed.
25785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        //   Just keep what we had.  No need to call it a failure.
25885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        return;
25985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    }
26085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    elements = newElems;
26185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    capacity = maxCapacity;
26285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    if (count > capacity) {
26385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        count = capacity;
264ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
265ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
266ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
267ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/**
268ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Change the size of this vector as follows: If newSize is smaller,
269ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * then truncate the array, possibly deleting held elements for i >=
270ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * newSize.  If newSize is larger, grow the array, filling in new
271ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * slots with NULL.
272ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */
273ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid UVector32::setSize(int32_t newSize) {
274ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int32_t i;
275ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if (newSize < 0) {
276ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return;
277ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
278ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if (newSize > count) {
279ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        UErrorCode ec = U_ZERO_ERROR;
280ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        if (!ensureCapacity(newSize, ec)) {
281ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            return;
282ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        }
283ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        for (i=count; i<newSize; ++i) {
284ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            elements[i] = 0;
285ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        }
286ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
287ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    count = newSize;
288ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
289ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
290ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
291ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
292ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
293ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/**
294ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Insert the given integer into this vector at its sorted position
295ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * as defined by 'compare'.  The current elements are assumed to
296ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * be sorted already.
297ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */
298ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid UVector32::sortedInsert(int32_t tok, UErrorCode& ec) {
299ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    // Perform a binary search for the location to insert tok at.  Tok
300ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    // will be inserted between two elements a and b such that a <=
301ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    // tok && tok < b, where there is a 'virtual' elements[-1] always
302ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    // less than tok and a 'virtual' elements[count] always greater
303ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    // than tok.
304ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int32_t min = 0, max = count;
305ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    while (min != max) {
306ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        int32_t probe = (min + max) / 2;
307ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        //int8_t c = (*compare)(elements[probe], tok);
308ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        //if (c > 0) {
309ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        if (elements[probe] > tok) {
310ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            max = probe;
311ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        } else {
312ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            // assert(c <= 0);
313ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            min = probe + 1;
314ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        }
315ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
316ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if (ensureCapacity(count + 1, ec)) {
317ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        for (int32_t i=count; i>min; --i) {
318ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            elements[i] = elements[i-1];
319ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        }
320ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        elements[min] = tok;
321ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        ++count;
322ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
323ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
324ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
325ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
326ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
327ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
328ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
329ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_NAMESPACE_END
330ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
331