Skip to content

Commit de7bbd7

Browse files
committed
Added Overload pattern to utils
1 parent 2e58221 commit de7bbd7

5 files changed

Lines changed: 56 additions & 70 deletions

File tree

eval/visitors/func_evaluator.cpp

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "sbg/set.hpp"
3636
#include "sbg/pw_map.hpp"
3737
#include "util/debug.hpp"
38+
#include "util/defs.hpp"
3839

3940
namespace SBG {
4041

@@ -130,7 +131,7 @@ ExprBaseType BuiltInOperators::oppositeEvaluator(const EBTList& args)
130131
Util::ERROR_UNLESS(args.size() == 1
131132
, "oppositeEvaluator: wrong number of arguments\n");
132133

133-
auto opposite_evaluator = Overload {
134+
auto opposite_evaluator = Util::Overload {
134135
[](LIB::NAT a)
135136
{
136137
return ExprBaseType{LIB::RATIONAL{static_cast<LIB::INT>(a), -1}};
@@ -151,7 +152,7 @@ ExprBaseType BuiltInOperators::cardinalEvaluator(const EBTList& args)
151152
Util::ERROR_UNLESS(args.size() == 1
152153
, "cardinalEvaluator: wrong number of arguments\n");
153154

154-
const auto cardinal_evaluator = Overload {
155+
const auto cardinal_evaluator = Util::Overload {
155156
[](LIB::Set a) { return (LIB::NAT) a.cardinal(); },
156157
[](auto a) {
157158
Util::ERROR("cardinalEvaluator: wrong argument ", a, " for #\n");
@@ -166,7 +167,7 @@ ExprBaseType BuiltInOperators::complementEvaluator(const EBTList& args)
166167
Util::ERROR_UNLESS(args.size() == 1
167168
, "complementEvaluator: wrong number of arguments\n");
168169

169-
const auto complement_evaluator = Overload {
170+
const auto complement_evaluator = Util::Overload {
170171
[](LIB::Set a) { return ExprBaseType{a.complement()}; },
171172
[](auto a) {
172173
Util::ERROR("complementEvaluator: wrong argument ", a
@@ -182,7 +183,7 @@ ExprBaseType BuiltInOperators::addEvaluator(const EBTList& args)
182183
Util::ERROR_UNLESS(args.size() == 2
183184
, "addEvaluator: wrong number of arguments\n");
184185

185-
const auto add_evaluator = Overload {
186+
const auto add_evaluator = Util::Overload {
186187
[](LIB::NAT a, LIB::NAT b) { return ExprBaseType{a + b}; },
187188
[](LIB::MD_NAT a, LIB::MD_NAT b) { return ExprBaseType{a + b}; },
188189
[](LIB::RATIONAL a, LIB::RATIONAL b) { return ExprBaseType{a + b}; },
@@ -209,7 +210,7 @@ ExprBaseType BuiltInOperators::subEvaluator(const EBTList& args)
209210
Util::ERROR_UNLESS(args.size() == 2
210211
, "subEvaluator: wrong number of arguments\n");
211212

212-
const auto sub_evaluator = Overload {
213+
const auto sub_evaluator = Util::Overload {
213214
[](LIB::NAT a, LIB::NAT b) {
214215
if (a > b) {
215216
return ExprBaseType{LIB::NAT{a - b}};
@@ -240,7 +241,7 @@ ExprBaseType BuiltInOperators::multEvaluator(const EBTList& args)
240241
Util::ERROR_UNLESS(args.size() == 2
241242
, "multEvaluator: wrong number of arguments\n");
242243

243-
const auto mult_evaluator = Overload {
244+
const auto mult_evaluator = Util::Overload {
244245
[](LIB::NAT a, LIB::NAT b)
245246
{
246247
return ExprBaseType{LIB::RATIONAL{static_cast<LIB::INT>(a*b)}};
@@ -268,7 +269,7 @@ ExprBaseType BuiltInOperators::eqEvaluator(const EBTList& args)
268269
Util::ERROR_UNLESS(args.size() == 2
269270
, "eqEvaluator: wrong number of arguments\n");
270271

271-
const auto eq_evaluator = Overload {
272+
const auto eq_evaluator = Util::Overload {
272273
[](LIB::MD_NAT a, LIB::MD_NAT b) { return a == b; },
273274
[](LIB::RATIONAL a, LIB::RATIONAL b) { return a == b; },
274275
[](LIB::Set a, LIB::Set b) { return a == b; },
@@ -289,7 +290,7 @@ ExprBaseType BuiltInOperators::lessEvaluator(const EBTList& args)
289290
Util::ERROR_UNLESS(args.size() == 2
290291
, "lessEvaluator: wrong number of arguments\n");
291292

292-
const auto less_evaluator = Overload {
293+
const auto less_evaluator = Util::Overload {
293294
[](LIB::MD_NAT a, LIB::MD_NAT b) { return a < b; },
294295
[](LIB::RATIONAL a, LIB::RATIONAL b) { return a < b; },
295296
[](auto a, auto b) {
@@ -306,7 +307,7 @@ ExprBaseType BuiltInOperators::capEvaluator(const EBTList& args)
306307
Util::ERROR_UNLESS(args.size() == 2
307308
, "capEvaluator: wrong number of arguments\n");
308309

309-
const auto cap_evaluator = Overload{
310+
const auto cap_evaluator = Util::Overload{
310311
[](LIB::Set a, LIB::Set b) { return ExprBaseType{a.intersection(b)}; },
311312
[](auto a, auto b) {
312313
Util::ERROR("capEvaluator: wrong arguments ", a, ", ", b
@@ -322,7 +323,7 @@ ExprBaseType BuiltInOperators::cupEvaluator(const EBTList& args)
322323
Util::ERROR_UNLESS(args.size() == 2
323324
, "cupEvaluator: wrong number of arguments\n");
324325

325-
const auto cup_evaluator = Overload{
326+
const auto cup_evaluator = Util::Overload{
326327
[](LIB::Set a, LIB::Set b) { return ExprBaseType{a.cup(b)}; },
327328
[](auto a, auto b) {
328329
Util::ERROR("cupEvaluator: wrong arguments ", a, ", ", b
@@ -338,7 +339,7 @@ ExprBaseType BuiltInOperators::diffEvaluator(const EBTList& args)
338339
Util::ERROR_UNLESS(args.size() == 2
339340
, "diffEvaluator: wrong number of arguments\n");
340341

341-
const auto diff_evaluator = Overload{
342+
const auto diff_evaluator = Util::Overload{
342343
[](LIB::Set a, LIB::Set b) { return ExprBaseType{a.difference(b)}; },
343344
[](auto a, auto b) {
344345
Util::ERROR("diffEvaluator: wrong arguments ", a, ", ", b
@@ -442,7 +443,7 @@ ExprBaseType BuiltInFunctions::emptyEvaluator(const EBTList& args)
442443
Util::ERROR_UNLESS(args.size() == 1
443444
, "emptyEvaluator: wrong number of arguments\n");
444445

445-
const auto empty_evaluator = Overload {
446+
const auto empty_evaluator = Util::Overload {
446447
[](LIB::Set a) { return a.isEmpty(); },
447448
[](auto a) {
448449
Util::ERROR("emptyEvaluator: wrong argument ", a, " for isEmpty\n");
@@ -457,7 +458,7 @@ ExprBaseType BuiltInFunctions::minEvaluator(const EBTList& args)
457458
Util::ERROR_UNLESS(args.size() == 1
458459
, "minEvaluator: wrong number of arguments\n");
459460

460-
const auto min_evaluator = Overload {
461+
const auto min_evaluator = Util::Overload {
461462
[](LIB::Set a) { return a.minElem(); },
462463
[](auto a) {
463464
Util::ERROR("minEvaluator: wrong argument ", a, " for minElem\n");
@@ -472,7 +473,7 @@ ExprBaseType BuiltInFunctions::maxEvaluator(const EBTList& args)
472473
Util::ERROR_UNLESS(args.size() == 1
473474
, "maxEvaluator: wrong number of arguments\n");
474475

475-
const auto max_evaluator = Overload {
476+
const auto max_evaluator = Util::Overload {
476477
[](LIB::Set a) { return a.maxElem(); },
477478
[](auto a) {
478479
Util::ERROR("maxEvaluator: wrong argument ", a, " for maxElem\n");
@@ -487,7 +488,7 @@ ExprBaseType BuiltInFunctions::restrictEvaluator(const EBTList& args)
487488
Util::ERROR_UNLESS(args.size() == 2
488489
, "restrictEvaluator: wrong number of arguments\n");
489490

490-
const auto restrict_evaluator = Overload {
491+
const auto restrict_evaluator = Util::Overload {
491492
[](LIB::PWMap a, LIB::Set b) { return ExprBaseType{a.restrict(b)}; },
492493
[](auto a, auto b) {
493494
Util::ERROR("restrictEvaluator: wrong arguments ", a, ", ", b
@@ -503,7 +504,7 @@ ExprBaseType BuiltInFunctions::composeEvaluator(const EBTList& args)
503504
Util::ERROR_UNLESS(args.size() == 2
504505
, "composeEvaluator: wrong number of arguments\n");
505506

506-
const auto compose_evaluator = Overload {
507+
const auto compose_evaluator = Util::Overload {
507508
[](LIB::Expression a, LIB::Expression b)
508509
{
509510
return ExprBaseType{a.composition(b)};
@@ -524,7 +525,7 @@ ExprBaseType BuiltInFunctions::inverseEvaluator(const EBTList& args)
524525
Util::ERROR_UNLESS(args.size() == 1
525526
, "inverseEvaluator: wrong number of arguments\n");
526527

527-
const auto inverse_evaluator = Overload {
528+
const auto inverse_evaluator = Util::Overload {
528529
[](LIB::Expression a) { return ExprBaseType{a.inverse()}; },
529530
[](LIB::PWMap a) { return ExprBaseType{a.inverse()}; },
530531
[](auto a) {
@@ -538,7 +539,7 @@ ExprBaseType BuiltInFunctions::inverseEvaluator(const EBTList& args)
538539
ExprBaseType BuiltInFunctions::imageEvaluator(const EBTList& args)
539540
{
540541
if (args.size() == 1) {
541-
const auto image_evaluator = Overload {
542+
const auto image_evaluator = Util::Overload {
542543
[](LIB::Map a) { return ExprBaseType{a.image()}; },
543544
[](LIB::PWMap a) { return ExprBaseType{a.image()}; },
544545
[](auto a) {
@@ -549,7 +550,7 @@ ExprBaseType BuiltInFunctions::imageEvaluator(const EBTList& args)
549550
return std::visit(image_evaluator, args[0]);
550551
}
551552
else if (args.size() == 2) {
552-
const auto image2_evaluator = Overload {
553+
const auto image2_evaluator = Util::Overload {
553554
[](LIB::Set a, LIB::Map b) { return ExprBaseType{b.image(a)}; },
554555
[](LIB::Set a, LIB::PWMap b) { return ExprBaseType{b.image(a)}; },
555556
[](auto a, auto b) {
@@ -571,7 +572,7 @@ ExprBaseType BuiltInFunctions::preImageEvaluator(const EBTList& args)
571572
Util::ERROR_UNLESS(args.size() == 2
572573
, "preImageEvaluator: wrong number of arguments\n");
573574

574-
const auto pre_image_evaluator = Overload {
575+
const auto pre_image_evaluator = Util::Overload {
575576
[](LIB::Set a, LIB::Map b) { return ExprBaseType{b.preImage(a)}; },
576577
[](LIB::Set a, LIB::PWMap b) { return ExprBaseType{b.preImage(a)}; },
577578
[](auto a, auto b) {
@@ -588,7 +589,7 @@ ExprBaseType BuiltInFunctions::domEvaluator(const EBTList& args)
588589
Util::ERROR_UNLESS(args.size() == 1
589590
, "domEvaluator: wrong number of arguments\n");
590591

591-
const auto dom_evaluator = Overload {
592+
const auto dom_evaluator = Util::Overload {
592593
[](LIB::PWMap a) { return ExprBaseType{a.domain()}; },
593594
[](auto a) {
594595
Util::ERROR("domEvaluator: wrong arguments ", a, "for dom\n");
@@ -603,7 +604,7 @@ ExprBaseType BuiltInFunctions::combineEvaluator(const EBTList& args)
603604
Util::ERROR_UNLESS(args.size() == 2
604605
, "combineEvaluator: wrong number of arguments\n");
605606

606-
const auto combine_evaluator = Overload {
607+
const auto combine_evaluator = Util::Overload {
607608
[](LIB::PWMap a, LIB::PWMap b) { return ExprBaseType{a.combine(b)}; },
608609
[](auto a, auto b) {
609610
Util::ERROR("combineEvaluator: wrong arguments ", a, ", ", b
@@ -619,7 +620,7 @@ ExprBaseType BuiltInFunctions::minMapEvaluator(const EBTList& args)
619620
Util::ERROR_UNLESS(args.size() == 2
620621
, "minMapEvaluator: wrong number of arguments\n");
621622

622-
const auto min_map_evaluator = Overload {
623+
const auto min_map_evaluator = Util::Overload {
623624
[](LIB::PWMap a, LIB::PWMap b) { return ExprBaseType{a.min(b)}; },
624625
[](auto a, auto b) {
625626
Util::ERROR("minMapEvaluator: wrong arguments ", a, ", ", b
@@ -636,7 +637,7 @@ ExprBaseType BuiltInFunctions::minMapEvaluator(const EBTList& args)
636637
// Util::ERROR_UNLESS(args.size() == 1
637638
// , "reduceEvaluator: wrong number of arguments\n");
638639
//
639-
// const auto reduce_evaluator = Overload {
640+
// const auto reduce_evaluator = Util::Overload {
640641
// [](LIB::PWMap a) { return ExprBaseType{a.reduce()}; },
641642
// [](auto a) {
642643
// Util::ERROR("reduceEvaluator: wrong argument ", a, " for reduce\n");
@@ -651,7 +652,7 @@ ExprBaseType BuiltInFunctions::minAdjEvaluator(const EBTList& args)
651652
Util::ERROR_UNLESS(args.size() == 2
652653
, "minAdjEvaluator: wrong number of arguments\n");
653654

654-
const auto min_adj_evaluator = Overload {
655+
const auto min_adj_evaluator = Util::Overload {
655656
[](LIB::PWMap a, LIB::PWMap b) { return ExprBaseType{a.minAdj(b)}; },
656657
[](auto a, auto b) {
657658
Util::ERROR("minAdjEvaluator: wrong arguments ", a, ", ", b
@@ -667,7 +668,7 @@ ExprBaseType BuiltInFunctions::mapInfEvaluator(const EBTList& args)
667668
Util::ERROR_UNLESS(args.size() == 1
668669
, "mapInfEvaluator: wrong number of arguments\n");
669670

670-
const auto inf_evaluator = Overload {
671+
const auto inf_evaluator = Util::Overload {
671672
[](LIB::PWMap a) { return ExprBaseType{a.mapInf()}; },
672673
[](auto a) {
673674
Util::ERROR("mapInfEvaluator: wrong argument ", a, " for mapInf\n");
@@ -684,7 +685,7 @@ ExprBaseType BuiltInFunctions::connectedEvaluator(const EBTList& args)
684685
Util::ERROR_UNLESS(args.size() == 1
685686
, "connectedEvaluator: wrong number of arguments\n");
686687

687-
const auto connected_evaluator = Overload {
688+
const auto connected_evaluator = Util::Overload {
688689
[](LIB::SBG a) { return ExprBaseType{connectedComponents(a)}; },
689690
[](auto a) {
690691
Util::ERROR("connectedEvaluator: wrong argument ", a, " for CC\n");
@@ -700,7 +701,7 @@ ExprBaseType BuiltInFunctions::matchingEvaluator(const EBTList& args)
700701
, "matchingEvaluator: wrong number of arguments\n");
701702

702703
LIB::Matching match_impl = LIB::MATCH_FACT.createMatchAlgorithm();
703-
const auto matching_evaluator = Overload {
704+
const auto matching_evaluator = Util::Overload {
704705
[&match_impl](LIB::BipartiteSBG a, LIB::NAT b) {
705706
// TODO return ExprBaseType{match_impl.calculate(a.copy(b))};
706707
return ExprBaseType{match_impl.calculate(a)};
@@ -724,7 +725,7 @@ ExprBaseType BuiltInFunctions::sccEvaluator(const EBTList& args)
724725
, "sccEvaluator: wrong number of arguments\n");
725726

726727
LIB::SCC scc_impl = LIB::SCC_FACT.createSCCAlgorithm();
727-
const auto scc_evaluator = Overload {
728+
const auto scc_evaluator = Util::Overload {
728729
[&scc_impl](LIB::DirectedSBG a) {
729730
return ExprBaseType{scc_impl.calculate(a).rmap()};
730731
},
@@ -743,7 +744,7 @@ ExprBaseType BuiltInFunctions::topoSortEvaluator(const EBTList& args)
743744
, "topoSortEvaluator: wrong number of arguments\n");
744745
745746
LIB::TopoSort ts_impl = LIB::TS_FACT.createTSAlgorithm();
746-
const auto ts_evaluator = Overload {
747+
const auto ts_evaluator = Util::Overload {
747748
[&ts_impl](LIB::DirectedSBG a) {
748749
return ExprBaseType{ts_impl.calculate(a)};
749750
},
@@ -761,7 +762,7 @@ ExprBaseType BuiltInFunctions::cutVertexEvaluator(const EBTList& args)
761762
, "cutVertexEvaluator: wrong number of arguments\n");
762763
763764
LIB::CutVertex cv_impl = LIB::CV_FACT.createCVAlgorithm();
764-
const auto cv_evaluator = Overload {
765+
const auto cv_evaluator = Util::Overload {
765766
[&cv_impl](LIB::DirectedSBG a) {
766767
return ExprBaseType{cv_impl.calculate(a)};
767768
},
@@ -781,7 +782,7 @@ ExprBaseType BuiltInFunctions::matchSCCEvaluator(const EBTList& args)
781782

782783
LIB::Matching match_impl = LIB::MATCH_FACT.createMatchAlgorithm();
783784
LIB::SCC scc_impl = LIB::SCC_FACT.createSCCAlgorithm();
784-
const auto match_scc_evaluator = Overload {
785+
const auto match_scc_evaluator = Util::Overload {
785786
[&match_impl, &scc_impl](LIB::BipartiteSBG a, LIB::NAT b) {
786787
// TODO LIB::MatchData match_result = match_impl.calculate(a.copy(b));
787788
LIB::MatchData match_result = match_impl.calculate(a);
@@ -807,7 +808,7 @@ ExprBaseType BuiltInFunctions::matchSCCEvaluator(const EBTList& args)
807808
/*
808809
ExprBaseType BuiltInFunctions::matchSCCTSEvaluator(const EBTList& args)
809810
{
810-
const auto match_scc_ts_evaluator = Overload {
811+
const auto match_scc_ts_evaluator = Util::Overload {
811812
[](LIB::SBG a, LIB::NAT b, bool c) {
812813
LIB::BFSMatching match(a.copy(b), c);
813814
LIB::Set match_res = match.calculate().matched_edges();

eval/visitors/func_evaluator.hpp

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,26 +36,6 @@ namespace Eval {
3636

3737
namespace detail {
3838

39-
////////////////////////////////////////////////////////////////////////////////
40-
// Overload pattern ------------------------------------------------------------
41-
////////////////////////////////////////////////////////////////////////////////
42-
43-
/**
44-
* @brief Provides in-place lambdas for visitation for the different
45-
* operations. These are needed because different structures share the same
46-
* functions (for example, isEmpty can be applied to intervals, sets, etc.).
47-
*/
48-
49-
template<class... Ts>
50-
class Overload : Ts... {
51-
public:
52-
using Ts::operator()...;
53-
Overload(Ts... ts) : Ts(ts)... {};
54-
};
55-
56-
template<class... Ts>
57-
Overload(Ts...) -> Overload<Ts...>;
58-
5939
////////////////////////////////////////////////////////////////////////////////
6040
// Built-in Operators ----------------------------------------------------------
6141
////////////////////////////////////////////////////////////////////////////////

sbg/map_detail.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ CompactSetImpl compactImage(const CompactSetImpl& s, const Expr& expr
9191

9292
Set MapDetail::image(const Set& s, const Expression& expr)
9393
{
94-
auto image_evaluator = Overload {
94+
auto image_evaluator = Util::Overload {
9595
[&](const UnorderedSet& a)
9696
{
9797
return Set{detail::compactImage<UnorderedSet, MultiDimInter
@@ -154,7 +154,7 @@ CompactSetImpl compactPreImage(const CompactSetImpl& s, const Expr& expr)
154154

155155
Set MapDetail::preImage(const Set& s, const Expression& expr)
156156
{
157-
auto pre_image_evaluator = Overload {
157+
auto pre_image_evaluator = Util::Overload {
158158
[&](const UnorderedSet& a)
159159
{
160160
return Set{detail::compactPreImage<UnorderedSet, MultiDimInter
@@ -264,7 +264,7 @@ Set MapDetail::lessImage(const Expression& expr1, const Expression& expr2)
264264
{
265265
Set result = SET_FACT.createSet();
266266

267-
auto less_image_evaluator = Overload {
267+
auto less_image_evaluator = Util::Overload {
268268
[&](UnorderedSet& a)
269269
{
270270
detail::lessImage<UnorderedSet, MultiDimInter, ExpressionImpl>(
@@ -438,7 +438,7 @@ MapVector MapDetail::reduce(const Map& m)
438438
return result;
439439
}
440440

441-
auto reduce_evaluator = Overload {
441+
auto reduce_evaluator = Util::Overload {
442442
[&](const UnorderedSet& a)
443443
{
444444
return MDICollectionReduce<UnorderedSet>(a, law._impl);

sbg/map_detail.hpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "sbg/linear_expr.hpp"
3535
#include "sbg/map.hpp"
3636
#include "sbg/multidim_inter.hpp"
37+
#include "util/defs.hpp"
3738

3839
namespace SBG {
3940

@@ -60,16 +61,6 @@ class MapDetail {
6061
, const ExpressionImpl& expr);
6162
};
6263

63-
template<class... Ts>
64-
class Overload : Ts... {
65-
public:
66-
using Ts::operator()...;
67-
Overload(Ts... ts) : Ts(ts)... {};
68-
};
69-
70-
template<class... Ts>
71-
Overload(Ts...) -> Overload<Ts...>;
72-
7364
} // namespace detail
7465

7566
} // namespace LIB

0 commit comments

Comments
 (0)