1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/intl/icu/source/common/uvectr32.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,304 @@ 1.4 +/* 1.5 +********************************************************************** 1.6 +* Copyright (C) 1999-2011, International Business Machines 1.7 +* Corporation and others. All Rights Reserved. 1.8 +********************************************************************** 1.9 +*/ 1.10 + 1.11 +// 1.12 +// UVector32 is a class implementing a vector of 32 bit integers. 1.13 +// It is similar to UVector, but holds int32_t values rather than pointers. 1.14 +// Most of the code is unchanged from UVector. 1.15 +// 1.16 + 1.17 +#ifndef UVECTOR32_H 1.18 +#define UVECTOR32_H 1.19 + 1.20 +#include "unicode/utypes.h" 1.21 +#include "unicode/uobject.h" 1.22 +#include "uhash.h" 1.23 +#include "uassert.h" 1.24 + 1.25 +U_NAMESPACE_BEGIN 1.26 + 1.27 + 1.28 + 1.29 +/** 1.30 + * <p>Ultralightweight C++ implementation of a <tt>void*</tt> vector 1.31 + * that is (mostly) compatible with java.util.Vector. 1.32 + * 1.33 + * <p>This is a very simple implementation, written to satisfy an 1.34 + * immediate porting need. As such, it is not completely fleshed out, 1.35 + * and it aims for simplicity and conformity. Nonetheless, it serves 1.36 + * its purpose (porting code from java that uses java.util.Vector) 1.37 + * well, and it could be easily made into a more robust vector class. 1.38 + * 1.39 + * <p><b>Design notes</b> 1.40 + * 1.41 + * <p>There is index bounds checking, but little is done about it. If 1.42 + * indices are out of bounds, either nothing happens, or zero is 1.43 + * returned. We <em>do</em> avoid indexing off into the weeds. 1.44 + * 1.45 + * <p>There is detection of out of memory, but the handling is very 1.46 + * coarse-grained -- similar to UnicodeString's protocol, but even 1.47 + * coarser. The class contains <em>one static flag</em> that is set 1.48 + * when any call to <tt>new</tt> returns zero. This allows the caller 1.49 + * to use several vectors and make just one check at the end to see if 1.50 + * a memory failure occurred. This is more efficient than making a 1.51 + * check after each call on each vector when doing many operations on 1.52 + * multiple vectors. The single static flag works best when memory 1.53 + * failures are infrequent, and when recovery options are limited or 1.54 + * nonexistent. 1.55 + * 1.56 + * <p><b>To do</b> 1.57 + * 1.58 + * <p>Improve the handling of index out of bounds errors. 1.59 + * 1.60 + * @author Alan Liu 1.61 + */ 1.62 +class U_COMMON_API UVector32 : public UObject { 1.63 +private: 1.64 + int32_t count; 1.65 + 1.66 + int32_t capacity; 1.67 + 1.68 + int32_t maxCapacity; // Limit beyond which capacity is not permitted to grow. 1.69 + 1.70 + int32_t* elements; 1.71 + 1.72 +public: 1.73 + UVector32(UErrorCode &status); 1.74 + 1.75 + UVector32(int32_t initialCapacity, UErrorCode &status); 1.76 + 1.77 + virtual ~UVector32(); 1.78 + 1.79 + /** 1.80 + * Assign this object to another (make this a copy of 'other'). 1.81 + * Use the 'assign' function to assign each element. 1.82 + */ 1.83 + void assign(const UVector32& other, UErrorCode &ec); 1.84 + 1.85 + /** 1.86 + * Compare this vector with another. They will be considered 1.87 + * equal if they are of the same size and all elements are equal, 1.88 + * as compared using this object's comparer. 1.89 + */ 1.90 + UBool operator==(const UVector32& other); 1.91 + 1.92 + /** 1.93 + * Equivalent to !operator==() 1.94 + */ 1.95 + inline UBool operator!=(const UVector32& other); 1.96 + 1.97 + //------------------------------------------------------------ 1.98 + // java.util.Vector API 1.99 + //------------------------------------------------------------ 1.100 + 1.101 + void addElement(int32_t elem, UErrorCode &status); 1.102 + 1.103 + void setElementAt(int32_t elem, int32_t index); 1.104 + 1.105 + void insertElementAt(int32_t elem, int32_t index, UErrorCode &status); 1.106 + 1.107 + int32_t elementAti(int32_t index) const; 1.108 + 1.109 + UBool equals(const UVector32 &other) const; 1.110 + 1.111 + int32_t lastElementi(void) const; 1.112 + 1.113 + int32_t indexOf(int32_t elem, int32_t startIndex = 0) const; 1.114 + 1.115 + UBool contains(int32_t elem) const; 1.116 + 1.117 + UBool containsAll(const UVector32& other) const; 1.118 + 1.119 + UBool removeAll(const UVector32& other); 1.120 + 1.121 + UBool retainAll(const UVector32& other); 1.122 + 1.123 + void removeElementAt(int32_t index); 1.124 + 1.125 + void removeAllElements(); 1.126 + 1.127 + int32_t size(void) const; 1.128 + 1.129 + UBool isEmpty(void) const; 1.130 + 1.131 + // Inline. Use this one for speedy size check. 1.132 + inline UBool ensureCapacity(int32_t minimumCapacity, UErrorCode &status); 1.133 + 1.134 + // Out-of-line, handles actual growth. Called by ensureCapacity() when necessary. 1.135 + UBool expandCapacity(int32_t minimumCapacity, UErrorCode &status); 1.136 + 1.137 + /** 1.138 + * Change the size of this vector as follows: If newSize is 1.139 + * smaller, then truncate the array, possibly deleting held 1.140 + * elements for i >= newSize. If newSize is larger, grow the 1.141 + * array, filling in new slows with zero. 1.142 + */ 1.143 + void setSize(int32_t newSize); 1.144 + 1.145 + //------------------------------------------------------------ 1.146 + // New API 1.147 + //------------------------------------------------------------ 1.148 + 1.149 + /** 1.150 + * Returns true if this vector contains none of the elements 1.151 + * of the given vector. 1.152 + * @param other vector to be checked for containment 1.153 + * @return true if the test condition is met 1.154 + */ 1.155 + UBool containsNone(const UVector32& other) const; 1.156 + 1.157 + 1.158 + /** 1.159 + * Insert the given integer into this vector at its sorted position. 1.160 + * The current elements are assumed to be sorted already. 1.161 + */ 1.162 + void sortedInsert(int32_t elem, UErrorCode& ec); 1.163 + 1.164 + /** 1.165 + * Returns a pointer to the internal array holding the vector. 1.166 + */ 1.167 + int32_t *getBuffer() const; 1.168 + 1.169 + /** 1.170 + * Set the maximum allowed buffer capacity for this vector/stack. 1.171 + * Default with no limit set is unlimited, go until malloc() fails. 1.172 + * A Limit of zero means unlimited capacity. 1.173 + * Units are vector elements (32 bits each), not bytes. 1.174 + */ 1.175 + void setMaxCapacity(int32_t limit); 1.176 + 1.177 + /** 1.178 + * ICU "poor man's RTTI", returns a UClassID for this class. 1.179 + */ 1.180 + static UClassID U_EXPORT2 getStaticClassID(); 1.181 + 1.182 + /** 1.183 + * ICU "poor man's RTTI", returns a UClassID for the actual class. 1.184 + */ 1.185 + virtual UClassID getDynamicClassID() const; 1.186 + 1.187 +private: 1.188 + void _init(int32_t initialCapacity, UErrorCode &status); 1.189 + 1.190 + // Disallow 1.191 + UVector32(const UVector32&); 1.192 + 1.193 + // Disallow 1.194 + UVector32& operator=(const UVector32&); 1.195 + 1.196 + 1.197 + // API Functions for Stack operations. 1.198 + // In the original UVector, these were in a separate derived class, UStack. 1.199 + // Here in UVector32, they are all together. 1.200 +public: 1.201 + UBool empty(void) const; // TODO: redundant, same as empty(). Remove it? 1.202 + 1.203 + int32_t peeki(void) const; 1.204 + 1.205 + int32_t popi(void); 1.206 + 1.207 + int32_t push(int32_t i, UErrorCode &status); 1.208 + 1.209 + int32_t *reserveBlock(int32_t size, UErrorCode &status); 1.210 + int32_t *popFrame(int32_t size); 1.211 +}; 1.212 + 1.213 + 1.214 +// UVector32 inlines 1.215 + 1.216 +inline UBool UVector32::ensureCapacity(int32_t minimumCapacity, UErrorCode &status) { 1.217 + if ((minimumCapacity >= 0) && (capacity >= minimumCapacity)) { 1.218 + return TRUE; 1.219 + } else { 1.220 + return expandCapacity(minimumCapacity, status); 1.221 + } 1.222 +} 1.223 + 1.224 +inline int32_t UVector32::elementAti(int32_t index) const { 1.225 + return (index >= 0 && count > 0 && count - index > 0) ? elements[index] : 0; 1.226 +} 1.227 + 1.228 + 1.229 +inline void UVector32::addElement(int32_t elem, UErrorCode &status) { 1.230 + if (ensureCapacity(count + 1, status)) { 1.231 + elements[count] = elem; 1.232 + count++; 1.233 + } 1.234 +} 1.235 + 1.236 +inline int32_t *UVector32::reserveBlock(int32_t size, UErrorCode &status) { 1.237 + if (ensureCapacity(count+size, status) == FALSE) { 1.238 + return NULL; 1.239 + } 1.240 + int32_t *rp = elements+count; 1.241 + count += size; 1.242 + return rp; 1.243 +} 1.244 + 1.245 +inline int32_t *UVector32::popFrame(int32_t size) { 1.246 + U_ASSERT(count >= size); 1.247 + count -= size; 1.248 + if (count < 0) { 1.249 + count = 0; 1.250 + } 1.251 + return elements+count-size; 1.252 +} 1.253 + 1.254 + 1.255 + 1.256 +inline int32_t UVector32::size(void) const { 1.257 + return count; 1.258 +} 1.259 + 1.260 +inline UBool UVector32::isEmpty(void) const { 1.261 + return count == 0; 1.262 +} 1.263 + 1.264 +inline UBool UVector32::contains(int32_t obj) const { 1.265 + return indexOf(obj) >= 0; 1.266 +} 1.267 + 1.268 +inline int32_t UVector32::lastElementi(void) const { 1.269 + return elementAti(count-1); 1.270 +} 1.271 + 1.272 +inline UBool UVector32::operator!=(const UVector32& other) { 1.273 + return !operator==(other); 1.274 +} 1.275 + 1.276 +inline int32_t *UVector32::getBuffer() const { 1.277 + return elements; 1.278 +} 1.279 + 1.280 + 1.281 +// UStack inlines 1.282 + 1.283 +inline UBool UVector32::empty(void) const { 1.284 + return isEmpty(); 1.285 +} 1.286 + 1.287 +inline int32_t UVector32::peeki(void) const { 1.288 + return lastElementi(); 1.289 +} 1.290 + 1.291 +inline int32_t UVector32::push(int32_t i, UErrorCode &status) { 1.292 + addElement(i, status); 1.293 + return i; 1.294 +} 1.295 + 1.296 +inline int32_t UVector32::popi(void) { 1.297 + int32_t result = 0; 1.298 + if (count > 0) { 1.299 + count--; 1.300 + result = elements[count]; 1.301 + } 1.302 + return result; 1.303 +} 1.304 + 1.305 +U_NAMESPACE_END 1.306 + 1.307 +#endif