uvector.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. * Ultralightweight C++ implementation of a `void*` vector
  23. * that is (mostly) compatible with java.util.Vector.
  24. *
  25. * 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. * *Design notes*
  32. *
  33. * 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 *do* avoid indexing off into the weeds.
  36. *
  37. * Since we don't have garbage collection, UVector was given the
  38. * option to *own* its contents. To employ this, set a deleter
  39. * function. The deleter is called on a `void *` pointer when that
  40. * pointer is released by the vector, either when the vector itself is
  41. * destructed, or when a call to `setElementAt()` overwrites an element,
  42. * or when a call to remove()` or one of its variants explicitly
  43. * removes an element. If no deleter is set, or the deleter is set to
  44. * zero, then it is assumed that the caller will delete elements as
  45. * needed.
  46. *
  47. * *Error Handling* Functions that can fail, from out of memory conditions
  48. * for example, include a UErrorCode parameter. Any function called
  49. * with an error code already indicating a failure will not modify the
  50. * vector in any way.
  51. *
  52. * For vectors that have a deleter function, any failure in inserting
  53. * an element into the vector will instead delete the element that
  54. * could not be adopted. This simplifies object ownership
  55. * management around calls to `addElement()` and `insertElementAt()`;
  56. * error or no, the function always takes ownership of an incoming object
  57. * from the caller.
  58. *
  59. * In order to implement methods such as `contains()` and `indexOf()`,
  60. * UVector needs a way to compare objects for equality. To do so, it
  61. * uses a comparison function, or "comparer." If the comparer is not
  62. * set, or is set to zero, then all such methods will act as if the
  63. * vector contains no element. That is, indexOf() will always return
  64. * -1, contains() will always return false, etc.
  65. *
  66. * <p><b>To do</b>
  67. *
  68. * <p>Improve the handling of index out of bounds errors.
  69. *
  70. * @author Alan Liu
  71. */
  72. class U_COMMON_API UVector : public UObject {
  73. // NOTE: UVector uses the UElement (union of void* and int32_t) as
  74. // its basic storage type. It uses UElementsAreEqual as its
  75. // comparison function. It uses UObjectDeleter as its deleter
  76. // function. This allows sharing of support functions with UHashtable.
  77. private:
  78. int32_t count = 0;
  79. int32_t capacity = 0;
  80. UElement* elements = nullptr;
  81. UObjectDeleter *deleter = nullptr;
  82. UElementsAreEqual *comparer = nullptr;
  83. public:
  84. UVector(UErrorCode &status);
  85. UVector(int32_t initialCapacity, UErrorCode &status);
  86. UVector(UObjectDeleter *d, UElementsAreEqual *c, UErrorCode &status);
  87. UVector(UObjectDeleter *d, UElementsAreEqual *c, int32_t initialCapacity, UErrorCode &status);
  88. virtual ~UVector();
  89. /**
  90. * Assign this object to another (make this a copy of 'other').
  91. * Use the 'assign' function to assign each element.
  92. */
  93. void assign(const UVector& other, UElementAssigner *assign, UErrorCode &ec);
  94. /**
  95. * Compare this vector with another. They will be considered
  96. * equal if they are of the same size and all elements are equal,
  97. * as compared using this object's comparer.
  98. */
  99. bool operator==(const UVector& other) const;
  100. /**
  101. * Equivalent to !operator==()
  102. */
  103. inline bool operator!=(const UVector& other) const {return !operator==(other);}
  104. //------------------------------------------------------------
  105. // java.util.Vector API
  106. //------------------------------------------------------------
  107. /**
  108. * Add an element at the end of the vector.
  109. * For use only with vectors that do not adopt their elements, which is to say,
  110. * have not set an element deleter function. See `adoptElement()`.
  111. */
  112. void addElement(void *obj, UErrorCode &status);
  113. /**
  114. * Add an element at the end of the vector.
  115. * For use only with vectors that adopt their elements, which is to say,
  116. * have set an element deleter function. See `addElement()`.
  117. *
  118. * If the element cannot be successfully added, it will be deleted. This is
  119. * normal ICU _adopt_ behavior - one way or another ownership of the incoming
  120. * object is transferred from the caller.
  121. *
  122. * `addElement()` and `adoptElement()` are separate functions to make it easier
  123. * to see what the function is doing at call sites. Having a single combined function,
  124. * as in earlier versions of UVector, had proved to be error-prone.
  125. */
  126. void adoptElement(void *obj, UErrorCode &status);
  127. void addElement(int32_t elem, UErrorCode &status);
  128. void setElementAt(void* obj, int32_t index);
  129. void setElementAt(int32_t elem, int32_t index);
  130. void insertElementAt(void* obj, int32_t index, UErrorCode &status);
  131. void insertElementAt(int32_t elem, int32_t index, UErrorCode &status);
  132. void* elementAt(int32_t index) const;
  133. int32_t elementAti(int32_t index) const;
  134. UBool equals(const UVector &other) const;
  135. inline void* firstElement() const {return elementAt(0);}
  136. inline void* lastElement() const {return elementAt(count-1);}
  137. inline int32_t lastElementi() const {return elementAti(count-1);}
  138. int32_t indexOf(void* obj, int32_t startIndex = 0) const;
  139. int32_t indexOf(int32_t obj, int32_t startIndex = 0) const;
  140. inline UBool contains(void* obj) const {return indexOf(obj) >= 0;}
  141. inline UBool contains(int32_t obj) const {return indexOf(obj) >= 0;}
  142. UBool containsAll(const UVector& other) const;
  143. UBool removeAll(const UVector& other);
  144. UBool retainAll(const UVector& other);
  145. void removeElementAt(int32_t index);
  146. UBool removeElement(void* obj);
  147. void removeAllElements();
  148. inline int32_t size() const {return count;}
  149. inline UBool isEmpty() const {return count == 0;}
  150. UBool ensureCapacity(int32_t minimumCapacity, UErrorCode &status);
  151. /**
  152. * Change the size of this vector as follows: If newSize is
  153. * smaller, then truncate the array, possibly deleting held
  154. * elements for i >= newSize. If newSize is larger, grow the
  155. * array, filling in new slots with nullptr.
  156. */
  157. void setSize(int32_t newSize, UErrorCode &status);
  158. /**
  159. * Fill in the given array with all elements of this vector.
  160. */
  161. void** toArray(void** result) const;
  162. //------------------------------------------------------------
  163. // New API
  164. //------------------------------------------------------------
  165. UObjectDeleter *setDeleter(UObjectDeleter *d);
  166. bool hasDeleter() {return deleter != nullptr;}
  167. UElementsAreEqual *setComparer(UElementsAreEqual *c);
  168. inline void* operator[](int32_t index) const {return elementAt(index);}
  169. /**
  170. * Removes the element at the given index from this vector and
  171. * transfer ownership of it to the caller. After this call, the
  172. * caller owns the result and must delete it and the vector entry
  173. * at 'index' is removed, shifting all subsequent entries back by
  174. * one index and shortening the size of the vector by one. If the
  175. * index is out of range or if there is no item at the given index
  176. * then 0 is returned and the vector is unchanged.
  177. */
  178. void* orphanElementAt(int32_t index);
  179. /**
  180. * Returns true if this vector contains none of the elements
  181. * of the given vector.
  182. * @param other vector to be checked for containment
  183. * @return true if the test condition is met
  184. */
  185. UBool containsNone(const UVector& other) const;
  186. /**
  187. * Insert the given object into this vector at its sorted position
  188. * as defined by 'compare'. The current elements are assumed to
  189. * be sorted already.
  190. */
  191. void sortedInsert(void* obj, UElementComparator *compare, UErrorCode& ec);
  192. /**
  193. * Insert the given integer into this vector at its sorted position
  194. * as defined by 'compare'. The current elements are assumed to
  195. * be sorted already.
  196. */
  197. void sortedInsert(int32_t obj, UElementComparator *compare, UErrorCode& ec);
  198. /**
  199. * Sort the contents of the vector, assuming that the contents of the
  200. * vector are of type int32_t.
  201. */
  202. void sorti(UErrorCode &ec);
  203. /**
  204. * Sort the contents of this vector, using a caller-supplied function
  205. * to do the comparisons. (It's confusing that
  206. * UVector's UElementComparator function is different from the
  207. * UComparator function type defined in uarrsort.h)
  208. */
  209. void sort(UElementComparator *compare, UErrorCode &ec);
  210. /**
  211. * Stable sort the contents of this vector using a caller-supplied function
  212. * of type UComparator to do the comparison. Provides more flexibility
  213. * than UVector::sort() because an additional user parameter can be passed to
  214. * the comparison function.
  215. */
  216. void sortWithUComparator(UComparator *compare, const void *context, UErrorCode &ec);
  217. /**
  218. * ICU "poor man's RTTI", returns a UClassID for this class.
  219. */
  220. static UClassID U_EXPORT2 getStaticClassID();
  221. /**
  222. * ICU "poor man's RTTI", returns a UClassID for the actual class.
  223. */
  224. virtual UClassID getDynamicClassID() const override;
  225. private:
  226. int32_t indexOf(UElement key, int32_t startIndex = 0, int8_t hint = 0) const;
  227. void sortedInsert(UElement e, UElementComparator *compare, UErrorCode& ec);
  228. public:
  229. // Disallow
  230. UVector(const UVector&) = delete;
  231. // Disallow
  232. UVector& operator=(const UVector&) = delete;
  233. };
  234. /**
  235. * Ultralightweight C++ implementation of a `void*` stack
  236. * that is (mostly) compatible with java.util.Stack. As in java, this
  237. * is merely a paper thin layer around UVector. See the UVector
  238. * documentation for further information.
  239. *
  240. * *Design notes*
  241. *
  242. * The element at index `n-1` is (of course) the top of the
  243. * stack.
  244. *
  245. * The poorly named `empty()` method doesn't empty the
  246. * stack; it determines if the stack is empty.
  247. *
  248. * @author Alan Liu
  249. */
  250. class U_COMMON_API UStack : public UVector {
  251. public:
  252. UStack(UErrorCode &status);
  253. UStack(int32_t initialCapacity, UErrorCode &status);
  254. UStack(UObjectDeleter *d, UElementsAreEqual *c, UErrorCode &status);
  255. UStack(UObjectDeleter *d, UElementsAreEqual *c, int32_t initialCapacity, UErrorCode &status);
  256. virtual ~UStack();
  257. // It's okay not to have a virtual destructor (in UVector)
  258. // because UStack has no special cleanup to do.
  259. inline UBool empty() const {return isEmpty();}
  260. inline void* peek() const {return lastElement();}
  261. inline int32_t peeki() const {return lastElementi();}
  262. /**
  263. * Pop and return an element from the stack.
  264. * For stacks with a deleter function, the caller takes ownership
  265. * of the popped element.
  266. */
  267. void* pop();
  268. int32_t popi();
  269. inline void* push(void* obj, UErrorCode &status) {
  270. if (hasDeleter()) {
  271. adoptElement(obj, status);
  272. return (U_SUCCESS(status)) ? obj : nullptr;
  273. } else {
  274. addElement(obj, status);
  275. return obj;
  276. }
  277. }
  278. inline int32_t push(int32_t i, UErrorCode &status) {
  279. addElement(i, status);
  280. return i;
  281. }
  282. /*
  283. If the object o occurs as an item in this stack,
  284. this method returns the 1-based distance from the top of the stack.
  285. */
  286. int32_t search(void* obj) const;
  287. /**
  288. * ICU "poor man's RTTI", returns a UClassID for this class.
  289. */
  290. static UClassID U_EXPORT2 getStaticClassID();
  291. /**
  292. * ICU "poor man's RTTI", returns a UClassID for the actual class.
  293. */
  294. virtual UClassID getDynamicClassID() const override;
  295. // Disallow
  296. UStack(const UStack&) = delete;
  297. // Disallow
  298. UStack& operator=(const UStack&) = delete;
  299. };
  300. U_NAMESPACE_END
  301. #endif