File tree Expand file tree Collapse file tree 2 files changed +58
-0
lines changed
main/java/org/apache/sysds/hops/rewriter
test/java/org/apache/sysds/test/component/codegen/rewrite/functions Expand file tree Collapse file tree 2 files changed +58
-0
lines changed Original file line number Diff line number Diff line change
1
+ package org .apache .sysds .hops .rewriter ;
2
+
3
+ public class RewriterAlphabetEncoder {
4
+
5
+ public static int [] fromBaseNNumber (int l , int n ) {
6
+ if (l == 0 )
7
+ return new int [] { 0 };
8
+
9
+ int numDigits = (int )(Math .log (l ) / Math .log (n )) + 1 ;
10
+ int [] digits = new int [numDigits ];
11
+
12
+ for (int i = numDigits - 1 ; i >= 0 ; i --) {
13
+ digits [i ] = l % n ;
14
+ l = l / n ;
15
+ }
16
+
17
+ return digits ;
18
+ }
19
+
20
+ public static int toBaseNNumber (int [] digits , int n ) {
21
+ if (digits .length == 0 )
22
+ throw new IllegalArgumentException ();
23
+
24
+ int multiplicator = 1 ;
25
+ int out = 0 ;
26
+
27
+ for (int i = digits .length - 1 ; i >= 0 ; i --) {
28
+ out += multiplicator * digits [i ];
29
+ multiplicator *= n ;
30
+ }
31
+
32
+ return out ;
33
+ }
34
+ }
Original file line number Diff line number Diff line change
1
+ package org .apache .sysds .test .component .codegen .rewrite .functions ;
2
+
3
+ import org .apache .sysds .hops .rewriter .RewriterAlphabetEncoder ;
4
+ import org .junit .Test ;
5
+
6
+ public class RewriterAlphabetTest {
7
+
8
+ @ Test
9
+ public void testDecode1 () {
10
+ int l = 27 ;
11
+ int n = 5 ;
12
+ int [] digits = RewriterAlphabetEncoder .fromBaseNNumber (l , n );
13
+ assert digits .length == 3 && digits [0 ] == 1 && digits [1 ] == 0 && digits [2 ] == 2 ;
14
+ }
15
+
16
+ @ Test
17
+ public void testEncode1 () {
18
+ int [] digits = new int [] { 1 , 0 , 2 };
19
+ int n = 5 ;
20
+ int l = RewriterAlphabetEncoder .toBaseNNumber (digits , n );
21
+ assert l == 27 ;
22
+ }
23
+
24
+ }
You can’t perform that action at this time.
0 commit comments