]> git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/unicode/sortkey.h
ICU-491.11.3.tar.gz
[apple/icu.git] / icuSources / i18n / unicode / sortkey.h
1 /*
2 *****************************************************************************
3 * Copyright (C) 1996-2011, 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
21 #ifndef SORTKEY_H
22 #define SORTKEY_H
23
24 #include "unicode/utypes.h"
25
26 /**
27 * \file
28 * \brief C++ API: Keys for comparing strings multiple times.
29 */
30
31 #if !UCONFIG_NO_COLLATION
32 #ifndef U_HIDE_DEPRECATED_API
33
34 #include "unicode/uobject.h"
35 #include "unicode/unistr.h"
36 #include "unicode/coll.h"
37
38 U_NAMESPACE_BEGIN
39
40 /* forward declaration */
41 class RuleBasedCollator;
42
43 /**
44 *
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
95 * @deprecated ICU 2.8 Use Collator::getSortKey(...) instead
96 */
97 class U_I18N_API CollationKey : public UObject {
98 public:
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.
104 * @deprecated ICU 2.8 Use Collator::getSortKey(...) instead
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.
113 * @deprecated ICU 2.8 Use Collator::getSortKey(...) instead
114 */
115 CollationKey(const uint8_t* values,
116 int32_t count);
117
118 /**
119 * Copy constructor.
120 * @param other the object to be copied.
121 * @deprecated ICU 2.8 Use Collator::getSortKey(...) instead
122 */
123 CollationKey(const CollationKey& other);
124
125 /**
126 * Sort key destructor.
127 * @deprecated ICU 2.8 Use Collator::getSortKey(...) instead
128 */
129 virtual ~CollationKey();
130
131 /**
132 * Assignment operator
133 * @param other the object to be copied.
134 * @deprecated ICU 2.8 Use Collator::getSortKey(...) instead
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.
142 * @deprecated ICU 2.8 Use Collator::getSortKey(...) instead
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.
150 * @deprecated ICU 2.8 Use Collator::getSortKey(...) instead
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.
159 * @deprecated ICU 2.8 Use Collator::getSortKey(...) instead
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.
170 * @deprecated ICU 2.8 Use Collator::getSortKey(...) instead
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
185 /**
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 &lt; targetKey,
190 * Collator::GREATER if sourceKey > targetKey and Collator::EQUAL
191 * otherwise.
192 * @deprecated ICU 2.6 use the overload with error code
193 */
194 Collator::EComparisonResult compareTo(const CollationKey& target) const;
195
196 /**
197 * Convenience method which does a string(bit-wise) comparison of the
198 * two collation keys.
199 * @param target target collation key to be compared with
200 * @param status error code
201 * @return Returns UCOL_LESS if sourceKey &lt; targetKey,
202 * UCOL_GREATER if sourceKey > targetKey and UCOL_EQUAL
203 * otherwise.
204 * @deprecated ICU 2.8 Use Collator::getSortKey(...) instead
205 */
206 UCollationResult compareTo(const CollationKey& target, UErrorCode &status) const;
207
208 /**
209 * Creates an integer that is unique to the collation key. NOTE: this
210 * is not the same as String.hashCode.
211 * <p>Example of use:
212 * <pre>
213 * . UErrorCode status = U_ZERO_ERROR;
214 * . Collator *myCollation = Collator::createInstance(Locale::US, status);
215 * . if (U_FAILURE(status)) return;
216 * . CollationKey key1, key2;
217 * . UErrorCode status1 = U_ZERO_ERROR, status2 = U_ZERO_ERROR;
218 * . myCollation->getCollationKey("abc", key1, status1);
219 * . if (U_FAILURE(status1)) { delete myCollation; return; }
220 * . myCollation->getCollationKey("ABC", key2, status2);
221 * . if (U_FAILURE(status2)) { delete myCollation; return; }
222 * . // key1.hashCode() != key2.hashCode()
223 * </pre>
224 * @return the hash value based on the string's collation order.
225 * @see UnicodeString#hashCode
226 * @deprecated ICU 2.8 Use Collator::getSortKey(...) instead
227 */
228 int32_t hashCode(void) const;
229
230 /**
231 * ICU "poor man's RTTI", returns a UClassID for the actual class.
232 * @deprecated ICU 2.8 Use Collator::getSortKey(...) instead
233 */
234 virtual UClassID getDynamicClassID() const;
235
236 /**
237 * ICU "poor man's RTTI", returns a UClassID for this class.
238 * @deprecated ICU 2.8 Use Collator::getSortKey(...) instead
239 */
240 static UClassID U_EXPORT2 getStaticClassID();
241
242 private:
243 /**
244 * Returns an array of the collation key values as 16-bit integers.
245 * The caller owns the storage and must delete it.
246 * @param values Output param of the collation key values.
247 * @param capacity Size of the values array.
248 * @param count output parameter of the number of collation key values
249 * @return a pointer to an array of 16-bit collation key values.
250 */
251 void adopt(uint8_t *values, int32_t capacity, int32_t count);
252 /**
253 * Set a new length for a new sort key in the existing fBytes.
254 */
255 void setLength(int32_t newLength);
256
257 /*
258 * Creates a collation key with a string.
259 */
260
261 /**
262 * If this CollationKey has capacity less than newSize,
263 * its internal capacity will be increased to newSize.
264 * @param newSize minimum size this CollationKey has to have
265 * @return this CollationKey
266 */
267 CollationKey& ensureCapacity(int32_t newSize);
268 /**
269 * Set the CollationKey to a "bogus" or invalid state
270 * @return this CollationKey
271 */
272 CollationKey& setToBogus(void);
273 /**
274 * Resets this CollationKey to an empty state
275 * @return this CollationKey
276 */
277 CollationKey& reset(void);
278
279 /**
280 * Allow private access to RuleBasedCollator
281 */
282 friend class RuleBasedCollator;
283 /**
284 * Bogus status
285 */
286 UBool fBogus;
287 /**
288 * Size of fBytes used to store the sortkey. i.e. up till the
289 * null-termination.
290 */
291 int32_t fCount;
292 /**
293 * Full size of the fBytes
294 */
295 int32_t fCapacity;
296 /**
297 * Unique hash value of this CollationKey
298 */
299 int32_t fHashCode;
300 /**
301 * Array to store the sortkey
302 */
303 uint8_t* fBytes;
304
305 };
306
307 inline UBool
308 CollationKey::operator!=(const CollationKey& other) const
309 {
310 return !(*this == other);
311 }
312
313 inline UBool
314 CollationKey::isBogus() const
315 {
316 return fBogus;
317 }
318
319 inline const uint8_t*
320 CollationKey::getByteArray(int32_t &count) const
321 {
322 count = fCount;
323 return fBytes;
324 }
325
326 U_NAMESPACE_END
327
328 #endif /* U_HIDE_DEPRECATED_API */
329 #endif /* #if !UCONFIG_NO_COLLATION */
330
331 #endif