struct atomicunorderedmap { ~atomicunorderedmap() { } _forceinline bool contains(int…
- User
struct atomic_unordered_map { ~atomic_unordered_map() { } __forceinline bool contains(int val) { if (table.index(val, false) != NULL) if (table.index(val, false).load(std::memory_order_relaxed) != true) return !table.index(val, false).load(std::memory_order_relaxed); return false; } __forceinline void emplace(int val) { if (table.index(val, false) != NULL) if (table.index(val, false).load(std::memory_order_relaxed) != true) table.index(val, true).store(new std::atomic_bool(false)); } __forceinline void erase(int val) { if (table.index(val, false) != NULL) { auto n = table.index(val, false); bool&& e = n.load(std::memory_order_relaxed); if (n.compare_exchange_strong(e, true, std::memory_order_acquire)) n = NULL; { } } } _declspec(noinline) void reserve(uint size) { table.reserve(size); } private: struct jump_table { std::atomic_bool index(int index, bool op) noexcept { if (op && (*data)[index] == NULL) { std::atomic_bool a = false; (*data)[index].store(a); } return &(*data)[index]; } declspec(noinline) void reserve(uint size) { size.fetch_add(size, std::memory_order_relaxed); data = std::make_uniquestd::atomic_bool*(new std::atomic_bool[size]); } private: std::unique_ptrstd::atomic_bool* data = nullptr; std::atomic_int size = 0; }; jump_table table; }; would this code waste memory for the indexes between 2 and 84 after I index 2 and 84?
- Assistant
No, this code does not waste memory for the indexes between 2 and 84 after indexing 2 and 84.
In the
jump_tablestruct, thereservefunction increases the size of thedataarray to accommodate the specified size. However, it does not allocate memory for each index individually. Instead, it increments thesizevariable and assigns a newstd::atomic_bool*array of the specified size usingstd::make_unique.Therefore, if you only index 2 and 84, the memory allocated will only be for those two indexes, and the memory for the other indexes in between will not be wasted.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.