Skip to content

Instantly share code, notes, and snippets.

@nonathaj
Created November 19, 2015 22:10
Show Gist options
  • Save nonathaj/d73e637df5f727e41b19 to your computer and use it in GitHub Desktop.
Save nonathaj/d73e637df5f727e41b19 to your computer and use it in GitHub Desktop.
C++ Hashed String - Allows fast comparison of strings in a small storage type (when you do NOT need to know the value of the string)
#include "HashedString.h"
namespace Engine
{
unsigned int HashedString::Hash(const char * i_string)
{
assert(i_string);
return Hash(reinterpret_cast<void *>(const_cast<char *>(i_string)), strlen(i_string));
}
uint32_t HashedString::Hash(const void * i_bytes, size_t i_bytecount)
{
// FNV hash, http://isthe.com/chongo/tech/comp/fnv/
register const unsigned char * p = static_cast<const unsigned char *>(i_bytes);
uint32_t hash = 2166136261;
for (size_t i = 0; i < i_bytecount; ++i)
hash = 16777619 * (hash ^ p[i]);
return hash ^ (hash >> 16);
};
} // namespace Engine
#ifndef _ENGINE_CONTAINERS_HASHEDSTRING_H
#define _ENGINE_CONTAINERS_HASHEDSTRING_H
#include <string>
#include <stdint.h>
namespace Engine
{
/*
HashedString Class
Used for storing a string as an integer.
Useful for string that are not printed and only compared wth other strings
*/
class HashedString
{
public:
inline HashedString();
inline HashedString(const char * i_string);
inline HashedString(const HashedString & i_other);
inline HashedString & operator=(const HashedString & i_other);
inline ~HashedString();
inline uint32_t Get() const;
inline bool operator==(const HashedString & i_other) const;
inline bool operator!=(const HashedString & i_other) const;
static uint32_t Hash(const char * i_string);
static uint32_t Hash(const void * i_bytes, uint32_t i_count);
private:
uint32_t m_Hash;
#ifdef DEBUG_KEEP_STRING
const char * m_pString;
#endif
};
}
#include "HashedString.inl"
namespace std
{
//Overload the std::hash to allow the hashedstring to be used in std containers, such as unordered_map
template<>
struct hash<Engine::HashedString>
{
uint32_t operator()(const Engine::HashedString &i_str) const { return hash<uint32_t>()(i_str.Get()); }
};
}
#endif //_ENGINE_CONTAINERS_HASHEDSTRING_H
#include <string.h>
namespace Engine
{
inline HashedString::HashedString()
:m_Hash(Hash(""))
{
}
inline HashedString::HashedString(const char * i_string)
:m_Hash(Hash(i_string))
#ifdef DEBUG_KEEP_STRING
, m_pString(strdup(i_string))
#endif
{
}
inline HashedString::HashedString(const HashedString & i_other)
:m_Hash(i_other.m_Hash)
#ifdef DEBUG_KEEP_STRING
, m_pString(strdup(i_other.m_pString))
#endif
{
}
inline HashedString::~HashedString()
{
#ifdef DEBUG_KEEP_STRING
if (m_pString)
free(m_pString)
#endif
}
inline HashedString & HashedString::operator=(const HashedString & i_other)
{
m_Hash = i_other.m_Hash;
#ifdef DEBUG_KEEP_STRING
if (m_pString)
free(m_pString)
m_pString = i_other.m_pString;
#endif
return *this;
}
inline uint32_t HashedString::Get(void) const
{
return m_Hash;
}
inline bool HashedString::operator==(const HashedString & i_other) const
{
return m_Hash == i_other.m_Hash;
}
inline bool HashedString::operator!=(const HashedString & i_other) const
{
return m_Hash != i_other.m_Hash;
}
} // namespace Engine
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment