Skip to content

Commit e78f0a0

Browse files
committed
Fix identity for relation in C++
1 parent 939d2d7 commit e78f0a0

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed

btypes_big_integer/src/main/cpp/BRelation.hpp

+16
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,22 @@ class BRelation : public BObject {
967967
return BRelation<T,T>(resultMap);
968968
}
969969

970+
static BRelation<BTuple<S,T>,BTuple<S,T>> identity2(const BRelation<S,T>& relation) {
971+
immer::map<BTuple<S,T>,immer::set<BTuple<S,T>, typename BSet<BTuple<S,T>>::Hash, typename BSet<BTuple<S,T>>::HashEqual>,
972+
typename BSet<BTuple<S,T>>::Hash,
973+
typename BSet<BTuple<S,T>>::HashEqual> resultMap;
974+
for(const std::pair<S,immer::set<T, typename BSet<T>::Hash, typename BSet<T>::HashEqual>>& pair : relation.map) {
975+
T domainElement = pair.first;
976+
immer::set<T, typename BSet<T>::Hash, typename BSet<T>::HashEqual> range = pair.second;
977+
for(const T& rangeElement : range) {
978+
immer::set<BTuple<S,T>, typename BSet<BTuple<S,T>>::Hash, typename BSet<BTuple<S,T>>::HashEqual> range;
979+
range = range.insert(BTuple<S,T>(domainElement, rangeElement));
980+
resultMap = resultMap.set(BTuple<S,T>(domainElement, rangeElement), range);
981+
}
982+
}
983+
return BRelation<BTuple<S,T>,BTuple<S,T>>(resultMap);
984+
}
985+
970986
BRelation<S,S> iterate(const BInteger& n) const {
971987
BRelation<S,S> thisRelation = (BRelation<S,S>) *this;
972988
BRelation<S,S> result = BRelation<S,S>::identity(this->domain()._union(thisRelation.range()));

btypes_primitives/src/main/cpp/BRelation.hpp

+17
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,23 @@ class BRelation : public BObject {
967967
return BRelation<T,T>(resultMap);
968968
}
969969

970+
971+
static BRelation<BTuple<S,T>,BTuple<S,T>> identity2(const BRelation<S,T>& relation) {
972+
immer::map<BTuple<S,T>,immer::set<BTuple<S,T>, typename BSet<BTuple<S,T>>::Hash, typename BSet<BTuple<S,T>>::HashEqual>,
973+
typename BSet<BTuple<S,T>>::Hash,
974+
typename BSet<BTuple<S,T>>::HashEqual> resultMap;
975+
for(const std::pair<S,immer::set<T, typename BSet<T>::Hash, typename BSet<T>::HashEqual>>& pair : relation.map) {
976+
T domainElement = pair.first;
977+
immer::set<T, typename BSet<T>::Hash, typename BSet<T>::HashEqual> range = pair.second;
978+
for(const T& rangeElement : range) {
979+
immer::set<BTuple<S,T>, typename BSet<BTuple<S,T>>::Hash, typename BSet<BTuple<S,T>>::HashEqual> range;
980+
range = range.insert(BTuple<S,T>(domainElement, rangeElement));
981+
resultMap = resultMap.set(BTuple<S,T>(domainElement, rangeElement), range);
982+
}
983+
}
984+
return BRelation<BTuple<S,T>,BTuple<S,T>>(resultMap);
985+
}
986+
970987
BRelation<S,S> iterate(const BInteger& n) const {
971988
BRelation<S,S> thisRelation = (BRelation<S,S>) *this;
972989
BRelation<S,S> result = BRelation<S,S>::identity(this->domain()._union(thisRelation.range()));

src/main/java/de/hhu/stups/codegenerator/generators/ExpressionGenerator.java

+5
Original file line numberDiff line numberDiff line change
@@ -794,8 +794,13 @@ private String generateTupleProjection(ExpressionOperatorNode node) {
794794
*/
795795
private String generateIdentity(List<String> expressionList, BType type) {
796796
ST identity = currentGroup.getInstanceOf("identity");
797+
if(type instanceof CoupleType) {
798+
TemplateHandler.add(identity, "leftType", typeGenerator.generate(((CoupleType) type).getLeft()));
799+
TemplateHandler.add(identity, "rightType", typeGenerator.generate(((CoupleType) type).getRight()));
800+
}
797801
TemplateHandler.add(identity, "type", typeGenerator.generate(type));
798802
TemplateHandler.add(identity, "arg", expressionList.get(0));
803+
TemplateHandler.add(identity, "relationalArg", type instanceof CoupleType);
799804
return identity.render();
800805
}
801806

src/main/resources/de/hhu/stups/codegenerator/CppTemplate.stg

+5-1
Original file line numberDiff line numberDiff line change
@@ -779,8 +779,12 @@ projection_tuple(arg, isProjection1) ::= <<
779779
(<arg>.<if(isProjection1)>projection1<else>projection2<endif>())
780780
>>
781781

782-
identity(type, arg) ::= <<
782+
identity(leftType, rightType, type, arg, relationalArg) ::= <<
783+
<if(relationalArg)>
784+
(BRelation\<<leftType>, <rightType> >::identity2(<arg>))
785+
<else>
783786
(BRelation\<<type>, <type> >::identity(<arg>))
787+
<endif>
784788
>>
785789

786790
cartesian_product(leftType, rightType, arg1, arg2) ::= <<

0 commit comments

Comments
 (0)