Files
2026-04-17 20:09:41 +03:00

84 lines
2.4 KiB
C++

// Native runtime - String key types for hashed containers
// Part of the AC6 Recompilation native foundation
#pragma once
#include <string>
#include <variant>
#include <native/string/utf8.h>
namespace rex::string {
namespace detail {
struct string_key_base {
private:
std::variant<std::string, std::string_view> value_;
public:
explicit string_key_base(const std::string_view value) : value_(value) {}
explicit string_key_base(std::string value) : value_(std::move(value)) {}
std::string_view view() const {
return std::holds_alternative<std::string>(value_) ? std::get<std::string>(value_)
: std::get<std::string_view>(value_);
}
};
} // namespace detail
struct string_key : detail::string_key_base {
public:
explicit string_key(const std::string_view value) : string_key_base(value) {}
explicit string_key(std::string value) : string_key_base(value) {}
static string_key create(const std::string_view value) { return string_key(std::string(value)); }
static string_key create(std::string value) { return string_key(value); }
bool operator==(const string_key& other) const { return other.view() == view(); }
size_t hash() const { return rex::string::utf8_hash_fnv1a(view()); }
struct Hash {
size_t operator()(const string_key& t) const { return t.hash(); }
};
};
struct string_key_case : detail::string_key_base {
public:
explicit string_key_case(const std::string_view value) : string_key_base(value) {}
explicit string_key_case(std::string value) : string_key_base(value) {}
static string_key_case create(const std::string_view value) {
return string_key_case(std::string(value));
}
static string_key_case create(std::string value) { return string_key_case(value); }
bool operator==(const string_key_case& other) const {
return rex::string::utf8_equal_case(other.view(), view());
}
size_t hash() const { return rex::string::utf8_hash_fnv1a_case(view()); }
struct Hash {
size_t operator()(const string_key_case& t) const { return t.hash(); }
};
};
} // namespace rex::string
namespace std {
template <>
struct hash<rex::string::string_key> {
std::size_t operator()(const rex::string::string_key& t) const { return t.hash(); }
};
template <>
struct hash<rex::string::string_key_case> {
std::size_t operator()(const rex::string::string_key_case& t) const { return t.hash(); }
};
} // namespace std