Vcsn  2.0
Be Rational
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
unordered_set.hh
Go to the documentation of this file.
1 #ifndef VCSN_MISC_UNORDERED_SET_HH
2 # define VCSN_MISC_UNORDERED_SET_HH
3 
4 # include <unordered_set>
5 
6 # include <vcsn/misc/hash.hh>
7 
8 namespace std
9 {
10 
11  /*-------------------------.
12  | hash(unordered_set<T>). |
13  `-------------------------*/
14 
15  template <typename Key, typename Hash, typename KeyEqual, typename Alloc>
16  struct hash<unordered_set<Key, Hash, KeyEqual, Alloc>>
17  {
18  using type = unordered_set<Key, Hash, KeyEqual, Alloc>;
19  size_t operator()(const type& ss) const
20  {
21  // Compute the sum of the hashes. Beware that we must be
22  // independant of the order, since this is an unordered_set.
23  Hash hasher;
24  size_t res = 0;
25  for (auto s: ss)
26  res += hasher(s);
27  return res;
28  }
29  };
30 }
31 
32 namespace vcsn
33 {
34 
36  template <typename Key, typename Hash, typename KeyEqual, typename Alloc>
37  bool
38  has(const std::unordered_set<Key, Hash, KeyEqual, Alloc>& s, const Key& k)
39  {
40  return s.find(k) != std::end(s);
41  }
42 
44  template <typename Key, typename Hash, typename KeyEqual, typename Alloc>
45  std::unordered_set<Key, Hash, KeyEqual, Alloc>
46  intersection(const std::unordered_set<Key, Hash, KeyEqual, Alloc>& s1,
47  const std::unordered_set<Key, Hash, KeyEqual, Alloc>& s2)
48  {
49  if (s2.size() < s1.size())
50  return intersection(s2, s1);
51  else
52  {
53  std::unordered_set<Key, Hash, KeyEqual, Alloc> res;
54  for (const auto& e : s1)
55  if (has(s2, e))
56  res.emplace(e);
57  return res;
58  }
59  }
60 
61 }
62 
63 #endif // !VCSN_MISC_UNORDERED_SET_HH
unordered_set< Key, Hash, KeyEqual, Alloc > type
std::set< T, Compare, Alloc > intersection(const std::set< T, Compare, Alloc > &set1, const std::set< T, Compare, Alloc > &set2)
The intersection of two sets.
bool has(const std::map< Key, Value, Compare, Alloc > &s, const Key &e)
Definition: map.hh:35