52 lines
1.7 KiB
Java
52 lines
1.7 KiB
Java
package expression;
|
|
|
|
import java.math.BigDecimal;
|
|
import java.math.BigInteger;
|
|
import java.util.List;
|
|
|
|
/**
|
|
* @author Doschennikov Nikita (me@fymio.us)
|
|
*/
|
|
public class Log2 extends AbstractExpression {
|
|
private final AbstractExpression operand;
|
|
|
|
public Log2(AbstractExpression operand) {
|
|
this.operand = operand;
|
|
}
|
|
|
|
private static int log2(int n) {
|
|
if (n <= 0) throw new ArithmeticException("log₂ of non-positive number: " + n);
|
|
int result = 0;
|
|
int v = n;
|
|
while (v > 1) {
|
|
v >>= 1;
|
|
result++;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
@Override public int evaluate(int x) { return log2(operand.evaluate(x)); }
|
|
@Override public int evaluate(int x, int y, int z) { return log2(operand.evaluate(x,y,z)); }
|
|
@Override public int evaluate(List<Integer> vars) { return log2(operand.evaluate(vars)); }
|
|
@Override public BigInteger evaluateBi(List<BigInteger> vars) { throw new UnsupportedOperationException(); }
|
|
@Override public BigDecimal evaluateBd(List<BigDecimal> vars) { throw new UnsupportedOperationException(); }
|
|
|
|
@Override public String toString() { return "log₂(" + operand + ")"; }
|
|
|
|
@Override
|
|
public String toMiniString() {
|
|
if (operand instanceof AbstractBinaryOperation) {
|
|
return "log₂(" + operand.toMiniString() + ")";
|
|
}
|
|
return "log₂ " + operand.toMiniString();
|
|
}
|
|
|
|
@Override
|
|
public boolean equals(Object obj) {
|
|
if (this == obj) return true;
|
|
if (!(obj instanceof Log2)) return false;
|
|
return operand.equals(((Log2) obj).operand);
|
|
}
|
|
|
|
@Override public int hashCode() { return operand.hashCode() ^ 0x4C4F4732; }
|
|
} |