]>
git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/unicode/sortkey.h
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *****************************************************************************
5 * Copyright (C) 1996-2014, International Business Machines Corporation and others.
7 *****************************************************************************
11 * Created by: Helena Shih
13 * Modification History:
15 * Date Name Description
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 *****************************************************************************
26 #include "unicode/utypes.h"
30 * \brief C++ API: Keys for comparing strings multiple times.
33 #if !UCONFIG_NO_COLLATION
35 #include "unicode/uobject.h"
36 #include "unicode/unistr.h"
37 #include "unicode/coll.h"
39 #if U_SHOW_CPLUSPLUS_API
42 /* forward declaration */
43 class RuleBasedCollator
;
44 class CollationKeyByteSink
;
48 * Collation keys are generated by the Collator class. Use the CollationKey objects
49 * instead of Collator to compare strings multiple times. A CollationKey
50 * preprocesses the comparison information from the Collator object to
51 * make the comparison faster. If you are not going to comparing strings
52 * multiple times, then using the Collator object is generally faster,
53 * since it only processes as much of the string as needed to make a
55 * <p> For example (with strength == tertiary)
56 * <p>When comparing "Abernathy" to "Baggins-Smythworthy", Collator
57 * only needs to process a couple of characters, while a comparison
58 * with CollationKeys will process all of the characters. On the other hand,
59 * if you are doing a sort of a number of fields, it is much faster to use
60 * CollationKeys, since you will be comparing strings multiple times.
61 * <p>Typical use of CollationKeys are in databases, where you store a CollationKey
62 * in a hidden field, and use it for sorting or indexing.
67 * UErrorCode success = U_ZERO_ERROR;
68 * Collator* myCollator = Collator::createInstance(success);
69 * CollationKey* keys = new CollationKey [3];
70 * myCollator->getCollationKey("Tom", keys[0], success );
71 * myCollator->getCollationKey("Dick", keys[1], success );
72 * myCollator->getCollationKey("Harry", keys[2], success );
74 * // Inside body of sort routine, compare keys this way:
76 * if(keys[0].compareTo( keys[1] ) > 0 ) {
77 * tmp = keys[0]; keys[0] = keys[1]; keys[1] = tmp;
82 * <p>Because Collator::compare()'s algorithm is complex, it is faster to sort
83 * long lists of words by retrieving collation keys with Collator::getCollationKey().
84 * You can then cache the collation keys and compare them using CollationKey::compareTo().
86 * <strong>Note:</strong> <code>Collator</code>s with different Locale,
87 * CollationStrength and DecompositionMode settings will return different
88 * CollationKeys for the same set of strings. Locales have specific
89 * collation rules, and the way in which secondary and tertiary differences
90 * are taken into account, for example, will result in different CollationKeys
95 * @see RuleBasedCollator
96 * @version 1.3 12/18/96
100 class U_I18N_API CollationKey
: public UObject
{
103 * This creates an empty collation key based on the null string. An empty
104 * collation key contains no sorting information. When comparing two empty
105 * collation keys, the result is Collator::EQUAL. Comparing empty collation key
106 * with non-empty collation key is always Collator::LESS.
113 * Creates a collation key based on the collation key values.
114 * @param values the collation key values
115 * @param count number of collation key values, including trailing nulls.
118 CollationKey(const uint8_t* values
,
123 * @param other the object to be copied.
126 CollationKey(const CollationKey
& other
);
129 * Sort key destructor.
132 virtual ~CollationKey();
135 * Assignment operator
136 * @param other the object to be copied.
139 const CollationKey
& operator=(const CollationKey
& other
);
142 * Compare if two collation keys are the same.
143 * @param source the collation key to compare to.
144 * @return Returns true if two collation keys are equal, false otherwise.
147 UBool
operator==(const CollationKey
& source
) const;
150 * Compare if two collation keys are not the same.
151 * @param source the collation key to compare to.
152 * @return Returns TRUE if two collation keys are different, FALSE otherwise.
155 UBool
operator!=(const CollationKey
& source
) const;
159 * Test to see if the key is in an invalid state. The key will be in an
160 * invalid state if it couldn't allocate memory for some operation.
161 * @return Returns TRUE if the key is in an invalid, FALSE otherwise.
164 UBool
isBogus(void) const;
167 * Returns a pointer to the collation key values. The storage is owned
168 * by the collation key and the pointer will become invalid if the key
170 * @param count the output parameter of number of collation key values,
171 * including any trailing nulls.
172 * @return a pointer to the collation key values.
175 const uint8_t* getByteArray(int32_t& count
) const;
177 #ifdef U_USE_COLLATION_KEY_DEPRECATES
179 * Extracts the collation key values into a new array. The caller owns
180 * this storage and should free it.
181 * @param count the output parameter of number of collation key values,
182 * including any trailing nulls.
183 * @obsolete ICU 2.6. Use getByteArray instead since this API will be removed in that release.
185 uint8_t* toByteArray(int32_t& count
) const;
188 #ifndef U_HIDE_DEPRECATED_API
190 * Convenience method which does a string(bit-wise) comparison of the
191 * two collation keys.
192 * @param target target collation key to be compared with
193 * @return Returns Collator::LESS if sourceKey < targetKey,
194 * Collator::GREATER if sourceKey > targetKey and Collator::EQUAL
196 * @deprecated ICU 2.6 use the overload with error code
198 Collator::EComparisonResult
compareTo(const CollationKey
& target
) const;
199 #endif /* U_HIDE_DEPRECATED_API */
202 * Convenience method which does a string(bit-wise) comparison of the
203 * two collation keys.
204 * @param target target collation key to be compared with
205 * @param status error code
206 * @return Returns UCOL_LESS if sourceKey < targetKey,
207 * UCOL_GREATER if sourceKey > targetKey and UCOL_EQUAL
211 UCollationResult
compareTo(const CollationKey
& target
, UErrorCode
&status
) const;
214 * Creates an integer that is unique to the collation key. NOTE: this
215 * is not the same as String.hashCode.
218 * . UErrorCode status = U_ZERO_ERROR;
219 * . Collator *myCollation = Collator::createInstance(Locale::US, status);
220 * . if (U_FAILURE(status)) return;
221 * . CollationKey key1, key2;
222 * . UErrorCode status1 = U_ZERO_ERROR, status2 = U_ZERO_ERROR;
223 * . myCollation->getCollationKey("abc", key1, status1);
224 * . if (U_FAILURE(status1)) { delete myCollation; return; }
225 * . myCollation->getCollationKey("ABC", key2, status2);
226 * . if (U_FAILURE(status2)) { delete myCollation; return; }
227 * . // key1.hashCode() != key2.hashCode()
229 * @return the hash value based on the string's collation order.
230 * @see UnicodeString#hashCode
233 int32_t hashCode(void) const;
236 * ICU "poor man's RTTI", returns a UClassID for the actual class.
239 virtual UClassID
getDynamicClassID() const;
242 * ICU "poor man's RTTI", returns a UClassID for this class.
245 static UClassID U_EXPORT2
getStaticClassID();
249 * Replaces the current bytes buffer with a new one of newCapacity
250 * and copies length bytes from the old buffer to the new one.
251 * @return the new buffer, or NULL if the allocation failed
253 uint8_t *reallocate(int32_t newCapacity
, int32_t length
);
255 * Set a new length for a new sort key in the existing fBytes.
257 void setLength(int32_t newLength
);
259 uint8_t *getBytes() {
260 return (fFlagAndLength
>= 0) ? fUnion
.fStackBuffer
: fUnion
.fFields
.fBytes
;
262 const uint8_t *getBytes() const {
263 return (fFlagAndLength
>= 0) ? fUnion
.fStackBuffer
: fUnion
.fFields
.fBytes
;
265 int32_t getCapacity() const {
266 return (fFlagAndLength
>= 0) ? (int32_t)sizeof(fUnion
) : fUnion
.fFields
.fCapacity
;
268 int32_t getLength() const { return fFlagAndLength
& 0x7fffffff; }
271 * Set the CollationKey to a "bogus" or invalid state
272 * @return this CollationKey
274 CollationKey
& setToBogus(void);
276 * Resets this CollationKey to an empty state
277 * @return this CollationKey
279 CollationKey
& reset(void);
282 * Allow private access to RuleBasedCollator
284 friend class RuleBasedCollator
;
285 friend class CollationKeyByteSink
;
287 // Class fields. sizeof(CollationKey) is intended to be 48 bytes
288 // on a machine with 64-bit pointers.
289 // We use a union to maximize the size of the internal buffer,
290 // similar to UnicodeString but not as tight and complex.
292 // (implicit) *vtable;
294 * Sort key length and flag.
295 * Bit 31 is set if the buffer is heap-allocated.
296 * Bits 30..0 contain the sort key length.
298 int32_t fFlagAndLength
;
300 * Unique hash value of this CollationKey.
301 * Special value 2 if the key is bogus.
303 mutable int32_t fHashCode
;
305 * fUnion provides 32 bytes for the internal buffer or for
308 union StackBufferOrFields
{
309 /** fStackBuffer is used iff fFlagAndLength>=0, else fFields is used */
310 uint8_t fStackBuffer
[32];
319 CollationKey::operator!=(const CollationKey
& other
) const
321 return !(*this == other
);
325 CollationKey::isBogus() const
327 return fHashCode
== 2; // kBogusHashCode
330 inline const uint8_t*
331 CollationKey::getByteArray(int32_t &count
) const
338 #endif // U_SHOW_CPLUSPLUS_API
340 #endif /* #if !UCONFIG_NO_COLLATION */