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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
#pragma once
///@file
#include <functional>
#include <vector>
#include <algorithm>
namespace nix {
/**
* Provides a map-like data structure but backed by a vector.
* The comparison `Comp` must be a linear order on `K`.
* This is mostly used for mapping symbols to values in the expression tree,
* where the data structure is immutable after having been built and fast linear
* access is important.
*/
template<typename K, typename V, typename Comp = std::less<>>
class LinearMap : private std::vector<std::pair<K, V>>
{
private:
using base = std::vector<std::pair<K, V>>;
auto lower_bound_of(const K & key)
{
return std::lower_bound(
base::begin(),
base::end(),
key,
[](const std::pair<K, V> & pair, const K & k) { return Comp{}(pair.first, k); }
);
}
auto lower_bound_of(const K & key) const
{
return std::lower_bound(
base::begin(),
base::end(),
key,
[](const std::pair<K, V> & pair, const K & k) { return Comp{}(pair.first, k); }
);
}
public:
using typename base::const_iterator, typename base::const_reverse_iterator, typename base::value_type;
LinearMap() = default;
LinearMap(size_t expectedSize)
{
reserve(expectedSize);
}
using base::size, base::reserve, base::clear, base::cbegin, base::cend, base::crbegin, base::crend;
/* Insert an element at the correct position, shifting later elements back by
* one place. Returns `true` if a previous element with that key was
* overwritten
*/
std::pair<const_iterator, bool> insert_or_assign(const K key, V value)
{
/* Fast path: We are inserting elements in order and thus the insertion is bigger than the
* current last element */
if (base::empty() || Comp{}(base::back().first, key)) {
base::emplace_back(std::move(key), std::move(value));
return std::pair(base::end() - 1, false);
}
auto i = lower_bound_of(key);
if (i != end() && i->first == key) {
i->second = std::move(value);
return std::pair(i, true);
} else {
auto i2 = base::insert(i, std::pair(key, std::move(value)));
return std::pair(i2, false);
}
}
/* Insert an arbitrary amount of values into the map with a callable function.
* This is a workaround to C++'s sad state of iterators and generators.
* The passed function gets access to the internal backing vector, and may append any number of
* elements to it. It is up to the passed function to ensure only elements are appended, and
* that the set of added elements is ordered and free of duplicates. After the insertion
* function terminates, the added items are merged into the map in O(n). Newly inserted elements
* override existing elements in the map.
*/
template<typename Fn>
void unsafe_insert_bulk(Fn func)
{
auto oldSize = this->size();
func(static_cast<base &>(*this));
std::inplace_merge(
base::begin(),
base::begin() + oldSize,
base::end(),
[](const auto & a, const auto & b) { return Comp{}(a.first, b.first); }
);
/* Deduplicate, prefer newly inserted */
auto it = base::begin(), jt = it;
while (jt != base::end()) {
*it = *jt++;
while (jt != base::end() && it->first == jt->first) {
*it = *jt++;
}
it++;
}
base::erase(it, base::end());
}
/* The inserted range must be sorted and free of duplicates */
template<typename It>
void insert_range_sorted(It it, It end)
{
unsafe_insert_bulk([&](auto & map) {
if constexpr (requires { end - it; }) {
map.reserve(map.size() + (end - it));
}
for (; it != end; ++it) {
map.emplace_back(it->first, it->second);
}
});
};
/* The inserted range must be free of duplicates */
template<typename It>
void insert_range(It it, It end)
{
unsafe_insert_bulk([&](auto & map) {
auto oldSize = map.size();
if constexpr (requires { end - it; }) {
map.reserve(map.size() + (end - it));
}
for (; it != end; ++it) {
map.emplace_back(it->first, it->second);
}
std::sort(map.begin() + oldSize, map.end(), Comp{});
});
};
const_iterator find(const K & key) const
{
if (auto i = lower_bound_of(key); i != base::end() && i->first == key) {
return i;
}
return base::end();
}
const_iterator begin() const
{
return cbegin();
}
const_iterator end() const
{
return cend();
}
const_reverse_iterator rbegin() const
{
return crbegin();
}
const_reverse_iterator rend() const
{
return crend();
}
};
} // namespace nix
|