TachyonUI 1.1.0
High-performance UI for building tools.
Loading...
Searching...
No Matches
gui_recent_id_map.h
Go to the documentation of this file.
1#pragma once
2
3#include <gui_core/gui_id.h>
4
5#include <cstdint>
6#include <iterator>
7#include <unordered_map>
8#include <utility>
9
10namespace tachyon
11{
14
23 template<typename V>
25 {
26 public:
27 static constexpr uint32_t kKeepFrames = 300;
28 static constexpr uint32_t kSweepPeriod = 64;
29
32 {
33 Entry& entry = m_entries[id];
34 entry.seenFrame = m_frame;
35 return entry.value;
36 }
37
39 void Touch(ID id)
40 {
41 if (auto it = m_entries.find(id); it != m_entries.end())
42 it->second.seenFrame = m_frame;
43 }
44
46 V* Find(ID id)
47 {
48 auto it = m_entries.find(id);
49 return it != m_entries.end() ? &it->second.value : nullptr;
50 }
51 const V* Find(ID id) const { return const_cast<RecentIDMap*>(this)->Find(id); }
52
53 void Erase(ID id) { m_entries.erase(id); }
54 void Clear() { m_entries.clear(); }
55 size_t Size() const { return m_entries.size(); }
56
58 template<typename Visit>
59 void ForEach(Visit&& visit)
60 {
61 for (auto& [id, entry] : m_entries)
62 visit(id, entry.value);
63 }
64
68 template<typename MayDrop>
69 void EndFrame(MayDrop&& mayDrop)
70 {
71 if (++m_frame % kSweepPeriod != 0)
72 return;
73 for (auto it = m_entries.begin(); it != m_entries.end(); )
74 {
75 if (m_frame - it->second.seenFrame > kKeepFrames && mayDrop(it->first, it->second.value))
76 it = m_entries.erase(it);
77 else
78 ++it;
79 }
80 }
81 void EndFrame() { EndFrame([](ID, V&) { return true; }); }
82
83 private:
84 struct Entry
85 {
86 V value{};
87 uint64_t seenFrame = 0;
88 };
89 std::unordered_map<ID, Entry> m_entries;
90 uint64_t m_frame = 0;
91 };
92
94} // namespace tachyon
Per-ID state kept between frames for the elements still built: an entry not written for kKeepFrames f...
Definition gui_recent_id_map.h:25
static constexpr uint32_t kKeepFrames
Definition gui_recent_id_map.h:27
void ForEach(Visit &&visit)
Calls visit(id, value) for each entry, in no particular order.
Definition gui_recent_id_map.h:59
V * Find(ID id)
The value for id, or null. Reading doesn't keep it.
Definition gui_recent_id_map.h:46
const V * Find(ID id) const
Definition gui_recent_id_map.h:51
void Touch(ID id)
Keeps id's entry, when there is one, for kKeepFrames more frames.
Definition gui_recent_id_map.h:39
static constexpr uint32_t kSweepPeriod
frames between sweeps
Definition gui_recent_id_map.h:28
void EndFrame()
Definition gui_recent_id_map.h:81
void Clear()
Definition gui_recent_id_map.h:54
size_t Size() const
Definition gui_recent_id_map.h:55
void EndFrame(MayDrop &&mayDrop)
Ends a frame.
Definition gui_recent_id_map.h:69
void Erase(ID id)
Definition gui_recent_id_map.h:53
V & operator[](ID id)
The value for id, made when absent; kept for kKeepFrames more frames.
Definition gui_recent_id_map.h:31
Definition gui_logging.h:24
A 64-bit element ID whose bit layout depends on its IDMode.
Definition gui_id.h:23