forked from apache/systemds
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
src/main/java/org/apache/sysds/hops/rewriter/RewriterAlphabetEncoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package org.apache.sysds.hops.rewriter; | ||
|
||
public class RewriterAlphabetEncoder { | ||
|
||
public static int[] fromBaseNNumber(int l, int n) { | ||
if (l == 0) | ||
return new int[] { 0 }; | ||
|
||
int numDigits = (int)(Math.log(l) / Math.log(n)) + 1; | ||
int[] digits = new int[numDigits]; | ||
|
||
for (int i = numDigits - 1; i >= 0; i--) { | ||
digits[i] = l % n; | ||
l = l / n; | ||
} | ||
|
||
return digits; | ||
} | ||
|
||
public static int toBaseNNumber(int[] digits, int n) { | ||
if (digits.length == 0) | ||
throw new IllegalArgumentException(); | ||
|
||
int multiplicator = 1; | ||
int out = 0; | ||
|
||
for (int i = digits.length - 1; i >= 0; i--) { | ||
out += multiplicator * digits[i]; | ||
multiplicator *= n; | ||
} | ||
|
||
return out; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
.../java/org/apache/sysds/test/component/codegen/rewrite/functions/RewriterAlphabetTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package org.apache.sysds.test.component.codegen.rewrite.functions; | ||
|
||
import org.apache.sysds.hops.rewriter.RewriterAlphabetEncoder; | ||
import org.junit.Test; | ||
|
||
public class RewriterAlphabetTest { | ||
|
||
@Test | ||
public void testDecode1() { | ||
int l = 27; | ||
int n = 5; | ||
int[] digits = RewriterAlphabetEncoder.fromBaseNNumber(l, n); | ||
assert digits.length == 3 && digits[0] == 1 && digits[1] == 0 && digits[2] == 2; | ||
} | ||
|
||
@Test | ||
public void testEncode1() { | ||
int[] digits = new int[] { 1, 0, 2 }; | ||
int n = 5; | ||
int l = RewriterAlphabetEncoder.toBaseNNumber(digits, n); | ||
assert l == 27; | ||
} | ||
|
||
} |