1ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/*
2ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru**********************************************************************
327f654740f2a26ad62a5c155af9199af9e69b889claireho*   Copyright (C) 1999-2010, International Business Machines
4ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*   Corporation and others.  All Rights Reserved.
5ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru**********************************************************************
6ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*/
7ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
8ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru//
9ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru//  UVector32 is a class implementing a vector of 32 bit integers.
10ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru//            It is similar to UVector, but holds int32_t values rather than pointers.
11ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru//            Most of the code is unchanged from UVector.
12ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru//
13ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
14ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#ifndef UVECTOR32_H
15ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define UVECTOR32_H
16ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
17ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/utypes.h"
18ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/uobject.h"
19ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "uhash.h"
20ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "uassert.h"
21ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
22ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_NAMESPACE_BEGIN
23ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
24ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
25ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
26ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/**
27ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * <p>Ultralightweight C++ implementation of a <tt>void*</tt> vector
28ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * that is (mostly) compatible with java.util.Vector.
29ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *
30ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * <p>This is a very simple implementation, written to satisfy an
31ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * immediate porting need.  As such, it is not completely fleshed out,
32ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * and it aims for simplicity and conformity.  Nonetheless, it serves
33ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * its purpose (porting code from java that uses java.util.Vector)
34ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * well, and it could be easily made into a more robust vector class.
35ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *
36ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * <p><b>Design notes</b>
37ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *
38ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * <p>There is index bounds checking, but little is done about it.  If
39ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * indices are out of bounds, either nothing happens, or zero is
40ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * returned.  We <em>do</em> avoid indexing off into the weeds.
41ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *
42ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * <p>There is detection of out of memory, but the handling is very
43ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * coarse-grained -- similar to UnicodeString's protocol, but even
44ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * coarser.  The class contains <em>one static flag</em> that is set
45ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * when any call to <tt>new</tt> returns zero.  This allows the caller
46ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * to use several vectors and make just one check at the end to see if
47ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * a memory failure occurred.  This is more efficient than making a
48ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * check after each call on each vector when doing many operations on
49ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * multiple vectors.  The single static flag works best when memory
50ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * failures are infrequent, and when recovery options are limited or
51ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * nonexistent.
52ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *
53ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * <p><b>To do</b>
54ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *
55ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * <p>Improve the handling of index out of bounds errors.
56ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *
57ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * @author Alan Liu
58ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */
59ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruclass U_COMMON_API UVector32 : public UObject {
60ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruprivate:
61ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int32_t   count;
62ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
63ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int32_t   capacity;
6485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
6585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    int32_t   maxCapacity;   // Limit beyond which capacity is not permitted to grow.
66ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
67ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int32_t*  elements;
68ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
69ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querupublic:
70ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    UVector32(UErrorCode &status);
71ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
72ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    UVector32(int32_t initialCapacity, UErrorCode &status);
73ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
74ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    virtual ~UVector32();
75ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
76ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    /**
77ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru     * Assign this object to another (make this a copy of 'other').
78ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru     * Use the 'assign' function to assign each element.
79ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru     */
80ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    void assign(const UVector32& other, UErrorCode &ec);
81ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
82ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    /**
83ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru     * Compare this vector with another.  They will be considered
84ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru     * equal if they are of the same size and all elements are equal,
85ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru     * as compared using this object's comparer.
86ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru     */
87ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    UBool operator==(const UVector32& other);
88ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
89ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    /**
90ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru     * Equivalent to !operator==()
91ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru     */
92ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    inline UBool operator!=(const UVector32& other);
93ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
94ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    //------------------------------------------------------------
95ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    // java.util.Vector API
96ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    //------------------------------------------------------------
97ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
98ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    void addElement(int32_t elem, UErrorCode &status);
99ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
100ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    void setElementAt(int32_t elem, int32_t index);
101ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
102ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    void insertElementAt(int32_t elem, int32_t index, UErrorCode &status);
103ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
104ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int32_t elementAti(int32_t index) const;
105ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
106ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    UBool equals(const UVector32 &other) const;
107ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
108ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int32_t lastElementi(void) const;
109ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
110ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int32_t indexOf(int32_t elem, int32_t startIndex = 0) const;
111ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
112ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    UBool contains(int32_t elem) const;
113ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
114ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    UBool containsAll(const UVector32& other) const;
115ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
116ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    UBool removeAll(const UVector32& other);
117ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
118ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    UBool retainAll(const UVector32& other);
119ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
120ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    void removeElementAt(int32_t index);
121ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
122ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    void removeAllElements();
123ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
124ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int32_t size(void) const;
125ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
126ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    UBool isEmpty(void) const;
127ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
128ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    // Inline.  Use this one for speedy size check.
129ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    inline UBool ensureCapacity(int32_t minimumCapacity, UErrorCode &status);
130ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
131ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    // Out-of-line, handles actual growth.  Called by ensureCapacity() when necessary.
132ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    UBool expandCapacity(int32_t minimumCapacity, UErrorCode &status);
133ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
134ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    /**
135ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru     * Change the size of this vector as follows: If newSize is
136ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru     * smaller, then truncate the array, possibly deleting held
137ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru     * elements for i >= newSize.  If newSize is larger, grow the
138ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru     * array, filling in new slows with zero.
139ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru     */
140ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    void setSize(int32_t newSize);
141ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
142ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    //------------------------------------------------------------
143ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    // New API
144ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    //------------------------------------------------------------
145ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
146ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    /**
147ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru     * Returns true if this vector contains none of the elements
148ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru     * of the given vector.
149ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru     * @param other vector to be checked for containment
150ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru     * @return true if the test condition is met
151ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru     */
152ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    UBool containsNone(const UVector32& other) const;
153ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
154ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
155ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    /**
156ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru     * Insert the given integer into this vector at its sorted position.
157ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru     * The current elements are assumed to be sorted already.
158ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru     */
159ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    void sortedInsert(int32_t elem, UErrorCode& ec);
160ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
161ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    /**
162ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru     * Returns a pointer to the internal array holding the vector.
163ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru     */
164ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int32_t *getBuffer() const;
165ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
166ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    /**
16785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho     * Set the maximum allowed buffer capacity for this vector/stack.
16885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho     * Default with no limit set is unlimited, go until malloc() fails.
16985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho     * A Limit of zero means unlimited capacity.
17085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho     * Units are vector elements (32 bits each), not bytes.
17185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho     */
17285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    void setMaxCapacity(int32_t limit);
17385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
17485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    /**
175ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru     * ICU "poor man's RTTI", returns a UClassID for this class.
176ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru     */
177ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    static UClassID U_EXPORT2 getStaticClassID();
178ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
179ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    /**
180ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru     * ICU "poor man's RTTI", returns a UClassID for the actual class.
181ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru     */
182ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    virtual UClassID getDynamicClassID() const;
183ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
184ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruprivate:
185ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    void _init(int32_t initialCapacity, UErrorCode &status);
186ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
187ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    // Disallow
188ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    UVector32(const UVector32&);
189ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
190ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    // Disallow
191ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    UVector32& operator=(const UVector32&);
192ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
193ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
194ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    //  API Functions for Stack operations.
195ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    //  In the original UVector, these were in a separate derived class, UStack.
196ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    //  Here in UVector32, they are all together.
197ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querupublic:
198ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    UBool empty(void) const;   // TODO:  redundant, same as empty().  Remove it?
199ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
200ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int32_t peeki(void) const;
201ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
202ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int32_t popi(void);
203ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
204ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int32_t push(int32_t i, UErrorCode &status);
205ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
206ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int32_t *reserveBlock(int32_t size, UErrorCode &status);
207ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int32_t *popFrame(int32_t size);
208ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru};
209ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
210ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
211ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// UVector32 inlines
212ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
213ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruinline UBool UVector32::ensureCapacity(int32_t minimumCapacity, UErrorCode &status) {
21427f654740f2a26ad62a5c155af9199af9e69b889claireho    if ((minimumCapacity >= 0) && (capacity >= minimumCapacity)) {
215ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return TRUE;
216ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    } else {
217ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return expandCapacity(minimumCapacity, status);
218ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
219ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
220ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
221ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruinline int32_t UVector32::elementAti(int32_t index) const {
222ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return (0 <= index && index < count) ? elements[index] : 0;
223ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
224ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
225ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
226ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruinline void UVector32::addElement(int32_t elem, UErrorCode &status) {
227ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if (ensureCapacity(count + 1, status)) {
228ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        elements[count] = elem;
229ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        count++;
230ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
231ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
232ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
233ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruinline int32_t *UVector32::reserveBlock(int32_t size, UErrorCode &status) {
23485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    if (ensureCapacity(count+size, status) == FALSE) {
23585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        return NULL;
23685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    }
237ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int32_t  *rp = elements+count;
238ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    count += size;
239ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return rp;
240ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
241ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
242ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruinline int32_t *UVector32::popFrame(int32_t size) {
243ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    U_ASSERT(count >= size);
244ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    count -= size;
245ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if (count < 0) {
246ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        count = 0;
247ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
248ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return elements+count-size;
249ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
250ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
251ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
252ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
253ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruinline int32_t UVector32::size(void) const {
254ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return count;
255ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
256ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
257ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruinline UBool UVector32::isEmpty(void) const {
258ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return count == 0;
259ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
260ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
261ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruinline UBool UVector32::contains(int32_t obj) const {
262ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return indexOf(obj) >= 0;
263ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
264ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
265ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruinline int32_t UVector32::lastElementi(void) const {
266ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return elementAti(count-1);
267ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
268ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
269ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruinline UBool UVector32::operator!=(const UVector32& other) {
270ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return !operator==(other);
271ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
272ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
273ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruinline int32_t *UVector32::getBuffer() const {
274ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return elements;
275ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
276ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
277ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
278ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// UStack inlines
279ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
280ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruinline UBool UVector32::empty(void) const {
281ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return isEmpty();
282ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
283ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
284ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruinline int32_t UVector32::peeki(void) const {
285ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return lastElementi();
286ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
287ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
288ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruinline int32_t UVector32::push(int32_t i, UErrorCode &status) {
289ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    addElement(i, status);
290ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return i;
291ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
292ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
293ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruinline int32_t UVector32::popi(void) {
294ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int32_t result = 0;
295ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if (count > 0) {
296ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        count--;
297ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        result = elements[count];
298ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
299ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return result;
300ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
301ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
302ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_NAMESPACE_END
303ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
304ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#endif
305