]>
git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/sortkey.cpp
2 *******************************************************************************
3 * Copyright (C) 1996-2012, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 *******************************************************************************
7 //===============================================================================
13 // Created by: Helena Shih
15 // Modification History:
17 // Date Name Description
19 // 6/20/97 helena Java class name change.
20 // 6/23/97 helena Added comments to make code more readable.
21 // 6/26/98 erm Canged to use byte arrays instead of UnicodeString
22 // 7/31/98 erm hashCode: minimum inc should be 2 not 1,
23 // Cleaned up operator=
24 // 07/12/99 helena HPUX 11 CC port.
25 // 03/06/01 synwee Modified compareTo, to handle the result of
26 // 2 string similar in contents, but one is longer
28 //===============================================================================
30 #include "unicode/utypes.h"
32 #if !UCONFIG_NO_COLLATION
34 #include "unicode/sortkey.h"
41 // A hash code of kInvalidHashCode indicates that the hash code needs
42 // to be computed. A hash code of kEmptyHashCode is used for empty keys
43 // and for any key whose computed hash code is kInvalidHashCode.
44 static const int32_t kInvalidHashCode
= 0;
45 static const int32_t kEmptyHashCode
= 1;
46 // The "bogus hash code" replaces a separate fBogus flag.
47 static const int32_t kBogusHashCode
= 2;
49 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(CollationKey
)
51 CollationKey::CollationKey()
52 : UObject(), fFlagAndLength(0),
53 fHashCode(kEmptyHashCode
)
57 // Create a collation key from a bit array.
58 CollationKey::CollationKey(const uint8_t* newValues
, int32_t count
)
59 : UObject(), fFlagAndLength(count
),
60 fHashCode(kInvalidHashCode
)
62 if (count
< 0 || (newValues
== NULL
&& count
!= 0) ||
63 (count
> getCapacity() && reallocate(count
, 0) == NULL
)) {
69 uprv_memcpy(getBytes(), newValues
, count
);
73 CollationKey::CollationKey(const CollationKey
& other
)
74 : UObject(other
), fFlagAndLength(other
.getLength()),
75 fHashCode(other
.fHashCode
)
83 int32_t length
= fFlagAndLength
;
84 if (length
> getCapacity() && reallocate(length
, 0) == NULL
) {
90 uprv_memcpy(getBytes(), other
.getBytes(), length
);
94 CollationKey::~CollationKey()
96 if(fFlagAndLength
< 0) { uprv_free(fUnion
.fFields
.fBytes
); }
99 uint8_t *CollationKey::reallocate(int32_t newCapacity
, int32_t length
) {
100 uint8_t *newBytes
= static_cast<uint8_t *>(uprv_malloc(newCapacity
));
101 if(newBytes
== NULL
) { return NULL
; }
103 uprv_memcpy(newBytes
, getBytes(), length
);
105 if(fFlagAndLength
< 0) { uprv_free(fUnion
.fFields
.fBytes
); }
106 fUnion
.fFields
.fBytes
= newBytes
;
107 fUnion
.fFields
.fCapacity
= newCapacity
;
108 fFlagAndLength
|= 0x80000000;
112 void CollationKey::setLength(int32_t newLength
) {
113 // U_ASSERT(newLength >= 0 && newLength <= getCapacity());
114 fFlagAndLength
= (fFlagAndLength
& 0x80000000) | newLength
;
115 fHashCode
= kInvalidHashCode
;
118 // set the key to an empty state
120 CollationKey::reset()
122 fFlagAndLength
&= 0x80000000;
123 fHashCode
= kEmptyHashCode
;
128 // set the key to a "bogus" or invalid state
130 CollationKey::setToBogus()
132 fFlagAndLength
&= 0x80000000;
133 fHashCode
= kBogusHashCode
;
139 CollationKey::operator==(const CollationKey
& source
) const
141 return getLength() == source
.getLength() &&
143 uprv_memcmp(getBytes(), source
.getBytes(), getLength()) == 0);
147 CollationKey::operator=(const CollationKey
& other
)
156 int32_t length
= other
.getLength();
157 if (length
> getCapacity() && reallocate(length
, 0) == NULL
) {
161 uprv_memcpy(getBytes(), other
.getBytes(), length
);
163 fFlagAndLength
= (fFlagAndLength
& 0x80000000) | length
;
164 fHashCode
= other
.fHashCode
;
170 // Bitwise comparison for the collation keys.
171 Collator::EComparisonResult
172 CollationKey::compareTo(const CollationKey
& target
) const
174 UErrorCode errorCode
= U_ZERO_ERROR
;
175 return static_cast<Collator::EComparisonResult
>(compareTo(target
, errorCode
));
178 // Bitwise comparison for the collation keys.
180 CollationKey::compareTo(const CollationKey
& target
, UErrorCode
&status
) const
182 if(U_SUCCESS(status
)) {
183 const uint8_t *src
= getBytes();
184 const uint8_t *tgt
= target
.getBytes();
186 // are we comparing the same string
190 UCollationResult result
;
192 // are we comparing different lengths?
193 int32_t minLength
= getLength();
194 int32_t targetLength
= target
.getLength();
195 if (minLength
< targetLength
) {
197 } else if (minLength
== targetLength
) {
200 minLength
= targetLength
;
201 result
= UCOL_GREATER
;
205 int diff
= uprv_memcmp(src
, tgt
, minLength
);
221 #ifdef U_USE_COLLATION_KEY_DEPRECATES
222 // Create a copy of the byte array.
224 CollationKey::toByteArray(int32_t& count
) const
226 uint8_t *result
= (uint8_t*) uprv_malloc( sizeof(uint8_t) * fCount
);
236 uprv_memcpy(result
, fBytes
, fCount
);
245 computeHashCode(const uint8_t *key
, int32_t length
) {
246 const char *s
= reinterpret_cast<const char *>(key
);
248 if (s
== NULL
|| length
== 0) {
249 hash
= kEmptyHashCode
;
251 hash
= ustr_hashCharsN(s
, length
);
252 if (hash
== kInvalidHashCode
|| hash
== kBogusHashCode
) {
253 hash
= kEmptyHashCode
;
260 CollationKey::hashCode() const
262 // (Cribbed from UnicodeString)
263 // We cache the hashCode; when it becomes invalid, due to any change to the
264 // string, we note this by setting it to kInvalidHashCode. [LIU]
266 // Note: This method is semantically const, but physically non-const.
268 if (fHashCode
== kInvalidHashCode
)
270 fHashCode
= computeHashCode(getBytes(), getLength());
278 U_CAPI
int32_t U_EXPORT2
279 ucol_keyHashCode(const uint8_t *key
,
282 return icu::computeHashCode(key
, length
);
285 #endif /* #if !UCONFIG_NO_COLLATION */