uvectr64.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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-2014, International Business Machines
  6. * Corporation and others. All Rights Reserved.
  7. **********************************************************************
  8. */
  9. //
  10. // UVector64 is a class implementing a vector of 64 bit integers.
  11. // It is similar to UVector32, but holds int64_t values rather than int32_t.
  12. // Most of the code is unchanged from UVector.
  13. //
  14. #ifndef UVECTOR64_H
  15. #define UVECTOR64_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 an <tt>int64_t</tt> vector
  23. * that has a subset of methods from UVector32
  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. */
  53. class U_COMMON_API UVector64 : public UObject {
  54. private:
  55. int32_t count;
  56. int32_t capacity;
  57. int32_t maxCapacity; // Limit beyond which capacity is not permitted to grow.
  58. int64_t* elements;
  59. public:
  60. UVector64(UErrorCode &status);
  61. UVector64(int32_t initialCapacity, UErrorCode &status);
  62. virtual ~UVector64();
  63. /**
  64. * Assign this object to another (make this a copy of 'other').
  65. * Use the 'assign' function to assign each element.
  66. */
  67. void assign(const UVector64& other, UErrorCode &ec);
  68. /**
  69. * Compare this vector with another. They will be considered
  70. * equal if they are of the same size and all elements are equal,
  71. * as compared using this object's comparer.
  72. */
  73. UBool operator==(const UVector64& other);
  74. /**
  75. * Equivalent to !operator==()
  76. */
  77. inline UBool operator!=(const UVector64& other);
  78. //------------------------------------------------------------
  79. // subset of java.util.Vector API
  80. //------------------------------------------------------------
  81. inline void addElement(int64_t elem, UErrorCode &status);
  82. void setElementAt(int64_t elem, int32_t index);
  83. void insertElementAt(int64_t elem, int32_t index, UErrorCode &status);
  84. inline int64_t elementAti(int32_t index) const;
  85. //UBool equals(const UVector64 &other) const;
  86. inline int64_t lastElementi(void) const;
  87. //int32_t indexOf(int64_t elem, int32_t startIndex = 0) const;
  88. //UBool contains(int64_t elem) const;
  89. //UBool containsAll(const UVector64& other) const;
  90. //UBool removeAll(const UVector64& other);
  91. //UBool retainAll(const UVector64& other);
  92. //void removeElementAt(int32_t index);
  93. void removeAllElements();
  94. inline int32_t size(void) const;
  95. inline UBool isEmpty(void) const { return count == 0; }
  96. // Inline. Use this one for speedy size check.
  97. inline UBool ensureCapacity(int32_t minimumCapacity, UErrorCode &status);
  98. // Out-of-line, handles actual growth. Called by ensureCapacity() when necessary.
  99. UBool expandCapacity(int32_t minimumCapacity, UErrorCode &status);
  100. /**
  101. * Change the size of this vector as follows: If newSize is
  102. * smaller, then truncate the array, possibly deleting held
  103. * elements for i >= newSize. If newSize is larger, grow the
  104. * array, filling in new slows with zero.
  105. */
  106. void setSize(int32_t newSize);
  107. //------------------------------------------------------------
  108. // New API
  109. //------------------------------------------------------------
  110. //UBool containsNone(const UVector64& other) const;
  111. //void sortedInsert(int64_t elem, UErrorCode& ec);
  112. /**
  113. * Returns a pointer to the internal array holding the vector.
  114. */
  115. inline int64_t *getBuffer() const;
  116. /**
  117. * Set the maximum allowed buffer capacity for this vector/stack.
  118. * Default with no limit set is unlimited, go until malloc() fails.
  119. * A Limit of zero means unlimited capacity.
  120. * Units are vector elements (64 bits each), not bytes.
  121. */
  122. void setMaxCapacity(int32_t limit);
  123. /**
  124. * ICU "poor man's RTTI", returns a UClassID for this class.
  125. */
  126. static UClassID U_EXPORT2 getStaticClassID();
  127. /**
  128. * ICU "poor man's RTTI", returns a UClassID for the actual class.
  129. */
  130. virtual UClassID getDynamicClassID() const;
  131. private:
  132. void _init(int32_t initialCapacity, UErrorCode &status);
  133. // Disallow
  134. UVector64(const UVector64&);
  135. // Disallow
  136. UVector64& operator=(const UVector64&);
  137. // API Functions for Stack operations.
  138. // In the original UVector, these were in a separate derived class, UStack.
  139. // Here in UVector64, they are all together.
  140. public:
  141. //UBool empty(void) const; // TODO: redundant, same as empty(). Remove it?
  142. //int64_t peeki(void) const;
  143. inline int64_t popi(void);
  144. inline int64_t push(int64_t i, UErrorCode &status);
  145. inline int64_t *reserveBlock(int32_t size, UErrorCode &status);
  146. inline int64_t *popFrame(int32_t size);
  147. };
  148. // UVector64 inlines
  149. inline UBool UVector64::ensureCapacity(int32_t minimumCapacity, UErrorCode &status) {
  150. if ((minimumCapacity >= 0) && (capacity >= minimumCapacity)) {
  151. return TRUE;
  152. } else {
  153. return expandCapacity(minimumCapacity, status);
  154. }
  155. }
  156. inline int64_t UVector64::elementAti(int32_t index) const {
  157. return (0 <= index && index < count) ? elements[index] : 0;
  158. }
  159. inline void UVector64::addElement(int64_t elem, UErrorCode &status) {
  160. if (ensureCapacity(count + 1, status)) {
  161. elements[count] = elem;
  162. count++;
  163. }
  164. }
  165. inline int64_t *UVector64::reserveBlock(int32_t size, UErrorCode &status) {
  166. if (ensureCapacity(count+size, status) == FALSE) {
  167. return NULL;
  168. }
  169. int64_t *rp = elements+count;
  170. count += size;
  171. return rp;
  172. }
  173. inline int64_t *UVector64::popFrame(int32_t size) {
  174. U_ASSERT(count >= size);
  175. count -= size;
  176. if (count < 0) {
  177. count = 0;
  178. }
  179. return elements+count-size;
  180. }
  181. inline int32_t UVector64::size(void) const {
  182. return count;
  183. }
  184. inline int64_t UVector64::lastElementi(void) const {
  185. return elementAti(count-1);
  186. }
  187. inline UBool UVector64::operator!=(const UVector64& other) {
  188. return !operator==(other);
  189. }
  190. inline int64_t *UVector64::getBuffer() const {
  191. return elements;
  192. }
  193. // UStack inlines
  194. inline int64_t UVector64::push(int64_t i, UErrorCode &status) {
  195. addElement(i, status);
  196. return i;
  197. }
  198. inline int64_t UVector64::popi(void) {
  199. int64_t result = 0;
  200. if (count > 0) {
  201. count--;
  202. result = elements[count];
  203. }
  204. return result;
  205. }
  206. U_NAMESPACE_END
  207. #endif