added std::hash for custom string type for use in unordered_map etc

This commit is contained in:
Noel Berry 2021-05-06 20:47:22 -07:00
parent e79ece2db3
commit 6f2cf82b8e
2 changed files with 61 additions and 13 deletions

View File

@ -3,6 +3,7 @@
#include <stdarg.h> #include <stdarg.h>
#include <cstdio> #include <cstdio>
#include <blah/containers/vector.h> #include <blah/containers/vector.h>
#include <functional>
namespace Blah namespace Blah
{ {
@ -36,6 +37,9 @@ namespace Blah
// append cstr // append cstr
Str& operator+=(const char* rhs) { return append(rhs); } Str& operator+=(const char* rhs) { return append(rhs); }
// append char
Str& operator+=(const char& rhs) { return append(rhs); }
// combine string // combine string
Str operator+(const Str& rhs) { Str str; str.append(*this).append(rhs); return str; } Str operator+(const Str& rhs) { Str str; str.append(*this).append(rhs); return str; }
@ -43,6 +47,9 @@ namespace Blah
// combine cstr // combine cstr
Str operator+(const char* rhs) { Str str; str.append(*this).append(rhs); return str; } 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 // implicit cast to cstr
operator char* () { return cstr(); } operator char* () { return cstr(); }
@ -186,10 +193,10 @@ namespace Blah
private: private:
static char empty_buffer[1]; static char empty_buffer[1];
char* m_buffer; char* m_buffer;
int m_length; int m_length;
int m_capacity; int m_capacity;
int m_local_size; int m_local_size;
}; };
// combine string // combine string
@ -209,8 +216,8 @@ namespace Blah
StrOf(const StrOf& rhs) : Str(T) { m_local_buffer[0] = '\0'; set(rhs); } StrOf(const StrOf& rhs) : Str(T) { m_local_buffer[0] = '\0'; set(rhs); }
// assignment operators // assignment operators
StrOf& operator=(const char* rhs) { set(rhs); return *this; } StrOf& operator=(const char* rhs) { set(rhs); return *this; }
StrOf& operator=(const Str& rhs) { set(rhs); return *this; } StrOf& operator=(const Str& rhs) { set(rhs); return *this; }
StrOf& operator=(const StrOf& rhs) { set(rhs); return *this; } StrOf& operator=(const StrOf& rhs) { set(rhs); return *this; }
// creates a string from the format // creates a string from the format
@ -243,4 +250,41 @@ namespace Blah
return str; 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;
}
};
} }

View File

@ -244,15 +244,19 @@ Str& Str::append_utf16(const u16* start, const u16* end, bool swap_endian)
Str& Str::trim() Str& Str::trim()
{ {
const char* s = begin(); if (m_length > 0)
const char* e = end() - 1; {
const char* s = begin();
const char* e = end() - 1;
while (isspace(*s) && s != e) while (isspace(*s) && s != e)
s++; s++;
while (isspace(*e) && s != e) while (isspace(*e) && s != e)
e--; e--;
set(s, e + 1);
}
set(s, e + 1);
return *this; return *this;
} }