carl  24.04
Computer ARithmetic Library
TypedSetCover.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "SetCover.h"
4 
5 #include <cassert>
6 #include <map>
7 #include <vector>
8 
9 namespace carl::covering {
10 
11 /**
12  * Represents a set cover problem where a set is represented by some type.
13  * It actually wraps a SetCover class and takes care of mapping the custom set type to an id type.
14  */
15 template<typename Set>
17 public:
18  template<typename T>
19  friend std::ostream& operator<<(std::ostream& os, const TypedSetCover<T>& tsc);
20 private:
21  /// The actual set cover.
23  /// Maps id to set.
24  std::vector<Set> mSets;
25  /// Maps set to id.
26  std::map<Set, std::size_t> mSetMap;
27 
28  /// Gets id for a set, creates a new id if necessary.
29  std::size_t get_set_id(const Set& s) {
30  auto it = mSetMap.try_emplace(s, mSetMap.size());
31  if (it.second) {
32  assert(it.first->second == mSets.size());
33  mSets.emplace_back(s);
34  }
35  return it.first->second;
36  }
37 public:
38  /// States that s covers the given element.
39  void set(const Set& s, std::size_t element) {
40  mSetCover.set(get_set_id(s), element);
41  }
42  /// States that s covers the given elements.
43  void set(const Set& s, const Bitset& elements) {
44  mSetCover.set(get_set_id(s), elements);
45  }
46 
47  const Set& get_set(std::size_t sid) const {
48  assert(sid < mSets.size());
49  return mSets[sid];
50  }
51 
52  /// Returns the underlying set cover.
53  explicit operator const SetCover&() const {
54  return mSetCover;
55  }
56  /// Returns the underlying set cover.
57  const auto& set_cover() const {
58  return mSetCover;
59  }
60  /// Returns the underlying set cover.
61  auto& set_cover() {
62  return mSetCover;
63  }
64 
65  /// Convenience function to run the given heuristic on this set cover.
66  template<typename F>
67  std::vector<Set> get_cover(F&& heuristic) {
68  std::vector<Set> res;
69  for (std::size_t id: heuristic(mSetCover)) {
70  res.emplace_back(mSets[id]);
71  }
72  return res;
73  }
74 };
75 
76 /// Print the typed set cover to os.
77 template<typename T>
78 std::ostream& operator<<(std::ostream& os, const TypedSetCover<T>& tsc) {
79  return os << static_cast<const SetCover&>(tsc);
80 }
81 
82 }
std::ostream & operator<<(std::ostream &os, const SetCover &sc)
Print the set cover to os.
Definition: SetCover.cpp:94
This class is a simple wrapper around boost::dynamic_bitset.
Definition: Bitset.h:20
Represents a set cover problem.
Definition: SetCover.h:15
void set(std::size_t set, std::size_t element)
States that s covers the given element.
Definition: SetCover.cpp:10
Represents a set cover problem where a set is represented by some type.
Definition: TypedSetCover.h:16
std::vector< Set > mSets
Maps id to set.
Definition: TypedSetCover.h:24
std::size_t get_set_id(const Set &s)
Gets id for a set, creates a new id if necessary.
Definition: TypedSetCover.h:29
auto & set_cover()
Returns the underlying set cover.
Definition: TypedSetCover.h:61
const Set & get_set(std::size_t sid) const
Definition: TypedSetCover.h:47
std::map< Set, std::size_t > mSetMap
Maps set to id.
Definition: TypedSetCover.h:26
SetCover mSetCover
The actual set cover.
Definition: TypedSetCover.h:22
friend std::ostream & operator<<(std::ostream &os, const TypedSetCover< T > &tsc)
Print the typed set cover to os.
Definition: TypedSetCover.h:78
void set(const Set &s, std::size_t element)
States that s covers the given element.
Definition: TypedSetCover.h:39
void set(const Set &s, const Bitset &elements)
States that s covers the given elements.
Definition: TypedSetCover.h:43
std::vector< Set > get_cover(F &&heuristic)
Convenience function to run the given heuristic on this set cover.
Definition: TypedSetCover.h:67
const auto & set_cover() const
Returns the underlying set cover.
Definition: TypedSetCover.h:57