Vcsn  2.3
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  set_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 set_intersection(s2, s1);
50  else
51  {
52  auto res = std::unordered_set<Key, Hash, KeyEqual, Alloc>{};
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)
Container set_intersection(const Container &s1, const Container &s2)
The intersection of two sets.
Definition: algorithm.hh:184
unordered_set< Key, Hash, KeyEqual, Alloc > type
return res
Definition: multiply.hh:398
ATTRIBUTE_PURE bool has(const boost::container::flat_set< Key, Compare, Allocator > &s, const Key &e)
Whether e is member of s.
Definition: setalpha.hh:25
Definition: a-star.hh:8