Vcsn  2.1
Be Rational
unordered_set.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <unordered_set>
4 
6 
7 namespace std
8 {
9 
10  /*-------------------------.
11  | hash(unordered_set<T>). |
12  `-------------------------*/
13 
14  template <typename Key, typename Hash, typename KeyEqual, typename Alloc>
15  struct hash<unordered_set<Key, Hash, KeyEqual, Alloc>>
16  {
17  using type = unordered_set<Key, Hash, KeyEqual, Alloc>;
18  size_t operator()(const type& ss) const
19  {
20  // Compute the sum of the hashes. Beware that we must be
21  // independant of the order, since this is an unordered_set.
22  Hash hasher;
23  size_t res = 0;
24  for (auto s: ss)
25  res += hasher(s);
26  return res;
27  }
28  };
29 }
30 
31 namespace vcsn
32 {
33 
35  template <typename Key, typename Hash, typename KeyEqual, typename Alloc>
36  bool
37  has(const std::unordered_set<Key, Hash, KeyEqual, Alloc>& s, const Key& k)
38  {
39  return s.find(k) != std::end(s);
40  }
41 
43  template <typename Key, typename Hash, typename KeyEqual, typename Alloc>
44  std::unordered_set<Key, Hash, KeyEqual, Alloc>
45  intersection(const std::unordered_set<Key, Hash, KeyEqual, Alloc>& s1,
46  const std::unordered_set<Key, Hash, KeyEqual, Alloc>& s2)
47  {
48  if (s2.size() < s1.size())
49  return intersection(s2, s1);
50  else
51  {
52  std::unordered_set<Key, Hash, KeyEqual, Alloc> res;
53  for (const auto& e : s1)
54  if (has(s2, e))
55  res.emplace(e);
56  return res;
57  }
58  }
59 }
STL namespace.
return hasher(v)
unordered_set< Key, Hash, KeyEqual, Alloc > type
ATTRIBUTE_PURE bool has(const std::deque< T, Allocator > &s, const T &e)
Whether e is member of s.
Definition: deque.hh:13
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.
Definition: set.hxx:15