mirror of
https://github.com/NoelFB/blah.git
synced 2024-11-25 16:18:57 +08:00
added std::hash for custom string type for use in unordered_map etc
This commit is contained in:
parent
e79ece2db3
commit
6f2cf82b8e
|
@ -3,6 +3,7 @@
|
|||
#include <stdarg.h>
|
||||
#include <cstdio>
|
||||
#include <blah/containers/vector.h>
|
||||
#include <functional>
|
||||
|
||||
namespace Blah
|
||||
{
|
||||
|
@ -37,12 +38,18 @@ namespace Blah
|
|||
// append cstr
|
||||
Str& operator+=(const char* rhs) { return append(rhs); }
|
||||
|
||||
// append char
|
||||
Str& operator+=(const char& rhs) { return append(rhs); }
|
||||
|
||||
// combine string
|
||||
Str operator+(const Str& rhs) { Str str; str.append(*this).append(rhs); return str; }
|
||||
|
||||
// combine cstr
|
||||
Str operator+(const char* rhs) { Str str; str.append(*this).append(rhs); return str; }
|
||||
|
||||
// combine char
|
||||
Str operator+(const char& rhs) { Str str; str.append(*this).append(rhs); return str; }
|
||||
|
||||
// implicit cast to cstr
|
||||
operator char* () { return cstr(); }
|
||||
|
||||
|
@ -244,3 +251,40 @@ namespace Blah
|
|||
return str;
|
||||
}
|
||||
}
|
||||
|
||||
namespace std
|
||||
{
|
||||
template <>
|
||||
struct hash<Blah::Str>
|
||||
{
|
||||
std::size_t operator()(const Blah::Str& key) const
|
||||
{
|
||||
std::size_t result = 2166136261U;
|
||||
|
||||
for (auto& it : key)
|
||||
{
|
||||
result ^= static_cast<size_t>(it);
|
||||
result *= 16777619U;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
template <int T>
|
||||
struct hash<Blah::StrOf<T>>
|
||||
{
|
||||
std::size_t operator()(const Blah::StrOf<T>& key) const
|
||||
{
|
||||
std::size_t result = 2166136261U;
|
||||
|
||||
for (auto& it : key)
|
||||
{
|
||||
result ^= static_cast<size_t>(it);
|
||||
result *= 16777619U;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
};
|
||||
}
|
|
@ -244,6 +244,8 @@ Str& Str::append_utf16(const u16* start, const u16* end, bool swap_endian)
|
|||
|
||||
Str& Str::trim()
|
||||
{
|
||||
if (m_length > 0)
|
||||
{
|
||||
const char* s = begin();
|
||||
const char* e = end() - 1;
|
||||
|
||||
|
@ -253,6 +255,8 @@ Str& Str::trim()
|
|||
e--;
|
||||
|
||||
set(s, e + 1);
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user