-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflat_impl.hpp
More file actions
65 lines (49 loc) · 1.25 KB
/
flat_impl.hpp
File metadata and controls
65 lines (49 loc) · 1.25 KB
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
#pragma once
#include <any>
#include <functional>
#include <memory>
#include <vector>
namespace context {
namespace detail {
template <class, class>
class flat_impl;
template <class Key, class KeyEqual = std::equal_to<Key>>
class flat_impl {
struct data {
Key key;
std::any value;
std::int64_t index;
};
using shared_list = std::shared_ptr<std::vector<data>>;
std::int64_t index_;
shared_list list_;
public:
static flat_impl make_root() {
flat_impl i;
i.index_ = -1;
i.list_ = std::make_shared<std::vector<data>>();
return i;
}
static flat_impl make(Key key, std::any value, const flat_impl& parent) {
flat_impl impl;
data d{std::move(key), std::move(value), parent.index_};
parent.list_->emplace_back(std::move(d));
impl.list_ = parent.list_;
impl.index_ = parent.list_->size() - 1;
assert(impl.index_ > parent.index_);
return impl;
}
std::any lookup(const Key& key) const {
auto i = index_;
while (i >= 0) {
const auto& d = (*list_)[i];
if (KeyEqual{}(d.key, key)) {
return d.value;
}
i = d.index;
}
return std::any{};
}
};
} // namespace detail
} // namespace context