XII Release 0.1.0
Loading...
Searching...
No Matches
xiiHashedString 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 = xiiMap<xiiUInt64, HashedData, xiiCompareHelper<xiiUInt64>, xiiStaticAllocatorWrapper>
 
using HashedType = StringStorage::Iterator
 

Public Member Functions

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

◆ Assign() [2/2]

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

◆ operator==()

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

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

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