]>
git.saurik.com Git - apple/icu.git/blob - icuSources/common/uvectr64.h
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 **********************************************************************
5 * Copyright (C) 1999-2014, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 **********************************************************************
11 // UVector64 is a class implementing a vector of 64 bit integers.
12 // It is similar to UVector32, but holds int64_t values rather than int32_t.
13 // Most of the code is unchanged from UVector.
19 #include "unicode/utypes.h"
20 #include "unicode/uobject.h"
29 * <p>Ultralightweight C++ implementation of an <tt>int64_t</tt> vector
30 * that has a subset of methods from UVector32
32 * <p>This is a very simple implementation, written to satisfy an
33 * immediate porting need. As such, it is not completely fleshed out,
34 * and it aims for simplicity and conformity. Nonetheless, it serves
35 * its purpose (porting code from java that uses java.util.Vector)
36 * well, and it could be easily made into a more robust vector class.
38 * <p><b>Design notes</b>
40 * <p>There is index bounds checking, but little is done about it. If
41 * indices are out of bounds, either nothing happens, or zero is
42 * returned. We <em>do</em> avoid indexing off into the weeds.
44 * <p>There is detection of out of memory, but the handling is very
45 * coarse-grained -- similar to UnicodeString's protocol, but even
46 * coarser. The class contains <em>one static flag</em> that is set
47 * when any call to <tt>new</tt> returns zero. This allows the caller
48 * to use several vectors and make just one check at the end to see if
49 * a memory failure occurred. This is more efficient than making a
50 * check after each call on each vector when doing many operations on
51 * multiple vectors. The single static flag works best when memory
52 * failures are infrequent, and when recovery options are limited or
57 * <p>Improve the handling of index out of bounds errors.
60 class U_COMMON_API UVector64
: public UObject
{
66 int32_t maxCapacity
; // Limit beyond which capacity is not permitted to grow.
71 UVector64(UErrorCode
&status
);
73 UVector64(int32_t initialCapacity
, UErrorCode
&status
);
78 * Assign this object to another (make this a copy of 'other').
79 * Use the 'assign' function to assign each element.
81 void assign(const UVector64
& other
, UErrorCode
&ec
);
84 * Compare this vector with another. They will be considered
85 * equal if they are of the same size and all elements are equal,
86 * as compared using this object's comparer.
88 UBool
operator==(const UVector64
& other
);
91 * Equivalent to !operator==()
93 inline UBool
operator!=(const UVector64
& other
);
95 //------------------------------------------------------------
96 // subset of java.util.Vector API
97 //------------------------------------------------------------
99 inline void addElement(int64_t elem
, UErrorCode
&status
);
101 void setElementAt(int64_t elem
, int32_t index
);
103 void insertElementAt(int64_t elem
, int32_t index
, UErrorCode
&status
);
105 inline int64_t elementAti(int32_t index
) const;
107 //UBool equals(const UVector64 &other) const;
109 inline int64_t lastElementi(void) const;
111 //int32_t indexOf(int64_t elem, int32_t startIndex = 0) const;
113 //UBool contains(int64_t elem) const;
115 //UBool containsAll(const UVector64& other) const;
117 //UBool removeAll(const UVector64& other);
119 //UBool retainAll(const UVector64& other);
121 //void removeElementAt(int32_t index);
123 void removeAllElements();
125 inline int32_t size(void) const;
127 inline UBool
isEmpty(void) const { return count
== 0; }
129 // Inline. Use this one for speedy size check.
130 inline UBool
ensureCapacity(int32_t minimumCapacity
, UErrorCode
&status
);
132 // Out-of-line, handles actual growth. Called by ensureCapacity() when necessary.
133 UBool
expandCapacity(int32_t minimumCapacity
, UErrorCode
&status
);
136 * Change the size of this vector as follows: If newSize is
137 * smaller, then truncate the array, possibly deleting held
138 * elements for i >= newSize. If newSize is larger, grow the
139 * array, filling in new slows with zero.
141 void setSize(int32_t newSize
);
143 //------------------------------------------------------------
145 //------------------------------------------------------------
147 //UBool containsNone(const UVector64& other) const;
150 //void sortedInsert(int64_t elem, UErrorCode& ec);
153 * Returns a pointer to the internal array holding the vector.
155 inline int64_t *getBuffer() const;
158 * Set the maximum allowed buffer capacity for this vector/stack.
159 * Default with no limit set is unlimited, go until malloc() fails.
160 * A Limit of zero means unlimited capacity.
161 * Units are vector elements (64 bits each), not bytes.
163 void setMaxCapacity(int32_t limit
);
166 * ICU "poor man's RTTI", returns a UClassID for this class.
168 static UClassID U_EXPORT2
getStaticClassID();
171 * ICU "poor man's RTTI", returns a UClassID for the actual class.
173 virtual UClassID
getDynamicClassID() const;
176 void _init(int32_t initialCapacity
, UErrorCode
&status
);
179 UVector64(const UVector64
&);
182 UVector64
& operator=(const UVector64
&);
185 // API Functions for Stack operations.
186 // In the original UVector, these were in a separate derived class, UStack.
187 // Here in UVector64, they are all together.
189 //UBool empty(void) const; // TODO: redundant, same as empty(). Remove it?
191 //int64_t peeki(void) const;
193 inline int64_t popi(void);
195 inline int64_t push(int64_t i
, UErrorCode
&status
);
197 inline int64_t *reserveBlock(int32_t size
, UErrorCode
&status
);
198 inline int64_t *popFrame(int32_t size
);
204 inline UBool
UVector64::ensureCapacity(int32_t minimumCapacity
, UErrorCode
&status
) {
205 if ((minimumCapacity
>= 0) && (capacity
>= minimumCapacity
)) {
208 return expandCapacity(minimumCapacity
, status
);
212 inline int64_t UVector64::elementAti(int32_t index
) const {
213 return (0 <= index
&& index
< count
) ? elements
[index
] : 0;
217 inline void UVector64::addElement(int64_t elem
, UErrorCode
&status
) {
218 if (ensureCapacity(count
+ 1, status
)) {
219 elements
[count
] = elem
;
224 inline int64_t *UVector64::reserveBlock(int32_t size
, UErrorCode
&status
) {
225 if (ensureCapacity(count
+size
, status
) == FALSE
) {
228 int64_t *rp
= elements
+count
;
233 inline int64_t *UVector64::popFrame(int32_t size
) {
234 U_ASSERT(count
>= size
);
239 return elements
+count
-size
;
244 inline int32_t UVector64::size(void) const {
248 inline int64_t UVector64::lastElementi(void) const {
249 return elementAti(count
-1);
252 inline UBool
UVector64::operator!=(const UVector64
& other
) {
253 return !operator==(other
);
256 inline int64_t *UVector64::getBuffer() const {
263 inline int64_t UVector64::push(int64_t i
, UErrorCode
&status
) {
264 addElement(i
, status
);
268 inline int64_t UVector64::popi(void) {
272 result
= elements
[count
];