ICU 73.2  73.2
sortkey.h
Go to the documentation of this file.
00001 // © 2016 and later: Unicode, Inc. and others.
00002 // License & terms of use: http://www.unicode.org/copyright.html
00003 /*
00004  *****************************************************************************
00005  * Copyright (C) 1996-2014, International Business Machines Corporation and others.
00006  * All Rights Reserved.
00007  *****************************************************************************
00008  *
00009  * File sortkey.h
00010  *
00011  * Created by: Helena Shih
00012  *
00013  * Modification History:
00014  *
00015  *  Date         Name          Description
00016  *
00017  *  6/20/97     helena      Java class name change.
00018  *  8/18/97     helena      Added internal API documentation.
00019  *  6/26/98     erm         Changed to use byte arrays and memcmp.
00020  *****************************************************************************
00021  */
00022 
00023 #ifndef SORTKEY_H
00024 #define SORTKEY_H
00025 
00026 #include "unicode/utypes.h"
00027 
00028 #if U_SHOW_CPLUSPLUS_API
00029 
00035 #if !UCONFIG_NO_COLLATION
00036 
00037 #include "unicode/uobject.h"
00038 #include "unicode/unistr.h"
00039 #include "unicode/coll.h"
00040 
00041 U_NAMESPACE_BEGIN
00042 
00043 /* forward declaration */
00044 class RuleBasedCollator;
00045 class CollationKeyByteSink;
00046 
00101 class U_I18N_API CollationKey : public UObject {
00102 public:
00110     CollationKey();
00111 
00112 
00119     CollationKey(const  uint8_t*    values,
00120                 int32_t     count);
00121 
00127     CollationKey(const CollationKey& other);
00128 
00133     virtual ~CollationKey();
00134 
00140     const   CollationKey&   operator=(const CollationKey& other);
00141 
00148     bool                    operator==(const CollationKey& source) const;
00149 
00156     bool                    operator!=(const CollationKey& source) const;
00157 
00158 
00165     UBool                   isBogus(void) const;
00166 
00176     const    uint8_t*       getByteArray(int32_t& count) const;
00177 
00178 #ifdef U_USE_COLLATION_KEY_DEPRECATES
00179 
00186     uint8_t*                toByteArray(int32_t& count) const;
00187 #endif
00188 
00189 #ifndef U_HIDE_DEPRECATED_API 
00190 
00199     Collator::EComparisonResult compareTo(const CollationKey& target) const;
00200 #endif  /* U_HIDE_DEPRECATED_API */
00201 
00212     UCollationResult compareTo(const CollationKey& target, UErrorCode &status) const;
00213 
00234     int32_t                 hashCode(void) const;
00235 
00240     virtual UClassID getDynamicClassID() const override;
00241 
00246     static UClassID U_EXPORT2 getStaticClassID();
00247 
00248 private:
00254     uint8_t *reallocate(int32_t newCapacity, int32_t length);
00258     void setLength(int32_t newLength);
00259 
00260     uint8_t *getBytes() {
00261         return (fFlagAndLength >= 0) ? fUnion.fStackBuffer : fUnion.fFields.fBytes;
00262     }
00263     const uint8_t *getBytes() const {
00264         return (fFlagAndLength >= 0) ? fUnion.fStackBuffer : fUnion.fFields.fBytes;
00265     }
00266     int32_t getCapacity() const {
00267         return (fFlagAndLength >= 0) ? (int32_t)sizeof(fUnion) : fUnion.fFields.fCapacity;
00268     }
00269     int32_t getLength() const { return fFlagAndLength & 0x7fffffff; }
00270 
00275     CollationKey&           setToBogus(void);
00280     CollationKey&           reset(void);
00281 
00285     friend  class           RuleBasedCollator;
00286     friend  class           CollationKeyByteSink;
00287 
00288     // Class fields. sizeof(CollationKey) is intended to be 48 bytes
00289     // on a machine with 64-bit pointers.
00290     // We use a union to maximize the size of the internal buffer,
00291     // similar to UnicodeString but not as tight and complex.
00292 
00293     // (implicit) *vtable;
00299     int32_t fFlagAndLength;
00304     mutable int32_t fHashCode;
00309     union StackBufferOrFields {
00311         uint8_t fStackBuffer[32];
00312         struct {
00313             uint8_t *fBytes;
00314             int32_t fCapacity;
00315         } fFields;
00316     } fUnion;
00317 };
00318 
00319 inline bool
00320 CollationKey::operator!=(const CollationKey& other) const
00321 {
00322     return !(*this == other);
00323 }
00324 
00325 inline UBool
00326 CollationKey::isBogus() const
00327 {
00328     return fHashCode == 2;  // kBogusHashCode
00329 }
00330 
00331 inline const uint8_t*
00332 CollationKey::getByteArray(int32_t &count) const
00333 {
00334     count = getLength();
00335     return getBytes();
00336 }
00337 
00338 U_NAMESPACE_END
00339 
00340 #endif /* #if !UCONFIG_NO_COLLATION */
00341 
00342 #endif /* U_SHOW_CPLUSPLUS_API */
00343 
00344 #endif