#pragma once #include #include #include #include #include #include #include template class StructTimestampMap { public: // Adding a key can move entries; use the reference before the next insertion. unsigned long& get(const void* field) { const auto end = times.begin() + count; const auto entry = std::lower_bound(times.begin(), end, field, [](const auto& item, const void* ptr) { return reinterpret_cast(item.first) < reinterpret_cast(ptr); }); if (entry != end && entry->first == field) { return entry->second; } assert(count < times.size()); std::move_backward(entry, end, end + 1); *entry = {field, 0}; ++count; return entry->second; } void reset(std::initializer_list fields) { for (const void* field : fields) { get(field) = 0; } } void resetAll() { for (std::size_t index = 0; index < count; ++index) { times[index].second = 0; } } private: std::array, Capacity> times{}; std::size_t count = 0; };