ICU-149 make UnicodeString delegate hashcode computation to uhash

X-SVN-Rev: 1048
This commit is contained in:
Alan Liu 2000-03-31 23:50:49 +00:00
parent 5569140fea
commit 34b14575f4
3 changed files with 17 additions and 33 deletions

View File

@ -409,6 +409,12 @@ uhash_hashUChars(const void *key) {
STRING_HASH(UChar, u_strlen(p), *p);
}
/* Used by UnicodeString to compute its hashcode - Not public API. */
U_CAPI int32_t
uhash_hashUCharsN(const UChar *key, int32_t length) {
STRING_HASH(UChar, length, *p);
}
U_CAPI int32_t
uhash_hashChars(const void *key) {
STRING_HASH(uint8_t, uprv_strlen((char*)p), *p);

View File

@ -364,6 +364,10 @@ uhash_hashUChars(const void *key);
U_CAPI int32_t
uhash_hashChars(const void *key);
/* Used by UnicodeString to compute its hashcode - Not public API. */
U_CAPI int32_t
uhash_hashUCharsN(const UChar *key, int32_t length);
/**
* Generate a case-insensitive hash code for a null-terminated char*
* string. If the string is not null-terminated do not use this

View File

@ -25,6 +25,7 @@
#include "unicode/ustring.h"
#include "mutex.h"
#include "unicode/unistr.h"
#include "uhash.h"
#if 0
//DEBUGGING
@ -992,40 +993,13 @@ UnicodeString::doReverse(UTextOffset start,
int32_t
UnicodeString::doHashCode()
{
const UChar *key = getArrayStart();
int32_t len = fLength;
int32_t hash = kInvalidHashCode;
const UChar *limit = key + len;
/*
We compute the hash by iterating sparsely over 64 (at most)
characters spaced evenly through the string. For each character,
we multiply the previous hash value by a prime number and add the
new character in, in the manner of an additive linear congruential
random number generator, thus producing a pseudorandom
deterministic value which should be well distributed over the
output range. [LIU] */
if(len <= 64) {
while(key < limit) {
hash = (hash * 37) + *key++;
/* Delegate hash computation to uhash. This makes UnicodeString
* hashing consistent with UChar* hashing. */
fHashCode = uhash_hashUCharsN(getArrayStart(), fLength);
if (fHashCode == kInvalidHashCode) {
fHashCode = kEmptyHashCode;
}
} else {
int32_t inc = (len+63)/64;
while(key < limit) {
hash = (hash * 37) + *key;
key += inc;
}
}
hash &= 0x7fffffff;
if(hash == kInvalidHashCode) {
hash = kEmptyHashCode;
}
fHashCode = hash;
return fHashCode;
return fHashCode;
}
//========================================