ezEngine  Milestone 9
ezHashedString Class Reference

This class is optimized to take nearly no memory (sizeof(void*)) and to allow very fast checks whether two strings are identical. More...

#include <HashedString.h>

Classes

struct  HashedData
 

Public Types

typedef ezMap< ezUInt32, HashedDataStringStorage
 
typedef StringStorage::Iterator HashedType
 

Public Member Functions

 EZ_DECLARE_MEM_RELOCATABLE_TYPE ()
 
 ezHashedString ()
 Initializes this string to the empty string.
 
 ezHashedString (const ezHashedString &rhs)
 Copies the given ezHashedString.
 
 ezHashedString (ezHashedString &&rhs)
 Moves the given ezHashedString.
 
 ~ezHashedString ()
 Releases the reference to the internal data. Does NOT deallocate any data, even if this held the last reference to some string.
 
void operator= (const ezHashedString &rhs)
 Copies the given ezHashedString.
 
void operator= (ezHashedString &&rhs)
 Moves the given ezHashedString.
 
template<size_t N>
void Assign (const char(&szString)[N])
 Assigning a new string from a string constant is a slow operation, but the hash computation can happen at compile time. More...
 
void Assign (ezHashingUtils::StringWrapper szString)
 Assigning a new string from a non-hashed string is a very slow operation, this should be used rarely. More...
 
bool operator== (const ezHashedString &rhs) const
 Comparing whether two hashed strings are identical is just a pointer comparison. This operation is what ezHashedString is optimized for. More...
 
bool operator!= (const ezHashedString &rhs) const
 
bool operator== (const ezTempHashedString &rhs) const
 Compares this string object to an ezTempHashedString object. This should be used whenever some object needs to be found and the string to compare against is not yet an ezHashedString object.
 
bool operator!= (const ezTempHashedString &rhs) const
 
bool operator< (const ezHashedString &rhs) const
 This operator allows sorting objects by hash value, not by alphabetical order.
 
bool operator< (const ezTempHashedString &rhs) const
 This operator allows sorting objects by hash value, not by alphabetical order.
 
const ezStringGetString () const
 Gives access to the actual string data, so you can do all the typical (read-only) string operations on it.
 
const char * GetData () const
 Gives access to the actual string data, so you can do all the typical (read-only) string operations on it.
 
ezUInt32 GetHash () const
 Returns the hash of the stored string.
 
bool IsEmpty () const
 Returns whether the string is empty.
 
void Clear ()
 Resets the string to the empty string.
 
EZ_ALWAYS_INLINE operator ezStringView () const
 Returns a string view to this string's data.
 
EZ_ALWAYS_INLINE ezStringView GetView () const
 Returns a string view to this string's data.
 
EZ_ALWAYS_INLINE operator const char * () const
 Returns a pointer to the internal Utf8 string.
 
template<size_t N>
EZ_FORCE_INLINE void Assign (const char(&szString)[N])
 

Detailed Description

This class is optimized to take nearly no memory (sizeof(void*)) and to allow very fast checks whether two strings are identical.

Internally only a reference to the string data is stored. The data itself is stored in a central location, where no duplicates are possible. Thus two identical strings will result in identical ezHashedString objects, which makes equality comparisons very easy (it's a pointer comparison).
Copying ezHashedString objects around and assigning between them is very fast as well.

Assigning from some other string type is rather slow though, as it requires thread synchronization.
You can also get access to the actual string data via GetString().

You should use ezHashedString whenever the size of the encapsulating object is important and when changes to the string itself are rare, but checks for equality might be frequent (e.g. in a system where objects are identified via their name).
At runtime when you need to compare ezHashedString objects with some temporary string object, used ezTempHashedString, as it will only use the string's hash value for comparison, but will not store the actual string anywhere.

Member Function Documentation

◆ Assign() [1/2]

template<size_t N>
void ezHashedString::Assign ( const char(&)  szString[N])

Assigning a new string from a string constant is a slow operation, but the hash computation can happen at compile time.

If you need to create an object to compare ezHashedString objects against, prefer to use ezTempHashedString. It will only compute the strings hash value, but does not require any thread synchronization.

◆ Assign() [2/2]

EZ_FORCE_INLINE void ezHashedString::Assign ( ezHashingUtils::StringWrapper  szString)

Assigning a new string from a non-hashed string is a very slow operation, this should be used rarely.

If you need to create an object to compare ezHashedString objects against, prefer to use ezTempHashedString. It will only compute the strings hash value, but does not require any thread synchronization.

◆ operator!=() [1/2]

bool ezHashedString::operator!= ( const ezHashedString rhs) const
inline
See also
operator==

◆ operator!=() [2/2]

bool ezHashedString::operator!= ( const ezTempHashedString rhs) const
inline
See also
operator==

◆ operator==()

bool ezHashedString::operator== ( const ezHashedString rhs) const
inline

Comparing whether two hashed strings are identical is just a pointer comparison. This operation is what ezHashedString is optimized for.

Note
Comparing between ezHashedString objects is always error-free, so even if two string had the same hash value, although they are different, this comparison function will not report they are the same.

The documentation for this class was generated from the following files: