Skip to content

Commit a7aa6bc

Browse files
committed
Delegation+AbsFact for Sets [unfinished]
1 parent b0b477e commit a7aa6bc

14 files changed

Lines changed: 1348 additions & 1161 deletions

parser/expr.cpp

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,7 @@ ExprRule<Iterator>::ExprRule(Iterator &it) :
305305

306306
// ------------ //
307307

308-
inter_times = interval[phx::push_back(qi::_val, qi::_1)]
309-
>> *(CARTPROD >> interval)[phx::push_back(qi::_val, qi::_1)];
308+
inter_times = interval % CARTPROD;
310309

311310
md_inter = inter_times[qi::_val = phx::construct<AST::MultiDimInter>(qi::_1)];
312311

@@ -336,9 +335,9 @@ ExprRule<Iterator>::ExprRule(Iterator &it) :
336335
[qi::_val = phx::construct<AST::SetUnaryOp>(qi::_1, qi::_2)];
337336

338337
set_binary = (OPAREN
339-
>> set
338+
>> set_expr
340339
>> set_bin
341-
>> set
340+
>> set_expr
342341
>> CPAREN)[qi::_val = phx::construct<AST::SetBinOp>(qi::_1, qi::_2, qi::_3)];
343342

344343
set_expr = set_unary[qi::_val = qi::_1]
@@ -361,19 +360,18 @@ ExprRule<Iterator>::ExprRule(Iterator &it) :
361360
, phx::construct<AST::UnaryOp>(AST::UnOp::neg, qi::_1))]
362361
| lexp_left[qi::_val = phx::construct<AST::LinearExp>(qi::_1, 0)];
363362

364-
lexp_binary = OPAREN
365-
>> lexp[qi::_val = qi::_1]
366-
>> CPAREN
367-
>> *(lexp_bin > OPAREN >> lexp >> CPAREN)
368-
[qi::_val = phx::construct<AST::LExpBinOp>(qi::_val, qi::_1, qi::_2)];
363+
lexp_binary = (OPAREN
364+
>> lexp_expr
365+
>> lexp_bin
366+
>> lexp_expr
367+
>> CPAREN)
368+
[qi::_val = phx::construct<AST::LExpBinOp>(qi::_1, qi::_2, qi::_3)];
369369

370370
lexp_expr = lexp_binary[qi::_val = qi::_1]
371371
| lexp[qi::_val = qi::_1];
372372

373373
// ------------ //
374374

375-
//lexp_pipe = lexp[phx::push_back(qi::_val, qi::_1)]
376-
// >> *(PIPE >> lexp)[phx::push_back(qi::_val, qi::_1)];
377375
lexp_pipe = lexp % PIPE;
378376

379377
mdlexp = lexp_pipe[qi::_val = phx::construct<AST::MDLExp>(qi::_1)];
@@ -402,7 +400,7 @@ ExprRule<Iterator>::ExprRule(Iterator &it) :
402400

403401
// ------------ //
404402

405-
sbg = (V >> set
403+
sbg = (V >> set_expr
406404
>> VMAP >> pwl_expr
407405
>> MAP1 >> pwl_expr
408406
>> MAP2 >> pwl_expr
@@ -411,7 +409,7 @@ ExprRule<Iterator>::ExprRule(Iterator &it) :
411409
[qi::_val = phx::construct<AST::SBG>(
412410
qi::_1, qi::_2, qi::_3, qi::_4, qi::_5, qi::_6
413411
)]
414-
| (V >> set
412+
| (V >> set_expr
415413
>> VMAP >> pwl_expr
416414
>> MAP1 >> pwl_expr
417415
>> MAP2 >> pwl_expr

parser/main.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ void usage()
7272
std::cout << "\n";
7373
std::cout << " * A SBG program starts with a list (possibly empty) of\n";
7474
std::cout << " assignments, and then continues with a list (possibly\n";
75-
std::cout << " empty) of expressions.\n";
75+
std::cout << " empty) of expressions, each one separated by a\n";
76+
std::cout << " semicolon.\n";
7677
std::cout << " * Each assignment or expression should be ended with a\n";
7778
std::cout << " semicolon ;\n";
7879
std::cout << " * All expressions defined in a SBG program should have the\n";
@@ -90,8 +91,9 @@ void usage()
9091
std::cout << " for linear expressions. As explained above, \"dims\"\n";
9192
std::cout << " is also reserved.\n";
9293
std::cout << " * Linear expresssions should include a numeric value for\n";
93-
std::cout << " its slope. That is, to express a constant expression\n";
94-
std::cout << " it should be written as: 0*x+h.\n\n";
94+
std::cout << " its slope, except if its value is equal to 1. That is,\n";
95+
std::cout << " to express a constant expression it should be written\n";
96+
std::cout << " as: 0*x+h.\n\n";
9597

9698
std::cout << "A brief list of the available expressions:\n";
9799
std::cout << " * Arithmetic.\n";

sbg/af_set.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*******************************************************************************
2+
3+
This file is part of Set--Based Graph Library.
4+
5+
SBG Library is free software: you can redistribute it and/or modify
6+
it under the terms of the GNU General Public License as published by
7+
the Free Software Foundation, either version 3 of the License, or
8+
(at your option) any later version.
9+
10+
SBG Library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU General Public License for more details.
14+
15+
You should have received a copy of the GNU General Public License
16+
along with SBG Library. If not, see <http://www.gnu.org/licenses/>.
17+
18+
******************************************************************************/
19+
20+
#include "sbg/af_set.hpp"
21+
22+
namespace SBG {
23+
24+
namespace LIB {
25+
26+
////////////////////////////////////////////////////////////////////////////////
27+
// Unordered Set AF ------------------------------------------------------------
28+
////////////////////////////////////////////////////////////////////////////////
29+
30+
Set UnordAF::createSet() const
31+
{
32+
return Set(std::make_unique<UnorderedSet>());
33+
}
34+
35+
Set UnordAF::createSet(const Util::MD_NAT &x) const
36+
{
37+
return Set(std::make_unique<UnorderedSet>(x));
38+
}
39+
40+
Set UnordAF::createSet(const Interval &i) const
41+
{
42+
return Set(std::make_unique<UnorderedSet>(i));
43+
}
44+
45+
Set UnordAF::createSet(const SetPiece &mdi) const
46+
{
47+
return Set(std::make_unique<UnorderedSet>(mdi));
48+
}
49+
50+
////////////////////////////////////////////////////////////////////////////////
51+
// Ordered Set (1 dimension, dense intervals) AF -------------------------------
52+
////////////////////////////////////////////////////////////////////////////////
53+
54+
Set OrdDenseAF::createSet() const
55+
{
56+
return Set(std::make_unique<OrderedDenseSet>());
57+
}
58+
59+
Set OrdDenseAF::createSet(const Util::MD_NAT &x) const
60+
{
61+
return Set(std::make_unique<OrderedDenseSet>(x));
62+
}
63+
64+
Set OrdDenseAF::createSet(const Interval &i) const
65+
{
66+
return Set(std::make_unique<OrderedDenseSet>(i));
67+
}
68+
69+
Set OrdDenseAF::createSet(const SetPiece &mdi) const
70+
{
71+
return Set(std::make_unique<OrderedDenseSet>(mdi));
72+
}
73+
74+
} // namespace LIB
75+
76+
} // namespace SBG

sbg/af_set.hpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/** @file af_set.hpp
2+
3+
@brief <b>Set Abstract Factory</b>
4+
5+
While developing the SBG library several implementations were proposed. First,
6+
two implementations were proposed for sets: unordered and ordered sets. For
7+
this reason, it was decided that delegation should be used to provide
8+
different implementations, and also an abstract factory was needed to create
9+
objects with the desired implementation.
10+
11+
<hr>
12+
13+
This file is part of Set--Based Graph Library.
14+
15+
SBG Library is free software: you can redistribute it and/or modify
16+
it under the terms of the GNU General Public License as published by
17+
the Free Software Foundation, either version 3 of the License, or
18+
(at your option) any later version.
19+
20+
SBG Library is distributed in the hope that it will be useful,
21+
but WITHOUT ANY WARRANTY; without even the implied warranty of
22+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23+
GNU General Public License for more details.
24+
25+
You should have received a copy of the GNU General Public License
26+
along with SBG Library. If not, see <http://www.gnu.org/licenses/>.
27+
28+
******************************************************************************/
29+
30+
#ifndef SBG_AF_SET_HPP
31+
#define SBG_AF_SET_HPP
32+
33+
#include "set.hpp"
34+
35+
namespace SBG {
36+
37+
namespace LIB {
38+
39+
struct SetAF {
40+
virtual ~SetAF() = default;
41+
42+
virtual Set createSet() const = 0;
43+
virtual Set createSet(const Util::MD_NAT &x) const = 0;
44+
virtual Set createSet(const Interval &i) const = 0;
45+
virtual Set createSet(const SetPiece &mdi) const = 0;
46+
};
47+
48+
struct UnordAF : public SetAF {
49+
Set createSet() const override;
50+
Set createSet(const Util::MD_NAT &x) const override;
51+
Set createSet(const Interval &i) const override;
52+
Set createSet(const SetPiece &mdi) const override;
53+
};
54+
55+
struct OrdDenseAF : public SetAF {
56+
Set createSet() const override;
57+
Set createSet(const Util::MD_NAT &x) const override;
58+
Set createSet(const Interval &i) const override;
59+
Set createSet(const SetPiece &mdi) const override;
60+
};
61+
62+
} // namespace LIB
63+
64+
} // namespace SBG
65+
66+
#endif

sbg/interval.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ namespace SBG {
2424
namespace LIB {
2525

2626
Interval::Interval() : begin_(1), step_(1), end_(0) {}
27-
Interval::Interval(const NAT &x) : begin_(x), step_(1), end_(x) {}
28-
Interval::Interval(const NAT &begin, const NAT &step, const NAT &end)
27+
Interval::Interval(Util::NAT x) : begin_(x), step_(1), end_(x) {}
28+
Interval::Interval(Util::NAT begin, Util::NAT step, Util::NAT end)
2929
: begin_(begin), step_(step), end_(end)
3030
{
3131
if (end >= begin) {
@@ -43,12 +43,9 @@ Interval::Interval(const NAT &begin, const NAT &step, const NAT &end)
4343
}
4444
}
4545

46-
NAT Interval::begin() const { return begin_; }
47-
NAT &Interval::begin_ref() { return begin_; }
48-
NAT Interval::step() const { return step_; }
49-
NAT &Interval::step_ref() { return step_; }
50-
NAT Interval::end() const { return end_; }
51-
NAT &Interval::end_ref() { return end_; }
46+
member_imp(Interval, Util::NAT, begin);
47+
member_imp(Interval, Util::NAT, step);
48+
member_imp(Interval, Util::NAT, end);
5249

5350
// Operators -------------------------------------------------------------------
5451

@@ -88,7 +85,7 @@ unsigned int Interval::cardinal() const
8885

8986
bool Interval::isEmpty() const { return end_ < begin_; }
9087

91-
bool Interval::isMember(const NAT &x) const
88+
bool Interval::isMember(Util::NAT x) const
9289
{
9390
if (x < begin_ || x > end_)
9491
return false;
@@ -131,7 +128,7 @@ Interval Interval::intersection(const Interval &other)const
131128

132129
// Extra operations ------------------------------------------------------------
133130

134-
Interval Interval::offset(const Util::NAT &off) const
131+
Interval Interval::offset(Util::NAT off) const
135132
{
136133
Util::NAT new_b = begin_ + off, new_e = end_ + off;
137134

sbg/interval.hpp

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,13 @@ using MD_NAT = Util::MD_NAT;
4747
struct Interval {
4848
using MaybeInterval = std::optional<Interval>;
4949

50-
NAT begin() const;
51-
NAT &begin_ref();
52-
NAT step() const;
53-
NAT &step_ref();
54-
NAT end() const;
55-
NAT &end_ref();
50+
member_class(NAT, begin);
51+
member_class(NAT, step);
52+
member_class(NAT, end);
5653

5754
Interval();
58-
Interval(const NAT &x);
59-
Interval(const NAT &begin, const NAT &step, const NAT &end);
55+
Interval(Util::NAT x);
56+
Interval(Util::NAT begin, Util::NAT step, Util::NAT end);
6057

6158
bool operator==(const Interval &i) const;
6259
bool operator!=(const Interval &i) const;
@@ -67,20 +64,15 @@ struct Interval {
6764
*/
6865
unsigned int cardinal() const;
6966
bool isEmpty() const;
70-
bool isMember(const NAT &x) const;
67+
bool isMember(NAT x) const;
7168
Interval intersection(const Interval &i2) const;
7269

7370
/**
7471
* @brief Extra operations.
7572
*/
76-
Interval offset(const Util::NAT &off) const;
73+
Interval offset(Util::NAT off) const;
7774
Interval least(const Interval &i2) const;
7875
MaybeInterval compact(const Interval &i2) const;
79-
80-
private:
81-
NAT begin_;
82-
NAT step_;
83-
NAT end_;
8476
};
8577
std::ostream &operator<<(std::ostream &out, const Interval &i);
8678

sbg/lexp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ LExp LExp::operator-(const LExp &other) const
4848

4949
std::ostream &operator<<(std::ostream &out, const LExp &le)
5050
{
51-
RAT slo = le.slope_, off = le.offset_;
51+
RAT slo = le.slope(), off = le.offset();
5252

5353
if (slo != 0 && slo != 1) {
5454
if (slo.numerator() != 1)

0 commit comments

Comments
 (0)