Xavax C++ Library | Class Index FAQ Overview |
hashCode
method.
The hash value returned by this method can be used
to accelerate string comparisons since two strings
cannot contain the same value if their hash values
are not equal. HString's
operator==
and
operator!=
exploit this feature.
The hash value for a HString object is computed as
s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
using integer arithmetic, where s[i]
is the ith character of the string,
n
is the length of the string,
and ^
indicates exponentiation.
This computation requires
n-1 iterations of a loop containing
one multiplication and one addition per loop
as shown in this example for n = 5
.
(((s[0] * 31 + s[1]) * 31 + s[2]) * 31 + s[3]) * 31 + s[4]
To further enhance performance,
HString employs a combination of delayed and
incremental evaluation.
The hash value is not computed until the first call to
hashCode
.
The hash value is saved and not computed again
unless the hash value is invalidated.
Inserting one or more characters into a string,
removing one or more characters from a string,
or truncating a string invalidates the hash value.
When appending to a string,
the hash value is computed incrementally.
Each character appended requires one additional
multiplication and addtion.
Constructor Summary |
HString()
Construct an empty string. |
HString(size_t size)
Construct an empty string with an initial capacity of size .
|
HString(const String& src)
Construct a new string that is a copy of src .
|
HString(const HString& src)
Construct a new string that is a copy of src .
|
HString(const char* s)
Construct a new string with the initial value taken from the null-terminated character string s .
|
HString(const char* s, size_t n)
Construct an new string with an initial capacity of n and the initial value taken
from the first n characters of s .
|
Operator Summary | |
bool
|
operator==(const HString& rhs)
Return true if the value of this object and the value of rhs are equal.
|
bool
|
operator!=(const HString& rhs)
Return true if the value of this object and the value of rhs are not equal.
|
Operators Inherited From String | |
operator=,
operator=,
operator+=,
operator+=,
operator+=,
operator(),
operator==,
operator==,
operator!=,
operator!=,
operator<=,
operator<=,
operator>=,
operator>=,
operator<,
operator<,
operator>,
operator>
|
Method Summary | |
unsigned
|
hashcode()
Returns the hash code for the string value. |
Methods Inherited From String | |
append,
append,
append,
append,
append,
insert,
insert,
insert,
insert,
insert,
length,
remove,
replace,
replace,
replace,
replace,
replace,
truncate
|
Methods Inherited From Object | |
cast<T>,
cast<T>,
classRecord,
ClassRecord
|
Related Classes | |
RCObject, RCPointer. String |
Constructor Detail |
HString()
HString(size_t size)
size
.
size
- the initial capacity.HString(const String& src)
src
- the string to be copied.HString(const HString& src)
src
- the string to be copied.HString(const char* s)
s
.
If s
is not null,
the initial capacity is the length of s
plus 1.
s
- the initial string value.HString(const char* s, size_t size)
n
and the initial value taken
from the first n
characters of s
.
s
- the initial string value.size
- the initial capacity.
Operator Detail |
bool operator==(const HString& rhs)
true
if the value of this
object is equal to the value of rhs
.
A character by character comparison is only performed
if the hash values for the two strings are identical.
rhs
- the right operand of the comparison.rhs
.bool operator!=(const HString& rhs)
true
if the value of this
object is not equal to the value of rhs
.
A character by character comparison is only performed
if the hash values for the two strings are identical.
rhs
- the right operand of the comparison.rhs
.
Method Detail |
unsigned long hashcode()
Example Code |
Copyright © 2003 Xavax Inc. -- All Rights Reserved