uvectr32.h 8.4 KB

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