PandA-2024.02
custom_set.hpp
Go to the documentation of this file.
1 /*
2  *
3  * _/_/_/ _/_/ _/ _/ _/_/_/ _/_/
4  * _/ _/ _/ _/ _/_/ _/ _/ _/ _/ _/
5  * _/_/_/ _/_/_/_/ _/ _/_/ _/ _/ _/_/_/_/
6  * _/ _/ _/ _/ _/ _/ _/ _/ _/
7  * _/ _/ _/ _/ _/ _/_/_/ _/ _/
8  *
9  * ***********************************************
10  * PandA Project
11  * URL: http://panda.dei.polimi.it
12  * Politecnico di Milano - DEIB
13  * System Architectures Group
14  * ***********************************************
15  * Copyright (C) 2004-2024 Politecnico di Milano
16  *
17  * This file is part of the PandA framework.
18  *
19  * The PandA framework is free software; you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation; either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program. If not, see <http://www.gnu.org/licenses/>.
31  *
32  */
33 
45 #ifndef CUSTOM_SET_HPP
46 #define CUSTOM_SET_HPP
47 
49 #include "config_HAVE_UNORDERED.hpp"
50 
51 #include <set>
52 #include <unordered_set>
53 
54 template <class _Value, class _Hash = std::hash<_Value>, class _Pred = std::equal_to<_Value>,
55  class _Alloc = std::allocator<_Value>>
56 using UnorderedSetStd = std::unordered_set<_Value, _Hash, _Pred, _Alloc>;
57 
58 template <typename Key, typename Compare = std::less<Key>, typename Alloc = std::allocator<Key>>
59 using OrderedSetStd = std::set<Key, Compare, Alloc>;
60 
61 #if !defined(__clang__) && (__GNUC__ == 4 && __GNUC_MINOR__ <= 8)
62 #ifndef NO_ABSEIL_HASH
63 #define NO_ABSEIL_HASH 1
64 #endif
65 #include <algorithm>
66 
67 template <class _Value, class _Hash = std::hash<_Value>, class _Pred = std::equal_to<_Value>,
68  class _Alloc = std::allocator<_Value>>
70 
71 template <class T, class _Hash = std::hash<T>, class _Pred = std::equal_to<T>, class _Alloc = std::allocator<T>>
72 class CustomUnorderedSet : public UnorderedSetStd<T, _Hash, _Pred, _Alloc>
73 {
74  public:
75  void operator+=(const CustomUnorderedSet& other)
76  {
77  typename CustomUnorderedSet<T>::const_iterator other_element, other_element_end = other.end();
78  for(other_element = other.begin(); other_element != other_element_end; ++other_element)
79  {
80  this->insert(*other_element);
81  }
82  }
83 
84  void operator-=(const CustomUnorderedSet& other)
85  {
86  typename CustomUnorderedSet<T>::const_iterator other_element, other_element_end = other.end();
87  for(other_element = other.begin(); other_element != other_element_end; ++other_element)
88  {
89  this->erase(*other_element);
90  }
91  }
92 
94  {
95  CustomUnorderedSet return_value = *this;
96  return_value -= other;
97  return return_value;
98  }
99 
101  {
102  CustomUnorderedSet return_value;
103  typename CustomUnorderedSet<T>::const_iterator other_element, other_element_end = other.end();
104  for(other_element = other.begin(); other_element != other_element_end; ++other_element)
105  {
106  if(this->find(*other_element) != this->end())
107  {
108  return_value.insert(*other_element);
109  }
110  }
111  return return_value;
112  }
113 };
114 
115 template <typename Key, typename Compare = std::less<Key>, typename _Alloc = std::allocator<Key>>
116 class CustomOrderedSet : public OrderedSetStd<Key, Compare, _Alloc>
117 {
118  public:
119  void operator+=(const CustomOrderedSet& other)
120  {
121  typename CustomOrderedSet<Key>::const_iterator other_element, other_element_end = other.end();
122  for(other_element = other.begin(); other_element != other_element_end; ++other_element)
123  {
124  this->insert(*other_element);
125  }
126  }
127 
128  void operator-=(const CustomOrderedSet& other)
129  {
130  typename CustomOrderedSet<Key>::const_iterator other_element, other_element_end = other.end();
131  for(other_element = other.begin(); other_element != other_element_end; ++other_element)
132  {
133  this->erase(*other_element);
134  }
135  }
136 
137  CustomOrderedSet operator-(const CustomOrderedSet& other) const
138  {
139  CustomOrderedSet return_value;
140  std::set_difference(this->begin(), this->end(), other.begin(), other.end(),
141  std::inserter(return_value, return_value.begin()));
142  return return_value;
143  }
144 
145  CustomOrderedSet Intersect(const CustomOrderedSet& other) const
146  {
147  CustomOrderedSet return_value;
148  typename CustomOrderedSet<Key>::const_iterator other_element, other_element_end = other.end();
149  for(other_element = other.begin(); other_element != other_element_end; ++other_element)
150  {
151  if(this->find(*other_element) != this->end())
152  {
153  return_value.insert(*other_element);
154  }
155  }
156  return return_value;
157  }
158 };
159 
160 #if HAVE_UNORDERED
161 template <typename T>
163 #else
164 template <typename T>
166 #endif
167 
168 #else
169 #ifndef NO_ABSEIL_HASH
170 #define NO_ABSEIL_HASH 0
171 #endif
172 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
173 #pragma GCC diagnostic push
174 #pragma GCC diagnostic ignored "-Wsign-conversion"
175 #pragma GCC diagnostic ignored "-Wconversion"
176 #pragma GCC diagnostic ignored "-Wpedantic"
177 #pragma GCC diagnostic ignored "-Wctor-dtor-privacy"
178 #pragma GCC diagnostic ignored "-Woverflow"
179 #pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
180 #pragma GCC diagnostic ignored "-Wstrict-overflow"
181 #else
182 #pragma GCC diagnostic warning "-Wsign-conversion"
183 #pragma GCC diagnostic warning "-Wconversion"
184 #pragma GCC diagnostic warning "-Wpedantic"
185 #pragma GCC diagnostic warning "-Wctor-dtor-privacy"
186 #pragma GCC diagnostic warning "-Woverflow"
187 #pragma GCC diagnostic warning "-Wzero-as-null-pointer-constant"
188 #endif
189 
190 #if defined(__clang__)
191 #pragma clang diagnostic push
192 #pragma clang diagnostic ignored "-Wsign-conversion"
193 #pragma clang diagnostic ignored "-Wconversion"
194 #pragma clang diagnostic ignored "-Wdouble-promotion"
195 #pragma clang diagnostic ignored "-Wshorten-64-to-32"
196 #pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant"
197 #pragma clang diagnostic ignored "-Wextra-semi"
198 #endif
199 
200 #include "absl/container/btree_set.h"
201 #include "absl/container/flat_hash_set.h"
202 #include "absl/container/node_hash_set.h"
203 #include "absl/hash/hash.h"
204 #include <set>
205 
206 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
207 #pragma GCC diagnostic pop
208 #endif
209 #if defined(__clang__)
210 #pragma clang diagnostic pop
211 #endif
212 
213 template <class _Value, class _Hash = typename absl::node_hash_set<_Value>::hasher,
214  class _Pred = typename absl::node_hash_set<_Value>::key_equal, class _Alloc = std::allocator<_Value>>
215 using UnorderedSetStdStable = absl::node_hash_set<_Value, _Hash, _Pred, _Alloc>;
216 
217 template <class T, class _Hash = typename absl::flat_hash_set<T>::hasher,
218  class _Eq = typename absl::flat_hash_set<T>::key_equal, class _Alloc = std::allocator<T>>
219 class CustomUnorderedSet : public absl::flat_hash_set<T, _Hash, _Eq, _Alloc>
220 {
221  public:
222  void operator+=(const CustomUnorderedSet& other)
223  {
224  typename CustomUnorderedSet<T>::const_iterator other_element, other_element_end = other.end();
225  for(other_element = other.begin(); other_element != other_element_end; ++other_element)
226  {
227  this->insert(*other_element);
228  }
229  }
230 
231  void operator-=(const CustomUnorderedSet& other)
232  {
233  typename CustomUnorderedSet<T>::const_iterator other_element, other_element_end = other.end();
234  for(other_element = other.begin(); other_element != other_element_end; ++other_element)
235  {
236  this->erase(*other_element);
237  }
238  }
239 
241  {
242  CustomUnorderedSet return_value = *this;
243  return_value -= other;
244  return return_value;
245  }
246 
248  {
249  CustomUnorderedSet return_value;
250  typename CustomUnorderedSet<T>::const_iterator other_element, other_element_end = other.end();
251  for(other_element = other.begin(); other_element != other_element_end; ++other_element)
252  {
253  if(this->find(*other_element) != this->end())
254  {
255  return_value.insert(*other_element);
256  }
257  }
258  return return_value;
259  }
260 };
261 
262 template <typename Key, typename Compare = std::less<Key>, typename Alloc = std::allocator<Key>>
263 class CustomOrderedSet : public absl::btree_set<Key, Compare, Alloc>
264 {
265  public:
266  void operator+=(const CustomOrderedSet& other)
267  {
268  typename CustomOrderedSet<Key>::const_iterator other_element, other_element_end = other.end();
269  for(other_element = other.begin(); other_element != other_element_end; ++other_element)
270  {
271  this->insert(*other_element);
272  }
273  }
274 
275  void operator-=(const CustomOrderedSet& other)
276  {
277  typename CustomOrderedSet<Key>::const_iterator other_element, other_element_end = other.end();
278  for(other_element = other.begin(); other_element != other_element_end; ++other_element)
279  {
280  this->erase(*other_element);
281  }
282  }
283 
285  {
286  CustomOrderedSet return_value;
287  std::set_difference(this->begin(), this->end(), other.begin(), other.end(),
288  std::inserter(return_value, return_value.begin()));
289  return return_value;
290  }
291 
293  {
294  CustomOrderedSet return_value;
295  typename CustomOrderedSet<Key>::const_iterator other_element, other_element_end = other.end();
296  for(other_element = other.begin(); other_element != other_element_end; ++other_element)
297  {
298  if(this->find(*other_element) != this->end())
299  {
300  return_value.insert(*other_element);
301  }
302  }
303  return return_value;
304  }
305 };
306 
307 #if HAVE_UNORDERED
308 template <typename T>
310 #else
311 template <typename T>
313 #endif
314 
315 #endif
316 #endif
absl::node_hash_set< _Value, _Hash, _Pred, _Alloc > UnorderedSetStdStable
Definition: custom_set.hpp:215
std::unordered_set< _Value, _Hash, _Pred, _Alloc > UnorderedSetStd
Autoheader include.
Definition: custom_set.hpp:56
CustomUnorderedSet Intersect(const CustomUnorderedSet &other) const
Definition: custom_set.hpp:247
void insert(node_tree **tree, int val)
Definition: tree.c:121
CustomOrderedSet Intersect(const CustomOrderedSet &other) const
Definition: custom_set.hpp:292
void operator-=(const CustomOrderedSet &other)
Definition: custom_set.hpp:275
void operator+=(const CustomOrderedSet &other)
Definition: custom_set.hpp:266
std::set< Key, Compare, Alloc > OrderedSetStd
Definition: custom_set.hpp:59
void operator+=(const CustomUnorderedSet &other)
Definition: custom_set.hpp:222
CustomOrderedSet operator-(const CustomOrderedSet &other) const
Definition: custom_set.hpp:284
void operator-=(const CustomUnorderedSet &other)
Definition: custom_set.hpp:231
CustomUnorderedSet operator-(const CustomUnorderedSet &other) const
Definition: custom_set.hpp:240

Generated on Mon Feb 12 2024 13:02:56 for PandA-2024.02 by doxygen 1.8.13