Plasma Engine  2.0
Loading...
Searching...
No Matches
plHashedString 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 = plMap<plUInt64, HashedData, plCompareHelper<plUInt64>, plStaticsAllocatorWrapper>
 
using HashedType = StringStorage::Iterator
 

Public Member Functions

 PL_DECLARE_MEM_RELOCATABLE_TYPE ()
 
 plHashedString ()
 Initializes this string to the empty string.
 
 plHashedString (const plHashedString &rhs)
 Copies the given plHashedString.
 
 plHashedString (plHashedString &&rhs)
 Moves the given plHashedString.
 
void operator= (const plHashedString &rhs)
 Copies the given plHashedString.
 
void operator= (plHashedString &&rhs)
 Moves the given plHashedString.
 
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.
 
template<size_t N>
void Assign (char(&string)[N])=delete
 
void Assign (plStringView sString)
 Assigning a new string from a non-hashed string is a very slow operation, this should be used rarely.
 
bool operator== (const plHashedString &rhs) const
 Comparing whether two hashed strings are identical is just a pointer comparison. This operation is what plHashedString is optimized for.
 
 PL_ADD_DEFAULT_OPERATOR_NOTEQUAL (const plHashedString &)
 
bool operator== (const plTempHashedString &rhs) const
 Compares this string object to an plTempHashedString object. This should be used whenever some object needs to be found and the string to compare against is not yet an plHashedString object.
 
 PL_ADD_DEFAULT_OPERATOR_NOTEQUAL (const plTempHashedString &)
 
bool operator< (const plHashedString &rhs) const
 This operator allows sorting objects by hash value, not by alphabetical order.
 
bool operator< (const plTempHashedString &rhs) const
 This operator allows sorting objects by hash value, not by alphabetical order.
 
const plStringGetString () 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.
 
plUInt64 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.
 
PL_ALWAYS_INLINE operator plStringView () const
 Returns a string view to this string's data.
 
PL_ALWAYS_INLINE plStringView GetView () const
 Returns a string view to this string's data.
 
PL_ALWAYS_INLINE operator const char * () const
 Returns a pointer to the internal Utf8 string.
 
template<size_t N>
PL_FORCE_INLINE void Assign (const char(&string)[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 plHashedString objects, which makes equality comparisons very easy (it's a pointer comparison).
Copying plHashedString 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 plHashedString 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 plHashedString objects with some temporary string object, used plTempHashedString, 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 plHashedString::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 plHashedString objects against, prefer to use plTempHashedString. It will only compute the strings hash value, but does not require any thread synchronization.

◆ Assign() [2/2]

PL_FORCE_INLINE void plHashedString::Assign ( plStringView 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 plHashedString objects against, prefer to use plTempHashedString. It will only compute the strings hash value, but does not require any thread synchronization.

◆ operator==()

bool plHashedString::operator== ( const plHashedString & rhs) const
inline

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

Note
Comparing between plHashedString 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: