Awali
Another Weighted Automata library
set.hxx
Go to the documentation of this file.
1 // This file is part of Awali.
2 // Copyright 2016-2022 Sylvain Lombardy, Victor Marsault, Jacques Sakarovitch
3 //
4 // Awali is a free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 #include <iostream>
18 
19 namespace awali {
20  namespace sttc {
21  namespace internal {
22  template <typename T>
23  inline
24  bool
25  has(const std::set<T>& s, const T& e)
26  {
27  return s.find(e) != std::end(s);
28  }
29 
30  template <typename Key, typename Value, typename Comp, typename Alloc>
31  inline
32  std::set<typename std::map<Key, Value, Comp, Alloc>::mapped_type>
33  image(const std::map<Key, Value, Comp, Alloc>& m)
34  {
35  std::set<typename std::map<Key, Value, Comp, Alloc>::mapped_type> res;
36  for (const auto& p: m)
37  res.insert(p.second);
38  return res;
39  }
40 
41 
42  template <typename T>
43  inline
44  std::set<T>
45  intersection(const std::set<T>& set1, const std::set<T>& set2)
46  {
47  std::set<T> res;
48  std::insert_iterator<std::set<T>> i{res, begin(res)};
49  std::set_intersection(begin(set1), end(set1),
50  begin(set2), end(set2),
51  i);
52  return res;
53  }
54 
55 
56  template <typename T>
57  inline
58  std::set<std::set<T>>
59  intersection_closure(std::set<std::set<T>> pset)
60  {
61  while (true)
62  {
63  bool done = true;
64  for (const auto& set1: pset)
65  for (const auto& set2: pset)
66  if (pset.emplace(intersection(set1, set2)).second)
67  done = false;
68  if (done)
69  break;
70  }
71  return pset;
72  }
73 
74 
75 
76  template <typename T>
77  inline
78  std::set<T>
79  get_union(const std::set<T>& set1, const std::set<T>& set2)
80  {
81  std::set<T> res;
82  std::insert_iterator<std::set<T>> i{res, begin(res)};
83  std::set_union(begin(set1), end(set1),
84  begin(set2), end(set2),
85  i);
86  return res;
87  }
88 
89  template <typename T>
90  inline
91  std::ostream&
92  print(const std::set<T>& set, std::ostream& o)
93  {
94  const char* sep = "";
95  for (const auto& m: set)
96  {
97  o << sep << m;
98  sep = ", ";
99  }
100  return o;
101  }
102 
103  template <typename Container1, typename Container2>
104  inline
105  bool subset(const Container1& set1, const Container2& set2)
106  {
107  return std::includes(set2.begin(), set2.end(),
108  set1.begin(), set1.end());
109  }
110  }
111  }
112 }//end of ns awali::stc
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.
std::ostream & print(const std::set< T, Compare, Alloc > &set, std::ostream &o)
Print with a separator. Meant to help debugging.
bool subset(const Container1 &set1, const Container2 &set2) ATTRIBUTE_PURE
Whether set1 ⊆ set2.
Definition: set.hxx:105
bool has(const std::map< Key, Value, Compare, Alloc > &s, const Key &e)
Definition: map.hh:53
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:33
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.
Main namespace of Awali.
Definition: ato.hh:22