|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "testing/config.hpp" |
| 4 | + |
| 5 | +#include "trade_v1/trade.hpp" |
| 6 | + |
| 7 | +#include "dumpster_v1/primes.hpp" |
| 8 | + |
| 9 | +#include "polyfill_v1/memory.hpp" |
| 10 | +#include <functional> |
| 11 | +#include <optional> |
| 12 | +#include <utility> |
| 13 | + |
| 14 | +namespace testing { |
| 15 | + |
| 16 | +/// A transactional hash map for testing purposes. |
| 17 | +template <class Key, |
| 18 | + class Mapped, |
| 19 | + class Hash = std::hash<Key>, |
| 20 | + class Equal = std::equal_to<Key>> |
| 21 | +class hash_map_tm; |
| 22 | + |
| 23 | +class hash_map_tm_private { |
| 24 | + template <class, class, class, class> friend class hash_map_tm; |
| 25 | + |
| 26 | + // This hack is a workaround for not having std::shared_ptr<T[]> support |
| 27 | + // in AppleClang. |
| 28 | + template <class T> struct array_hack { |
| 29 | + void operator delete(void *self) { delete[] reinterpret_cast<T *>(self); } |
| 30 | + T &at(size_t i) { return reinterpret_cast<T *>(this)[i]; } |
| 31 | + }; |
| 32 | +}; |
| 33 | + |
| 34 | +template <class Key, class Mapped, class Hash, class Equal> |
| 35 | +class hash_map_tm : hash_map_tm_private { |
| 36 | + template <class T> using ptr_t = std::shared_ptr<T>; |
| 37 | + |
| 38 | + struct node_t; |
| 39 | + |
| 40 | + using link = trade::atom<std::shared_ptr<node_t>>; |
| 41 | + |
| 42 | + trade::atom<size_t> m_item_count; |
| 43 | + trade::atom<size_t> m_buckets_count; |
| 44 | + trade::atom<ptr_t<array_hack<link>>> m_buckets; |
| 45 | + |
| 46 | +public: |
| 47 | + using size_type = size_t; |
| 48 | + |
| 49 | + using key_type = Key; |
| 50 | + using mapped_type = Mapped; |
| 51 | + |
| 52 | + hash_map_tm(); |
| 53 | + |
| 54 | + hash_map_tm(const hash_map_tm &) = delete; |
| 55 | + hash_map_tm &operator=(const hash_map_tm &) = delete; |
| 56 | + |
| 57 | + size_t size() const; |
| 58 | + |
| 59 | + bool empty() const; |
| 60 | + |
| 61 | + void clear(); |
| 62 | + |
| 63 | + void swap(hash_map_tm &that); |
| 64 | + |
| 65 | + template <class ForwardableMapped, class Config = trade::stack_t<1024>> |
| 66 | + bool add_or_set(const Key &key, |
| 67 | + ForwardableMapped &&mapped, |
| 68 | + Config config = trade::stack<1024>); |
| 69 | + |
| 70 | + std::optional<Mapped> try_get(const Key &key) const; |
| 71 | + |
| 72 | + bool remove(const Key &key); |
| 73 | + |
| 74 | +#ifndef NDEBUG |
| 75 | + static std::atomic<size_t> s_live_nodes; // Only for testing purposes |
| 76 | +#endif |
| 77 | +}; |
| 78 | + |
| 79 | +// ----------------------------------------------------------------------------- |
| 80 | + |
| 81 | +template <class Key, class Mapped, class Hash, class Equal> |
| 82 | +struct hash_map_tm<Key, Mapped, Hash, Equal>::node_t { |
| 83 | +#ifndef NDEBUG |
| 84 | + ~node_t() { --s_live_nodes; } |
| 85 | +#endif |
| 86 | + template <class ForwardableKey, class ForwardableMapped> |
| 87 | + node_t(ForwardableKey &&key, ForwardableMapped &&value) |
| 88 | + : m_next(nullptr), m_key(std::forward<ForwardableKey>(key)), |
| 89 | + m_mapped(std::forward<ForwardableMapped>(value)) { |
| 90 | +#ifndef NDEBUG |
| 91 | + ++s_live_nodes; |
| 92 | +#endif |
| 93 | + } |
| 94 | + link m_next; |
| 95 | + const Key m_key; |
| 96 | + trade::atom<Mapped> m_mapped; |
| 97 | +}; |
| 98 | + |
| 99 | +// |
| 100 | + |
| 101 | +template <class Key, class Mapped, class Hash, class Equal> |
| 102 | +hash_map_tm<Key, Mapped, Hash, Equal>::hash_map_tm() |
| 103 | + : m_item_count(0), m_buckets_count(0), m_buckets(nullptr) {} |
| 104 | + |
| 105 | +template <class Key, class Mapped, class Hash, class Equal> |
| 106 | +size_t hash_map_tm<Key, Mapped, Hash, Equal>::size() const { |
| 107 | + return trade::atomically(trade::assume_readonly, |
| 108 | + [&]() { return m_item_count.load(); }); |
| 109 | +} |
| 110 | + |
| 111 | +template <class Key, class Mapped, class Hash, class Equal> |
| 112 | +bool hash_map_tm<Key, Mapped, Hash, Equal>::empty() const { |
| 113 | + return trade::atomically(trade::assume_readonly, |
| 114 | + [&]() { return m_item_count == 0; }); |
| 115 | +} |
| 116 | + |
| 117 | +template <class Key, class Mapped, class Hash, class Equal> |
| 118 | +void hash_map_tm<Key, Mapped, Hash, Equal>::clear() { |
| 119 | + trade::atomically([&]() { |
| 120 | + m_item_count = 0; |
| 121 | + m_buckets_count = 0; |
| 122 | + m_buckets = nullptr; |
| 123 | + }); |
| 124 | +} |
| 125 | + |
| 126 | +template <class Key, class Mapped, class Hash, class Equal> |
| 127 | +void hash_map_tm<Key, Mapped, Hash, Equal>::swap(hash_map_tm &that) { |
| 128 | + trade::atomically([&]() { |
| 129 | + std::swap(m_item_count.ref(), that.m_item_count.ref()); |
| 130 | + std::swap(m_buckets_count.ref(), that.m_buckets_count.ref()); |
| 131 | + std::swap(m_buckets.ref(), that.m_buckets.ref()); |
| 132 | + }); |
| 133 | +} |
| 134 | + |
| 135 | +template <class Key, class Mapped, class Hash, class Equal> |
| 136 | +template <class ForwardableMapped, class Config> |
| 137 | +bool hash_map_tm<Key, Mapped, Hash, Equal>::add_or_set( |
| 138 | + const Key &key, ForwardableMapped &&mapped, Config config) { |
| 139 | + auto key_hash = Hash()(key); |
| 140 | + |
| 141 | + return trade::atomically(config, [&]() { |
| 142 | + auto item_count = m_item_count.load(); |
| 143 | + auto buckets_count = m_buckets_count.load(); |
| 144 | + auto buckets = m_buckets.load(); |
| 145 | + |
| 146 | + if (buckets_count <= item_count) { |
| 147 | + auto old_buckets = std::move(buckets); |
| 148 | + auto old_buckets_count = buckets_count; |
| 149 | + |
| 150 | + m_buckets_count = buckets_count = |
| 151 | + dumpster::prime_less_than_next_pow_2_or_1(old_buckets_count * 2 + 1); |
| 152 | + m_buckets = buckets = ptr_t<array_hack<link>>( |
| 153 | + reinterpret_cast<array_hack<link> *>(new link[buckets_count])); |
| 154 | + |
| 155 | + for (size_t i = 0; i < old_buckets_count; ++i) { |
| 156 | + auto work = old_buckets->at(i).load(); |
| 157 | + while (work) { |
| 158 | + auto &ref_next = work->m_next.ref(); |
| 159 | + auto &ref_bucket = |
| 160 | + buckets->at(Hash()(work->m_key) % buckets_count).ref(); |
| 161 | + auto next = std::move(ref_next); |
| 162 | + ref_next = std::move(ref_bucket); |
| 163 | + ref_bucket = std::move(work); |
| 164 | + work = std::move(next); |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + auto prev = &buckets->at(key_hash % buckets_count); |
| 170 | + while (true) { |
| 171 | + if (auto node = prev->load()) { |
| 172 | + if (Equal()(node->m_key, key)) { |
| 173 | + node->m_mapped = std::forward<ForwardableMapped>(mapped); |
| 174 | + return false; |
| 175 | + } else { |
| 176 | + prev = &node->m_next; |
| 177 | + } |
| 178 | + } else { |
| 179 | + prev->ref().reset( |
| 180 | + new node_t(key, std::forward<ForwardableMapped>(mapped))); |
| 181 | + m_item_count = item_count + 1; |
| 182 | + return true; |
| 183 | + } |
| 184 | + } |
| 185 | + }); |
| 186 | +} |
| 187 | + |
| 188 | +template <class Key, class Mapped, class Hash, class Equal> |
| 189 | +std::optional<Mapped> |
| 190 | +hash_map_tm<Key, Mapped, Hash, Equal>::try_get(const Key &key) const { |
| 191 | + auto key_hash = Hash()(key); |
| 192 | + return trade::atomically( |
| 193 | + trade::assume_readonly, [&]() -> std::optional<Mapped> { |
| 194 | + if (auto buckets_count = m_buckets_count.load()) |
| 195 | + for (auto node = |
| 196 | + m_buckets.load()->at(key_hash % buckets_count).load(); |
| 197 | + node; |
| 198 | + node = node->m_next) |
| 199 | + if (Equal()(node->m_key, key)) |
| 200 | + return node->m_mapped.load(); |
| 201 | + return std::nullopt; |
| 202 | + }); |
| 203 | +} |
| 204 | + |
| 205 | +template <class Key, class Mapped, class Hash, class Equal> |
| 206 | +bool hash_map_tm<Key, Mapped, Hash, Equal>::remove(const Key &key) { |
| 207 | + auto key_hash = Hash()(key); |
| 208 | + return trade::atomically([&]() { |
| 209 | + if (auto buckets_count = m_buckets_count.load()) { |
| 210 | + auto prev = &m_buckets.load()->at(key_hash % buckets_count); |
| 211 | + while (true) { |
| 212 | + auto node = prev->load(); |
| 213 | + if (!node) |
| 214 | + break; |
| 215 | + if (Equal()(node->m_key, key)) { |
| 216 | + *prev = node->m_next; |
| 217 | + return true; |
| 218 | + } |
| 219 | + prev = &node->m_next; |
| 220 | + } |
| 221 | + } |
| 222 | + return false; |
| 223 | + }); |
| 224 | +} |
| 225 | + |
| 226 | +#ifndef NDEBUG |
| 227 | +template <class Key, class Mapped, class Hash, class Equal> |
| 228 | +std::atomic<size_t> hash_map_tm<Key, Mapped, Hash, Equal>::s_live_nodes = 0; |
| 229 | +#endif |
| 230 | + |
| 231 | +} // namespace testing |
0 commit comments