For example (with strength == tertiary) 57 *
When comparing "Abernathy" to "Baggins-Smythworthy", Collator 58 * only needs to process a couple of characters, while a comparison 59 * with CollationKeys will process all of the characters. On the other hand, 60 * if you are doing a sort of a number of fields, it is much faster to use 61 * CollationKeys, since you will be comparing strings multiple times. 62 *
Typical use of CollationKeys are in databases, where you store a CollationKey 63 * in a hidden field, and use it for sorting or indexing. 64 * 65 *
Example of use: 66 *
67 * \code 68 * UErrorCode success = U_ZERO_ERROR; 69 * Collator* myCollator = Collator::createInstance(success); 70 * CollationKey* keys = new CollationKey [3]; 71 * myCollator->getCollationKey("Tom", keys[0], success ); 72 * myCollator->getCollationKey("Dick", keys[1], success ); 73 * myCollator->getCollationKey("Harry", keys[2], success ); 74 * 75 * // Inside body of sort routine, compare keys this way: 76 * CollationKey tmp; 77 * if(keys[0].compareTo( keys[1] ) > 0 ) { 78 * tmp = keys[0]; keys[0] = keys[1]; keys[1] = tmp; 79 * } 80 * //... 81 * \endcode 82 *
Because Collator::compare()'s algorithm is complex, it is faster to sort 84 * long lists of words by retrieving collation keys with Collator::getCollationKey(). 85 * You can then cache the collation keys and compare them using CollationKey::compareTo(). 86 *
87 * Note: Collators with different Locale, 88 * CollationStrength and DecompositionMode settings will return different 89 * CollationKeys for the same set of strings. Locales have specific 90 * collation rules, and the way in which secondary and tertiary differences 91 * are taken into account, for example, will result in different CollationKeys 92 * for same strings. 93 *
Collator
94 95 * @see Collator 96 * @see RuleBasedCollator 97 * @version 1.3 12/18/96 98 * @author Helena Shih 99 * @stable ICU 2.0 100 */ 101 class U_I18N_API CollationKey : public UObject { 102 public: 103 /** 104 * This creates an empty collation key based on the null string. An empty 105 * collation key contains no sorting information. When comparing two empty 106 * collation keys, the result is Collator::EQUAL. Comparing empty collation key 107 * with non-empty collation key is always Collator::LESS. 108 * @stable ICU 2.0 109 */ 110 CollationKey(); 111 112 113 /** 114 * Creates a collation key based on the collation key values. 115 * @param values the collation key values 116 * @param count number of collation key values, including trailing nulls. 117 * @stable ICU 2.0 118 */ 119 CollationKey(const uint8_t* values, 120 int32_t count); 121 122 /** 123 * Copy constructor. 124 * @param other the object to be copied. 125 * @stable ICU 2.0 126 */ 127 CollationKey(const CollationKey& other); 128 129 /** 130 * Sort key destructor. 131 * @stable ICU 2.0 132 */ 133 virtual ~CollationKey(); 134 135 /** 136 * Assignment operator 137 * @param other the object to be copied. 138 * @stable ICU 2.0 139 */ 140 const CollationKey& operator=(const CollationKey& other); 141 142 /** 143 * Compare if two collation keys are the same. 144 * @param source the collation key to compare to. 145 * @return Returns true if two collation keys are equal, false otherwise. 146 * @stable ICU 2.0 147 */ 148 bool operator==(const CollationKey& source) const; 149 150 /** 151 * Compare if two collation keys are not the same. 152 * @param source the collation key to compare to. 153 * @return Returns true if two collation keys are different, false otherwise. 154 * @stable ICU 2.0 155 */ 156 bool operator!=(const CollationKey& source) const; 157 158 159 /** 160 * Test to see if the key is in an invalid state. The key will be in an 161 * invalid state if it couldn't allocate memory for some operation. 162 * @return Returns true if the key is in an invalid, false otherwise. 163 * @stable ICU 2.0 164 */ 165 UBool isBogus(void) const; 166 167 /** 168 * Returns a pointer to the collation key values. The storage is owned 169 * by the collation key and the pointer will become invalid if the key 170 * is deleted. 171 * @param count the output parameter of number of collation key values, 172 * including any trailing nulls. 173 * @return a pointer to the collation key values. 174 * @stable ICU 2.0 175 */ 176 const uint8_t* getByteArray(int32_t& count) const; 177 178 #ifdef U_USE_COLLATION_KEY_DEPRECATES 179 /** 180 * Extracts the collation key values into a new array. The caller owns 181 * this storage and should free it. 182 * @param count the output parameter of number of collation key values, 183 * including any trailing nulls. 184 * @obsolete ICU 2.6. Use getByteArray instead since this API will be removed in that release. 185 */ 186 uint8_t* toByteArray(int32_t& count) const; 187 #endif 188 189 #ifndef U_HIDE_DEPRECATED_API 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 * @return Returns Collator::LESS if sourceKey < targetKey, 195 * Collator::GREATER if sourceKey > targetKey and Collator::EQUAL 196 * otherwise. 197 * @deprecated ICU 2.6 use the overload with error code 198 */ 199 Collator::EComparisonResult compareTo(const CollationKey& target) const; 200 #endif /* U_HIDE_DEPRECATED_API */ 201 202 /** 203 * Convenience method which does a string(bit-wise) comparison of the 204 * two collation keys. 205 * @param target target collation key to be compared with 206 * @param status error code 207 * @return Returns UCOL_LESS if sourceKey < targetKey, 208 * UCOL_GREATER if sourceKey > targetKey and UCOL_EQUAL 209 * otherwise. 210 * @stable ICU 2.6 211 */ 212 UCollationResult compareTo(const CollationKey& target, UErrorCode &status) const; 213 214 /** 215 * Creates an integer that is unique to the collation key. NOTE: this 216 * is not the same as String.hashCode. 217 *
Example of use: 218 *
219 * . UErrorCode status = U_ZERO_ERROR; 220 * . Collator *myCollation = Collator::createInstance(Locale::US, status); 221 * . if (U_FAILURE(status)) return; 222 * . CollationKey key1, key2; 223 * . UErrorCode status1 = U_ZERO_ERROR, status2 = U_ZERO_ERROR; 224 * . myCollation->getCollationKey("abc", key1, status1); 225 * . if (U_FAILURE(status1)) { delete myCollation; return; } 226 * . myCollation->getCollationKey("ABC", key2, status2); 227 * . if (U_FAILURE(status2)) { delete myCollation; return; } 228 * . // key1.hashCode() != key2.hashCode() 229 *