MCPcopy Create free account
hub / github.com/Apache553/SubtitleFontHelper / SimpleLRU

Class SimpleLRU

SubtitleFontAutoLoaderDaemon/Prefetch.cpp:13–124  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

11
12template <typename T>
13class SimpleLRU
14{
15private:
16 struct Node
17 {
18 Node* m_prev;
19 Node* m_next;
20
21 T m_data;
22
23 // detach a node from chain, return next node
24 static Node& Detach(Node& node)
25 {
26 if (node.m_prev)
27 {
28 node.m_prev->m_next = node.m_next;
29 }
30 node.m_next->m_prev = node.m_prev;
31 return *node.m_next;
32 }
33
34 // attach node before 'pos',return the attached node
35 static Node& Attach(Node& node, Node& pos)
36 {
37 node.m_next = &pos;
38 node.m_prev = pos.m_prev;
39 pos.m_prev = &node;
40 if (node.m_prev)
41 node.m_prev->m_next = &node;
42 return node;
43 }
44 };
45
46 Node m_end{nullptr, nullptr};
47 std::vector<Node> m_nodes;
48 Node* m_head = &m_end;
49
50 std::unordered_map<T, Node*> m_hashmap;
51
52 size_t m_capacity;
53
54 std::mutex m_lock;
55
56 void AdjustToHead(Node& node, bool inList)
57 {
58 if (inList)
59 {
60 if (&node == m_head)
61 return;
62 Node::Detach(node);
63 }
64 Node::Attach(node, *m_head);
65 m_head = &node;
66 }
67
68public:
69 SimpleLRU(size_t initialSize)
70 : m_capacity(initialSize)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected