mCRL2
Loading...
Searching...
No Matches
container_utility.h
Go to the documentation of this file.
1// Author(s): Wieger Wesselink
2// Copyright: see the accompanying file COPYING or copy at
3// https://github.com/mCRL2org/mCRL2/blob/master/COPYING
4//
5// Distributed under the Boost Software License, Version 1.0.
6// (See accompanying file LICENSE_1_0.txt or copy at
7// http://www.boost.org/LICENSE_1_0.txt)
8//
9
10#ifndef MCRL2_UTILITIES_DETAIL_CONTAINER_UTILITY_H
11#define MCRL2_UTILITIES_DETAIL_CONTAINER_UTILITY_H
12
13#include <algorithm>
14#include <iterator>
15#include <vector>
16#include <map>
17#include <set>
18#include <unordered_set>
20
21namespace mcrl2 {
22
23namespace utilities {
24
25namespace detail {
26
29template <typename Map>
30typename Map::mapped_type map_element(const Map& m, const typename Map::key_type& key)
31{
32 auto i = m.find(key);
33 if (i == m.end())
34 {
35 std::ostringstream out;
36 out << "missing key in map!";
37 throw mcrl2::runtime_error(out.str());
38 }
39 return i->second;
40}
41
42// Returns the value corresponding to the given key in map m, or undefined_value if no such value exists.
43template <typename Map>
44typename Map::mapped_type mapped_value(const Map& m, const typename Map::key_type& key, const typename Map::mapped_type& undefined_value)
45{
46 auto i = m.find(key);
47 if (i != m.end())
48 {
49 return i->second;
50 }
51 return undefined_value;
52}
53
56template <typename Container>
57bool contains(const Container& c, const typename Container::value_type& v)
58{
59 return std::find(c.begin(), c.end(), v) != c.end();
60}
61
62// specialization
63template <typename T>
64bool contains(const std::set<T>& c, const typename std::set<T>::value_type& v)
65{
66 return c.find(v) != c.end();
67}
68
69// specialization
70template <typename T>
71bool contains(const std::multiset<T>& c, const typename std::multiset<T>::value_type& v)
72{
73 return c.find(v) != c.end();
74}
75
76// specialization
77template <typename T>
78bool contains(const std::unordered_set<T>& c, const typename std::set<T>::value_type& v)
79{
80 return c.find(v) != c.end();
81}
82
85template <typename Key, typename T>
86bool has_key(const std::map<Key, T>& c, const Key& v)
87{
88 return c.find(v) != c.end();
89}
90
93template <typename Key, typename T>
94bool has_key(const std::multimap<Key, T>& c, const Key& v)
95{
96 return c.find(v) != c.end();
97}
98
99// Remove an element from v, and return it.
100template <typename Container>
101typename Container::value_type pick_element(Container& v)
102{
103 auto i = v.begin();
104 auto result = *i;
105 v.erase(i);
106 return result;
107}
108
109// inserts elements of c into s
110template <typename T, typename Container>
111void set_insert(std::set<T>& s, const Container& c)
112{
113 for (auto i = c.begin(); i != c.end(); ++i)
114 {
115 s.insert(*i);
116 }
117}
118
119// removes elements of c from s
120template <typename T, typename Container>
121void set_remove(std::set<T>& s, const Container& c)
122{
123 for (auto i = c.begin(); i != c.end(); ++i)
124 {
125 s.erase(*i);
126 }
127}
128
129// C++11; works for sets and maps
130// Removes elements that satisfy predicate pred.
131template< typename ContainerT, typename PredicateT >
132void remove_if(ContainerT& items, const PredicateT& predicate)
133{
134 for (auto it = items.begin(); it != items.end();)
135 {
136 if (predicate(*it))
137 {
138 it = items.erase(it);
139 }
140 else
141 {
142 ++it;
143 }
144 }
145}
146
148template <typename InputIterator1, typename InputIterator2>
149bool has_empty_intersection(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2)
150{
151 while (first1 != last1 && first2 != last2)
152 {
153 if (*first1 < *first2)
154 {
155 ++first1;
156 }
157 else if (*first2 < *first1)
158 {
159 ++first2;
160 }
161 else
162 {
163 return false;
164 }
165 }
166 return true;
167}
168
169template <typename SetContainer1, typename SetContainer2>
170bool has_empty_intersection(const SetContainer1& s1, const SetContainer2& s2)
171{
172 return has_empty_intersection(s1.begin(), s1.end(), s2.begin(), s2.end());
173}
174
179template <typename T>
180std::set<T> set_union(const std::set<T>& x, const std::set<T>& y)
181{
182 std::set<T> result;
183 std::set_union(x.begin(), x.end(), y.begin(), y.end(), std::inserter(result, result.begin()));
184 return result;
185}
186
191template <typename T>
192std::set<T> set_difference(const std::set<T>& x, const std::set<T>& y)
193{
194 std::set<T> result;
195 std::set_difference(x.begin(), x.end(), y.begin(), y.end(), std::inserter(result, result.begin()));
196 return result;
197}
198
203template <typename T>
204std::set<T> set_intersection(const std::set<T>& x, const std::set<T>& y)
205{
206 std::set<T> result;
207 std::set_intersection(x.begin(), x.end(), y.begin(), y.end(), std::inserter(result, result.begin()));
208 return result;
209}
210
215template <typename T>
216bool set_includes(const std::set<T>& x, const std::set<T>& y)
217{
218 return std::includes(x.begin(), x.end(), y.begin(), y.end());
219}
220
221
222template <typename T>
223std::vector<T> as_vector(const std::set<T>& x)
224{
225 return std::vector<T>(x.begin(), x.end());
226}
227
228template <typename T>
229std::set<T> as_set(const std::vector<T>& x)
230{
231 return std::set<T>(x.begin(), x.end());
232}
233
234} // namespace detail
235
236} // namespace utilities
237
238} // namespace mcrl2
239
240#endif // MCRL2_UTILITIES_DETAIL_CONTAINER_UTILITY_H
Standard exception class for reporting runtime errors.
Definition exception.h:27
Exception classes for use in libraries and tools.
bool set_includes(const std::set< T > &x, const std::set< T > &y)
Returns if y is included in x.
void set_insert(std::set< T > &s, const Container &c)
std::set< T > set_union(const std::set< T > &x, const std::set< T > &y)
Returns the union of two sets.
bool contains(const atermpp::indexed_set< Key, ThreadSafe, Hash, Equals, Allocator, KeyTable > &c, const typename atermpp::indexed_set< Key, ThreadSafe, Hash, Equals, Allocator, KeyTable >::key_type &v, const std::size_t thread_index=0)
Definition indexed_set.h:86
void set_remove(std::set< T > &s, const Container &c)
bool has_empty_intersection(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2)
Returns true if the sorted ranges [first1, ..., last1) and [first2, ..., last2) have an empty interse...
Container::value_type pick_element(Container &v)
std::vector< T > as_vector(const std::set< T > &x)
bool has_key(const std::map< Key, T > &c, const Key &v)
Returns the value corresponding to the given key in the set m. If the key is not present,...
std::set< T > as_set(const std::vector< T > &x)
Map::mapped_type map_element(const Map &m, const typename Map::key_type &key)
Returns the value corresponding to the given key in the map m. If the key is not present,...
void remove_if(ContainerT &items, const PredicateT &predicate)
Map::mapped_type mapped_value(const Map &m, const typename Map::key_type &key, const typename Map::mapped_type &undefined_value)
std::set< T > set_intersection(const std::set< T > &x, const std::set< T > &y)
Returns the intersection of two sets.
std::set< T > set_difference(const std::set< T > &x, const std::set< T > &y)
Returns the difference of two sets.
A class that takes a linear process specification and checks all tau-summands of that LPS for conflue...
Definition indexed_set.h:72