12#include <ankerl/unordered_dense.h>
36 constexpr String() noexcept = default;
45 return std::string_view(s1->chars, s1->size) == std::string_view(s2->chars, s2->size);
57 static_assert(
sizeof(String) ==
sizeof(size_t),
"String.chars should be 0");
60 constexpr Sym(uintptr_t ptr) noexcept
64 constexpr Sym() noexcept = default;
68 [[nodiscard]] constexpr
bool empty() const noexcept {
return ptr_ == 0; }
69 [[nodiscard]]
constexpr size_t size() const noexcept {
70 if (
empty())
return 0;
72 return ((
const String*)ptr_)->size;
74 [[nodiscard]]
constexpr uintptr_t
raw() const noexcept {
return ptr_; }
83 constexpr char front() const noexcept {
return (*
this)[0]; }
84 constexpr char back() const noexcept {
return (*
this)[
size() - 1]; }
89 constexpr auto begin() const noexcept {
return view().data(); }
92 constexpr auto cend() const noexcept {
return end(); }
93 constexpr auto rbegin() const noexcept {
return std::reverse_iterator(
end()); }
94 constexpr auto rend() const noexcept {
return std::reverse_iterator(
begin()); }
96 constexpr auto crend() const noexcept {
return rend(); }
101 friend constexpr auto operator<=>(Sym s1, Sym s2)
noexcept {
return s1.view() <=> s2.view(); }
102 friend constexpr bool operator==(Sym s1, Sym s2)
noexcept {
return s1.ptr_ == s2.ptr_; }
107 friend constexpr std::strong_ordering
operator<=>(Sym s,
char c)
noexcept {
return cmp<false>(s, c); }
108 friend constexpr std::strong_ordering
operator<=>(
char c, Sym s)
noexcept {
return cmp<true>(s, c); }
109 friend constexpr bool operator==(Sym s,
char c)
noexcept {
return (s.size() == 1) && (s[0] == c); }
110 friend constexpr bool operator==(
char c, Sym s)
noexcept {
return (s.size() == 1) && (s[0] == c); }
115 friend constexpr auto operator<=>(Sym lhs,
const std::convertible_to<std::string_view>
auto& rhs)
noexcept {
116 return lhs.view() <=> std::string_view(rhs);
118 friend constexpr auto operator<=>(
const std::convertible_to<std::string_view>
auto& lhs, Sym rhs)
noexcept {
119 return std::string_view(lhs) <=> rhs.view();
122 friend constexpr bool operator==(Sym lhs,
const std::convertible_to<std::string_view>
auto& rhs)
noexcept {
123 return lhs.view() == std::string_view(rhs);
126 friend constexpr bool operator==(
const std::convertible_to<std::string_view>
auto& lhs, Sym rhs)
noexcept {
127 return std::string_view(lhs) == rhs.view();
136 [[nodiscard]]
constexpr std::string_view
view() const noexcept {
137 if (
empty())
return {std::bit_cast<const char*>(&ptr_), 0};
140 uintptr_t offset = std::endian::native == std::endian::little ? 1 : 0;
142 auto S = std::bit_cast<const String*>(ptr_);
143 return std::string_view(S->chars, S->size);
145 constexpr operator std::string_view() const noexcept {
return view(); }
150 constexpr std::string
str()
const {
return std::string(
view()); }
151 constexpr explicit operator std::string()
const {
return str(); }
152 constexpr explicit operator bool() const noexcept {
return ptr_; }
166 size_t operator()(Sym s)
const noexcept {
return ankerl::unordered_dense::hash<uintptr_t>()(s.ptr_); }
170 constexpr bool operator()(Sym a, Sym b)
const noexcept {
return a.ptr_ == b.ptr_; }
176 static constexpr std::strong_ordering cmp(
Sym s,
char c)
noexcept {
177 const auto n = s.size();
178 if (n == 0)
return Rev ? std::strong_ordering::greater : std::strong_ordering::less;
180 auto cmp = s[0] <=> c;
181 if (cmp != 0)
return cmp;
183 return (n == 1) ? std::strong_ordering::equal
184 : (Rev ? std::strong_ordering::less : std::strong_ordering::greater);
198struct std::hash<
fe::Sym> {
210using SymMap = ankerl::unordered_dense::map<Sym, V, Sym::Hash, Sym::Eq>;
211using SymSet = ankerl::unordered_dense::set<Sym, Sym::Hash, Sym::Eq>;
221template<
class V,
size_t Size>
225 static constexpr size_t Capacity = std::bit_ceil(2 * Size);
230 assert(!sym.
empty() &&
"the empty Sym%bol marks a free slot");
231 for (
auto i = idx(sym);; i = (i + 1) & Mask) {
232 assert(slots_[i].sym != sym &&
"already present");
233 if (slots_[i].sym.
empty()) {
234 slots_[i] = {sym, std::move(v)};
243 for (
auto i = idx(sym);; i = (i + 1) & Mask) {
244 if (slots_[i].sym.
empty())
return {};
245 if (slots_[i].sym == sym)
return slots_[i].v;
253 static constexpr size_t Mask =
Capacity - 1;
254 static constexpr size_t Shift = 64 - std::bit_width(Mask);
255 static constexpr uint64_t Magic = 0x9E3779B97F4A7C15ull;
259 static size_t idx(
Sym sym) {
return size_t((uint64_t(sym.
raw()) * Magic) >> Shift); }
266 std::array<Slot, Capacity> slots_ = {};
289 Sym sym(
const std::string& s) {
return sym((std::string_view)s); }
291 Sym sym(
const char* s) {
return s ==
nullptr ?
Sym() :
sym(std::string_view(s)); }
298 swap(p1.strings_, p2.strings_);
299 swap(p1.pool_, p2.pool_ );
305 ankerl::unordered_dense::set<const String*, String::Hash, String::Equal> pool_;
308static_assert(std::is_trivially_copyable_v<Sym>);
309static_assert(
sizeof(uintptr_t) ==
sizeof(
void*),
"uintptr_t must match pointer size");
310static_assert(std::has_unique_object_representations_v<uintptr_t>);
311static_assert(std::endian::native == std::endian::little || std::endian::native == std::endian::big,
312 "mixed endianness not supported");
An arena pre-allocates so-called pages of size Arena::page_size_.
Sym sym(const char *s)
s is a null-terminated C-string.
Sym sym(std::string_view s)
SymPool & operator=(SymPool)=delete
SymPool(const SymPool &)=delete
Sym sym(const std::string &s)
SymPool(SymPool &&other) noexcept
friend void swap(SymPool &p1, SymPool &p2) noexcept
A fixed-capacity Symbol -> V map for a closed set of Size entries: filled once, then only looked up -...
static constexpr size_t Capacity
Twice Size, rounded up to a power of two, so the load factor stays below 1/2.
void emplace(Sym sym, V v)
bool contains(Sym sym) const
std::optional< V > find(Sym sym) const
Yields nothing if sym is not present - the empty Symbol never is.
A Symbol just wraps a pointer to Sym::String, so pass Sym itself around as value.
constexpr uintptr_t raw() const noexcept
friend constexpr std::strong_ordering operator<=>(char c, Sym s) noexcept
friend constexpr bool operator==(Sym lhs, const std::convertible_to< std::string_view > auto &rhs) noexcept
friend constexpr auto operator<=>(Sym s1, Sym s2) noexcept
constexpr auto rend() const noexcept
constexpr auto begin() const noexcept
constexpr char front() const noexcept
constexpr std::string_view operator*() const noexcept
constexpr bool empty() const noexcept
static constexpr size_t Short_String_Mask
static constexpr size_t Short_String_Bytes
constexpr auto cend() const noexcept
friend constexpr bool operator==(char c, Sym s) noexcept
constexpr size_t size() const noexcept
constexpr Sym() noexcept=default
constexpr char back() const noexcept
friend constexpr auto operator<=>(const std::convertible_to< std::string_view > auto &lhs, Sym rhs) noexcept
friend constexpr bool operator==(Sym s1, Sym s2) noexcept
constexpr auto crbegin() const noexcept
constexpr char operator[](size_t i) const noexcept
friend constexpr bool operator==(Sym s, char c) noexcept
constexpr auto crend() const noexcept
friend std::ostream & operator<<(std::ostream &os, Sym sym)
constexpr auto rbegin() const noexcept
friend constexpr bool operator==(const std::convertible_to< std::string_view > auto &lhs, Sym rhs) noexcept
constexpr std::string str() const
This involves a copy.
constexpr auto cbegin() const noexcept
friend constexpr auto operator<=>(Sym lhs, const std::convertible_to< std::string_view > auto &rhs) noexcept
constexpr auto end() const noexcept
constexpr std::string_view view() const noexcept
friend constexpr std::strong_ordering operator<=>(Sym s, char c) noexcept
ankerl::unordered_dense::set< Sym, Sym::Hash, Sym::Eq > SymSet
ankerl::unordered_dense::map< Sym, V, Sym::Hash, Sym::Eq > SymMap
Hashes the characters of a string; transparent, so std::string keys may be looked up by std::string_v...
constexpr bool operator()(Sym a, Sym b) const noexcept
size_t operator()(Sym s) const noexcept
constexpr bool operator()(const String *s1, const String *s2) const noexcept
Hashes the characters, not the pointer - String::Equal compares them, and the two have to agree.
size_t operator()(const String *s) const noexcept
constexpr String() noexcept=default