slot_vector.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <algorithm>
  5. #include <bit>
  6. #include <numeric>
  7. #include <type_traits>
  8. #include <utility>
  9. #include <vector>
  10. #include "common/assert.h"
  11. #include "common/common_types.h"
  12. #include "common/polyfill_ranges.h"
  13. namespace Common {
  14. struct SlotId {
  15. static constexpr u32 INVALID_INDEX = std::numeric_limits<u32>::max();
  16. constexpr auto operator<=>(const SlotId&) const noexcept = default;
  17. constexpr explicit operator bool() const noexcept {
  18. return index != INVALID_INDEX;
  19. }
  20. u32 index = INVALID_INDEX;
  21. };
  22. template <class T>
  23. requires std::is_nothrow_move_assignable_v<T> && std::is_nothrow_move_constructible_v<T>
  24. class SlotVector {
  25. public:
  26. class Iterator {
  27. friend SlotVector<T>;
  28. public:
  29. constexpr Iterator() = default;
  30. Iterator& operator++() noexcept {
  31. const u64* const bitset = slot_vector->stored_bitset.data();
  32. const u32 size = static_cast<u32>(slot_vector->stored_bitset.size()) * 64;
  33. if (id.index < size) {
  34. do {
  35. ++id.index;
  36. } while (id.index < size && !IsValid(bitset));
  37. if (id.index == size) {
  38. id.index = SlotId::INVALID_INDEX;
  39. }
  40. }
  41. return *this;
  42. }
  43. Iterator operator++(int) noexcept {
  44. const Iterator copy{*this};
  45. ++*this;
  46. return copy;
  47. }
  48. bool operator==(const Iterator& other) const noexcept {
  49. return id.index == other.id.index;
  50. }
  51. bool operator!=(const Iterator& other) const noexcept {
  52. return id.index != other.id.index;
  53. }
  54. std::pair<SlotId, T*> operator*() const noexcept {
  55. return {id, std::addressof((*slot_vector)[id])};
  56. }
  57. T* operator->() const noexcept {
  58. return std::addressof((*slot_vector)[id]);
  59. }
  60. private:
  61. Iterator(SlotVector<T>* slot_vector_, SlotId id_) noexcept
  62. : slot_vector{slot_vector_}, id{id_} {}
  63. bool IsValid(const u64* bitset) const noexcept {
  64. return ((bitset[id.index / 64] >> (id.index % 64)) & 1) != 0;
  65. }
  66. SlotVector<T>* slot_vector;
  67. SlotId id;
  68. };
  69. ~SlotVector() noexcept {
  70. size_t index = 0;
  71. for (u64 bits : stored_bitset) {
  72. for (size_t bit = 0; bits; ++bit, bits >>= 1) {
  73. if ((bits & 1) != 0) {
  74. values[index + bit].object.~T();
  75. }
  76. }
  77. index += 64;
  78. }
  79. delete[] values;
  80. }
  81. [[nodiscard]] T& operator[](SlotId id) noexcept {
  82. ValidateIndex(id);
  83. return values[id.index].object;
  84. }
  85. [[nodiscard]] const T& operator[](SlotId id) const noexcept {
  86. ValidateIndex(id);
  87. return values[id.index].object;
  88. }
  89. template <typename... Args>
  90. [[nodiscard]] SlotId insert(Args&&... args) noexcept {
  91. const u32 index = FreeValueIndex();
  92. new (&values[index].object) T(std::forward<Args>(args)...);
  93. SetStorageBit(index);
  94. return SlotId{index};
  95. }
  96. void erase(SlotId id) noexcept {
  97. values[id.index].object.~T();
  98. free_list.push_back(id.index);
  99. ResetStorageBit(id.index);
  100. }
  101. [[nodiscard]] Iterator begin() noexcept {
  102. const auto it = std::ranges::find_if(stored_bitset, [](u64 value) { return value != 0; });
  103. if (it == stored_bitset.end()) {
  104. return end();
  105. }
  106. const u32 word_index = static_cast<u32>(std::distance(it, stored_bitset.begin()));
  107. const SlotId first_id{word_index * 64 + static_cast<u32>(std::countr_zero(*it))};
  108. return Iterator(this, first_id);
  109. }
  110. [[nodiscard]] Iterator end() noexcept {
  111. return Iterator(this, SlotId{SlotId::INVALID_INDEX});
  112. }
  113. [[nodiscard]] size_t size() const noexcept {
  114. return values_capacity - free_list.size();
  115. }
  116. private:
  117. struct NonTrivialDummy {
  118. NonTrivialDummy() noexcept {}
  119. };
  120. union Entry {
  121. Entry() noexcept : dummy{} {}
  122. ~Entry() noexcept {}
  123. NonTrivialDummy dummy;
  124. T object;
  125. };
  126. void SetStorageBit(u32 index) noexcept {
  127. stored_bitset[index / 64] |= u64(1) << (index % 64);
  128. }
  129. void ResetStorageBit(u32 index) noexcept {
  130. stored_bitset[index / 64] &= ~(u64(1) << (index % 64));
  131. }
  132. bool ReadStorageBit(u32 index) noexcept {
  133. return ((stored_bitset[index / 64] >> (index % 64)) & 1) != 0;
  134. }
  135. void ValidateIndex(SlotId id) const noexcept {
  136. DEBUG_ASSERT(id);
  137. DEBUG_ASSERT(id.index / 64 < stored_bitset.size());
  138. DEBUG_ASSERT(((stored_bitset[id.index / 64] >> (id.index % 64)) & 1) != 0);
  139. }
  140. [[nodiscard]] u32 FreeValueIndex() noexcept {
  141. if (free_list.empty()) {
  142. Reserve(values_capacity ? (values_capacity << 1) : 1);
  143. }
  144. const u32 free_index = free_list.back();
  145. free_list.pop_back();
  146. return free_index;
  147. }
  148. void Reserve(size_t new_capacity) noexcept {
  149. Entry* const new_values = new Entry[new_capacity];
  150. size_t index = 0;
  151. for (u64 bits : stored_bitset) {
  152. for (size_t bit = 0; bits; ++bit, bits >>= 1) {
  153. const size_t i = index + bit;
  154. if ((bits & 1) == 0) {
  155. continue;
  156. }
  157. T& old_value = values[i].object;
  158. new (&new_values[i].object) T(std::move(old_value));
  159. old_value.~T();
  160. }
  161. index += 64;
  162. }
  163. stored_bitset.resize((new_capacity + 63) / 64);
  164. const size_t old_free_size = free_list.size();
  165. free_list.resize(old_free_size + (new_capacity - values_capacity));
  166. std::iota(free_list.begin() + old_free_size, free_list.end(),
  167. static_cast<u32>(values_capacity));
  168. delete[] values;
  169. values = new_values;
  170. values_capacity = new_capacity;
  171. }
  172. Entry* values = nullptr;
  173. size_t values_capacity = 0;
  174. std::vector<u64> stored_bitset;
  175. std::vector<u32> free_list;
  176. };
  177. } // namespace Common
  178. template <>
  179. struct std::hash<Common::SlotId> {
  180. size_t operator()(const Common::SlotId& id) const noexcept {
  181. return std::hash<u32>{}(id.index);
  182. }
  183. };