00001 /* 00002 ***************************************************************************** 00003 * Copyright (C) 1996-2012, International Business Machines Corporation and others. 00004 * All Rights Reserved. 00005 ***************************************************************************** 00006 * 00007 * File sortkey.h 00008 * 00009 * Created by: Helena Shih 00010 * 00011 * Modification History: 00012 * 00013 * Date Name Description 00014 * 00015 * 6/20/97 helena Java class name change. 00016 * 8/18/97 helena Added internal API documentation. 00017 * 6/26/98 erm Changed to use byte arrays and memcmp. 00018 ***************************************************************************** 00019 */ 00020 00021 #ifndef SORTKEY_H 00022 #define SORTKEY_H 00023 00024 #include "unicode/utypes.h" 00025 00031 #if !UCONFIG_NO_COLLATION 00032 #ifndef U_HIDE_DEPRECATED_API 00033 00034 #include "unicode/uobject.h" 00035 #include "unicode/unistr.h" 00036 #include "unicode/coll.h" 00037 00038 U_NAMESPACE_BEGIN 00039 00040 /* forward declaration */ 00041 class RuleBasedCollator; 00042 00097 class U_I18N_API CollationKey : public UObject { 00098 public: 00106 CollationKey(); 00107 00108 00115 CollationKey(const uint8_t* values, 00116 int32_t count); 00117 00123 CollationKey(const CollationKey& other); 00124 00129 virtual ~CollationKey(); 00130 00136 const CollationKey& operator=(const CollationKey& other); 00137 00144 UBool operator==(const CollationKey& source) const; 00145 00152 UBool operator!=(const CollationKey& source) const; 00153 00154 00161 UBool isBogus(void) const; 00162 00172 const uint8_t* getByteArray(int32_t& count) const; 00173 00174 #ifdef U_USE_COLLATION_KEY_DEPRECATES 00175 00182 uint8_t* toByteArray(int32_t& count) const; 00183 #endif 00184 00194 Collator::EComparisonResult compareTo(const CollationKey& target) const; 00195 00206 UCollationResult compareTo(const CollationKey& target, UErrorCode &status) const; 00207 00228 int32_t hashCode(void) const; 00229 00234 virtual UClassID getDynamicClassID() const; 00235 00240 static UClassID U_EXPORT2 getStaticClassID(); 00241 00242 private: 00248 uint8_t *reallocate(int32_t newCapacity, int32_t length); 00252 void setLength(int32_t newLength); 00253 00254 uint8_t *getBytes() { 00255 return (fFlagAndLength >= 0) ? fUnion.fStackBuffer : fUnion.fFields.fBytes; 00256 } 00257 const uint8_t *getBytes() const { 00258 return (fFlagAndLength >= 0) ? fUnion.fStackBuffer : fUnion.fFields.fBytes; 00259 } 00260 int32_t getCapacity() const { 00261 return (fFlagAndLength >= 0) ? (int32_t)sizeof(fUnion) : fUnion.fFields.fCapacity; 00262 } 00263 int32_t getLength() const { return fFlagAndLength & 0x7fffffff; } 00264 00269 CollationKey& setToBogus(void); 00274 CollationKey& reset(void); 00275 00279 friend class RuleBasedCollator; 00280 friend class CollationKeyByteSink; 00281 00282 // Class fields. sizeof(CollationKey) is intended to be 48 bytes 00283 // on a machine with 64-bit pointers. 00284 // We use a union to maximize the size of the internal buffer, 00285 // similar to UnicodeString but not as tight and complex. 00286 00287 // (implicit) *vtable; 00293 int32_t fFlagAndLength; 00298 mutable int32_t fHashCode; 00303 union StackBufferOrFields { 00305 uint8_t fStackBuffer[32]; 00306 struct { 00307 uint8_t *fBytes; 00308 int32_t fCapacity; 00309 } fFields; 00310 } fUnion; 00311 }; 00312 00313 inline UBool 00314 CollationKey::operator!=(const CollationKey& other) const 00315 { 00316 return !(*this == other); 00317 } 00318 00319 inline UBool 00320 CollationKey::isBogus() const 00321 { 00322 return fHashCode == 2; // kBogusHashCode 00323 } 00324 00325 inline const uint8_t* 00326 CollationKey::getByteArray(int32_t &count) const 00327 { 00328 count = getLength(); 00329 return getBytes(); 00330 } 00331 00332 U_NAMESPACE_END 00333 00334 #endif /* U_HIDE_DEPRECATED_API */ 00335 #endif /* #if !UCONFIG_NO_COLLATION */ 00336 00337 #endif