4#ifndef GREM_CORE_DATA_STRING_POOL_HPP
5#define GREM_CORE_DATA_STRING_POOL_HPP
35 [[nodiscard]] constexpr
bool operator==(const
StringID&) const = default;
36 [[nodiscard]] constexpr auto operator<=>(const
StringID&) const = default;
52template <
typename CharT,
typename Allocator = std::allocator<CharT>>
55 using Length = std::uint32_t;
57 static constexpr std::size_t PADDED_LENGTH_SIZE_IN_CHARS = (
sizeof(Length) +
sizeof(CharT) - 1) /
sizeof(CharT);
58 static constexpr std::size_t PADDED_LENGTH_SIZE_IN_BYTES = PADDED_LENGTH_SIZE_IN_CHARS *
sizeof(CharT);
60 struct LengthAsBytes {
61 std::byte bytes[
sizeof(Length)];
64 struct PaddedLengthAsBytes {
65 std::byte bytes[PADDED_LENGTH_SIZE_IN_BYTES]{};
67 constexpr PaddedLengthAsBytes() =
default;
69 constexpr explicit PaddedLengthAsBytes(LengthAsBytes lengthBytes) {
70 for (std::size_t i = 0; i < PADDED_LENGTH_SIZE_IN_BYTES; ++i) {
71 bytes[i] = lengthBytes.bytes[i];
75 constexpr explicit operator LengthAsBytes()
const {
76 LengthAsBytes result{};
77 for (std::size_t i = 0; i <
sizeof(Length); ++i) {
78 result.bytes[i] = bytes[i];
84 struct LengthAsChars {
85 CharT chars[PADDED_LENGTH_SIZE_IN_CHARS];
87 constexpr void writeTo(CharT* output)
const {
88 for (std::size_t i = 0; i < PADDED_LENGTH_SIZE_IN_CHARS; ++i) {
93 constexpr void readFrom(
const CharT* input) {
94 for (std::size_t i = 0; i < PADDED_LENGTH_SIZE_IN_CHARS; ++i) {
108 constexpr explicit StringPoolBase(std::size_t initialCapacity,
const Allocator& allocator = Allocator())
109 : hashTableSlots(std::bit_ceil(initialCapacity + (initialCapacity + 1) / 2),
StringID::INVALID, allocator)
110 , buffer(initialCapacity * 8, allocator) {}
127 *
this = std::move(other);
136 hashTableSlots.clear();
137 hashTableOccupancy = 0;
143 throw std::length_error{
"Maximum string length exceeded."};
145 if (hashTableOccupancy >= (hashTableSlots.size() / 3) * 2) {
146 const std::size_t newHashTableCapacity = std::max(hashTableSlots.size() * 2, std::size_t{64});
147 if (newHashTableCapacity <= hashTableSlots.size()) {
148 throw std::length_error{
"Maximum string pool hash table capacity exceeded."};
151 for (
const StringID key : hashTableSlots) {
153 const std::size_t hash = std::hash<StringViewBase<CharT>>{}((*this)[key]);
154 std::size_t slotIndex = hash & (newHashTableCapacity - 1);
156 slotIndex = (slotIndex + 1) & (newHashTableCapacity - 1);
158 newHashTableSlots[slotIndex] = key;
161 hashTableSlots = std::move(newHashTableSlots);
163 if (hashTableOccupancy == 0 && !
string.empty()) {
167 const std::size_t hash = std::hash<StringViewBase<CharT>>{}(string);
168 std::size_t slotIndex = hash & (hashTableSlots.size() - 1);
169 while (hashTableSlots[slotIndex] !=
StringID::INVALID && (*
this)[hashTableSlots[slotIndex]] !=
string) {
170 slotIndex = (slotIndex + 1) & (hashTableSlots.size() - 1);
172 StringID& slot = hashTableSlots[slotIndex];
176 const std::size_t offset = buffer.size();
177 const std::size_t addedSize =
sizeof(LengthAsChars) +
string.size() + 1;
178 constexpr std::size_t MAX_SIZE = std::min(std::size_t{std::numeric_limits<std::ptrdiff_t>::max()}, std::size_t{std::numeric_limits<StringID::index_type>::max()});
179 if (addedSize > MAX_SIZE || offset > MAX_SIZE - addedSize) {
180 throw std::length_error{
"Maximum string pool size exceeded."};
182 buffer.resize(buffer.size() + addedSize);
183 const Length
length =
static_cast<Length
>(
string.size());
184 const LengthAsChars lengthAsChars = std::bit_cast<LengthAsChars>(PaddedLengthAsBytes{std::bit_cast<LengthAsBytes>(
length)});
185 lengthAsChars.writeTo(buffer.data() + offset);
186 string.copy(buffer.data() + offset +
sizeof(LengthAsChars),
string.size());
187 buffer[offset +
sizeof(LengthAsChars) +
string.size()] = CharT();
190 ++hashTableOccupancy;
198 const std::size_t hash = std::hash<StringViewBase<CharT>>{}(string);
199 std::size_t slotIndex = hash & (hashTableSlots.size() - 1);
200 while (hashTableSlots[slotIndex] !=
StringID::INVALID && (*
this)[hashTableSlots[slotIndex]] !=
string) {
201 slotIndex = (slotIndex + 1) & (hashTableSlots.size() - 1);
203 return hashTableSlots[slotIndex];
207 GREM_ASSERT(std::size_t{
id.getIndex()} < buffer.size());
208 LengthAsChars lengthAsChars{};
209 GREM_ASSERT(std::size_t{
id.getIndex()} +
sizeof(LengthAsChars) < buffer.size());
210 lengthAsChars.readFrom(buffer.data() +
id.getIndex());
211 const Length
length = std::bit_cast<Length>(
static_cast<LengthAsBytes
>(std::bit_cast<PaddedLengthAsBytes>(lengthAsChars)));
212 GREM_ASSERT(std::size_t{
id.getIndex()} +
sizeof(LengthAsChars) + std::size_t{
length} < buffer.size());
217 GREM_ASSERT(std::size_t{
id.getIndex()} < buffer.size());
218 GREM_ASSERT(std::size_t{
id.getIndex()} +
sizeof(LengthAsChars) < buffer.size());
219 return buffer.data() + std::size_t{
id.getIndex()} +
sizeof(LengthAsChars);
223 return buffer.get_allocator();
227 using StringIDAllocator =
typename std::allocator_traits<Allocator>::template rebind_alloc<StringID>;
230 std::size_t hashTableOccupancy = 0;
239struct std::hash<
grem::StringID> {
241 return indexHasher(
id.getIndex());
245 [[no_unique_address]] std::hash<grem::StringID::index_type> indexHasher;
#define GREM_ASSERT(expression)
Definition assertions.hpp:77
Definition StringPool.hpp:53
constexpr StringPoolBase(const StringPoolBase &other)
Definition StringPool.hpp:115
constexpr allocator_type get_allocator() const noexcept
Definition StringPool.hpp:222
constexpr StringViewBase< CharT > operator[](StringID id) const
Definition StringPool.hpp:206
constexpr void clear() noexcept
Definition StringPool.hpp:135
constexpr StringID find(StringViewBase< CharT > string) const noexcept
Definition StringPool.hpp:194
constexpr StringID insert(StringViewBase< CharT > string)
Definition StringPool.hpp:141
constexpr StringPoolBase & operator=(const StringPoolBase &other)=default
constexpr StringPoolBase(const StringPoolBase &other, const Allocator &allocator)
Definition StringPool.hpp:118
constexpr StringPoolBase(const Allocator &allocator)
Definition StringPool.hpp:112
constexpr ~StringPoolBase()=default
constexpr StringPoolBase() noexcept
Definition StringPool.hpp:105
constexpr StringPoolBase & operator=(StringPoolBase &&other) noexcept=default
Allocator allocator_type
Definition StringPool.hpp:101
constexpr StringPoolBase(std::size_t initialCapacity, const Allocator &allocator=Allocator())
Definition StringPool.hpp:108
constexpr const CharT * c_str(StringID id) const
Definition StringPool.hpp:216
static constexpr std::size_t MAX_STRING_LENGTH
Definition StringPool.hpp:103
constexpr StringPoolBase(StringPoolBase &&other) noexcept=default
constexpr StringPoolBase(StringPoolBase &&other, const Allocator &allocator) noexcept
Definition StringPool.hpp:125
std::basic_string_view< CharT, Traits > StringViewBase
Definition StringView.hpp:14
StringPoolBase< char > StringPool
Definition StringPool.hpp:234
GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL length(T a)
Definition math.hpp:2001
Definition StringPool.hpp:23
static const StringID INVALID
Definition StringPool.hpp:27
StringID() noexcept=default
constexpr index_type getIndex() const noexcept
Definition StringPool.hpp:38
static constexpr StringID atExplicitOffset(index_type index) noexcept
Definition StringPool.hpp:29
std::uint32_t index_type
Definition StringPool.hpp:24
static const StringID EMPTY
Definition StringPool.hpp:26
std::size_t operator()(grem::StringID id) const
Definition StringPool.hpp:240