Skip to content
Snippets Groups Projects
Commit edd59e4f authored by Florian Angerer's avatar Florian Angerer
Browse files

Added shortcut for identity permutation.

parent 96c3ea93
Branches
No related tags found
No related merge requests found
...@@ -109,6 +109,7 @@ public abstract class APerm extends RBuiltinNode.Arg3 { ...@@ -109,6 +109,7 @@ public abstract class APerm extends RBuiltinNode.Arg3 {
@Specialization @Specialization
protected RAbstractVector aPerm(RAbstractVector vector, RAbstractIntVector permVector, byte resize, protected RAbstractVector aPerm(RAbstractVector vector, RAbstractIntVector permVector, byte resize,
@Cached("createBinaryProfile()") ConditionProfile isIdentityProfile,
@Cached("create()") GetDimAttributeNode getDimsNode, @Cached("create()") GetDimAttributeNode getDimsNode,
@Cached("create()") SetDimAttributeNode setDimsNode, @Cached("create()") SetDimAttributeNode setDimsNode,
@Cached("create()") GetDimNamesAttributeNode getDimNamesNode) { @Cached("create()") GetDimNamesAttributeNode getDimNamesNode) {
...@@ -116,6 +117,9 @@ public abstract class APerm extends RBuiltinNode.Arg3 { ...@@ -116,6 +117,9 @@ public abstract class APerm extends RBuiltinNode.Arg3 {
int[] dim = getDimsNode.getDimensions(vector); int[] dim = getDimsNode.getDimensions(vector);
checkErrorConditions(dim); checkErrorConditions(dim);
int[] perm = getPermute(dim, permVector); int[] perm = getPermute(dim, permVector);
if (isIdentityProfile.profile(isIdentityPermutation(dim.length, perm))) {
return vector;
}
int[] posV = new int[dim.length]; int[] posV = new int[dim.length];
int[] pDim = applyPermute(dim, perm, false); int[] pDim = applyPermute(dim, perm, false);
...@@ -153,8 +157,18 @@ public abstract class APerm extends RBuiltinNode.Arg3 { ...@@ -153,8 +157,18 @@ public abstract class APerm extends RBuiltinNode.Arg3 {
return result; return result;
} }
private static boolean isIdentityPermutation(int dimLength, int[] perm) {
for (int i = 0; i < dimLength; i++) {
if (i != perm[i]) {
return false;
}
}
return true;
}
@Specialization @Specialization
protected RAbstractVector aPerm(RAbstractVector vector, RAbstractStringVector permVector, byte resize, protected RAbstractVector aPerm(RAbstractVector vector, RAbstractStringVector permVector, byte resize,
@Cached("createBinaryProfile()") ConditionProfile isIdentityProfile,
@Cached("create()") GetDimAttributeNode getDimsNode, @Cached("create()") GetDimAttributeNode getDimsNode,
@Cached("create()") SetDimAttributeNode setDimsNode, @Cached("create()") SetDimAttributeNode setDimsNode,
@Cached("create()") GetDimNamesAttributeNode getDimNamesNode) { @Cached("create()") GetDimNamesAttributeNode getDimNamesNode) {
...@@ -176,7 +190,7 @@ public abstract class APerm extends RBuiltinNode.Arg3 { ...@@ -176,7 +190,7 @@ public abstract class APerm extends RBuiltinNode.Arg3 {
} }
// Note: if this turns out to be slow, we can cache the permutation // Note: if this turns out to be slow, we can cache the permutation
return aPerm(vector, RDataFactory.createIntVector(perm, true), resize, getDimsNode, setDimsNode, getDimNamesNode); return aPerm(vector, RDataFactory.createIntVector(perm, true), resize, isIdentityProfile, getDimsNode, setDimsNode, getDimNamesNode);
} }
private static int[] getReverse(int[] dim) { private static int[] getReverse(int[] dim) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment