11# include <absl/container/flat_hash_map.h>
12# include <absl/container/flat_hash_set.h>
14# include <unordered_map>
15# include <unordered_set>
20static_assert(std::endian::native == std::endian::little || std::endian::native == std::endian::big,
21 "mixed endianess not supported");
45 constexpr String() noexcept = default;
54 bool res = s1->size == s2->size;
55 for (
size_t i = 0, e = s1->size; res && i != e; ++i) res &= s1->chars[i] == s2->chars[i];
62 return std::hash<std::string_view>()(std::string_view(s->chars, s->size));
67 template<
class H>
friend constexpr H AbslHashValue(H h,
const String*
string)
noexcept {
68 return H::combine(std::move(h), std::string_view(string->chars, string->size));
73 static_assert(
sizeof(String) ==
sizeof(size_t),
"String.chars should be 0");
76 constexpr Sym(uintptr_t ptr) noexcept
80 constexpr Sym() noexcept = default;
84 constexpr
bool empty() const noexcept {
return ptr_ == 0; }
85 constexpr size_t size() const noexcept {
86 if (
empty())
return 0;
88 return ((
const String*)ptr_)->size;
98 constexpr char front() const noexcept {
return (*
this)[0]; }
99 constexpr char back() const noexcept {
return (*
this)[
size() - 1]; }
107 constexpr auto cend() const noexcept {
return end(); }
108 constexpr auto rbegin() const noexcept {
return std::reverse_iterator(
end()); }
109 constexpr auto rend() const noexcept {
return std::reverse_iterator(
begin()); }
111 constexpr auto crend() const noexcept {
return rend(); }
117 constexpr bool operator==(
Sym other)
const noexcept {
return this->ptr_ == other.ptr_; }
118 constexpr bool operator!=(
Sym other)
const noexcept {
return this->ptr_ != other.ptr_; }
121 if (s == 0)
return std::strong_ordering::less;
122 auto cmp = (*this)[0] <=> c;
123 if (s == 1)
return cmp;
124 return cmp == 0 ? std::strong_ordering::greater : cmp;
126 constexpr auto operator==(
char c)
const noexcept {
return (*
this) <=> c == 0; }
127 constexpr auto operator!=(
char c)
const noexcept {
return (*
this) <=> c != 0; }
132 constexpr const char*
c_str() const noexcept {
return view().data(); }
133 constexpr operator const char*()
const noexcept {
return c_str(); }
135 constexpr std::string_view
view() const noexcept {
136 if (
empty())
return {(
const char*)&ptr_, 0};
139 uintptr_t offset = std::endian::native == std::endian::little ? 1 : 0;
141 return std::string_view(((
const String*)ptr_)->chars, ((
const String*)ptr_)->
size);
143 constexpr operator std::string_view() const noexcept {
return view(); }
148 constexpr std::string
str() const noexcept {
return std::string(
view()); }
149 constexpr explicit operator std::string() const noexcept {
return str(); }
150 constexpr explicit operator bool() const noexcept {
return ptr_; }
154 template<
class H>
friend constexpr H AbslHashValue(H h,
Sym sym)
noexcept {
155 return H::combine(std::move(h), sym.ptr_);
158 friend struct ::std::hash<fe::Sym>;
172template<>
struct std::hash<
fe::Sym> {
173 size_t operator()(
fe::Sym sym)
const noexcept {
return std::hash<uintptr_t>()(sym.ptr_); }
183template<
class V>
using SymMap = absl::flat_hash_map<Sym, V>;
184using SymSet = absl::flat_hash_set<Sym>;
186template<
class V>
using SymMap = std::unordered_map<Sym, V>;
204 : pool_(container_.allocator<const
String*>()) {}
216 if (s.empty())
return Sym();
217 auto size = s.size();
220 uintptr_t ptr = size;
223 if constexpr (std::endian::native == std::endian::little)
224 for (uintptr_t i = 0, shift = 8; i != size; ++i, shift += 8) ptr |= (uintptr_t(s[i]) << shift);
227 ptr |= (uintptr_t(s[i]) << shift);
231 auto state = strings_.
state();
233 new (ptr)
String(s.size());
234 *std::copy(s.begin(), s.end(), ptr->chars) =
'\0';
235 auto [i, ins] = pool_.emplace(ptr);
236 if (ins)
return Sym((uintptr_t)ptr);
238 return Sym((uintptr_t)*i);
240 Sym sym(
const std::string& s) {
return sym((std::string_view)s); }
243 return s ==
nullptr || *s ==
'\0' ?
Sym() :
sym(std::string_view(s, strlen(s)));
251 swap(p1.strings_, p2.strings_ );
253 swap(p1.container_, p2.container_);
255 swap(p1.pool_, p2.pool_ );
262 absl::flat_hash_set<const String*, absl::Hash<const String*>, String::Equal> pool_;
265 std::unordered_set<const String*, String::Hash, String::Equal, Arena::Allocator<const String*>> pool_;
An arena pre-allocates so-called pages of size Arena::page_size_.
constexpr void deallocate(size_t num_bytes) noexcept
Removes num_bytes again.
constexpr void * allocate(size_t num_bytes, size_t align)
Get n bytes of fresh memory.
State state() const noexcept
Hash set where all strings - wrapped in Symbol - live in.
Sym sym(std::string_view s)
SymPool & operator=(SymPool)=delete
SymPool(const SymPool &)=delete
Sym sym(const std::string &s)
SymPool(SymPool &&other) noexcept
constexpr Sym sym(const char *s)
s is a null-terminated C-string.
friend void swap(SymPool &p1, SymPool &p2) noexcept
A Symbol just wraps a pointer to Sym::String, so pass Sym itself around as value.
constexpr auto operator<=>(Sym other) const 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
constexpr size_t size() const noexcept
constexpr Sym() noexcept=default
constexpr char back() const noexcept
friend std::ostream & operator<<(std::ostream &o, Sym sym)
constexpr bool operator!=(Sym other) const noexcept
constexpr auto crbegin() const noexcept
constexpr bool operator==(Sym other) const noexcept
constexpr char operator[](size_t i) const noexcept
constexpr auto crend() const noexcept
constexpr auto rbegin() const noexcept
constexpr std::string str() const noexcept
This involves a copy.
constexpr const char * c_str() const noexcept
constexpr auto operator<=>(char c) const noexcept
constexpr auto cbegin() const noexcept
constexpr auto operator==(char c) const noexcept
constexpr auto end() const noexcept
constexpr std::string_view view() const noexcept
constexpr auto operator!=(char c) const noexcept
std::unordered_map< Sym, V > SymMap
std::unordered_set< Sym > SymSet
constexpr bool operator()(const String *s1, const String *s2) const noexcept
size_t operator()(const String *s) const noexcept
constexpr String() noexcept=default