Vcsn  2.1
Be Rational
set.hxx
Go to the documentation of this file.
1 #include <iostream>
2 
3 namespace vcsn
4 {
5  template <typename T, typename Compare, typename Alloc>
6  bool
7  has(const std::set<T, Compare, Alloc>& s, const T& e)
8  {
9  return s.find(e) != std::end(s);
10  }
11 
12 
13  template <typename T, typename Compare, typename Alloc>
14  std::set<T, Compare, Alloc>
15  intersection(const std::set<T, Compare, Alloc>& set1,
16  const std::set<T, Compare, Alloc>& set2)
17  {
18  using set_t = std::set<T, Compare, Alloc>;
19  set_t res;
20  std::insert_iterator<set_t> i{res, begin(res)};
21  std::set_intersection(begin(set1), end(set1),
22  begin(set2), end(set2),
23  i,
24  set1.key_comp());
25  return res;
26  }
27 
28 
29  template <typename T, typename Compare, typename Alloc>
30  std::set<std::set<T, Compare, Alloc>>
31  intersection_closure(std::set<std::set<T, Compare, Alloc>> pset)
32  {
33  while (true)
34  {
35  bool done = true;
36  for (const auto& set1: pset)
37  for (const auto& set2: pset)
38  if (pset.emplace(intersection(set1, set2)).second)
39  done = false;
40  if (done)
41  break;
42  }
43  return pset;
44  }
45 
46 
47  template <typename T, typename Compare, typename Alloc>
48  std::set<T, Compare, Alloc>
49  get_union(const std::set<T, Compare, Alloc>& set1,
50  const std::set<T, Compare, Alloc>& set2)
51  {
52  using set_t = std::set<T, Compare, Alloc>;
53  set_t res;
54  std::insert_iterator<set_t> i{res, begin(res)};
55  std::set_union(begin(set1), end(set1),
56  begin(set2), end(set2),
57  i,
58  set1.key_comp());
59  return res;
60  }
61 
62 
63  template <typename T, typename Compare, typename Alloc>
64  std::ostream&
65  print(const std::set<T, Compare, Alloc>& set, std::ostream& o)
66  {
67  const char* sep = "";
68  for (const auto& m: set)
69  {
70  o << sep << m;
71  sep = ", ";
72  }
73  return o;
74  }
75 
76 
77  template <typename Container>
78  bool subset(const Container& set1, const Container& set2)
79  {
80  return std::includes(set2.begin(), set2.end(),
81  set1.begin(), set1.end(),
82  set1.key_comp());
83  }
84 }
std::set< std::set< T, Compare, Alloc > > intersection_closure(std::set< std::set< T, Compare, Alloc >> pset)
The set of all the intersections of the sets in pset.
Definition: set.hxx:31
bool subset(const Container &set1, const Container &set2) ATTRIBUTE_PURE
Whether set1 ⊆ set2.
Definition: set.hxx:78
std::ostream & print(const Aut &aut, std::ostream &out, const std::string &format)
Definition: print.hh:77
std::set< T, Compare, Alloc > get_union(const std::set< T, Compare, Alloc > &set1, const std::set< T, Compare, Alloc > &set2)
The union of two sets.
Definition: set.hxx:49
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