-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstd_unordered_map_index.h
100 lines (87 loc) · 2.68 KB
/
std_unordered_map_index.h
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
92
93
94
95
96
97
98
99
100
//
// Created by Shujian Qian on 2023-11-29.
//
#ifndef EPIC__STD_UNORDERED_MAP_INDEX_H
#define EPIC__STD_UNORDERED_MAP_INDEX_H
#include <queue>
#include <limits>
#include <unordered_map>
#include <base_index.h>
namespace epic {
/**
* This is not a thread-safe implementation.
*
* @tparam KeyType
* @tparam ValueType
*/
template<typename KeyType, typename ValueType, KeyType InvalidKey = std::numeric_limits<KeyType>::max(),
ValueType InvalidValue = std::numeric_limits<ValueType>::max()>
class StdUnorderedMapIndex : public BaseIndex<KeyType, ValueType, InvalidKey, InvalidValue>
{
std::queue<ValueType> free_list;
std::unordered_map<KeyType, ValueType> map;
public:
using Iterator = typename BaseIndex<KeyType, ValueType, InvalidKey, InvalidValue>::Iterator;
constexpr static KeyType INVALID_KEY = InvalidKey;
constexpr static ValueType INVALID_VALUE = InvalidValue;
explicit StdUnorderedMapIndex(ValueType max_row_id)
: map(max_row_id)
{
for (ValueType i = 0; i < max_row_id; i++)
{
free_list.push(i);
}
}
~StdUnorderedMapIndex() override = default;
inline ValueType searchOrInsert(KeyType key) override
{
ValueType value = free_list.front();
auto res = map.emplace(std::make_pair(key, value));
if (res.second)
{
free_list.pop();
}
return res.first->second;
}
inline ValueType searchOrInsert(KeyType key, bool &inserted) override
{
ValueType value = free_list.front();
auto res = map.emplace(std::make_pair(key, value));
if (res.second)
{
free_list.pop();
inserted = true;
}
else
{
inserted = false;
}
return res.first->second;
}
inline ValueType search(KeyType key) override
{
auto it = map.find(key);
if (it == map.end())
{
return BaseIndex<KeyType, ValueType>::INVALID_VALUE;
}
return it->second;
}
inline std::unique_ptr<Iterator> searchRange(KeyType start, KeyType end) override
{
throw std::runtime_error("Hashtable index does not support range search.");
return nullptr;
}
inline std::unique_ptr<Iterator> searchRangeReverse(KeyType start, KeyType end) override
{
throw std::runtime_error("Hashtable index does not support range search.");
return nullptr;
}
inline std::unique_ptr<Iterator> searchAfter(KeyType start) override
{
throw std::runtime_error("Hashtable index does not support range search.");
return nullptr;
}
};
} // namespace epic
#endif // EPIC__STD_UNORDERED_MAP_INDEX_H