Skip to content

Commit 1e5add5

Browse files
committed
<refactor>[SBG TS]: added TSCImplementation, deleted TopologicalSorting::_kind
1 parent 49ad938 commit 1e5add5

8 files changed

Lines changed: 111 additions & 145 deletions

File tree

algorithms/sorting/topological/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ target_sources(
33
PRIVATE
44
min_vertex_ts.cpp
55
topological_sorting.cpp
6-
ts_fact.cpp
6+
ts_impl.cpp
77
)

algorithms/sorting/topological/min_vertex_ts.cpp

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
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

3735
MinVertexTS::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)
6663
PWMap 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

algorithms/sorting/topological/min_vertex_ts.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class MinVertexTS {
6262
Set _same_SV;
6363
Set _independent;
6464
Set _visitedSV;
65+
unsigned int _max_repetition_depth;
6566
};
6667

6768
} // namespace detail

algorithms/sorting/topological/topological_sorting.cpp

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,41 +18,21 @@
1818
******************************************************************************/
1919

2020
#include "algorithms/sorting/topological/topological_sorting.hpp"
21+
#include "algorithms/sorting/topological/ts_impl.hpp"
2122
#include "util/debug.hpp"
2223
#include "util/time_profiler.hpp"
2324

2425
namespace SBG {
2526

2627
namespace LIB {
2728

28-
////////////////////////////////////////////////////////////////////////////////
29-
// Topological Sorting Algorithm implementations -------------------------------
30-
////////////////////////////////////////////////////////////////////////////////
31-
32-
std::ostream& operator<<(std::ostream& out, const TSKind kind)
33-
{
34-
switch (kind) {
35-
case TSKind::kMinVertex: {
36-
out << "minimum vertex";
37-
break;
38-
}
39-
40-
default: {
41-
Util::ERROR("TSKind::operator<<: unsupported topological sorting "
42-
, "algorithm implementation");
43-
break;
44-
}
45-
}
46-
47-
return out;
48-
}
49-
5029
////////////////////////////////////////////////////////////////////////////////
5130
// Topological Sorting Algorithm Interface -------------------------------------
5231
////////////////////////////////////////////////////////////////////////////////
5332

54-
TopologicalSorting::TopologicalSorting(TSKind kind) : _kind(kind), _impl()
33+
TopologicalSorting::TopologicalSorting() : _impl()
5534
{
35+
TSKind kind = TS_IMPL.kind();
5636
switch (kind) {
5737
case TSKind::kMinVertex: {
5838
_impl = detail::MinVertexTS{};

algorithms/sorting/topological/topological_sorting.hpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,17 @@ using TSImpl = std::variant<MinVertexTS>;
4141

4242
} // namespace detail
4343

44-
enum class TSKind { kMinVertex };
45-
46-
std::ostream& operator<<(std::ostream& out, const TSKind kind);
47-
4844
////////////////////////////////////////////////////////////////////////////////
4945
// Topological Sorting Algorithm -----------------------------------------------
5046
////////////////////////////////////////////////////////////////////////////////
5147

5248
class TopologicalSorting {
5349
public:
54-
TopologicalSorting(TSKind kind);
50+
TopologicalSorting();
5551

5652
PWMap calculate(const DirectedSBG& dsbg, const PWMap& pmap);
5753

5854
private:
59-
TSKind _kind;
6055
detail::TSImpl _impl;
6156
};
6257

algorithms/sorting/topological/ts_fact.hpp

Lines changed: 0 additions & 72 deletions
This file was deleted.

algorithms/sorting/topological/ts_fact.cpp renamed to algorithms/sorting/topological/ts_impl.cpp

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,60 +17,47 @@
1717
1818
******************************************************************************/
1919

20-
#include "algorithms/sorting/topological/ts_fact.hpp"
20+
#include "algorithms/sorting/topological/ts_impl.hpp"
2121
#include "util/debug.hpp"
2222

2323
namespace SBG {
2424

2525
namespace LIB {
2626

2727
////////////////////////////////////////////////////////////////////////////////
28-
// Minimum Vertex Topological Sorting Factory ----------------------------------
28+
// Topological Sorting implementations
2929
////////////////////////////////////////////////////////////////////////////////
3030

31-
TopologicalSorting MinVertexTSFact::createTSAlgorithm() const
31+
std::ostream& operator<<(std::ostream& out, const TSKind kind)
3232
{
33-
return TopologicalSorting{TSKind::kMinVertex};
34-
}
35-
36-
////////////////////////////////////////////////////////////////////////////////
37-
// Factory for clients ---------------------------------------------------------
38-
////////////////////////////////////////////////////////////////////////////////
39-
40-
TSFactory::TSFactory()
41-
: _kind(TSKind::kMinVertex), _impl(MinVertexTSFact{}) {}
42-
43-
TSFactory& TSFactory::instance()
44-
{
45-
static TSFactory _instance;
46-
return _instance;
47-
}
48-
49-
const TSKind& TSFactory::kind() const { return _kind; }
50-
51-
void TSFactory::set_ts_fact(TSKind kind)
52-
{
53-
_kind = kind;
5433
switch (kind) {
5534
case TSKind::kMinVertex: {
56-
_impl = MinVertexTSFact{};
35+
out << "minimum vertex";
5736
break;
5837
}
5938

6039
default: {
61-
Util::ERROR("TSFactory::set_ts_fact: unsupported topological sorting ",
62-
"implementation");
40+
Util::ERROR("TSKind::operator<<: unsupported topological sorting "
41+
, "algorithm implementation");
6342
break;
6443
}
6544
}
45+
46+
return out;
6647
}
6748

68-
TopologicalSorting TSFactory::createTSAlgorithm() const
49+
TSImplementation::TSImplementation() : _kind(TSKind::kMinVertex) {}
50+
51+
TSImplementation& TSImplementation::instance()
6952
{
70-
return std::visit([](const auto& a) { return a.createTSAlgorithm(); }
71-
, _impl);
53+
static TSImplementation _instance;
54+
return _instance;
7255
}
7356

57+
const TSKind& TSImplementation::kind() const { return _kind; }
58+
59+
void TSImplementation::set_ts_fact(TSKind kind) { _kind = kind; }
60+
7461
} // namespace LIB
7562

7663
} // namespace SBG
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/** @file ts_impl.hpp
2+
3+
@brief <b>Topological Sorting Algorithm Implementation</b>
4+
5+
<hr>
6+
7+
This file is part of Set--Based Graph Library.
8+
9+
SBG Library is free software: you can redistribute it and/or modify
10+
it under the terms of the GNU General Public License as published by
11+
the Free Software Foundation, either version 3 of the License, or
12+
(at your option) any later version.
13+
14+
SBG Library is distributed in the hope that it will be useful,
15+
but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
GNU General Public License for more details.
18+
19+
You should have received a copy of the GNU General Public License
20+
along with SBG Library. If not, see <http://www.gnu.org/licenses/>.
21+
22+
******************************************************************************/
23+
24+
#ifndef SBGRAPH_ALGORITHMS_SORTING_TOPOLOGICAL_TS_IMPL_HPP_
25+
#define SBGRAPH_ALGORITHMS_SORTING_TOPOLOGICAL_TS_IMPL_HPP_
26+
27+
#include <iosfwd>
28+
29+
namespace SBG {
30+
31+
namespace LIB {
32+
33+
////////////////////////////////////////////////////////////////////////////////
34+
// Topological Sorting implementations -----------------------------------------
35+
////////////////////////////////////////////////////////////////////////////////
36+
37+
enum class TSKind { kMinVertex };
38+
39+
std::ostream& operator<<(std::ostream& out, const TSKind kind);
40+
41+
#define TS_IMPL TSImplementation::instance()
42+
43+
/**
44+
* @brief Singleton that keeps record of the chosen sorting implementation.
45+
*/
46+
class TSImplementation {
47+
public:
48+
~TSImplementation() = default;
49+
50+
static TSImplementation& instance();
51+
52+
const TSKind& kind() const;
53+
void set_ts_fact(TSKind kind);
54+
55+
private:
56+
TSImplementation();
57+
58+
TSKind _kind;
59+
};
60+
61+
} // namespace LIB
62+
63+
} // namespace SBG
64+
65+
#endif // SBGRAPH_ALGORITHMS_SORTING_TOPOLOGICAL_TS_IMPL_HPP_

0 commit comments

Comments
 (0)