7 #ifndef RETDEC_UTILS_CONTAINER_H
8 #define RETDEC_UTILS_CONTAINER_H
20 #include <unordered_set>
43 template<
class Container,
typename Item>
44 bool hasItem(
const Container &container,
const Item &item) {
45 return container.find(item) != container.end();
51 template<
typename Item>
52 bool hasItem(
const std::list<Item> &container,
const Item &item) {
54 return find(container.begin(), container.end(), item) != container.end();
60 template<
typename Item>
61 bool hasItem(
const std::vector<Item> &container,
const Item &item) {
63 return find(container.begin(), container.end(), item) != container.end();
74 template<
typename Item>
75 const Item &
getNthItem(
const std::vector<Item> &container, std::size_t n) {
76 assert(1 <= n && n <= container.size() &&
"n is out of bounds");
78 return container[n - 1];
89 template<
typename Item>
90 const Item &
getNthItem(
const std::list<Item> &container, std::size_t n) {
91 assert(1 <= n && n <= container.size() &&
"n is out of bounds");
93 auto itemIt = container.begin();
94 std::advance(itemIt, n - 1);
111 template<
class Container,
typename Item>
113 Item defaultValue = Item()) {
114 auto i = container.find(item);
115 return i != container.end() ? *i : defaultValue;
121 template<
typename Item>
123 const Item &item, Item defaultValue = Item()) {
125 auto i = std::find(container.begin(), container.end(), item);
126 return i != container.end() ? *i : defaultValue;
132 template<
typename Item>
134 const Item &item, Item defaultValue = Item()) {
136 auto i = std::find(container.begin(), container.end(), item);
137 return i != container.end() ? *i : defaultValue;
145 template<
typename Item>
149 v.erase(std::remove(v.begin(), v.end(), item), v.end());
157 template<
class Container>
165 template<
typename Item>
176 template<
typename Item>
200 template<
typename OutputContainer,
typename InputContainer,
typename Predicate>
201 OutputContainer
filterTo(
const InputContainer &input,
const Predicate &predicate) {
203 decltype(begin) end(input.end());
222 template<
typename Container,
typename Predicate>
223 Container
filter(
const Container &input,
const Predicate &predicate) {
224 return filterTo<Container>(input, predicate);
238 void addToSet(
const std::set<T> &from, std::set<T> &to) {
239 to.insert(from.begin(), from.end());
251 std::set<T>
setUnion(
const std::set<T> &s1,
const std::set<T> &s2) {
253 std::set_union(s1.begin(), s1.end(), s2.begin(), s2.end(),
254 std::inserter(result, result.end()));
269 std::set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(),
270 std::inserter(result, result.end()));
285 std::set_difference(s1.begin(), s1.end(), s2.begin(), s2.end(),
286 std::inserter(result, result.end()));
347 template<
typename Map>
349 std::set<typename Map::key_type> keys;
351 keys.insert(p.first);
362 template<
typename Map>
364 std::set<typename Map::mapped_type> keys;
366 keys.insert(p.second);
378 template<
typename Map>
379 bool mapHasKey(
const Map &m,
const typename Map::key_type &k) {
380 return m.find(k) != m.end();
390 template<
typename Map>
391 bool mapHasValue(
const Map &m,
const typename Map::mapped_type &v) {
407 template<
typename Map>
410 const typename Map::key_type &key,
411 typename Map::mapped_type defaultValue =
typename Map::mapped_type()) {
412 auto i = m.find(key);
413 return i != m.end() ? i->second : defaultValue;
422 template<
typename Map>
424 auto max = std::max_element(m.begin(), m.end(),
425 [] (
const auto &p1,
const auto &p2) { return p1.second < p2.second; });
426 return max != m.end() ? max->second :
typename Map::mapped_type();
444 template<
typename Map>
446 const typename Map::key_type &key,
447 const typename Map::mapped_type &value,
449 auto i = m.find(key);
454 return m.emplace(key, value).first->second;
469 template<
typename K,
typename V>
471 std::map<V, K> result;
472 for (
const auto &p : m) {
473 result.emplace(p.second, p.first);
491 template <
class Elem>
508 std::pair<Elem, bool>
insert(
const Elem& val) {
509 auto p =
_data.insert(val);
510 return {*p.first, p.second};
513 bool has(
const Elem& val)
const {
546 template <
typename T>
549 return static_cast<std::size_t
>(t);
An adapter of an iterator range in which some elements of the range are skipped.
Definition: filter_iterator.h:40
Definition: container.h:492
std::pair< Elem, bool > insert(const Elem &val)
Definition: container.h:508
void clear()
Definition: container.h:504
bool has(const Elem &val) const
Definition: container.h:513
NonIterableSet()
Definition: container.h:494
std::set< Elem > _data
Definition: container.h:522
NonIterableSet(std::initializer_list< Elem > il)
Definition: container.h:498
bool hasNot(const Elem &val) const
Definition: container.h:517
An adapter of an iterator range in which some elements of the range are skipped.
const Item & getNthItem(const std::vector< Item > &container, std::size_t n)
Returns the n-th item in container.
Definition: container.h:75
std::set< typename Map::mapped_type > getValuesFromMap(const Map &m)
Returns all values in the given map m.
Definition: container.h:363
bool shareSomeItem(const std::set< T > &s1, const std::set< T > &s2)
Returns true if s1 and s2 have at least one item in common.
Definition: container.h:332
Container filter(const Container &input, const Predicate &predicate)
Returns Container with items from input that satistfy predicate.
Definition: container.h:223
void removeItem(std::vector< Item > &v, const Item &item)
Removes all occurrences of the given item from the given vector.
Definition: container.h:146
bool hasItem(const Container &container, const Item &item)
Returns true if container contains item, false otherwise.
Definition: container.h:44
std::set< T > setIntersection(const std::set< T > &s1, const std::set< T > &s2)
Returns the set intersection s1 \cap s2.
Definition: container.h:267
Item getValueOrDefault(const Container &container, const Item &item, Item defaultValue=Item())
Returns the found value if container contains item, defaultValue otherwise.
Definition: container.h:112
bool mapHasValue(const Map &m, const typename Map::mapped_type &v)
Returns true if the given map m has a value v, false otherwise.
Definition: container.h:391
Map::mapped_type mapGetValueOrDefault(const Map &m, const typename Map::key_type &key, typename Map::mapped_type defaultValue=typename Map::mapped_type())
Returns the value associated to the given key in m, or defaultValue if there is no key in m.
Definition: container.h:408
Map::mapped_type & addToMap(const typename Map::key_type &key, const typename Map::mapped_type &value, Map &m)
Adds the pair <key, value> to map m.
Definition: container.h:445
void clear(Container &container)
Clears the given container.
Definition: container.h:158
std::set< T > setUnion(const std::set< T > &s1, const std::set< T > &s2)
Returns the set union s1 \cup s2.
Definition: container.h:251
std::set< T > setDifference(const std::set< T > &s1, const std::set< T > &s2)
Returns the set difference s1 \setminus s2.
Definition: container.h:283
bool areDisjoint(const std::set< T > &s1, const std::set< T > &s2)
Returns true if s1 is disjoint with s2.
Definition: container.h:320
OutputContainer filterTo(const InputContainer &input, const Predicate &predicate)
Returns OutputContainer with items from input that satistfy predicate.
Definition: container.h:201
Map::mapped_type mapGetMaxValue(const Map &m)
Returns the maximum value from m.
Definition: container.h:423
std::set< typename Map::key_type > getKeysFromMap(const Map &m)
Returns all keys in the given map m.
Definition: container.h:348
bool mapHasKey(const Map &m, const typename Map::key_type &k)
Returns true if the given map m has a key k, false otherwise.
Definition: container.h:379
void removeFromSet(std::set< T > &from, const std::set< T > &toRemove)
Removes all values that are in toRemove from from.
Definition: container.h:296
std::map< V, K > getMapWithSwappedKeysAndValues(const std::map< K, V > &m)
Returns a new map that has swapped keys and values.
Definition: container.h:470
void addToSet(const std::set< T > &from, std::set< T > &to)
Adds all values from from into to.
Definition: container.h:238
Definition: archive_wrapper.h:19
Definition: container.h:545
std::size_t operator()(T t) const
Definition: container.h:547