Xavax C++ Library Class Index   FAQ   Overview

Class xavax::HString

HString is a string class derived from String that implements the 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
long
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()
Construct an empty string.

HString

HString(size_t size)
Construct an empty string with an initial capacity of size.
Parameters:
size - the initial capacity.

HString

HString(const String& src)
Construct a new string that is a copy of src.
Parameters:
src - the string to be copied.

HString

HString(const HString& src)
Construct a new string that is a copy of src.
Parameters:
src - the string to be copied.

HString

HString(const char* s)
Construct a new string with the initial value taken from the null-terminated character string s. If s is not null, the initial capacity is the length of s plus 1.
Parameters:
s - the initial string value.

HString

HString(const char* s, size_t size)
Construct an new string with an initial capacity of n and the initial value taken from the first n characters of s.
Parameters:
s - the initial string value.
size - the initial capacity.

Operator Detail

operator==

bool operator==(const HString& rhs)
Returns 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.
Parameters:
rhs - the right operand of the comparison.
Returns:
if the value of this object is equal to the value of rhs.

operator!=

bool operator!=(const HString& rhs)
Returns 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.
Parameters:
rhs - the right operand of the comparison.
Returns:
if the value of this object is equal to the value of rhs.

Method Detail

hashcode

unsigned long hashcode()
Returns the hash code for this object.
Returns:
a hash code.

Example Code

Copyright © 2003 Xavax Inc. -- All Rights Reserved