sortkey.h 11 KB

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