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