Skip to content

Commit e865fd4

Browse files
[iss-77]
* Debugged MDI compact * Debugged MDI compact * Added fixed points * New UnorderedSet::compact() version
1 parent 14d3c3c commit e865fd4

8 files changed

Lines changed: 119 additions & 73 deletions

File tree

algorithms/matching/matching.cpp

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,6 @@ void BFSMatching::minReachable()
214214

215215
// Matching --------------------------------------------------------------------
216216

217-
bool notEqId(const Map &sbgmap) { return !(sbgmap.isId()); }
218-
219217
MatchInfo::MatchInfo(Set matched_edges, bool fully_matchedU)
220218
: matched_edges_(matched_edges), fully_matchedU_(fully_matchedU) {}
221219

@@ -309,10 +307,9 @@ member_imp(BFSMatching, bool, debug);
309307

310308
Set BFSMatching::edgesInPaths() const
311309
{
312-
// Vertices that are successor of other vertices in a path
313-
Set succs = smap().filterMap([](const Map &sbgmap) {
314-
return notEqId(sbgmap);
315-
}).image();
310+
// Vertices that are successors of other vertices in a path
311+
Set not_fixed = smap().dom().difference(smap().fixedPoints());
312+
Set succs = smap().restrict(not_fixed).image();
316313
// Edges whose endings are successors
317314
Set ending_edges = mapD().preImage(succs);
318315
// Map from a 'successor' edge to its start
@@ -325,13 +322,12 @@ Set BFSMatching::edgesInPaths() const
325322

326323
Set BFSMatching::edgesSameRepLR(const PWMap &rmaprmapd) const
327324
{
328-
PWMap rmap_neq_id = rmap().filterMap([](const Map &sbgmap) {
329-
return notEqId(sbgmap);
330-
});
325+
Set not_fixed = rmap().dom().difference(rmap().fixedPoints());
326+
PWMap rmap_neq_id = rmap().restrict(not_fixed);
331327
PWMap rmapb = rmap_neq_id.composition(mapB());
332-
PWMap rmaprmapd_neq_id = rmaprmapd.filterMap([](const Map &sbgmap) {
333-
return notEqId(sbgmap);
334-
});
328+
329+
not_fixed = rmaprmapd.dom().difference(rmaprmapd.fixedPoints());
330+
PWMap rmaprmapd_neq_id = rmaprmapd.restrict(not_fixed);
335331
PWMap rmaprmapdb = rmaprmapd_neq_id.composition(mapB());
336332

337333
return rmapb.equalImage(rmaprmapdb);

algorithms/misc/causalization_builders.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ namespace MISC {
2626

2727
using namespace SBG::LIB;
2828

29-
bool eqId(const Map &sbgmap) { return sbgmap.isId(); }
30-
3129
DSBG buildSCCFromMatching(const BFSMatching &match)
3230
{
3331
const PWMapAF &fact = match.fact();
@@ -74,9 +72,7 @@ DSBG buildSortFromSCC(const SCC &scc, const PWMap &rmap)
7472
mapD = mapD.compact();
7573

7674
PWMap aux_rmap = rmap.compact();
77-
PWMap reps_rmap = aux_rmap.filterMap([](const Map &sbgmap) {
78-
return eqId(sbgmap);
79-
});
75+
PWMap reps_rmap = aux_rmap.restrict(aux_rmap.fixedPoints());
8076
Set V = reps_rmap.dom();
8177

8278
PWMap Vmap = dsbg.Vmap().restrict(V);

sbg/map.cpp

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,7 @@ std::ostream &operator<<(std::ostream &out, const Map &m)
136136

137137
// Map operations --------------------------------------------------------------
138138

139-
std::size_t Map::arity() const
140-
{
141-
if (dom_.isEmpty())
142-
return 0;
143-
144-
return dom_.arity();
145-
}
139+
std::size_t Map::arity() const { return exp_.arity(); }
146140

147141
bool Map::isEmpty() const { return dom_.isEmpty(); }
148142

@@ -217,6 +211,26 @@ Map Map::composition(const Map &other) const
217211
return Map(fact_, res_dom, res_exp);
218212
}
219213

214+
Set Map::fixedPoints() const
215+
{
216+
Interval univ_one_dim(0, 1, Inf);
217+
SetPiece allowed;
218+
for (unsigned int j = 0; j < arity(); ++j) {
219+
LExp jth = exp_[j];
220+
221+
if (jth.isId())
222+
allowed.emplaceBack(univ_one_dim);
223+
224+
else if (jth.isConstant())
225+
allowed.emplaceBack(Interval(jth.offset().toNat()));
226+
227+
else
228+
return fact_.createSet();
229+
}
230+
231+
return dom_.intersection(fact_.createSet(allowed));
232+
}
233+
220234
// Extra operations ------------------------------------------------------------
221235

222236
Map Map::minInv() const

sbg/map.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ struct Map {
131131
*/
132132
Map composition(const Map &other) const;
133133

134+
/**
135+
* @brief Calculates the set of elements in the domain such that f(x) = x.
136+
*/
137+
Set fixedPoints() const;
138+
134139
// Extra operations ----------------------------------------------------------
135140

136141
/**

sbg/multidim_inter.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -185,17 +185,19 @@ MaybeMDI MultiDimInter::compact(const MultiDimInter &other) const
185185
unsigned int j = 0;
186186
for (; j < arity(); ++j) {
187187
auto ith = operator[](j).compact(other[j]);
188-
if (ith) {
189-
res.emplaceBack(ith.value());
190-
++j;
191-
break;
192-
}
188+
if (operator[](j) == other[j])
189+
res.emplaceBack(operator[](j));
193190

194-
else if (operator[](j) != other[j])
195-
return {};
191+
else {
192+
if (ith) {
193+
res.emplaceBack(ith.value());
194+
++j;
195+
break;
196+
}
196197

197-
else
198-
res.emplaceBack(operator[](j));
198+
else
199+
return {};
200+
}
199201
}
200202

201203
for (; j < arity(); ++j) {

sbg/pw_map.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,7 @@ PWMapDelegPtr UnordPWMap::operator-(const PWMapDelegate &other) const
166166
if (isEmpty() || other.isEmpty())
167167
return res;
168168

169-
Interval all(0, 1, Inf);
170-
Set univ = fact_.createSet(SetPiece(arity(), all));
169+
Set univ = fact_.createSet(SetPiece(arity(), Interval(0, 1, Inf)));
171170

172171
UnordPWMapCRef othr = static_cast<UnordPWMapCRef>(other);
173172
for (const Map &m1 : pieces_) {
@@ -386,6 +385,16 @@ PWMapDelegPtr UnordPWMap::mapInf(unsigned int n) const
386385

387386
PWMapDelegPtr UnordPWMap::mapInf() const { return mapInf(0); }
388387

388+
Set UnordPWMap::fixedPoints() const
389+
{
390+
Set res = fact_.createSet();
391+
392+
for (const Map &m : pieces_)
393+
res = res.disjointCup(m.fixedPoints());
394+
395+
return res;
396+
}
397+
389398
// Extra operations ------------------------------------------------------------
390399

391400
PWMapDelegPtr UnordPWMap::concatenation(const PWMapDelegate &other) const
@@ -838,6 +847,8 @@ PWMap PWMap::mapInf(unsigned int n) const { return delegate_->mapInf(n); }
838847

839848
PWMap PWMap::mapInf() const { return delegate_->mapInf(); }
840849

850+
Set PWMap::fixedPoints() const { return delegate_->fixedPoints(); }
851+
841852
PWMap PWMap::concatenation(const PWMap &other) const
842853
{
843854
return delegate_->concatenation(*other.delegate_);

sbg/pw_map.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ struct PWMapDelegate {
163163
*/
164164
virtual PWMapDelegPtr mapInf() const = 0;
165165

166+
/**
167+
* @brief Calculates the set of elements in the domain such that f(x) = x.
168+
*/
169+
virtual Set fixedPoints() const = 0;
170+
166171
// Extra operations ----------------------------------------------------------
167172

168173
/**
@@ -317,6 +322,7 @@ struct UnordPWMap : public PWMapDelegate {
317322

318323
PWMapDelegPtr mapInf(unsigned int n) const override;
319324
PWMapDelegPtr mapInf() const override;
325+
Set fixedPoints() const override;
320326

321327
// Extra operations ----------------------------------------------------------
322328

@@ -399,6 +405,7 @@ struct PWMap {
399405

400406
PWMap mapInf(unsigned int n) const;
401407
PWMap mapInf() const;
408+
Set fixedPoints() const;
402409

403410
// Extra operations ----------------------------------------------------------
404411

sbg/set.cpp

Lines changed: 53 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
1818
******************************************************************************/
1919

20+
#include <set>
21+
2022
#include "sbg/set.hpp"
2123

2224
namespace SBG {
@@ -234,8 +236,8 @@ SetDelegPtr UnorderedSet::complementAtom() const
234236
for (const Interval &i : mdi) dense_mdi.emplaceBack(Interval(i.begin(), 1, i.end()));
235237
SetPiece during_mdi = dense_mdi;
236238

237-
Interval univ(0, 1, Inf);
238-
SetPiece all(mdi.arity(), univ);
239+
Interval univ_one_dim(0, 1, Inf);
240+
SetPiece univ(mdi.arity(), univ_one_dim);
239241

240242
unsigned int dim = 0;
241243
for (const Interval &i : mdi) {
@@ -245,9 +247,9 @@ SetDelegPtr UnorderedSet::complementAtom() const
245247
if (i.begin() != 0) {
246248
Interval i_res(0, 1, i.begin() - 1);
247249
if (!i_res.isEmpty()) {
248-
all[dim] = i_res;
249-
c.push_back(all);
250-
all[dim] = univ;
250+
univ[dim] = i_res;
251+
c.push_back(univ);
252+
univ[dim] = univ_one_dim;
251253
}
252254
}
253255

@@ -268,12 +270,12 @@ SetDelegPtr UnorderedSet::complementAtom() const
268270
if (i.end() < Inf) {
269271
Interval i_res(i.end() + 1, 1, Inf);
270272
if (!i_res.isEmpty()) {
271-
all[dim] = i_res;
272-
c.push_back(all);
273-
all[dim] = univ;
273+
univ[dim] = i_res;
274+
c.push_back(univ);
275+
univ[dim] = univ_one_dim;
274276
}
275277
}
276-
all[dim] = dense_mdi[dim];
278+
univ[dim] = dense_mdi[dim];
277279
during_mdi[dim] = i;
278280

279281
// Insert results of current dim
@@ -289,15 +291,17 @@ SetDelegPtr UnorderedSet::complement() const
289291
{
290292
SetDelegPtr res = std::make_unique<UnorderedSet>(MDIUnordSet());
291293

292-
auto first_it = pieces_.begin();
293-
SetPiece first = *first_it;
294-
res = std::move(UnorderedSet(first).complementAtom());
294+
if (!isEmpty()) {
295+
auto first_it = pieces_.begin();
296+
SetPiece first = *first_it;
297+
res = std::move(UnorderedSet(first).complementAtom());
295298

296-
++first_it;
297-
MDIUnordSet second(first_it, pieces_.end());
298-
for (const SetPiece &mdi : second) {
299-
SetDelegPtr c = UnorderedSet(mdi).complementAtom();
300-
res = std::move(res->intersection(*c));
299+
++first_it;
300+
MDIUnordSet second(first_it, pieces_.end());
301+
for (const SetPiece &mdi : second) {
302+
SetDelegPtr c = UnorderedSet(mdi).complementAtom();
303+
res = std::move(res->intersection(*c));
304+
}
301305
}
302306

303307
return res;
@@ -355,27 +359,38 @@ SetDelegPtr UnorderedSet::offset(const MD_NAT &off) const
355359

356360
SetDelegPtr UnorderedSet::compact() const
357361
{
358-
// New idea TODO
359-
// MDIUnordSet old_compact = pieces_, compact = old_compact;
360-
// SetPiece ith(compact.begin());
361-
// do {
362-
// for (const SetPiece &mdi : compact) {
363-
// auto ith_compact = ith.compact(mdi);
364-
// if (ith_compact)
365-
// ith = ith_compact.value();
366-
// }
367-
// MDIUnordSet aux_compact = compact;
368-
// for (const SetPiece &mdi : aux_compact) {
369-
// if (!ith.intersection(mdi).isEmpty())
370-
// compact.erase(mdi);
371-
// }
372-
// compact.emplace(ith);
373-
//} while (old_compact != compact);
374-
375-
// std::shared_ptr<UnorderedSet> res = std::make_shared<UnorderedSet>();
376-
// res->pieces_ = pieces_.compact();
377-
378-
return std::make_unique<UnorderedSet>(pieces_);
362+
MDIUnordSet res;
363+
364+
if (!isEmpty()) {
365+
std::set<SetPiece> prev(pieces_.begin(), pieces_.end()), actual = prev;
366+
do {
367+
prev = actual;
368+
actual = std::set<SetPiece>();
369+
370+
std::set<SetPiece>::iterator ith = prev.begin(), last = prev.end();
371+
std::set<SetPiece> to_erase;
372+
for (; ith != last; ++ith) {
373+
SetPiece ith_compact = *ith;
374+
std::set<SetPiece>::iterator next = ith;
375+
++next;
376+
for (; next != last; ++next) {
377+
MaybeMDI new_compact = ith_compact.compact(*next);
378+
if (new_compact) {
379+
ith_compact = new_compact.value();
380+
to_erase.insert(*next);
381+
}
382+
}
383+
384+
if (to_erase.find(ith_compact) == to_erase.end())
385+
actual.insert(ith_compact);
386+
}
387+
} while (actual != prev);
388+
389+
for (const SetPiece &mdi : actual)
390+
res.push_back(mdi);
391+
}
392+
393+
return std::make_unique<UnorderedSet>(res);
379394
}
380395

381396
////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)