|
8 | 8 | */
|
9 | 9 | package cryptator;
|
10 | 10 |
|
11 |
| -import java.math.BigInteger; |
12 |
| -import java.util.function.BinaryOperator; |
13 |
| - |
14 | 11 | import org.chocosolver.solver.expression.discrete.arithmetic.ArExpression;
|
| 12 | +import org.chocosolver.solver.expression.discrete.relational.ReExpression; |
15 | 13 | import org.chocosolver.solver.variables.IntVar;
|
16 | 14 | import org.chocosolver.util.tools.VariableUtils;
|
17 | 15 |
|
| 16 | +import java.math.BigInteger; |
| 17 | +import java.util.function.BinaryOperator; |
| 18 | + |
18 | 19 | /**
|
19 | 20 | * @see https://en.wikipedia.org/wiki/Relational_operator
|
20 | 21 | */
|
21 | 22 | public enum CryptaOperator {
|
22 |
| - ADD("+", (a, b) -> a.add(b), (a, b) -> a.add(b)), |
23 |
| - SUB("-", (a, b) -> a.subtract(b), (a, b) -> a.sub(b)), |
24 |
| - MUL("*", (a, b) -> a.multiply(b), (a, b) -> a.mul(b)), |
25 |
| - DIV("//", (a, b) -> a.divide(b), (a, b) -> a.div(b)), |
26 |
| - FDIV("/", (a, b) -> fdiv(a, b), (a, b) -> fdiv(a, b)), |
27 |
| - MOD("%", (a, b) -> a.mod(b), (a, b) -> a.mod(b)), |
28 |
| - POW("^", (a, b) -> a.pow(b.intValue()), (a, b) -> a.pow(b)), |
29 |
| - ID("", (a, b) -> BigInteger.ZERO, (a, b) -> null), |
30 |
| - EQ("=", (a, b) -> toBigInt(a.compareTo(b) == 0), (a, b) -> a.eq(b)), |
31 |
| - NE("!=", (a, b) -> toBigInt(a.compareTo(b) != 0), (a, b) -> a.ne(b)), |
32 |
| - LT("<", (a, b) -> toBigInt(a.compareTo(b) < 0), (a, b) -> a.lt(b)), |
33 |
| - GT(">", (a, b) -> toBigInt(a.compareTo(b) > 0), (a, b) -> a.gt(b)), |
34 |
| - LE("<=", (a, b) -> toBigInt(a.compareTo(b) <= 0), (a, b) -> a.le(b)), |
35 |
| - GE(">=", (a, b) -> toBigInt(a.compareTo(b) >= 0), (a, b) -> a.ge(b)); |
| 23 | + ADD("+", (a, b) -> a.add(b), (a, b) -> a.add(b)), |
| 24 | + SUB("-", (a, b) -> a.subtract(b), (a, b) -> a.sub(b)), |
| 25 | + MUL("*", (a, b) -> a.multiply(b), (a, b) -> a.mul(b)), |
| 26 | + DIV("//", (a, b) -> a.divide(b), (a, b) -> a.div(b)), |
| 27 | + FDIV("/", (a, b) -> fdiv(a, b), (a, b) -> fdiv(a, b)), |
| 28 | + MOD("%", (a, b) -> a.mod(b), (a, b) -> a.mod(b)), |
| 29 | + POW("^", (a, b) -> a.pow(b.intValue()), (a, b) -> a.pow(b)), |
| 30 | + ID("", (a, b) -> BigInteger.ZERO, (a, b) -> null), |
| 31 | + EQ("=", (a, b) -> toBigInt(a.compareTo(b) == 0), (a, b) -> a.eq(b)), |
| 32 | + NE("!=", (a, b) -> toBigInt(a.compareTo(b) != 0), (a, b) -> a.ne(b)), |
| 33 | + LT("<", (a, b) -> toBigInt(a.compareTo(b) < 0), (a, b) -> a.lt(b)), |
| 34 | + GT(">", (a, b) -> toBigInt(a.compareTo(b) > 0), (a, b) -> a.gt(b)), |
| 35 | + LE("<=", (a, b) -> toBigInt(a.compareTo(b) <= 0), (a, b) -> a.le(b)), |
| 36 | + GE(">=", (a, b) -> toBigInt(a.compareTo(b) >= 0), (a, b) -> a.ge(b)), |
| 37 | + |
| 38 | + AND(";", (a, b) -> toBigInt(!a.equals(BigInteger.ZERO) && !b.equals(BigInteger.ZERO)), (a, b) -> ((ReExpression) a).and((ReExpression) b)); |
| 39 | + |
| 40 | + public final String token; |
| 41 | + |
| 42 | + public final BinaryOperator<BigInteger> function; |
36 | 43 |
|
37 |
| - public final String token; |
| 44 | + public final BinaryOperator<ArExpression> expression; |
38 | 45 |
|
39 |
| - public final BinaryOperator<BigInteger> function; |
| 46 | + CryptaOperator(String token, BinaryOperator<BigInteger> function, BinaryOperator<ArExpression> expression) { |
| 47 | + this.token = token; |
| 48 | + this.function = function; |
| 49 | + this.expression = expression; |
| 50 | + } |
40 | 51 |
|
41 |
| - public final BinaryOperator<ArExpression> expression; |
| 52 | + public static CryptaOperator valueOfToken(String token) { |
| 53 | + if (token == null) return null; |
| 54 | + for (CryptaOperator operator : CryptaOperator.values()) { |
| 55 | + if (token.equals(operator.getToken())) return operator; |
| 56 | + } |
| 57 | + throw new IllegalArgumentException("Unknown token: " + token); |
| 58 | + } |
42 | 59 |
|
43 |
| - private CryptaOperator(String token, BinaryOperator<BigInteger> function, BinaryOperator<ArExpression> expression) { |
44 |
| - this.token = token; |
45 |
| - this.function = function; |
46 |
| - this.expression = expression; |
47 |
| - } |
| 60 | + private static final BigInteger toBigInt(boolean b) { |
| 61 | + return b ? BigInteger.ONE : BigInteger.ZERO; |
| 62 | + } |
48 | 63 |
|
49 |
| - public final String getToken() { |
50 |
| - return token; |
51 |
| - } |
| 64 | + private static final BigInteger fdiv(BigInteger a, BigInteger b) { |
| 65 | + final BigInteger[] r = a.divideAndRemainder(b); |
| 66 | + if (r[1].equals(BigInteger.ZERO)) return r[0]; |
| 67 | + else throw new ArithmeticException("The remainder of the division is non-zero."); |
| 68 | + } |
52 | 69 |
|
53 |
| - public final BinaryOperator<BigInteger> getFunction() { |
54 |
| - return function; |
55 |
| - } |
| 70 | + private static final ArExpression fdiv(ArExpression a, ArExpression b) { |
| 71 | + final IntVar va = a.intVar(); |
| 72 | + final IntVar vb = b.intVar(); |
| 73 | + final int[] bounds = VariableUtils.boundsForDivision(va, vb); |
| 74 | + final IntVar q = a.getModel().intVar(bounds[0], bounds[1]); |
| 75 | + // Post auxiliary constraint to emulate floor division. |
| 76 | + q.mul(b).eq(a).post(); |
| 77 | + return q; |
| 78 | + } |
56 | 79 |
|
57 |
| - public final BinaryOperator<ArExpression> getExpression() { |
58 |
| - return expression; |
59 |
| - } |
| 80 | + public final String getToken() { |
| 81 | + return token; |
| 82 | + } |
60 | 83 |
|
61 |
| - public static CryptaOperator valueOfToken(String token) { |
62 |
| - if(token == null) return null; |
63 |
| - for(CryptaOperator operator : CryptaOperator.values()) { |
64 |
| - if(token.equals(operator.getToken())) return operator; |
65 |
| - } |
66 |
| - throw new IllegalArgumentException("Unknown token: " + token); |
67 |
| - } |
68 |
| - |
69 |
| - private static final BigInteger toBigInt(boolean b) { |
70 |
| - return b ? BigInteger.ONE : BigInteger.ZERO; |
71 |
| - } |
| 84 | + public final BinaryOperator<BigInteger> getFunction() { |
| 85 | + return function; |
| 86 | + } |
72 | 87 |
|
73 |
| - private static final BigInteger fdiv(BigInteger a, BigInteger b) { |
74 |
| - final BigInteger[] r = a.divideAndRemainder(b); |
75 |
| - if(r[1].equals(BigInteger.ZERO)) return r[0]; |
76 |
| - else throw new ArithmeticException("The remainder of the division is non-zero."); |
77 |
| - } |
78 |
| - |
79 |
| - private static final ArExpression fdiv(ArExpression a, ArExpression b) { |
80 |
| - final IntVar va = a.intVar(); |
81 |
| - final IntVar vb = b.intVar(); |
82 |
| - final int[] bounds = VariableUtils.boundsForDivision(va, vb); |
83 |
| - final IntVar q = a.getModel().intVar(bounds[0], bounds[1]); |
84 |
| - // Post auxiliary constraint to emulate floor division. |
85 |
| - q.mul(b).eq(a).post(); |
86 |
| - return q; |
87 |
| - } |
| 88 | + public final BinaryOperator<ArExpression> getExpression() { |
| 89 | + return expression; |
| 90 | + } |
88 | 91 | }
|
0 commit comments