michael@0: /* michael@0: ***************************************************************************** michael@0: * Copyright (C) 1996-2013, International Business Machines Corporation and others. michael@0: * All Rights Reserved. michael@0: ***************************************************************************** michael@0: * michael@0: * File sortkey.h michael@0: * michael@0: * Created by: Helena Shih michael@0: * michael@0: * Modification History: michael@0: * michael@0: * Date Name Description michael@0: * michael@0: * 6/20/97 helena Java class name change. michael@0: * 8/18/97 helena Added internal API documentation. michael@0: * 6/26/98 erm Changed to use byte arrays and memcmp. michael@0: ***************************************************************************** michael@0: */ michael@0: michael@0: #ifndef SORTKEY_H michael@0: #define SORTKEY_H michael@0: michael@0: #include "unicode/utypes.h" michael@0: michael@0: /** michael@0: * \file michael@0: * \brief C++ API: Keys for comparing strings multiple times. michael@0: */ michael@0: michael@0: #if !UCONFIG_NO_COLLATION michael@0: michael@0: #include "unicode/uobject.h" michael@0: #include "unicode/unistr.h" michael@0: #include "unicode/coll.h" michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: /* forward declaration */ michael@0: class RuleBasedCollator; michael@0: michael@0: /** michael@0: * michael@0: * Collation keys are generated by the Collator class. Use the CollationKey objects michael@0: * instead of Collator to compare strings multiple times. A CollationKey michael@0: * preprocesses the comparison information from the Collator object to michael@0: * make the comparison faster. If you are not going to comparing strings michael@0: * multiple times, then using the Collator object is generally faster, michael@0: * since it only processes as much of the string as needed to make a michael@0: * comparison. michael@0: *
For example (with strength == tertiary) michael@0: *
When comparing "Abernathy" to "Baggins-Smythworthy", Collator michael@0: * only needs to process a couple of characters, while a comparison michael@0: * with CollationKeys will process all of the characters. On the other hand, michael@0: * if you are doing a sort of a number of fields, it is much faster to use michael@0: * CollationKeys, since you will be comparing strings multiple times. michael@0: *
Typical use of CollationKeys are in databases, where you store a CollationKey michael@0: * in a hidden field, and use it for sorting or indexing. michael@0: * michael@0: *
Example of use: michael@0: *
michael@0: * \code michael@0: * UErrorCode success = U_ZERO_ERROR; michael@0: * Collator* myCollator = Collator::createInstance(success); michael@0: * CollationKey* keys = new CollationKey [3]; michael@0: * myCollator->getCollationKey("Tom", keys[0], success ); michael@0: * myCollator->getCollationKey("Dick", keys[1], success ); michael@0: * myCollator->getCollationKey("Harry", keys[2], success ); michael@0: * michael@0: * // Inside body of sort routine, compare keys this way: michael@0: * CollationKey tmp; michael@0: * if(keys[0].compareTo( keys[1] ) > 0 ) { michael@0: * tmp = keys[0]; keys[0] = keys[1]; keys[1] = tmp; michael@0: * } michael@0: * //... michael@0: * \endcode michael@0: *michael@0: *
Because Collator::compare()'s algorithm is complex, it is faster to sort michael@0: * long lists of words by retrieving collation keys with Collator::getCollationKey(). michael@0: * You can then cache the collation keys and compare them using CollationKey::compareTo(). michael@0: *
michael@0: * Note: Collator
s with different Locale,
michael@0: * CollationStrength and DecompositionMode settings will return different
michael@0: * CollationKeys for the same set of strings. Locales have specific
michael@0: * collation rules, and the way in which secondary and tertiary differences
michael@0: * are taken into account, for example, will result in different CollationKeys
michael@0: * for same strings.
michael@0: *
michael@0: michael@0: * @see Collator michael@0: * @see RuleBasedCollator michael@0: * @version 1.3 12/18/96 michael@0: * @author Helena Shih michael@0: * @stable ICU 2.0 michael@0: */ michael@0: class U_I18N_API CollationKey : public UObject { michael@0: public: michael@0: /** michael@0: * This creates an empty collation key based on the null string. An empty michael@0: * collation key contains no sorting information. When comparing two empty michael@0: * collation keys, the result is Collator::EQUAL. Comparing empty collation key michael@0: * with non-empty collation key is always Collator::LESS. michael@0: * @stable ICU 2.0 michael@0: */ michael@0: CollationKey(); michael@0: michael@0: michael@0: /** michael@0: * Creates a collation key based on the collation key values. michael@0: * @param values the collation key values michael@0: * @param count number of collation key values, including trailing nulls. michael@0: * @stable ICU 2.0 michael@0: */ michael@0: CollationKey(const uint8_t* values, michael@0: int32_t count); michael@0: michael@0: /** michael@0: * Copy constructor. michael@0: * @param other the object to be copied. michael@0: * @stable ICU 2.0 michael@0: */ michael@0: CollationKey(const CollationKey& other); michael@0: michael@0: /** michael@0: * Sort key destructor. michael@0: * @stable ICU 2.0 michael@0: */ michael@0: virtual ~CollationKey(); michael@0: michael@0: /** michael@0: * Assignment operator michael@0: * @param other the object to be copied. michael@0: * @stable ICU 2.0 michael@0: */ michael@0: const CollationKey& operator=(const CollationKey& other); michael@0: michael@0: /** michael@0: * Compare if two collation keys are the same. michael@0: * @param source the collation key to compare to. michael@0: * @return Returns true if two collation keys are equal, false otherwise. michael@0: * @stable ICU 2.0 michael@0: */ michael@0: UBool operator==(const CollationKey& source) const; michael@0: michael@0: /** michael@0: * Compare if two collation keys are not the same. michael@0: * @param source the collation key to compare to. michael@0: * @return Returns TRUE if two collation keys are different, FALSE otherwise. michael@0: * @stable ICU 2.0 michael@0: */ michael@0: UBool operator!=(const CollationKey& source) const; michael@0: michael@0: michael@0: /** michael@0: * Test to see if the key is in an invalid state. The key will be in an michael@0: * invalid state if it couldn't allocate memory for some operation. michael@0: * @return Returns TRUE if the key is in an invalid, FALSE otherwise. michael@0: * @stable ICU 2.0 michael@0: */ michael@0: UBool isBogus(void) const; michael@0: michael@0: /** michael@0: * Returns a pointer to the collation key values. The storage is owned michael@0: * by the collation key and the pointer will become invalid if the key michael@0: * is deleted. michael@0: * @param count the output parameter of number of collation key values, michael@0: * including any trailing nulls. michael@0: * @return a pointer to the collation key values. michael@0: * @stable ICU 2.0 michael@0: */ michael@0: const uint8_t* getByteArray(int32_t& count) const; michael@0: michael@0: #ifdef U_USE_COLLATION_KEY_DEPRECATES michael@0: /** michael@0: * Extracts the collation key values into a new array. The caller owns michael@0: * this storage and should free it. michael@0: * @param count the output parameter of number of collation key values, michael@0: * including any trailing nulls. michael@0: * @obsolete ICU 2.6. Use getByteArray instead since this API will be removed in that release. michael@0: */ michael@0: uint8_t* toByteArray(int32_t& count) const; michael@0: #endif michael@0: michael@0: #ifndef U_HIDE_DEPRECATED_API michael@0: /** michael@0: * Convenience method which does a string(bit-wise) comparison of the michael@0: * two collation keys. michael@0: * @param target target collation key to be compared with michael@0: * @return Returns Collator::LESS if sourceKey < targetKey, michael@0: * Collator::GREATER if sourceKey > targetKey and Collator::EQUAL michael@0: * otherwise. michael@0: * @deprecated ICU 2.6 use the overload with error code michael@0: */ michael@0: Collator::EComparisonResult compareTo(const CollationKey& target) const; michael@0: #endif /* U_HIDE_DEPRECATED_API */ michael@0: michael@0: /** michael@0: * Convenience method which does a string(bit-wise) comparison of the michael@0: * two collation keys. michael@0: * @param target target collation key to be compared with michael@0: * @param status error code michael@0: * @return Returns UCOL_LESS if sourceKey < targetKey, michael@0: * UCOL_GREATER if sourceKey > targetKey and UCOL_EQUAL michael@0: * otherwise. michael@0: * @stable ICU 2.6 michael@0: */ michael@0: UCollationResult compareTo(const CollationKey& target, UErrorCode &status) const; michael@0: michael@0: /** michael@0: * Creates an integer that is unique to the collation key. NOTE: this michael@0: * is not the same as String.hashCode. michael@0: *
Example of use: michael@0: *
michael@0: * . UErrorCode status = U_ZERO_ERROR; michael@0: * . Collator *myCollation = Collator::createInstance(Locale::US, status); michael@0: * . if (U_FAILURE(status)) return; michael@0: * . CollationKey key1, key2; michael@0: * . UErrorCode status1 = U_ZERO_ERROR, status2 = U_ZERO_ERROR; michael@0: * . myCollation->getCollationKey("abc", key1, status1); michael@0: * . if (U_FAILURE(status1)) { delete myCollation; return; } michael@0: * . myCollation->getCollationKey("ABC", key2, status2); michael@0: * . if (U_FAILURE(status2)) { delete myCollation; return; } michael@0: * . // key1.hashCode() != key2.hashCode() michael@0: *michael@0: * @return the hash value based on the string's collation order. michael@0: * @see UnicodeString#hashCode michael@0: * @stable ICU 2.0 michael@0: */ michael@0: int32_t hashCode(void) const; michael@0: michael@0: /** michael@0: * ICU "poor man's RTTI", returns a UClassID for the actual class. michael@0: * @stable ICU 2.2 michael@0: */ michael@0: virtual UClassID getDynamicClassID() const; michael@0: michael@0: /** michael@0: * ICU "poor man's RTTI", returns a UClassID for this class. michael@0: * @stable ICU 2.2 michael@0: */ michael@0: static UClassID U_EXPORT2 getStaticClassID(); michael@0: michael@0: private: michael@0: /** michael@0: * Replaces the current bytes buffer with a new one of newCapacity michael@0: * and copies length bytes from the old buffer to the new one. michael@0: * @return the new buffer, or NULL if the allocation failed michael@0: */ michael@0: uint8_t *reallocate(int32_t newCapacity, int32_t length); michael@0: /** michael@0: * Set a new length for a new sort key in the existing fBytes. michael@0: */ michael@0: void setLength(int32_t newLength); michael@0: michael@0: uint8_t *getBytes() { michael@0: return (fFlagAndLength >= 0) ? fUnion.fStackBuffer : fUnion.fFields.fBytes; michael@0: } michael@0: const uint8_t *getBytes() const { michael@0: return (fFlagAndLength >= 0) ? fUnion.fStackBuffer : fUnion.fFields.fBytes; michael@0: } michael@0: int32_t getCapacity() const { michael@0: return (fFlagAndLength >= 0) ? (int32_t)sizeof(fUnion) : fUnion.fFields.fCapacity; michael@0: } michael@0: int32_t getLength() const { return fFlagAndLength & 0x7fffffff; } michael@0: michael@0: /** michael@0: * Set the CollationKey to a "bogus" or invalid state michael@0: * @return this CollationKey michael@0: */ michael@0: CollationKey& setToBogus(void); michael@0: /** michael@0: * Resets this CollationKey to an empty state michael@0: * @return this CollationKey michael@0: */ michael@0: CollationKey& reset(void); michael@0: michael@0: /** michael@0: * Allow private access to RuleBasedCollator michael@0: */ michael@0: friend class RuleBasedCollator; michael@0: friend class CollationKeyByteSink; michael@0: michael@0: // Class fields. sizeof(CollationKey) is intended to be 48 bytes michael@0: // on a machine with 64-bit pointers. michael@0: // We use a union to maximize the size of the internal buffer, michael@0: // similar to UnicodeString but not as tight and complex. michael@0: michael@0: // (implicit) *vtable; michael@0: /** michael@0: * Sort key length and flag. michael@0: * Bit 31 is set if the buffer is heap-allocated. michael@0: * Bits 30..0 contain the sort key length. michael@0: */ michael@0: int32_t fFlagAndLength; michael@0: /** michael@0: * Unique hash value of this CollationKey. michael@0: * Special value 2 if the key is bogus. michael@0: */ michael@0: mutable int32_t fHashCode; michael@0: /** michael@0: * fUnion provides 32 bytes for the internal buffer or for michael@0: * pointer+capacity. michael@0: */ michael@0: union StackBufferOrFields { michael@0: /** fStackBuffer is used iff fFlagAndLength>=0, else fFields is used */ michael@0: uint8_t fStackBuffer[32]; michael@0: struct { michael@0: uint8_t *fBytes; michael@0: int32_t fCapacity; michael@0: } fFields; michael@0: } fUnion; michael@0: }; michael@0: michael@0: inline UBool michael@0: CollationKey::operator!=(const CollationKey& other) const michael@0: { michael@0: return !(*this == other); michael@0: } michael@0: michael@0: inline UBool michael@0: CollationKey::isBogus() const michael@0: { michael@0: return fHashCode == 2; // kBogusHashCode michael@0: } michael@0: michael@0: inline const uint8_t* michael@0: CollationKey::getByteArray(int32_t &count) const michael@0: { michael@0: count = getLength(); michael@0: return getBytes(); michael@0: } michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: #endif /* #if !UCONFIG_NO_COLLATION */ michael@0: michael@0: #endif