sortkey.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. *****************************************************************************
  5. * Copyright (C) 1996-2014, International Business Machines Corporation and others.
  6. * All Rights Reserved.
  7. *****************************************************************************
  8. *
  9. * File sortkey.h
  10. *
  11. * Created by: Helena Shih
  12. *
  13. * Modification History:
  14. *
  15. * Date Name Description
  16. *
  17. * 6/20/97 helena Java class name change.
  18. * 8/18/97 helena Added internal API documentation.
  19. * 6/26/98 erm Changed to use byte arrays and memcmp.
  20. *****************************************************************************
  21. */
  22. #ifndef SORTKEY_H
  23. #define SORTKEY_H
  24. #include "unicode/utypes.h"
  25. #if U_SHOW_CPLUSPLUS_API
  26. /**
  27. * \file
  28. * \brief C++ API: Keys for comparing strings multiple times.
  29. */
  30. #if !UCONFIG_NO_COLLATION
  31. #include "unicode/uobject.h"
  32. #include "unicode/unistr.h"
  33. #include "unicode/coll.h"
  34. U_NAMESPACE_BEGIN
  35. /* forward declaration */
  36. class RuleBasedCollator;
  37. class CollationKeyByteSink;
  38. /**
  39. *
  40. * Collation keys are generated by the Collator class. Use the CollationKey objects
  41. * instead of Collator to compare strings multiple times. A CollationKey
  42. * preprocesses the comparison information from the Collator object to
  43. * make the comparison faster. If you are not going to comparing strings
  44. * multiple times, then using the Collator object is generally faster,
  45. * since it only processes as much of the string as needed to make a
  46. * comparison.
  47. * <p> For example (with strength == tertiary)
  48. * <p>When comparing "Abernathy" to "Baggins-Smythworthy", Collator
  49. * only needs to process a couple of characters, while a comparison
  50. * with CollationKeys will process all of the characters. On the other hand,
  51. * if you are doing a sort of a number of fields, it is much faster to use
  52. * CollationKeys, since you will be comparing strings multiple times.
  53. * <p>Typical use of CollationKeys are in databases, where you store a CollationKey
  54. * in a hidden field, and use it for sorting or indexing.
  55. *
  56. * <p>Example of use:
  57. * <pre>
  58. * \code
  59. * UErrorCode success = U_ZERO_ERROR;
  60. * Collator* myCollator = Collator::createInstance(success);
  61. * CollationKey* keys = new CollationKey [3];
  62. * myCollator->getCollationKey("Tom", keys[0], success );
  63. * myCollator->getCollationKey("Dick", keys[1], success );
  64. * myCollator->getCollationKey("Harry", keys[2], success );
  65. *
  66. * // Inside body of sort routine, compare keys this way:
  67. * CollationKey tmp;
  68. * if(keys[0].compareTo( keys[1] ) > 0 ) {
  69. * tmp = keys[0]; keys[0] = keys[1]; keys[1] = tmp;
  70. * }
  71. * //...
  72. * \endcode
  73. * </pre>
  74. * <p>Because Collator::compare()'s algorithm is complex, it is faster to sort
  75. * long lists of words by retrieving collation keys with Collator::getCollationKey().
  76. * You can then cache the collation keys and compare them using CollationKey::compareTo().
  77. * <p>
  78. * <strong>Note:</strong> <code>Collator</code>s with different Locale,
  79. * CollationStrength and DecompositionMode settings will return different
  80. * CollationKeys for the same set of strings. Locales have specific
  81. * collation rules, and the way in which secondary and tertiary differences
  82. * are taken into account, for example, will result in different CollationKeys
  83. * for same strings.
  84. * <p>
  85. * @see Collator
  86. * @see RuleBasedCollator
  87. * @version 1.3 12/18/96
  88. * @author Helena Shih
  89. * @stable ICU 2.0
  90. */
  91. class U_I18N_API CollationKey : public UObject {
  92. public:
  93. /**
  94. * This creates an empty collation key based on the null string. An empty
  95. * collation key contains no sorting information. When comparing two empty
  96. * collation keys, the result is Collator::EQUAL. Comparing empty collation key
  97. * with non-empty collation key is always Collator::LESS.
  98. * @stable ICU 2.0
  99. */
  100. CollationKey();
  101. /**
  102. * Creates a collation key based on the collation key values.
  103. * @param values the collation key values
  104. * @param count number of collation key values, including trailing nulls.
  105. * @stable ICU 2.0
  106. */
  107. CollationKey(const uint8_t* values,
  108. int32_t count);
  109. /**
  110. * Copy constructor.
  111. * @param other the object to be copied.
  112. * @stable ICU 2.0
  113. */
  114. CollationKey(const CollationKey& other);
  115. /**
  116. * Sort key destructor.
  117. * @stable ICU 2.0
  118. */
  119. virtual ~CollationKey();
  120. /**
  121. * Assignment operator
  122. * @param other the object to be copied.
  123. * @stable ICU 2.0
  124. */
  125. const CollationKey& operator=(const CollationKey& other);
  126. /**
  127. * Compare if two collation keys are the same.
  128. * @param source the collation key to compare to.
  129. * @return Returns true if two collation keys are equal, false otherwise.
  130. * @stable ICU 2.0
  131. */
  132. bool operator==(const CollationKey& source) const;
  133. /**
  134. * Compare if two collation keys are not the same.
  135. * @param source the collation key to compare to.
  136. * @return Returns true if two collation keys are different, false otherwise.
  137. * @stable ICU 2.0
  138. */
  139. bool operator!=(const CollationKey& source) const;
  140. /**
  141. * Test to see if the key is in an invalid state. The key will be in an
  142. * invalid state if it couldn't allocate memory for some operation.
  143. * @return Returns true if the key is in an invalid, false otherwise.
  144. * @stable ICU 2.0
  145. */
  146. UBool isBogus() const;
  147. /**
  148. * Returns a pointer to the collation key values. The storage is owned
  149. * by the collation key and the pointer will become invalid if the key
  150. * is deleted.
  151. * @param count the output parameter of number of collation key values,
  152. * including any trailing nulls.
  153. * @return a pointer to the collation key values.
  154. * @stable ICU 2.0
  155. */
  156. const uint8_t* getByteArray(int32_t& count) const;
  157. #ifdef U_USE_COLLATION_KEY_DEPRECATES
  158. /**
  159. * Extracts the collation key values into a new array. The caller owns
  160. * this storage and should free it.
  161. * @param count the output parameter of number of collation key values,
  162. * including any trailing nulls.
  163. * @obsolete ICU 2.6. Use getByteArray instead since this API will be removed in that release.
  164. */
  165. uint8_t* toByteArray(int32_t& count) const;
  166. #endif
  167. #ifndef U_HIDE_DEPRECATED_API
  168. /**
  169. * Convenience method which does a string(bit-wise) comparison of the
  170. * two collation keys.
  171. * @param target target collation key to be compared with
  172. * @return Returns Collator::LESS if sourceKey &lt; targetKey,
  173. * Collator::GREATER if sourceKey > targetKey and Collator::EQUAL
  174. * otherwise.
  175. * @deprecated ICU 2.6 use the overload with error code
  176. */
  177. Collator::EComparisonResult compareTo(const CollationKey& target) const;
  178. #endif /* U_HIDE_DEPRECATED_API */
  179. /**
  180. * Convenience method which does a string(bit-wise) comparison of the
  181. * two collation keys.
  182. * @param target target collation key to be compared with
  183. * @param status error code
  184. * @return Returns UCOL_LESS if sourceKey &lt; targetKey,
  185. * UCOL_GREATER if sourceKey > targetKey and UCOL_EQUAL
  186. * otherwise.
  187. * @stable ICU 2.6
  188. */
  189. UCollationResult compareTo(const CollationKey& target, UErrorCode &status) const;
  190. /**
  191. * Creates an integer that is unique to the collation key. NOTE: this
  192. * is not the same as String.hashCode.
  193. * <p>Example of use:
  194. * <pre>
  195. * . UErrorCode status = U_ZERO_ERROR;
  196. * . Collator *myCollation = Collator::createInstance(Locale::US, status);
  197. * . if (U_FAILURE(status)) return;
  198. * . CollationKey key1, key2;
  199. * . UErrorCode status1 = U_ZERO_ERROR, status2 = U_ZERO_ERROR;
  200. * . myCollation->getCollationKey("abc", key1, status1);
  201. * . if (U_FAILURE(status1)) { delete myCollation; return; }
  202. * . myCollation->getCollationKey("ABC", key2, status2);
  203. * . if (U_FAILURE(status2)) { delete myCollation; return; }
  204. * . // key1.hashCode() != key2.hashCode()
  205. * </pre>
  206. * @return the hash value based on the string's collation order.
  207. * @see UnicodeString#hashCode
  208. * @stable ICU 2.0
  209. */
  210. int32_t hashCode() const;
  211. /**
  212. * ICU "poor man's RTTI", returns a UClassID for the actual class.
  213. * @stable ICU 2.2
  214. */
  215. virtual UClassID getDynamicClassID() const override;
  216. /**
  217. * ICU "poor man's RTTI", returns a UClassID for this class.
  218. * @stable ICU 2.2
  219. */
  220. static UClassID U_EXPORT2 getStaticClassID();
  221. private:
  222. /**
  223. * Replaces the current bytes buffer with a new one of newCapacity
  224. * and copies length bytes from the old buffer to the new one.
  225. * @return the new buffer, or nullptr if the allocation failed
  226. */
  227. uint8_t *reallocate(int32_t newCapacity, int32_t length);
  228. /**
  229. * Set a new length for a new sort key in the existing fBytes.
  230. */
  231. void setLength(int32_t newLength);
  232. uint8_t *getBytes() {
  233. return (fFlagAndLength >= 0) ? fUnion.fStackBuffer : fUnion.fFields.fBytes;
  234. }
  235. const uint8_t *getBytes() const {
  236. return (fFlagAndLength >= 0) ? fUnion.fStackBuffer : fUnion.fFields.fBytes;
  237. }
  238. int32_t getCapacity() const {
  239. return (fFlagAndLength >= 0) ? (int32_t)sizeof(fUnion) : fUnion.fFields.fCapacity;
  240. }
  241. int32_t getLength() const { return fFlagAndLength & 0x7fffffff; }
  242. /**
  243. * Set the CollationKey to a "bogus" or invalid state
  244. * @return this CollationKey
  245. */
  246. CollationKey& setToBogus();
  247. /**
  248. * Resets this CollationKey to an empty state
  249. * @return this CollationKey
  250. */
  251. CollationKey& reset();
  252. /**
  253. * Allow private access to RuleBasedCollator
  254. */
  255. friend class RuleBasedCollator;
  256. friend class CollationKeyByteSink;
  257. // Class fields. sizeof(CollationKey) is intended to be 48 bytes
  258. // on a machine with 64-bit pointers.
  259. // We use a union to maximize the size of the internal buffer,
  260. // similar to UnicodeString but not as tight and complex.
  261. // (implicit) *vtable;
  262. /**
  263. * Sort key length and flag.
  264. * Bit 31 is set if the buffer is heap-allocated.
  265. * Bits 30..0 contain the sort key length.
  266. */
  267. int32_t fFlagAndLength;
  268. /**
  269. * Unique hash value of this CollationKey.
  270. * Special value 2 if the key is bogus.
  271. */
  272. mutable int32_t fHashCode;
  273. /**
  274. * fUnion provides 32 bytes for the internal buffer or for
  275. * pointer+capacity.
  276. */
  277. union StackBufferOrFields {
  278. /** fStackBuffer is used iff fFlagAndLength>=0, else fFields is used */
  279. uint8_t fStackBuffer[32];
  280. struct {
  281. uint8_t *fBytes;
  282. int32_t fCapacity;
  283. } fFields;
  284. } fUnion;
  285. };
  286. inline bool
  287. CollationKey::operator!=(const CollationKey& other) const
  288. {
  289. return !(*this == other);
  290. }
  291. inline UBool
  292. CollationKey::isBogus() const
  293. {
  294. return fHashCode == 2; // kBogusHashCode
  295. }
  296. inline const uint8_t*
  297. CollationKey::getByteArray(int32_t &count) const
  298. {
  299. count = getLength();
  300. return getBytes();
  301. }
  302. U_NAMESPACE_END
  303. #endif /* #if !UCONFIG_NO_COLLATION */
  304. #endif /* U_SHOW_CPLUSPLUS_API */
  305. #endif