ezEngine  Release 25.03
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

using StringStorage = ezMap< ezUInt64, HashedData, ezCompareHelper< ezUInt64 >, ezStaticsAllocatorWrapper >
 
using HashedType = StringStorage::Iterator
 

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.
 
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(&string)[N])
 Assigning a new string from a string constant is a slow operation, but the hash computation can happen at compile time. More...
 
template<size_t N>
void Assign (char(&string)[N])=delete
 
void Assign (ezStringView sString)
 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...
 
 EZ_ADD_DEFAULT_OPERATOR_NOTEQUAL (const ezHashedString &)
 
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.
 
ezUInt64 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.
 
EZ_ALWAYS_INLINE bool operator== (const char *szString) const
 
 EZ_ADD_DEFAULT_OPERATOR_NOTEQUAL (const char *)
 
template<size_t N>
EZ_FORCE_INLINE void Assign (const char(&string)[N])
 

Static Public Member Functions

static ezResult LookupStringHash (ezUInt64 uiHash, ezStringView &out_sResult)
 Attempts to find a known string for the given hash value. More...
 

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(&)  string[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 ( ezStringView  sString)

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.

◆ LookupStringHash()

ezResult ezHashedString::LookupStringHash ( ezUInt64  uiHash,
ezStringView out_sResult 
)
static

Attempts to find a known string for the given hash value.

Careful, this is a slow operation (involving a mutex). It is only meant for debug output purposes. The string hash may not be known, if the value was never assigned to any ezHashedString, in which case EZ_FAILURE is returned.

◆ 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: