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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
/*
* Copyright (c) 2015-2017, Intel Corporation
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Intel Corporation nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/** \file
* \brief Convenience template functions for containers.
*/
#ifndef UTIL_CONTAINER_H
#define UTIL_CONTAINER_H
#include "ue2common.h"
#include <algorithm>
#include <cassert>
#include <cstring>
#include <set>
#include <type_traits>
#include <utility>
#include <vector>
namespace ue2 {
// Existence check for associative containers.
template<typename C>
bool contains(const C &container, const typename C::key_type &key) {
return container.find(key) != container.end();
}
template<typename C, typename It>
bool contains_any_of(const C &container, const std::pair<It, It> &range) {
return std::find_first_of(range.first, range.second, container.begin(),
container.end()) != range.second;
}
template<typename C, typename It>
void insert(C *container, const std::pair<It, It> &range) {
container->insert(range.first, range.second);
}
template<typename C, typename It>
void insert(C *container, typename C::iterator pos,
const std::pair<It, It> &range) {
container->insert(pos, range.first, range.second);
}
template<typename C, typename D>
void insert(C *container, const D &donor) {
container->insert(donor.begin(), donor.end());
}
template<typename C, typename D>
void insert(C *container, typename C::iterator pos, const D &donor) {
container->insert(pos, donor.begin(), donor.end());
}
/**
* \brief Constructs a vector from a range bounded by the given pair of
* iterators.
*/
template <typename It>
auto make_vector_from(const std::pair<It, It> &range)
-> std::vector<decltype(*range.first)> {
using T = decltype(*range.first);
return std::vector<T>(range.first, range.second);
}
/** \brief Sort a sequence container and remove duplicates. */
template <typename C, typename Compare = std::less<typename C::value_type>>
void sort_and_unique(C &container, Compare comp = Compare()) {
std::sort(std::begin(container), std::end(container), comp);
container.erase(std::unique(std::begin(container), std::end(container)),
std::end(container));
}
/** \brief Returns a set containing the keys in the given associative
* container. */
template <typename C>
std::set<typename C::key_type> assoc_keys(const C &container) {
std::set<typename C::key_type> keys;
for (const auto &elem : container) {
keys.insert(elem.first);
}
return keys;
}
/**
* \brief Return the length in bytes of the given vector of (POD) objects.
*/
template <typename T, typename Alloc>
typename std::vector<T, Alloc>::size_type
byte_length(const std::vector<T, Alloc> &vec) {
static_assert(std::is_pod<T>::value, "should be pod");
return vec.size() * sizeof(T);
}
/**
* \brief Copy the given vector of POD objects to the given location in memory.
* It is safe to give this function an empty vector.
*/
template<typename T, typename Alloc>
void *copy_bytes(void *dest, const std::vector<T, Alloc> &vec) {
static_assert(std::is_pod<T>::value, "should be pod");
assert(dest);
// Since we're generally using this function to write into the bytecode,
// dest should be appropriately aligned for T.
assert(ISALIGNED_N(dest, alignof(T)));
if (vec.empty()) {
return dest; // Protect memcpy against null pointers.
}
assert(vec.data() != nullptr);
return std::memcpy(dest, vec.data(), byte_length(vec));
}
template<typename OrderedContainer1, typename OrderedContainer2>
bool is_subset_of(const OrderedContainer1 &small, const OrderedContainer2 &big) {
static_assert(std::is_same<typename OrderedContainer1::value_type,
typename OrderedContainer2::value_type>::value,
"Both containers should have the same value_type");
auto sit = small.begin();
auto bit = big.begin();
if (small.size() > big.size()) {
return false;
}
while (sit != small.end()) {
if (bit == big.end()) {
return false;
}
if (*sit == *bit) {
++sit;
++bit;
continue;
}
if (*bit < *sit) {
++bit;
continue;
}
return false;
}
return true;
}
template<typename OrderedContainer1, typename OrderedContainer2>
bool has_intersection(const OrderedContainer1 &a, const OrderedContainer2 &b) {
static_assert(std::is_same<typename OrderedContainer1::value_type,
typename OrderedContainer2::value_type>::value,
"Both containers should have the same value_type");
auto ait = a.begin();
auto bit = b.begin();
while (ait != a.end() && bit != b.end()) {
if (*ait == *bit) {
return true;
}
if (*ait < *bit) {
++ait;
} else {
++bit;
}
}
return false;
}
/**
* \brief Erase the elements (by value) in the donor container from the given
* container.
*/
template<typename C, typename D>
void erase_all(C *container, const D &donor) {
for (const auto &elem : donor) {
container->erase(elem);
}
}
template<typename C, typename Pred>
bool any_of_in(const C &c, Pred p) {
return std::any_of(c.begin(), c.end(), std::move(p));
}
template<typename C, typename Pred>
bool all_of_in(const C &c, Pred p) {
return std::all_of(c.begin(), c.end(), std::move(p));
}
} // namespace ue2
#ifdef DUMP_SUPPORT
#include <sstream>
#include <string>
namespace ue2 {
/**
* \brief Dump a container of stream-printable objects into a comma-separated
* list in a string.
*/
template<class C>
std::string as_string_list(const C &c) {
std::ostringstream oss;
for (auto it = c.begin(); it != c.end(); ++it) {
if (it != c.begin()) {
oss << ", ";
}
oss << *it;
}
return oss.str();
}
} // namespace ue2
#endif // DUMP_SUPPORT
#endif // UTIL_CONTAINER_H
|