1919
2020#include " algorithms/sorting/topological/min_vertex_ts.hpp"
2121#include " sbg/natural.hpp"
22- #include " sbg/pwmap_fact.hpp"
23- #include " sbg/set_fact.hpp"
2422#include " util/debug.hpp"
2523#include " util/logger.hpp"
2624
@@ -35,9 +33,8 @@ namespace detail {
3533// //////////////////////////////////////////////////////////////////////////////
3634
3735MinVertexTS::MinVertexTS ()
38- : _smap(PWMAP_FACT .createPWMap()), _dsbg(), _visitedSV(SET_FACT .createSet())
39- , _priority(SET_FACT .createSet()), _same_SV(SET_FACT .createSet())
40- , _independent(SET_FACT .createSet()) {}
36+ : _smap(), _dsbg(), _visitedSV(), _priority(), _same_SV(), _independent()
37+ , _max_repetition_depth(0 ) {}
4138
4239/*
4340 * @brief Given any two vertices u and v of the repetition, it checks that any
@@ -66,21 +63,32 @@ void checkSorting(const PWMap& result, const DirectedSBG& dsbg)
6663PWMap MinVertexTS::repetition (const Set& init_V
6764 , const DirectedSBG& dsbg) const
6865{
69- PWMap result = PWMAP_FACT . createPWMap () ;
66+ PWMap result;
7067
7168 PWMap Vmap = dsbg.Vmap ();
7269 Set Vj = init_V;
7370 Set init_SV = Vmap.image (init_V);
71+ Expression final_expr;
7472 bool repetition = true ;
73+ unsigned int n = 0 ;
7574 do {
7675 PWMap jth_smap = _smap.restrict (Vj);
7776 Set V_plus = Vmap.preImage (Vmap.image (Vj)).difference (_smap.domain ());
78- result.emplace (V_plus, (*jth_smap.begin ()).law ());
77+ final_expr = (*jth_smap.begin ()).law ();
78+ result.emplace (V_plus, final_expr);
7979 Vj = _smap.image (Vj);
8080 repetition = !Vmap.image (Vj).intersection (init_SV).isEmpty ();
81- } while (!repetition);
82-
83- checkSorting (result, dsbg);
81+ ++n;
82+ } while (!repetition && n < _max_repetition_depth);
83+
84+ if (repetition) {
85+ Expression init_expr = (*_smap.restrict (init_V).begin ()).law ();
86+ if (init_expr == final_expr) {
87+ checkSorting (result, dsbg);
88+ }
89+ } else {
90+ result;
91+ }
8492
8593 return result;
8694}
@@ -114,22 +122,22 @@ PWMap MinVertexTS::calculate(const DirectedSBG& dsbg
114122
115123 _dsbg = dsbg;
116124
117- _smap = PWMAP_FACT . createPWMap () ;
125+ _smap = PWMap{} ;
118126
119127 if (dsbg.V ().isEmpty ()) {
120128 return _smap;
121129 }
122130
123131 _priority = _dsbg.V ();
124132 _same_SV = _dsbg.V ();
125- Set visited_SV = SET_FACT . createSet () ;
133+ Set visited_SV;
126134 Expression successor_expr{_dsbg.V ().arity (), 1 , 0 };
127135 MD_NAT vj;
128136 MD_NAT old_vj = _dsbg.V ().difference (_dsbg.mapD ().image ()).minElem ();
129137 do {
130138 // Find new minimum vertex, and add it to the sorting
131139 vj = getMinVertex ();
132- Set vj_set = SET_FACT . createSet (vj) ;
140+ Set vj_set{vj} ;
133141 successor_expr = Expression{vj, old_vj};
134142 _smap.emplace (vj_set, successor_expr);
135143
@@ -140,8 +148,10 @@ PWMap MinVertexTS::calculate(const DirectedSBG& dsbg
140148 PWMap smap_plus = repetition (vj_set, dsbg);
141149 _smap = std::move (smap_plus).combine (std::move (_smap));
142150 vj = _smap.domain ().difference (_smap.image ()).minElem ();
151+ _max_repetition_depth = 0 ;
143152 } else {
144153 _visitedSV = std::move (_visitedSV).disjointCup (Vmap.image (vj_set));
154+ _max_repetition_depth++;
145155 }
146156
147157 // Update values for new iteration
0 commit comments