1999-08-16 21:50:52 +00:00
|
|
|
/*
|
1999-11-22 20:25:35 +00:00
|
|
|
*******************************************************************************
|
2001-03-21 20:31:13 +00:00
|
|
|
* Copyright (C) 1996-2001, International Business Machines Corporation and *
|
1999-11-22 20:25:35 +00:00
|
|
|
* others. All Rights Reserved. *
|
|
|
|
*******************************************************************************
|
1999-08-16 21:50:52 +00:00
|
|
|
*/
|
|
|
|
//===============================================================================
|
|
|
|
//
|
|
|
|
// File sortkey.cpp
|
|
|
|
//
|
2002-02-28 01:42:40 +00:00
|
|
|
//
|
1999-08-16 21:50:52 +00:00
|
|
|
//
|
|
|
|
// Created by: Helena Shih
|
|
|
|
//
|
|
|
|
// Modification History:
|
|
|
|
//
|
|
|
|
// Date Name Description
|
|
|
|
//
|
|
|
|
// 6/20/97 helena Java class name change.
|
|
|
|
// 6/23/97 helena Added comments to make code more readable.
|
|
|
|
// 6/26/98 erm Canged to use byte arrays instead of UnicodeString
|
|
|
|
// 7/31/98 erm hashCode: minimum inc should be 2 not 1,
|
|
|
|
// Cleaned up operator=
|
|
|
|
// 07/12/99 helena HPUX 11 CC port.
|
2002-02-28 01:42:40 +00:00
|
|
|
// 03/06/01 synwee Modified compareTo, to handle the result of
|
2001-03-07 06:39:48 +00:00
|
|
|
// 2 string similar in contents, but one is longer
|
|
|
|
// than the other
|
1999-08-16 21:50:52 +00:00
|
|
|
//===============================================================================
|
|
|
|
|
2002-09-20 01:54:48 +00:00
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
|
|
|
#if !UCONFIG_NO_COLLATION
|
|
|
|
|
1999-12-28 23:57:50 +00:00
|
|
|
#include "unicode/sortkey.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
#include "cmemory.h"
|
2000-12-12 01:15:30 +00:00
|
|
|
#include "uhash.h"
|
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// A hash code of kInvalidHashCode indicates that the has code needs
|
|
|
|
// to be computed. A hash code of kEmptyHashCode is used for empty keys
|
|
|
|
// and for any key whose computed hash code is kInvalidHashCode.
|
2002-02-28 01:42:40 +00:00
|
|
|
#define kInvalidHashCode ((int32_t)0)
|
|
|
|
#define kEmptyHashCode ((int32_t)1)
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-06-29 00:04:16 +00:00
|
|
|
const char CollationKey::fgClassID=0;
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
CollationKey::CollationKey()
|
2002-07-02 23:58:34 +00:00
|
|
|
: UObject(), fBogus(FALSE), fCount(0), fCapacity(0),
|
1999-08-16 21:50:52 +00:00
|
|
|
fHashCode(kEmptyHashCode), fBytes(NULL)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a collation key from a bit array.
|
|
|
|
CollationKey::CollationKey(const uint8_t* newValues, int32_t count)
|
2002-07-02 23:58:34 +00:00
|
|
|
: UObject(), fBogus(FALSE), fCount(count), fCapacity(count),
|
1999-08-16 21:50:52 +00:00
|
|
|
fHashCode(kInvalidHashCode)
|
|
|
|
{
|
2000-12-12 01:15:30 +00:00
|
|
|
fBytes = (uint8_t *)uprv_malloc(count);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
if (fBytes == NULL)
|
|
|
|
{
|
|
|
|
setToBogus();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1999-12-28 23:57:50 +00:00
|
|
|
uprv_memcpy(fBytes, newValues, fCount);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CollationKey::CollationKey(const CollationKey& other)
|
2002-07-02 23:58:34 +00:00
|
|
|
: UObject(other), fBogus(FALSE), fCount(other.fCount), fCapacity(other.fCapacity),
|
2000-08-23 23:48:04 +00:00
|
|
|
fHashCode(other.fHashCode), fBytes(NULL)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
if (other.fBogus)
|
|
|
|
{
|
|
|
|
setToBogus();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2000-12-12 01:15:30 +00:00
|
|
|
fBytes = (uint8_t *)uprv_malloc(fCapacity);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
if (fBytes == NULL)
|
|
|
|
{
|
|
|
|
setToBogus();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1999-12-28 23:57:50 +00:00
|
|
|
uprv_memcpy(fBytes, other.fBytes, other.fCount);
|
1999-10-22 00:41:21 +00:00
|
|
|
if(fCapacity>fCount) {
|
1999-12-28 23:57:50 +00:00
|
|
|
uprv_memset(fBytes+fCount, 0, fCapacity-fCount);
|
1999-10-22 00:41:21 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CollationKey::~CollationKey()
|
|
|
|
{
|
2000-12-12 01:15:30 +00:00
|
|
|
uprv_free(fBytes);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2000-12-12 01:15:30 +00:00
|
|
|
void CollationKey::adopt(uint8_t *values, int32_t count) {
|
|
|
|
if(fBytes != NULL) {
|
|
|
|
uprv_free(fBytes);
|
|
|
|
}
|
|
|
|
fBogus = FALSE;
|
|
|
|
fBytes = values;
|
|
|
|
fCount = count;
|
|
|
|
fCapacity = count;
|
|
|
|
fHashCode = kInvalidHashCode;
|
|
|
|
}
|
2001-06-26 17:41:10 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// set the key to an empty state
|
|
|
|
CollationKey&
|
|
|
|
CollationKey::reset()
|
2002-02-28 01:42:40 +00:00
|
|
|
{
|
1999-08-16 21:50:52 +00:00
|
|
|
fCount = 0;
|
|
|
|
fBogus = FALSE;
|
|
|
|
fHashCode = kEmptyHashCode;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the key to a "bogus" or invalid state
|
|
|
|
CollationKey&
|
|
|
|
CollationKey::setToBogus()
|
|
|
|
{
|
2001-03-15 22:56:02 +00:00
|
|
|
uprv_free(fBytes);
|
1999-08-16 21:50:52 +00:00
|
|
|
fBytes = NULL;
|
|
|
|
|
|
|
|
fCapacity = 0;
|
|
|
|
fCount = 0;
|
|
|
|
fHashCode = kInvalidHashCode;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool
|
1999-08-16 21:50:52 +00:00
|
|
|
CollationKey::operator==(const CollationKey& source) const
|
|
|
|
{
|
|
|
|
return (this->fCount == source.fCount &&
|
|
|
|
(this->fBytes == source.fBytes ||
|
1999-12-28 23:57:50 +00:00
|
|
|
uprv_memcmp(this->fBytes, source.fBytes, this->fCount) == 0));
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const CollationKey&
|
|
|
|
CollationKey::operator=(const CollationKey& other)
|
|
|
|
{
|
|
|
|
if (this != &other)
|
|
|
|
{
|
|
|
|
if (other.isBogus())
|
|
|
|
{
|
|
|
|
return setToBogus();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (other.fBytes != NULL)
|
|
|
|
{
|
|
|
|
ensureCapacity(other.fCount);
|
|
|
|
|
|
|
|
if (isBogus())
|
|
|
|
{
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
fHashCode = other.fHashCode;
|
1999-12-28 23:57:50 +00:00
|
|
|
uprv_memcpy(fBytes, other.fBytes, fCount);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-06-26 17:41:10 +00:00
|
|
|
fCount = 0;
|
|
|
|
fBogus = FALSE;
|
|
|
|
fHashCode = kEmptyHashCode;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bitwise comparison for the collation keys.
|
|
|
|
// NOTE: this is somewhat messy 'cause we can't count
|
|
|
|
// on memcmp returning the exact values which match
|
|
|
|
// Collator::EComparisonResult
|
2003-03-19 13:21:51 +00:00
|
|
|
Collator::EComparisonResult
|
1999-08-16 21:50:52 +00:00
|
|
|
CollationKey::compareTo(const CollationKey& target) const
|
|
|
|
{
|
2002-02-28 01:42:40 +00:00
|
|
|
uint8_t *src = this->fBytes;
|
|
|
|
uint8_t *tgt = target.fBytes;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-02-28 01:42:40 +00:00
|
|
|
// are we comparing the same string
|
|
|
|
if (src == tgt)
|
2003-03-19 13:21:51 +00:00
|
|
|
return Collator::EQUAL;
|
2002-02-28 01:42:40 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
int count = (this->fCount < target.fCount) ? this->fCount : target.fCount;
|
|
|
|
if (count == 0)
|
|
|
|
{
|
|
|
|
// If count is 0, at least one of the keys is empty.
|
|
|
|
// An empty key is always LESS than a non-empty one
|
|
|
|
// and EQUAL to another empty
|
|
|
|
if (this->fCount < target.fCount)
|
|
|
|
{
|
|
|
|
return Collator::LESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->fCount > target.fCount)
|
|
|
|
{
|
|
|
|
return Collator::GREATER;
|
|
|
|
}
|
|
|
|
return Collator::EQUAL;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
int minLength;
|
2003-03-19 13:21:51 +00:00
|
|
|
Collator::EComparisonResult result;
|
2002-02-28 01:42:40 +00:00
|
|
|
|
|
|
|
// are we comparing different lengths?
|
|
|
|
if (this->fCount != target.fCount) {
|
|
|
|
if (this->fCount < target.fCount) {
|
|
|
|
minLength = this->fCount;
|
2003-03-19 13:21:51 +00:00
|
|
|
result = Collator::LESS;
|
2002-02-28 01:42:40 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
minLength = target.fCount;
|
2003-03-19 13:21:51 +00:00
|
|
|
result = Collator::GREATER;
|
2002-02-28 01:42:40 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2001-03-07 06:39:48 +00:00
|
|
|
else {
|
2002-02-28 01:42:40 +00:00
|
|
|
minLength = target.fCount;
|
2003-03-19 13:21:51 +00:00
|
|
|
result = Collator::EQUAL;
|
2001-03-07 06:39:48 +00:00
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
|
|
|
|
if (minLength > 0) {
|
|
|
|
int diff = uprv_memcmp(src, tgt, minLength);
|
|
|
|
if (diff > 0) {
|
2003-03-19 13:21:51 +00:00
|
|
|
return Collator::GREATER;
|
2002-02-28 01:42:40 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if (diff < 0) {
|
2003-03-19 13:21:51 +00:00
|
|
|
return Collator::LESS;
|
2002-02-28 01:42:40 +00:00
|
|
|
}
|
2001-03-07 18:11:12 +00:00
|
|
|
}
|
2001-03-07 06:39:48 +00:00
|
|
|
|
2002-02-28 01:42:40 +00:00
|
|
|
return result;
|
|
|
|
/*
|
|
|
|
if (result < 0)
|
|
|
|
{
|
2001-03-07 06:39:48 +00:00
|
|
|
return Collator::LESS;
|
2002-02-28 01:42:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (result > 0)
|
|
|
|
{
|
|
|
|
return Collator::GREATER;
|
|
|
|
}
|
|
|
|
return Collator::EQUAL;
|
|
|
|
*/
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2003-05-01 17:43:01 +00:00
|
|
|
// Bitwise comparison for the collation keys.
|
|
|
|
UCollationResult
|
|
|
|
CollationKey::compareTo(const CollationKey& target, UErrorCode &status) const
|
|
|
|
{
|
|
|
|
uint8_t *src = this->fBytes;
|
|
|
|
uint8_t *tgt = target.fBytes;
|
|
|
|
|
|
|
|
// are we comparing the same string
|
|
|
|
if (src == tgt)
|
|
|
|
return UCOL_EQUAL;
|
|
|
|
|
|
|
|
int minLength;
|
|
|
|
UCollationResult result;
|
|
|
|
|
|
|
|
// are we comparing different lengths?
|
|
|
|
if (this->fCount != target.fCount) {
|
|
|
|
if (this->fCount < target.fCount) {
|
|
|
|
minLength = this->fCount;
|
|
|
|
result = UCOL_LESS;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
minLength = target.fCount;
|
|
|
|
result = UCOL_GREATER;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
minLength = target.fCount;
|
|
|
|
result = UCOL_EQUAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (minLength > 0) {
|
|
|
|
int diff = uprv_memcmp(src, tgt, minLength);
|
|
|
|
if (diff > 0) {
|
|
|
|
return UCOL_GREATER;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (diff < 0) {
|
|
|
|
return UCOL_LESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
CollationKey&
|
|
|
|
CollationKey::ensureCapacity(int32_t newSize)
|
|
|
|
{
|
|
|
|
if (fCapacity < newSize)
|
|
|
|
{
|
2000-12-12 01:15:30 +00:00
|
|
|
uprv_free(fBytes);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2000-12-12 01:15:30 +00:00
|
|
|
fBytes = (uint8_t *)uprv_malloc(newSize);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
if (fBytes == NULL)
|
|
|
|
{
|
|
|
|
return setToBogus();
|
|
|
|
}
|
|
|
|
|
1999-12-28 23:57:50 +00:00
|
|
|
uprv_memset(fBytes, 0, fCapacity);
|
1999-08-16 21:50:52 +00:00
|
|
|
fCapacity = newSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
fBogus = FALSE;
|
|
|
|
fCount = newSize;
|
|
|
|
fHashCode = kInvalidHashCode;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2003-05-01 01:31:28 +00:00
|
|
|
#ifdef U_USE_COLLATION_KEY_DEPRECATES
|
1999-08-16 21:50:52 +00:00
|
|
|
// Create a copy of the byte array.
|
|
|
|
uint8_t*
|
|
|
|
CollationKey::toByteArray(int32_t& count) const
|
|
|
|
{
|
2002-02-28 01:42:40 +00:00
|
|
|
uint8_t *result = (uint8_t*) uprv_malloc( sizeof(uint8_t) * fCount );
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
if (result == NULL)
|
|
|
|
{
|
|
|
|
count = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
count = fCount;
|
1999-12-28 23:57:50 +00:00
|
|
|
uprv_memcpy(result, fBytes, fCount);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2002-02-28 01:42:40 +00:00
|
|
|
return result;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2003-05-01 01:31:28 +00:00
|
|
|
#endif
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
int32_t
|
|
|
|
CollationKey::hashCode() const
|
|
|
|
{
|
|
|
|
// (Cribbed from UnicodeString)
|
|
|
|
// We cache the hashCode; when it becomes invalid, due to any change to the
|
|
|
|
// string, we note this by setting it to kInvalidHashCode. [LIU]
|
|
|
|
|
|
|
|
// Note: This method is semantically const, but physically non-const.
|
|
|
|
|
|
|
|
if (fHashCode == kInvalidHashCode)
|
|
|
|
{
|
2001-10-16 18:31:13 +00:00
|
|
|
UHashTok key;
|
2001-07-06 19:53:12 +00:00
|
|
|
key.pointer = fBytes;
|
|
|
|
((CollationKey *)this)->fHashCode = uhash_hashChars(key);
|
2000-12-12 01:15:30 +00:00
|
|
|
#if 0
|
1999-08-16 21:50:52 +00:00
|
|
|
// 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 a additive linear congruential random number generator,
|
|
|
|
// thus producing a pseudorandom deterministic value which should be well
|
|
|
|
// distributed over the output range. [LIU]
|
|
|
|
const uint8_t *p = fBytes, *limit = fBytes + fCount;
|
|
|
|
int32_t inc = (fCount >= 256) ? fCount/128 : 2; // inc = max(fSize/64, 1);
|
|
|
|
int32_t hash = 0;
|
|
|
|
|
|
|
|
while (p < limit)
|
|
|
|
{
|
2002-02-28 01:42:40 +00:00
|
|
|
hash = ( hash * 37 ) + ((p[0] << 8) + p[1]);
|
1999-08-16 21:50:52 +00:00
|
|
|
p += inc;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we happened to get kInvalidHashCode, replace it with kEmptyHashCode
|
|
|
|
if (hash == kInvalidHashCode)
|
|
|
|
{
|
|
|
|
hash = kEmptyHashCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
((CollationKey *)this)->fHashCode = hash; // cast away const
|
2000-12-12 01:15:30 +00:00
|
|
|
#endif
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return fHashCode;
|
|
|
|
}
|
2001-10-08 23:26:58 +00:00
|
|
|
|
|
|
|
U_NAMESPACE_END
|
2002-09-20 01:54:48 +00:00
|
|
|
|
|
|
|
#endif /* #if !UCONFIG_NO_COLLATION */
|