uvector.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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-2016, International Business Machines
  6. * Corporation and others. All Rights Reserved.
  7. **********************************************************************
  8. * Date Name Description
  9. * 10/22/99 alan Creation. This is an internal header.
  10. * It should not be exported.
  11. **********************************************************************
  12. */
  13. #ifndef UVECTOR_H
  14. #define UVECTOR_H
  15. #include "unicode/utypes.h"
  16. #include "unicode/uobject.h"
  17. #include "cmemory.h"
  18. #include "uarrsort.h"
  19. #include "uelement.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>Since we don't have garbage collection, UVector was given the
  49. * option to <em>own</em>its contents. To employ this, set a deleter
  50. * function. The deleter is called on a void* pointer when that
  51. * pointer is released by the vector, either when the vector itself is
  52. * destructed, or when a call to setElementAt() overwrites an element,
  53. * or when a call to remove() or one of its variants explicitly
  54. * removes an element. If no deleter is set, or the deleter is set to
  55. * zero, then it is assumed that the caller will delete elements as
  56. * needed.
  57. *
  58. * <p>In order to implement methods such as contains() and indexOf(),
  59. * UVector needs a way to compare objects for equality. To do so, it
  60. * uses a comparison function, or "comparer." If the comparer is not
  61. * set, or is set to zero, then all such methods will act as if the
  62. * vector contains no element. That is, indexOf() will always return
  63. * -1, contains() will always return FALSE, etc.
  64. *
  65. * <p><b>To do</b>
  66. *
  67. * <p>Improve the handling of index out of bounds errors.
  68. *
  69. * @author Alan Liu
  70. */
  71. class U_COMMON_API UVector : public UObject {
  72. // NOTE: UVector uses the UHashKey (union of void* and int32_t) as
  73. // its basic storage type. It uses UElementsAreEqual as its
  74. // comparison function. It uses UObjectDeleter as its deleter
  75. // function. These are named for hashtables, but used here as-is
  76. // rather than duplicating the type. This allows sharing of
  77. // support functions.
  78. private:
  79. int32_t count;
  80. int32_t capacity;
  81. UElement* elements;
  82. UObjectDeleter *deleter;
  83. UElementsAreEqual *comparer;
  84. public:
  85. UVector(UErrorCode &status);
  86. UVector(int32_t initialCapacity, UErrorCode &status);
  87. UVector(UObjectDeleter *d, UElementsAreEqual *c, UErrorCode &status);
  88. UVector(UObjectDeleter *d, UElementsAreEqual *c, int32_t initialCapacity, UErrorCode &status);
  89. virtual ~UVector();
  90. /**
  91. * Assign this object to another (make this a copy of 'other').
  92. * Use the 'assign' function to assign each element.
  93. */
  94. void assign(const UVector& other, UElementAssigner *assign, UErrorCode &ec);
  95. /**
  96. * Compare this vector with another. They will be considered
  97. * equal if they are of the same size and all elements are equal,
  98. * as compared using this object's comparer.
  99. */
  100. UBool operator==(const UVector& other);
  101. /**
  102. * Equivalent to !operator==()
  103. */
  104. inline UBool operator!=(const UVector& other);
  105. //------------------------------------------------------------
  106. // java.util.Vector API
  107. //------------------------------------------------------------
  108. void addElement(void* obj, UErrorCode &status);
  109. void addElement(int32_t elem, UErrorCode &status);
  110. void setElementAt(void* obj, int32_t index);
  111. void setElementAt(int32_t elem, int32_t index);
  112. void insertElementAt(void* obj, int32_t index, UErrorCode &status);
  113. void insertElementAt(int32_t elem, int32_t index, UErrorCode &status);
  114. void* elementAt(int32_t index) const;
  115. int32_t elementAti(int32_t index) const;
  116. UBool equals(const UVector &other) const;
  117. inline void* firstElement(void) const;
  118. inline void* lastElement(void) const;
  119. inline int32_t lastElementi(void) const;
  120. int32_t indexOf(void* obj, int32_t startIndex = 0) const;
  121. int32_t indexOf(int32_t obj, int32_t startIndex = 0) const;
  122. inline UBool contains(void* obj) const;
  123. inline UBool contains(int32_t obj) const;
  124. UBool containsAll(const UVector& other) const;
  125. UBool removeAll(const UVector& other);
  126. UBool retainAll(const UVector& other);
  127. void removeElementAt(int32_t index);
  128. UBool removeElement(void* obj);
  129. void removeAllElements();
  130. inline int32_t size(void) const;
  131. inline UBool isEmpty(void) const;
  132. UBool ensureCapacity(int32_t minimumCapacity, UErrorCode &status);
  133. /**
  134. * Change the size of this vector as follows: If newSize is
  135. * smaller, then truncate the array, possibly deleting held
  136. * elements for i >= newSize. If newSize is larger, grow the
  137. * array, filling in new slots with NULL.
  138. */
  139. void setSize(int32_t newSize, UErrorCode &status);
  140. /**
  141. * Fill in the given array with all elements of this vector.
  142. */
  143. void** toArray(void** result) const;
  144. //------------------------------------------------------------
  145. // New API
  146. //------------------------------------------------------------
  147. UObjectDeleter *setDeleter(UObjectDeleter *d);
  148. UElementsAreEqual *setComparer(UElementsAreEqual *c);
  149. inline void* operator[](int32_t index) const;
  150. /**
  151. * Removes the element at the given index from this vector and
  152. * transfer ownership of it to the caller. After this call, the
  153. * caller owns the result and must delete it and the vector entry
  154. * at 'index' is removed, shifting all subsequent entries back by
  155. * one index and shortening the size of the vector by one. If the
  156. * index is out of range or if there is no item at the given index
  157. * then 0 is returned and the vector is unchanged.
  158. */
  159. void* orphanElementAt(int32_t index);
  160. /**
  161. * Returns true if this vector contains none of the elements
  162. * of the given vector.
  163. * @param other vector to be checked for containment
  164. * @return true if the test condition is met
  165. */
  166. UBool containsNone(const UVector& other) const;
  167. /**
  168. * Insert the given object into this vector at its sorted position
  169. * as defined by 'compare'. The current elements are assumed to
  170. * be sorted already.
  171. */
  172. void sortedInsert(void* obj, UElementComparator *compare, UErrorCode& ec);
  173. /**
  174. * Insert the given integer into this vector at its sorted position
  175. * as defined by 'compare'. The current elements are assumed to
  176. * be sorted already.
  177. */
  178. void sortedInsert(int32_t obj, UElementComparator *compare, UErrorCode& ec);
  179. /**
  180. * Sort the contents of the vector, assuming that the contents of the
  181. * vector are of type int32_t.
  182. */
  183. void sorti(UErrorCode &ec);
  184. /**
  185. * Sort the contents of this vector, using a caller-supplied function
  186. * to do the comparisons. (It's confusing that
  187. * UVector's UElementComparator function is different from the
  188. * UComparator function type defined in uarrsort.h)
  189. */
  190. void sort(UElementComparator *compare, UErrorCode &ec);
  191. /**
  192. * Stable sort the contents of this vector using a caller-supplied function
  193. * of type UComparator to do the comparison. Provides more flexibility
  194. * than UVector::sort() because an additional user parameter can be passed to
  195. * the comparison function.
  196. */
  197. void sortWithUComparator(UComparator *compare, const void *context, UErrorCode &ec);
  198. /**
  199. * ICU "poor man's RTTI", returns a UClassID for this class.
  200. */
  201. static UClassID U_EXPORT2 getStaticClassID();
  202. /**
  203. * ICU "poor man's RTTI", returns a UClassID for the actual class.
  204. */
  205. virtual UClassID getDynamicClassID() const;
  206. private:
  207. void _init(int32_t initialCapacity, UErrorCode &status);
  208. int32_t indexOf(UElement key, int32_t startIndex = 0, int8_t hint = 0) const;
  209. void sortedInsert(UElement e, UElementComparator *compare, UErrorCode& ec);
  210. // Disallow
  211. UVector(const UVector&);
  212. // Disallow
  213. UVector& operator=(const UVector&);
  214. };
  215. /**
  216. * <p>Ultralightweight C++ implementation of a <tt>void*</tt> stack
  217. * that is (mostly) compatible with java.util.Stack. As in java, this
  218. * is merely a paper thin layer around UVector. See the UVector
  219. * documentation for further information.
  220. *
  221. * <p><b>Design notes</b>
  222. *
  223. * <p>The element at index <tt>n-1</tt> is (of course) the top of the
  224. * stack.
  225. *
  226. * <p>The poorly named <tt>empty()</tt> method doesn't empty the
  227. * stack; it determines if the stack is empty.
  228. *
  229. * @author Alan Liu
  230. */
  231. class U_COMMON_API UStack : public UVector {
  232. public:
  233. UStack(UErrorCode &status);
  234. UStack(int32_t initialCapacity, UErrorCode &status);
  235. UStack(UObjectDeleter *d, UElementsAreEqual *c, UErrorCode &status);
  236. UStack(UObjectDeleter *d, UElementsAreEqual *c, int32_t initialCapacity, UErrorCode &status);
  237. virtual ~UStack();
  238. // It's okay not to have a virtual destructor (in UVector)
  239. // because UStack has no special cleanup to do.
  240. inline UBool empty(void) const;
  241. inline void* peek(void) const;
  242. inline int32_t peeki(void) const;
  243. void* pop(void);
  244. int32_t popi(void);
  245. inline void* push(void* obj, UErrorCode &status);
  246. inline int32_t push(int32_t i, UErrorCode &status);
  247. /*
  248. If the object o occurs as an item in this stack,
  249. this method returns the 1-based distance from the top of the stack.
  250. */
  251. int32_t search(void* obj) const;
  252. /**
  253. * ICU "poor man's RTTI", returns a UClassID for this class.
  254. */
  255. static UClassID U_EXPORT2 getStaticClassID();
  256. /**
  257. * ICU "poor man's RTTI", returns a UClassID for the actual class.
  258. */
  259. virtual UClassID getDynamicClassID() const;
  260. private:
  261. // Disallow
  262. UStack(const UStack&);
  263. // Disallow
  264. UStack& operator=(const UStack&);
  265. };
  266. // UVector inlines
  267. inline int32_t UVector::size(void) const {
  268. return count;
  269. }
  270. inline UBool UVector::isEmpty(void) const {
  271. return count == 0;
  272. }
  273. inline UBool UVector::contains(void* obj) const {
  274. return indexOf(obj) >= 0;
  275. }
  276. inline UBool UVector::contains(int32_t obj) const {
  277. return indexOf(obj) >= 0;
  278. }
  279. inline void* UVector::firstElement(void) const {
  280. return elementAt(0);
  281. }
  282. inline void* UVector::lastElement(void) const {
  283. return elementAt(count-1);
  284. }
  285. inline int32_t UVector::lastElementi(void) const {
  286. return elementAti(count-1);
  287. }
  288. inline void* UVector::operator[](int32_t index) const {
  289. return elementAt(index);
  290. }
  291. inline UBool UVector::operator!=(const UVector& other) {
  292. return !operator==(other);
  293. }
  294. // UStack inlines
  295. inline UBool UStack::empty(void) const {
  296. return isEmpty();
  297. }
  298. inline void* UStack::peek(void) const {
  299. return lastElement();
  300. }
  301. inline int32_t UStack::peeki(void) const {
  302. return lastElementi();
  303. }
  304. inline void* UStack::push(void* obj, UErrorCode &status) {
  305. addElement(obj, status);
  306. return obj;
  307. }
  308. inline int32_t UStack::push(int32_t i, UErrorCode &status) {
  309. addElement(i, status);
  310. return i;
  311. }
  312. U_NAMESPACE_END
  313. #endif