]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/unicode/sortkey.h
ICU-64260.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / unicode / sortkey.h
CommitLineData
f3c0d7a5
A
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
b75a7d8f 3/*
73c04bcf 4 *****************************************************************************
57a6839d 5 * Copyright (C) 1996-2014, International Business Machines Corporation and others.
374ca955 6 * All Rights Reserved.
73c04bcf 7 *****************************************************************************
374ca955
A
8 *
9 * File sortkey.h
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.
73c04bcf 20 *****************************************************************************
374ca955 21 */
b75a7d8f
A
22
23#ifndef SORTKEY_H
24#define SORTKEY_H
25
26#include "unicode/utypes.h"
27
73c04bcf
A
28/**
29 * \file
30 * \brief C++ API: Keys for comparing strings multiple times.
31 */
32
b75a7d8f
A
33#if !UCONFIG_NO_COLLATION
34
35#include "unicode/uobject.h"
36#include "unicode/unistr.h"
37#include "unicode/coll.h"
38
f3c0d7a5 39#if U_SHOW_CPLUSPLUS_API
b75a7d8f
A
40U_NAMESPACE_BEGIN
41
42/* forward declaration */
43class RuleBasedCollator;
57a6839d 44class CollationKeyByteSink;
b75a7d8f
A
45
46/**
73c04bcf 47 *
b75a7d8f
A
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
54 * comparison.
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.
63 *
64 * <p>Example of use:
65 * <pre>
66 * \code
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 );
73 *
74 * // Inside body of sort routine, compare keys this way:
75 * CollationKey tmp;
76 * if(keys[0].compareTo( keys[1] ) > 0 ) {
77 * tmp = keys[0]; keys[0] = keys[1]; keys[1] = tmp;
78 * }
79 * //...
80 * \endcode
81 * </pre>
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().
85 * <p>
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
91 * for same strings.
92 * <p>
93
94 * @see Collator
95 * @see RuleBasedCollator
96 * @version 1.3 12/18/96
97 * @author Helena Shih
51004dcb 98 * @stable ICU 2.0
b75a7d8f
A
99 */
100class U_I18N_API CollationKey : public UObject {
101public:
102 /**
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.
51004dcb 107 * @stable ICU 2.0
b75a7d8f
A
108 */
109 CollationKey();
110
111
112 /**
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.
51004dcb 116 * @stable ICU 2.0
b75a7d8f
A
117 */
118 CollationKey(const uint8_t* values,
119 int32_t count);
120
121 /**
122 * Copy constructor.
123 * @param other the object to be copied.
51004dcb 124 * @stable ICU 2.0
b75a7d8f
A
125 */
126 CollationKey(const CollationKey& other);
127
128 /**
129 * Sort key destructor.
51004dcb 130 * @stable ICU 2.0
b75a7d8f 131 */
374ca955 132 virtual ~CollationKey();
b75a7d8f
A
133
134 /**
135 * Assignment operator
136 * @param other the object to be copied.
51004dcb 137 * @stable ICU 2.0
b75a7d8f
A
138 */
139 const CollationKey& operator=(const CollationKey& other);
140
141 /**
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.
51004dcb 145 * @stable ICU 2.0
b75a7d8f
A
146 */
147 UBool operator==(const CollationKey& source) const;
148
149 /**
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.
51004dcb 153 * @stable ICU 2.0
b75a7d8f
A
154 */
155 UBool operator!=(const CollationKey& source) const;
156
157
158 /**
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.
51004dcb 162 * @stable ICU 2.0
b75a7d8f
A
163 */
164 UBool isBogus(void) const;
165
166 /**
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
169 * is deleted.
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.
51004dcb 173 * @stable ICU 2.0
b75a7d8f
A
174 */
175 const uint8_t* getByteArray(int32_t& count) const;
176
177#ifdef U_USE_COLLATION_KEY_DEPRECATES
178 /**
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.
184 */
185 uint8_t* toByteArray(int32_t& count) const;
186#endif
187
51004dcb 188#ifndef U_HIDE_DEPRECATED_API
b75a7d8f
A
189 /**
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 &lt; targetKey,
194 * Collator::GREATER if sourceKey > targetKey and Collator::EQUAL
195 * otherwise.
196 * @deprecated ICU 2.6 use the overload with error code
197 */
198 Collator::EComparisonResult compareTo(const CollationKey& target) const;
51004dcb 199#endif /* U_HIDE_DEPRECATED_API */
b75a7d8f
A
200
201 /**
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 &lt; targetKey,
207 * UCOL_GREATER if sourceKey > targetKey and UCOL_EQUAL
208 * otherwise.
51004dcb 209 * @stable ICU 2.6
b75a7d8f
A
210 */
211 UCollationResult compareTo(const CollationKey& target, UErrorCode &status) const;
212
213 /**
214 * Creates an integer that is unique to the collation key. NOTE: this
215 * is not the same as String.hashCode.
216 * <p>Example of use:
217 * <pre>
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()
228 * </pre>
229 * @return the hash value based on the string's collation order.
230 * @see UnicodeString#hashCode
51004dcb 231 * @stable ICU 2.0
b75a7d8f
A
232 */
233 int32_t hashCode(void) const;
234
235 /**
236 * ICU "poor man's RTTI", returns a UClassID for the actual class.
51004dcb 237 * @stable ICU 2.2
b75a7d8f 238 */
374ca955 239 virtual UClassID getDynamicClassID() const;
b75a7d8f
A
240
241 /**
242 * ICU "poor man's RTTI", returns a UClassID for this class.
51004dcb 243 * @stable ICU 2.2
b75a7d8f 244 */
374ca955 245 static UClassID U_EXPORT2 getStaticClassID();
b75a7d8f
A
246
247private:
248 /**
51004dcb
A
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
252 */
253 uint8_t *reallocate(int32_t newCapacity, int32_t length);
4388f060
A
254 /**
255 * Set a new length for a new sort key in the existing fBytes.
256 */
257 void setLength(int32_t newLength);
b75a7d8f 258
51004dcb
A
259 uint8_t *getBytes() {
260 return (fFlagAndLength >= 0) ? fUnion.fStackBuffer : fUnion.fFields.fBytes;
261 }
262 const uint8_t *getBytes() const {
263 return (fFlagAndLength >= 0) ? fUnion.fStackBuffer : fUnion.fFields.fBytes;
264 }
265 int32_t getCapacity() const {
266 return (fFlagAndLength >= 0) ? (int32_t)sizeof(fUnion) : fUnion.fFields.fCapacity;
267 }
268 int32_t getLength() const { return fFlagAndLength & 0x7fffffff; }
b75a7d8f 269
374ca955
A
270 /**
271 * Set the CollationKey to a "bogus" or invalid state
272 * @return this CollationKey
273 */
b75a7d8f 274 CollationKey& setToBogus(void);
374ca955
A
275 /**
276 * Resets this CollationKey to an empty state
277 * @return this CollationKey
278 */
b75a7d8f 279 CollationKey& reset(void);
51004dcb 280
374ca955
A
281 /**
282 * Allow private access to RuleBasedCollator
283 */
b75a7d8f 284 friend class RuleBasedCollator;
51004dcb
A
285 friend class CollationKeyByteSink;
286
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.
291
292 // (implicit) *vtable;
374ca955 293 /**
51004dcb
A
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.
297 */
298 int32_t fFlagAndLength;
374ca955 299 /**
51004dcb
A
300 * Unique hash value of this CollationKey.
301 * Special value 2 if the key is bogus.
374ca955 302 */
51004dcb 303 mutable int32_t fHashCode;
374ca955 304 /**
51004dcb
A
305 * fUnion provides 32 bytes for the internal buffer or for
306 * pointer+capacity.
307 */
308 union StackBufferOrFields {
309 /** fStackBuffer is used iff fFlagAndLength>=0, else fFields is used */
310 uint8_t fStackBuffer[32];
311 struct {
312 uint8_t *fBytes;
313 int32_t fCapacity;
314 } fFields;
315 } fUnion;
b75a7d8f
A
316};
317
b75a7d8f
A
318inline UBool
319CollationKey::operator!=(const CollationKey& other) const
320{
321 return !(*this == other);
322}
323
324inline UBool
325CollationKey::isBogus() const
326{
51004dcb 327 return fHashCode == 2; // kBogusHashCode
b75a7d8f
A
328}
329
330inline const uint8_t*
331CollationKey::getByteArray(int32_t &count) const
332{
51004dcb
A
333 count = getLength();
334 return getBytes();
b75a7d8f
A
335}
336
337U_NAMESPACE_END
f3c0d7a5 338#endif // U_SHOW_CPLUSPLUS_API
b75a7d8f
A
339
340#endif /* #if !UCONFIG_NO_COLLATION */
341
342#endif