2 *****************************************************************************
3 * Copyright (C) 1996-2013, International Business Machines Corporation and others.
5 *****************************************************************************
9 * Created by: Helena Shih
11 * Modification History:
13 * Date Name Description
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 *****************************************************************************
24 #include "unicode/utypes.h"
28 * \brief C++ API: Keys for comparing strings multiple times.
31 #if !UCONFIG_NO_COLLATION
33 #include "unicode/uobject.h"
34 #include "unicode/unistr.h"
35 #include "unicode/coll.h"
39 /* forward declaration */
40 class RuleBasedCollator;
44 * Collation keys are generated by the Collator class. Use the CollationKey objects
45 * instead of Collator to compare strings multiple times. A CollationKey
46 * preprocesses the comparison information from the Collator object to
47 * make the comparison faster. If you are not going to comparing strings
48 * multiple times, then using the Collator object is generally faster,
49 * since it only processes as much of the string as needed to make a
51 * <p> For example (with strength == tertiary)
52 * <p>When comparing "Abernathy" to "Baggins-Smythworthy", Collator
53 * only needs to process a couple of characters, while a comparison
54 * with CollationKeys will process all of the characters. On the other hand,
55 * if you are doing a sort of a number of fields, it is much faster to use
56 * CollationKeys, since you will be comparing strings multiple times.
57 * <p>Typical use of CollationKeys are in databases, where you store a CollationKey
58 * in a hidden field, and use it for sorting or indexing.
63 * UErrorCode success = U_ZERO_ERROR;
64 * Collator* myCollator = Collator::createInstance(success);
65 * CollationKey* keys = new CollationKey [3];
66 * myCollator->getCollationKey("Tom", keys[0], success );
67 * myCollator->getCollationKey("Dick", keys[1], success );
68 * myCollator->getCollationKey("Harry", keys[2], success );
70 * // Inside body of sort routine, compare keys this way:
72 * if(keys[0].compareTo( keys[1] ) > 0 ) {
73 * tmp = keys[0]; keys[0] = keys[1]; keys[1] = tmp;
78 * <p>Because Collator::compare()'s algorithm is complex, it is faster to sort
79 * long lists of words by retrieving collation keys with Collator::getCollationKey().
80 * You can then cache the collation keys and compare them using CollationKey::compareTo().
82 * <strong>Note:</strong> <code>Collator</code>s with different Locale,
83 * CollationStrength and DecompositionMode settings will return different
84 * CollationKeys for the same set of strings. Locales have specific
85 * collation rules, and the way in which secondary and tertiary differences
86 * are taken into account, for example, will result in different CollationKeys
91 * @see RuleBasedCollator
92 * @version 1.3 12/18/96
96 class U_I18N_API CollationKey : public UObject {
99 * This creates an empty collation key based on the null string. An empty
100 * collation key contains no sorting information. When comparing two empty
101 * collation keys, the result is Collator::EQUAL. Comparing empty collation key
102 * with non-empty collation key is always Collator::LESS.
109 * Creates a collation key based on the collation key values.
110 * @param values the collation key values
111 * @param count number of collation key values, including trailing nulls.
114 CollationKey(const uint8_t* values,
119 * @param other the object to be copied.
122 CollationKey(const CollationKey& other);
125 * Sort key destructor.
128 virtual ~CollationKey();
131 * Assignment operator
132 * @param other the object to be copied.
135 const CollationKey& operator=(const CollationKey& other);
138 * Compare if two collation keys are the same.
139 * @param source the collation key to compare to.
140 * @return Returns true if two collation keys are equal, false otherwise.
143 UBool operator==(const CollationKey& source) const;
146 * Compare if two collation keys are not the same.
147 * @param source the collation key to compare to.
148 * @return Returns TRUE if two collation keys are different, FALSE otherwise.
151 UBool operator!=(const CollationKey& source) const;
155 * Test to see if the key is in an invalid state. The key will be in an
156 * invalid state if it couldn't allocate memory for some operation.
157 * @return Returns TRUE if the key is in an invalid, FALSE otherwise.
160 UBool isBogus(void) const;
163 * Returns a pointer to the collation key values. The storage is owned
164 * by the collation key and the pointer will become invalid if the key
166 * @param count the output parameter of number of collation key values,
167 * including any trailing nulls.
168 * @return a pointer to the collation key values.
171 const uint8_t* getByteArray(int32_t& count) const;
173 #ifdef U_USE_COLLATION_KEY_DEPRECATES
175 * Extracts the collation key values into a new array. The caller owns
176 * this storage and should free it.
177 * @param count the output parameter of number of collation key values,
178 * including any trailing nulls.
179 * @obsolete ICU 2.6. Use getByteArray instead since this API will be removed in that release.
181 uint8_t* toByteArray(int32_t& count) const;
184 #ifndef U_HIDE_DEPRECATED_API
186 * Convenience method which does a string(bit-wise) comparison of the
187 * two collation keys.
188 * @param target target collation key to be compared with
189 * @return Returns Collator::LESS if sourceKey < targetKey,
190 * Collator::GREATER if sourceKey > targetKey and Collator::EQUAL
192 * @deprecated ICU 2.6 use the overload with error code
194 Collator::EComparisonResult compareTo(const CollationKey& target) const;
195 #endif /* U_HIDE_DEPRECATED_API */
198 * Convenience method which does a string(bit-wise) comparison of the
199 * two collation keys.
200 * @param target target collation key to be compared with
201 * @param status error code
202 * @return Returns UCOL_LESS if sourceKey < targetKey,
203 * UCOL_GREATER if sourceKey > targetKey and UCOL_EQUAL
207 UCollationResult compareTo(const CollationKey& target, UErrorCode &status) const;
210 * Creates an integer that is unique to the collation key. NOTE: this
211 * is not the same as String.hashCode.
214 * . UErrorCode status = U_ZERO_ERROR;
215 * . Collator *myCollation = Collator::createInstance(Locale::US, status);
216 * . if (U_FAILURE(status)) return;
217 * . CollationKey key1, key2;
218 * . UErrorCode status1 = U_ZERO_ERROR, status2 = U_ZERO_ERROR;
219 * . myCollation->getCollationKey("abc", key1, status1);
220 * . if (U_FAILURE(status1)) { delete myCollation; return; }
221 * . myCollation->getCollationKey("ABC", key2, status2);
222 * . if (U_FAILURE(status2)) { delete myCollation; return; }
223 * . // key1.hashCode() != key2.hashCode()
225 * @return the hash value based on the string's collation order.
226 * @see UnicodeString#hashCode
229 int32_t hashCode(void) const;
232 * ICU "poor man's RTTI", returns a UClassID for the actual class.
235 virtual UClassID getDynamicClassID() const;
238 * ICU "poor man's RTTI", returns a UClassID for this class.
241 static UClassID U_EXPORT2 getStaticClassID();
245 * Replaces the current bytes buffer with a new one of newCapacity
246 * and copies length bytes from the old buffer to the new one.
247 * @return the new buffer, or NULL if the allocation failed
249 uint8_t *reallocate(int32_t newCapacity, int32_t length);
251 * Set a new length for a new sort key in the existing fBytes.
253 void setLength(int32_t newLength);
255 uint8_t *getBytes() {
256 return (fFlagAndLength >= 0) ? fUnion.fStackBuffer : fUnion.fFields.fBytes;
258 const uint8_t *getBytes() const {
259 return (fFlagAndLength >= 0) ? fUnion.fStackBuffer : fUnion.fFields.fBytes;
261 int32_t getCapacity() const {
262 return (fFlagAndLength >= 0) ? (int32_t)sizeof(fUnion) : fUnion.fFields.fCapacity;
264 int32_t getLength() const { return fFlagAndLength & 0x7fffffff; }
267 * Set the CollationKey to a "bogus" or invalid state
268 * @return this CollationKey
270 CollationKey& setToBogus(void);
272 * Resets this CollationKey to an empty state
273 * @return this CollationKey
275 CollationKey& reset(void);
278 * Allow private access to RuleBasedCollator
280 friend class RuleBasedCollator;
281 friend class CollationKeyByteSink;
283 // Class fields. sizeof(CollationKey) is intended to be 48 bytes
284 // on a machine with 64-bit pointers.
285 // We use a union to maximize the size of the internal buffer,
286 // similar to UnicodeString but not as tight and complex.
288 // (implicit) *vtable;
290 * Sort key length and flag.
291 * Bit 31 is set if the buffer is heap-allocated.
292 * Bits 30..0 contain the sort key length.
294 int32_t fFlagAndLength;
296 * Unique hash value of this CollationKey.
297 * Special value 2 if the key is bogus.
299 mutable int32_t fHashCode;
301 * fUnion provides 32 bytes for the internal buffer or for
304 union StackBufferOrFields {
305 /** fStackBuffer is used iff fFlagAndLength>=0, else fFields is used */
306 uint8_t fStackBuffer[32];
315 CollationKey::operator!=(const CollationKey& other) const
317 return !(*this == other);
321 CollationKey::isBogus() const
323 return fHashCode == 2; // kBogusHashCode
326 inline const uint8_t*
327 CollationKey::getByteArray(int32_t &count) const
335 #endif /* #if !UCONFIG_NO_COLLATION */