blob: dac490b06847ecaa46d36a54bb4b3720a0538686 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
/*
* SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/>
*
* SPDX-License-Identifier: EPL-2.0
*/
package tools.refinery.logic.term.int_;
import tools.refinery.logic.substitution.Substitution;
import tools.refinery.logic.term.Term;
public class IntPowTerm extends IntBinaryTerm {
public IntPowTerm(Term<Integer> left, Term<Integer> right) {
super(left, right);
}
@Override
public Term<Integer> doSubstitute(Substitution substitution, Term<Integer> substitutedLeft,
Term<Integer> substitutedRight) {
return new IntPowTerm(substitutedLeft, substitutedRight);
}
@Override
protected Integer doEvaluate(Integer leftValue, Integer rightValue) {
return rightValue < 0 ? null : power(leftValue, rightValue);
}
private static int power(int base, int exponent) {
int accum = 1;
while (exponent > 0) {
if (exponent % 2 == 1) {
accum = accum * base;
}
base = base * base;
exponent = exponent / 2;
}
return accum;
}
@Override
public String toString() {
return "(%s ** %s)".formatted(getLeft(), getRight());
}
}
|