Vcsn  2.0
Be Rational
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
set.hxx
Go to the documentation of this file.
1 #include <iostream>
2 
3 namespace vcsn
4 {
5  template <typename T>
6  inline
7  bool
8  has(const std::set<T>& s, const T& e)
9  {
10  return s.find(e) != std::end(s);
11  }
12 
13  template <typename Key, typename Value, typename Comp, typename Alloc>
14  inline
15  std::set<typename std::map<Key, Value, Comp, Alloc>::mapped_type>
16  image(const std::map<Key, Value, Comp, Alloc>& m)
17  {
18  std::set<typename std::map<Key, Value, Comp, Alloc>::mapped_type> res;
19  for (const auto& p: m)
20  res.insert(p.second);
21  return res;
22  }
23 
24 
25  template <typename T>
26  inline
27  std::set<T>
28  intersection(const std::set<T>& set1, const std::set<T>& set2)
29  {
30  std::set<T> res;
31  std::insert_iterator<std::set<T>> i{res, begin(res)};
32  std::set_intersection(begin(set1), end(set1),
33  begin(set2), end(set2),
34  i);
35  return res;
36  }
37 
38 
39  template <typename T>
40  inline
41  std::set<std::set<T>>
42  intersection_closure(std::set<std::set<T>> pset)
43  {
44  while (true)
45  {
46  bool done = true;
47  for (const auto& set1: pset)
48  for (const auto& set2: pset)
49  if (pset.emplace(intersection(set1, set2)).second)
50  done = false;
51  if (done)
52  break;
53  }
54  return pset;
55  }
56 
57 
58 
59  template <typename T>
60  inline
61  std::set<T>
62  get_union(const std::set<T>& set1, const std::set<T>& set2)
63  {
64  std::set<T> res;
65  std::insert_iterator<std::set<T>> i{res, begin(res)};
66  std::set_union(begin(set1), end(set1),
67  begin(set2), end(set2),
68  i);
69  return res;
70  }
71 
72  template <typename T>
73  inline
74  std::ostream&
75  print(const std::set<T>& set, std::ostream& o)
76  {
77  const char* sep = "";
78  for (const auto& m: set)
79  {
80  o << sep << m;
81  sep = ", ";
82  }
83  return o;
84  }
85 
86  template <typename Container1, typename Container2>
87  inline
88  bool subset(const Container1& set1, const Container2& set2)
89  {
90  return std::includes(set2.begin(), set2.end(),
91  set1.begin(), set1.end());
92  }
93 }
bool subset(const Container1 &set1, const Container2 &set2) ATTRIBUTE_PURE
Whether set1 ⊆ set2.
Definition: set.hxx:88
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.
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.
std::ostream & print(const ValueSet &vs, const typename ValueSet::value_t &v, std::ostream &o, const std::string &format)
Applies to (ValueSet, Value, ostream, string): for expansionset, polynomialset, ratexpset, and weightset.
Definition: print.hh:51
std::set< typename std::map< Key, Value, Comp, Alloc >::mapped_type > image(const std::map< Key, Value, Comp, Alloc > &m)
The set of values of a map.
Definition: set.hxx:16
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