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