forked from mapsme/omim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked_map.hpp
91 lines (74 loc) · 1.63 KB
/
linked_map.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#pragma once
#include "base/assert.hpp"
#include <list>
#include <unordered_map>
namespace base
{
template <typename Key, typename Value, template <typename...> typename Map = std::unordered_map>
class LinkedMap
{
public:
using KeyType = Key;
using ValueType = Value;
using ListType = std::list<std::pair<KeyType, ValueType>>;
using MapType = Map<Key, typename ListType::iterator>;
template <typename T>
bool Emplace(KeyType const & key, T && value)
{
if (m_map.find(key) != m_map.cend())
return false;
m_list.emplace_back(key, std::forward<T>(value));
m_map.emplace(key, std::prev(m_list.end()));
return true;
}
void PopFront()
{
CHECK(!m_map.empty(), ());
m_map.erase(m_list.front().first);
m_list.pop_front();
}
bool Erase(KeyType const & key)
{
auto const it = m_map.find(key);
if (it == m_map.cend())
return false;
m_list.erase(it->second);
m_map.erase(it);
return true;
}
bool Contains(KeyType const & key) const
{
return m_map.find(key) != m_map.cend();
}
ValueType const & Get(KeyType const & key) const
{
auto const it = m_map.find(key);
CHECK(it != m_map.cend(), ());
return it->second->second;
}
ValueType & Front()
{
return m_list.front().second;
}
ValueType const & Front() const
{
return m_list.front().second;
}
void Swap(LinkedMap & linkedMap)
{
m_map.swap(linkedMap.m_map);
m_list.swap(linkedMap.m_list);
}
size_t Size() const
{
return m_list.size();
}
bool IsEmpty() const
{
return m_list.empty();
}
private:
ListType m_list;
MapType m_map;
};
} // namespace base