Skip to content

Commit

Permalink
New matching
Browse files Browse the repository at this point in the history
  • Loading branch information
Kalashnikovni committed Oct 28, 2023
1 parent cf6561d commit 5fe9189
Show file tree
Hide file tree
Showing 16 changed files with 796 additions and 58 deletions.
2 changes: 1 addition & 1 deletion eval/defs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ MaybeVValue VarEnv::operator[](VKey k) const
FuncEnv::FuncEnv() {}
FuncEnvType FuncEnv::mapping_ = {{"isEmpty", 0}, {"isMember", 1}, {"minElem", 2}, {"maxElem", 3}, {"lt", 4},
{"compose", 5}, {"inv", 6}, {"image", 7}, {"preImage", 8}, {"dom", 9}, {"combine", 10}, {"minMap", 11},
{"reduce", 12}, {"minAdj", 13}, {"CC", 14}, {"minReach", 15}};
{"reduce", 12}, {"minAdj", 13}, {"CC", 14}, {"minReach", 15}, {"matching", 16}};

MaybeFValue FuncEnv::operator[](FKey k) const
{
Expand Down
2 changes: 1 addition & 1 deletion eval/defs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ struct FuncEnv{
};

typedef enum { empty, member, min, max, lt, comp, inv, im, preim, dom, comb, min_map, red, min_adj
, connected, min_reach } Func;
, connected, min_reach, matching } Func;

// Classes for pretty printing ------------------------------------------------

Expand Down
28 changes: 28 additions & 0 deletions eval/visitors/eval_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ auto connected_visitor_ = Util::Overload {
}
};

/*
auto min_reach_visitor_ = Util::Overload {
[](LIB::BaseDSBG a) {
LIB::BaseMR mr(a);
Expand All @@ -281,7 +282,22 @@ auto min_reach_visitor_ = Util::Overload {
return ExprBaseType();
}
};
*/

auto matching_visitor_ = Util::Overload {
[](LIB::BaseSBG a) {
LIB::BaseMatch match(a);
return ExprBaseType(match.calculate().matched_edges());
},
[](LIB::CanonSBG a) {
LIB::CanonMatch match(a);
return ExprBaseType(match.calculate().matched_edges());
},
[](auto a) {
Util::ERROR("Wrong arguments for minReach");
return ExprBaseType();
}
};

// -----------------------------------------------------------------------------
// Expression evaluator --------------------------------------------------------
Expand Down Expand Up @@ -564,6 +580,7 @@ ExprBaseType EvalExpression::operator()(AST::Call v) const
}
break;

/*
case Eval::Func::min_reach:
if (eval_args.size() == 1) {
arity_ok = true;
Expand All @@ -573,6 +590,17 @@ ExprBaseType EvalExpression::operator()(AST::Call v) const
return result;
}
break;
*/

case Eval::Func::matching:
if (eval_args.size() == 1) {
arity_ok = true;

SBGBaseType g = Apply(EvalGraph{}, eval_args[0]);
ExprBaseType result = std::visit(matching_visitor_, g);
return result;
}
break;

default:
Util::ERROR("EvalExpression: function %s not implemented", vname.c_str());
Expand Down
13 changes: 9 additions & 4 deletions sbg/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,17 +199,20 @@ SBGMap<Set> composition(SBGMap<Set> sbgmap1, SBGMap<Set> sbgmap2)
// Extra functions -------------------------------------------------------------

template<typename Set>
SBGMap<Set> minInv(SBGMap<Set> sbgmap)
SBGMap<Set> minInv(Set d, SBGMap<Set> sbgmap)
{
Set dom = sbgmap.dom();
Set dom = sbgmap.dom(), res_dom = image(restrict(d, sbgmap));
Exp e = sbgmap.exp();

if (cardinal(dom) == 1 || isConstant(e))
return SBGMap<Set>(image(sbgmap), Exp(minElem(dom)));
return SBGMap<Set>(res_dom, Exp(minElem(sbgmap.dom())));

return SBGMap<Set>(image(sbgmap), inverse(e));
return SBGMap<Set>(res_dom, inverse(e));
}

template<typename Set>
SBGMap<Set> minInv(SBGMap<Set> sbgmap) { return minInv(sbgmap.dom(), sbgmap); }

template<typename Set>
std::size_t hash_value(const SBGMap<Set> &sbgmap)
{
Expand All @@ -230,6 +233,7 @@ template UnordSet image<UnordSet>(UnordSet subdom, BaseMap sbgmap);
template UnordSet preImage<UnordSet>(BaseMap sbgmap);
template UnordSet preImage<UnordSet>(UnordSet subdom, BaseMap sbgmap);
template BaseMap composition<UnordSet>(BaseMap sbgmap1, BaseMap sbgmap2);
template BaseMap minInv<UnordSet>(UnordSet im, BaseMap sbgmap);
template BaseMap minInv<UnordSet>(BaseMap sbgmap);
template std::size_t hash_value<UnordSet>(const BaseMap &sbgmap);

Expand All @@ -241,6 +245,7 @@ template OrdSet image<OrdSet>(OrdSet subdom, CanonMap sbgmap);
template OrdSet preImage<OrdSet>(CanonMap sbgmap);
template OrdSet preImage<OrdSet>(OrdSet subdom, CanonMap sbgmap);
template CanonMap composition<OrdSet>(CanonMap sbgmap1, CanonMap sbgmap2);
template CanonMap minInv<OrdSet>(OrdSet im, CanonMap sbgmap);
template CanonMap minInv<OrdSet>(CanonMap sbgmap);
template std::size_t hash_value<OrdSet>(const CanonMap &sbgmap);

Expand Down
2 changes: 2 additions & 0 deletions sbg/map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ SBGMap<Set> composition(SBGMap<Set> sbgmap1, SBGMap<Set> sbgmap2);
* @brief Extra operations.
*/

template<typename Set>
SBGMap<Set> minInv(Set im, SBGMap<Set> sbgmap);
template<typename Set>
SBGMap<Set> minInv(SBGMap<Set> sbgmap);

Expand Down
10 changes: 10 additions & 0 deletions sbg/ord_pw_mdinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,16 @@ OrdPWMDInter concatenation(OrdPWMDInter pwi1, OrdPWMDInter pwi2)
return res;
}

OrdPWMDInter filterSet(bool (*f)(SetPiece), OrdPWMDInter pwi)
{
OrdPWMDInter res;

BOOST_FOREACH (SetPiece mdi, pwi)
if (f(mdi)) res.emplace_hint(res.end(), mdi);

return res;
}

MDInterOrdSet boundedTraverse(OrdPWMDInter pwi1, OrdPWMDInter pwi2, SetPiece (*func)(SetPiece, SetPiece))
{
MDInterOrdSet result;
Expand Down
2 changes: 2 additions & 0 deletions sbg/ord_pw_mdinter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ MDInterOrdSet canonize(MDInterOrdSet ii);
*/
OrdPWMDInter concatenation(OrdPWMDInter pwi1, OrdPWMDInter pwi2);

OrdPWMDInter filterSet(bool (*f)(SetPiece), OrdPWMDInter pwi);

/** @function boundedTraverse
*
* @brief Traverse pwis in order until one reaches its end, obtaining an ordered
Expand Down
73 changes: 60 additions & 13 deletions sbg/pw_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ PWMap<Set>::PWMap(Set s) : maps_() {
}
}
template<typename Set>
PWMap<Set>::PWMap(SBGMap<Set> sm) : maps_() { maps_ref().emplace(sm); }
PWMap<Set>::PWMap(SBGMap<Set> sm) : maps_() {
if (!isEmpty(sm.dom()))
maps_ref().emplace(sm);
}
template<typename Set>
PWMap<Set>::PWMap(MapSet<Set> maps) : maps_(maps) {}

Expand Down Expand Up @@ -542,9 +545,9 @@ PWMap<Set> minMap(Set dom, Exp e1, Exp e2, Exp e3, Exp e4)
BOOST_FOREACH (SetPiece dom_piece, dom) {
PWMap ith = minMap<Set>(dom_piece, e1, e2, e3, e4);
BOOST_FOREACH (SBGMap sbgmap, ith) {
if (sbgmap.exp() == e2) d2 = concatenation(d2, sbgmap.dom());
if (sbgmap.exp() == e2) d2 = cup(d2, sbgmap.dom()); // HERE

else d3 = concatenation(d3, sbgmap.dom());
else d3 = cup(d3, sbgmap.dom()); // HERE
}
}

Expand Down Expand Up @@ -719,20 +722,22 @@ PWMap<Set> minAdjMap(PWMap<Set> pw1, PWMap<Set> pw2)
}

template<typename Set>
PWMap<Set> minInv(PWMap<Set> pw)
PWMap<Set> minInv(Set d, PWMap<Set> pw)
{
PWMap<Set> res;

if (!isEmpty(pw)) {
SBGMap<Set> first = *(pw.maps_ref().begin());
res.emplace(minInv(first));
BOOST_FOREACH (SBGMap<Set> sbgmap, pw.maps()) {
SBGMap<Set> ith = minInv(sbgmap);
res.emplace(minInv(d, first));
BOOST_FOREACH (SBGMap<Set> sbgmap, pw) {
SBGMap<Set> ith = minInv(d, sbgmap);
Set cap_dom = intersection(ith.dom(), dom(res));

if (!isEmpty(cap_dom)) {
PWMap<Set> min = minMap(res, PWMap<Set>(ith));
res = combine(min, res);
Set diff = difference(ith.dom(), dom(res));
res.emplace(SBGMap<Set>(diff, ith.exp()));
}

else res.emplace(ith);
Expand All @@ -743,7 +748,10 @@ PWMap<Set> minInv(PWMap<Set> pw)
}

template<typename Set>
PWMap<Set> filterMap(PWMap<Set> pw, bool (*f)(SBGMap<Set>))
PWMap<Set> minInv(PWMap<Set> pw) { return minInv(dom(pw), pw); }

template<typename Set>
PWMap<Set> filterMap(bool (*f)(SBGMap<Set>), PWMap<Set> pw)
{
PWMap<Set> res;

Expand All @@ -763,14 +771,47 @@ Set equalImage(PWMap<Set> pw1, PWMap<Set> pw2)
Set cap_dom = intersection(sbgmap1.dom(), sbgmap2.dom());
if (!isEmpty(cap_dom)) {
SBGMap<Set> map1(cap_dom, sbgmap1.exp()), map2(cap_dom, sbgmap2.exp());
if (map1 == map2) res = concatenation(res, cap_dom);
if (map1 == map2) res = cup(res, cap_dom);
}
}
}

return res;
}

template<typename Set>
PWMap<Set> offsetDom(PWMap<Set> off, PWMap<Set> pw)
{
PWMap<Set> res;

BOOST_FOREACH (SBGMap<Set> sbgmap, pw) {
Set ith_dom = image(sbgmap.dom(), off);
res.emplace_hint(res.end(), SBGMap<Set>(ith_dom, sbgmap.exp()));
}

return res;
}

template<typename Set>
PWMap<Set> offsetImage(Util::MD_NAT off, PWMap<Set> pw)
{
PWMap<Set> res;

BOOST_FOREACH (SBGMap<Set> sbgmap, pw) {
Exp e = sbgmap.exp(), res_e;
parallel_foreach2 (off, e.exps_ref()) {
Util::NAT o = boost::get<0>(items);
LExp lexp = boost::get<1>(items);
LExp res_lexp(lexp.slope(), lexp.offset() + (Util::RATIONAL) o);
res_e.emplaceBack(res_lexp);
}

res.emplace_hint(res.end(), SBGMap<Set>(sbgmap.dom(), res_e));
}

return res;
}

template<typename Set>
PWMap<Set> normalize(PWMap<Set> pw)
{
Expand All @@ -781,9 +822,9 @@ PWMap<Set> normalize(PWMap<Set> pw)
if (isEmpty(intersection(sbgmap1.dom(), visited))) {
Set ith(sbgmap1.dom());
BOOST_FOREACH (SBGMap<Set> sbgmap2, pw)
if (sbgmap1.exp() == sbgmap2.exp()) concatenation(ith, sbgmap2.dom());
if (sbgmap1.exp() == sbgmap2.exp()) cup(ith, sbgmap2.dom()); //HERE

visited = concatenation(visited, ith);
visited = cup(visited, ith); //HERE
res.emplace(SBGMap<Set>(ith, sbgmap1.exp()));
}
}
Expand Down Expand Up @@ -820,9 +861,12 @@ template BasePWMap reduce<UnordSet>(BaseMap sbgmap);
template BasePWMap reduce<UnordSet>(BasePWMap pw);
template BasePWMap minAdjMap<UnordSet>(BasePWMap pw1, BasePWMap pw2, BasePWMap pw3);
template BasePWMap minAdjMap<UnordSet>(BasePWMap pw1, BasePWMap pw2);
template BasePWMap minInv<UnordSet>(UnordSet im, BasePWMap pw);
template BasePWMap minInv<UnordSet>(BasePWMap pw);
template BasePWMap filterMap<UnordSet>(BasePWMap pw, bool (*f)(BaseMap));
template BasePWMap filterMap<UnordSet>(bool (*f)(BaseMap), BasePWMap pw);
template UnordSet equalImage<UnordSet>(BasePWMap pw1, BasePWMap pw2);
template BasePWMap offsetDom<UnordSet>(BasePWMap off, BasePWMap pw);
template BasePWMap offsetImage<UnordSet>(Util::MD_NAT off, BasePWMap pw);
template BasePWMap normalize<UnordSet>(BasePWMap pw);

template std::ostream &operator<<(std::ostream &out, const MapSet<OrdSet> &ms);
Expand Down Expand Up @@ -852,9 +896,12 @@ template CanonPWMap reduce<OrdSet>(CanonMap sbgmap);
template CanonPWMap reduce<OrdSet>(CanonPWMap pw);
template CanonPWMap minAdjMap<OrdSet>(CanonPWMap pw1, CanonPWMap pw2, CanonPWMap pw3);
template CanonPWMap minAdjMap<OrdSet>(CanonPWMap pw1, CanonPWMap pw2);
template CanonPWMap minInv<OrdSet>(OrdSet im, CanonPWMap pw);
template CanonPWMap minInv<OrdSet>(CanonPWMap pw);
template CanonPWMap filterMap<OrdSet>(CanonPWMap pw, bool (*f)(CanonMap));
template CanonPWMap filterMap<OrdSet>(bool (*f)(CanonMap), CanonPWMap pw);
template OrdSet equalImage<OrdSet>(CanonPWMap pw1, CanonPWMap pw2);
template CanonPWMap offsetDom<OrdSet>(CanonPWMap off, CanonPWMap pw);
template CanonPWMap offsetImage<OrdSet>(Util::MD_NAT off, CanonPWMap pw);
template CanonPWMap normalize<OrdSet>(CanonPWMap pw);

} // namespace LIB
Expand Down
9 changes: 8 additions & 1 deletion sbg/pw_map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,16 +170,23 @@ PWMap<Set> minAdjMap(PWMap<Set> pw1, PWMap<Set> pw2, PWMap<Set> pw3);
template<typename Set>
PWMap<Set> minAdjMap(PWMap<Set> pw1, PWMap<Set> pw2);

template<typename Set>
PWMap<Set> minInv(Set im, PWMap<Set> pw);
template<typename Set>
PWMap<Set> minInv(PWMap<Set> pw);

template<typename Set>
PWMap<Set> filterMap(PWMap<Set> pw, bool (*f)(SBGMap<Set>));
PWMap<Set> filterMap(bool (*f)(SBGMap<Set>), PWMap<Set> pw);

// Returns elements in both doms, that have the same image in both maps
template<typename Set>
Set equalImage(PWMap<Set> pw1, PWMap<Set> pw2);

template<typename Set>
PWMap<Set> offsetDom(PWMap<Set> off, PWMap<Set> pw);
template<typename Set>
PWMap<Set> offsetImage(Util::MD_NAT off, PWMap<Set> pw);

template<typename Set>
PWMap<Set> normalize(PWMap<Set> pw);

Expand Down
Loading

0 comments on commit 5fe9189

Please sign in to comment.