sortkey.h 11 KB

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