ICU 51.2  51.2
sortkey.h
Go to the documentation of this file.
1 /*
2  *****************************************************************************
3  * Copyright (C) 1996-2013, International Business Machines Corporation and others.
4  * All Rights Reserved.
5  *****************************************************************************
6  *
7  * File sortkey.h
8  *
9  * Created by: Helena Shih
10  *
11  * Modification History:
12  *
13  * Date Name Description
14  *
15  * 6/20/97 helena Java class name change.
16  * 8/18/97 helena Added internal API documentation.
17  * 6/26/98 erm Changed to use byte arrays and memcmp.
18  *****************************************************************************
19  */
20 
21 #ifndef SORTKEY_H
22 #define SORTKEY_H
23 
24 #include "unicode/utypes.h"
25 
31 #if !UCONFIG_NO_COLLATION
32 
33 #include "unicode/uobject.h"
34 #include "unicode/unistr.h"
35 #include "unicode/coll.h"
36 
38 
39 /* forward declaration */
40 class RuleBasedCollator;
41 
97 public:
105  CollationKey();
106 
107 
114  CollationKey(const uint8_t* values,
115  int32_t count);
116 
122  CollationKey(const CollationKey& other);
123 
128  virtual ~CollationKey();
129 
135  const CollationKey& operator=(const CollationKey& other);
136 
143  UBool operator==(const CollationKey& source) const;
144 
151  UBool operator!=(const CollationKey& source) const;
152 
153 
160  UBool isBogus(void) const;
161 
171  const uint8_t* getByteArray(int32_t& count) const;
172 
173 #ifdef U_USE_COLLATION_KEY_DEPRECATES
174 
181  uint8_t* toByteArray(int32_t& count) const;
182 #endif
183 
184 #ifndef U_HIDE_DEPRECATED_API
185 
194  Collator::EComparisonResult compareTo(const CollationKey& target) const;
195 #endif /* U_HIDE_DEPRECATED_API */
196 
207  UCollationResult compareTo(const CollationKey& target, UErrorCode &status) const;
208 
229  int32_t hashCode(void) const;
230 
235  virtual UClassID getDynamicClassID() const;
236 
241  static UClassID U_EXPORT2 getStaticClassID();
242 
243 private:
249  uint8_t *reallocate(int32_t newCapacity, int32_t length);
253  void setLength(int32_t newLength);
254 
255  uint8_t *getBytes() {
256  return (fFlagAndLength >= 0) ? fUnion.fStackBuffer : fUnion.fFields.fBytes;
257  }
258  const uint8_t *getBytes() const {
259  return (fFlagAndLength >= 0) ? fUnion.fStackBuffer : fUnion.fFields.fBytes;
260  }
261  int32_t getCapacity() const {
262  return (fFlagAndLength >= 0) ? (int32_t)sizeof(fUnion) : fUnion.fFields.fCapacity;
263  }
264  int32_t getLength() const { return fFlagAndLength & 0x7fffffff; }
265 
270  CollationKey& setToBogus(void);
275  CollationKey& reset(void);
276 
280  friend class RuleBasedCollator;
281  friend class CollationKeyByteSink;
282 
283  // Class fields. sizeof(CollationKey) is intended to be 48 bytes
284  // on a machine with 64-bit pointers.
285  // We use a union to maximize the size of the internal buffer,
286  // similar to UnicodeString but not as tight and complex.
287 
288  // (implicit) *vtable;
294  int32_t fFlagAndLength;
299  mutable int32_t fHashCode;
304  union StackBufferOrFields {
306  uint8_t fStackBuffer[32];
307  struct {
308  uint8_t *fBytes;
309  int32_t fCapacity;
310  } fFields;
311  } fUnion;
312 };
313 
314 inline UBool
316 {
317  return !(*this == other);
318 }
319 
320 inline UBool
321 CollationKey::isBogus() const
322 {
323  return fHashCode == 2; // kBogusHashCode
324 }
325 
326 inline const uint8_t*
327 CollationKey::getByteArray(int32_t &count) const
328 {
329  count = getLength();
330  return getBytes();
331 }
332 
334 
335 #endif /* #if !UCONFIG_NO_COLLATION */
336 
337 #endif