20 #ifndef LIMI_INTERNAL_ANTICHAIN_H
21 #define LIMI_INTERNAL_ANTICHAIN_H
25 #include <unordered_map>
26 #include <unordered_set>
29 #include "../generics.h"
53 template <
class A,
class B,
class HashA = std::hash<A>,
class HashB = std::hash<B>,
class CompareA = std::equal_to<A>,
class CompareB = std::equal_to<B>>
57 typedef std::unordered_set<B, HashB, CompareB> b_set;
58 typedef std::shared_ptr<const b_set> pb_set;
61 std::unordered_map<A,std::vector<std::pair<pb_set,bool>>, HashA, CompareA> datastore;
66 inline bool contained(
const b_set& set1,
const b_set& set2)
const {
67 for(
auto it=set1.begin(); it!=set1.end(); it++) {
68 if (set2.find(*it) == set2.end())
80 inline void add_unchecked(
const A& a,
const pb_set& b,
bool dirty =
false) {
81 datastore[a].push_back(std::make_pair(b, dirty));
92 void add(
const A& a,
const pb_set& b,
bool dirty =
false) {
93 std::vector<std::pair<pb_set,bool>>& b_sets = datastore[a];
95 for (
auto it = b_sets.begin(); it != b_sets.end();) {
97 if (contained(*it->first, *b)) {
101 if (contained(*b, *it->first)) {
102 it = b_sets.erase(it);
108 b_sets.push_back(std::make_pair(b,dirty));
117 auto b_sets = datastore.find(a);
118 if (b_sets == datastore.end())
120 for (
auto it = b_sets->second.begin(); it != b_sets->second.end(); it++) {
121 if (contained(*it->first, *b))
133 return datastore.size();
140 for(std::pair<
const A,std::vector<std::pair<pb_set,bool>>>& ds : datastore) {
141 ds.second.erase(std::remove_if( ds.second.begin(), ds.second.end(), [](std::pair<pb_set,bool> el) {
return el.second; } ), ds.second.end());
154 for(
const std::pair<A,std::vector<std::pair<pb_set,bool>>>& ds : datastore) {
155 out <<
"For element " << printerA(ds.first) << std::endl;
156 for (
const std::pair<pb_set,bool>& set1 : ds.second) {
158 print_set(*set1.first, out, printerB);
159 if (set1.second) out <<
"_d";
170 #endif // LIMI_INTERNAL_ANTICHAIN_H
The printer base is what custom printers need to inherit from.
Definition: generics.h:81
The main namespace of the library.
Definition: antichain_algo.h:40
void clean_dirty()
Remove elements marked as dirty.
Definition: antichain.h:139
void print(std::ostream &out, const printer_base< A > &printerA=printer< A >(), const printer_base< B > &printerB=printer< B >()) const
Print the antichain on the screen.
Definition: antichain.h:153
void add_unchecked(const A &a, const pb_set &b, bool dirty=false)
Add to the antichain an element without checking if the invariant is preserved.
Definition: antichain.h:80
An Antichain of minimal elements.
Definition: antichain.h:54
bool contains(const A &a, const pb_set &b) const
Tests if the element (a,b) or a smaller element is already contained in the antichain.
Definition: antichain.h:116
The default implementation of Limi::printer_base.
Definition: generics.h:104
unsigned size() const
Returns the size of the antichain.
Definition: antichain.h:132
void add(const A &a, const pb_set &b, bool dirty=false)
Add element (a,b) to the antichain.
Definition: antichain.h:92