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