package expression;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;

/**
 * @author Doschennikov Nikita (me@fymio.us)
 */
public class Sqrt extends AbstractExpression {
    private final AbstractExpression operand;

    public Sqrt(AbstractExpression operand) {
        this.operand = operand;
    }

    private static int sqrtInt(int n) {
        if (n < 0) throw new ArithmeticException("sqrt of negative number: " + n);
        if (n == 0) return 0;
        // Binary search: floor(sqrt(n)), result <= 46340 (sqrt(MAX_INT) < 46341)
        int lo = 0, hi = 46340;
        while (lo < hi) {
            int mid = (lo + hi + 1) / 2;
            if (mid * mid <= n) {
                lo = mid;
            } else {
                hi = mid - 1;
            }
        }
        return lo;
    }

    @Override public int        evaluate(int x)               { return sqrtInt(operand.evaluate(x)); }
    @Override public int        evaluate(int x, int y, int z) { return sqrtInt(operand.evaluate(x,y,z)); }
    @Override public int        evaluate(List<Integer> vars)  { return sqrtInt(operand.evaluate(vars)); }
    @Override public BigInteger evaluateBi(List<BigInteger> vars) { return operand.evaluateBi(vars).sqrt(); }
    @Override public BigDecimal evaluateBd(List<BigDecimal> vars) {
        throw new UnsupportedOperationException("sqrt not supported for BigDecimal");
    }

    @Override public String toString()     { return "√(" + operand + ")"; }
    // @Override public String toMiniString() { return "√" + operand.toMiniString(); }

    @Override
    public String toMiniString() {
        if (operand instanceof AbstractBinaryOperation) {
            return "√(" + operand.toMiniString() + ")";
        }
        return "√" + operand.toMiniString();
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (!(obj instanceof Sqrt)) return false;
        return operand.equals(((Sqrt) obj).operand);
    }

    @Override public int hashCode() { return operand.hashCode() ^ 0x53515254; }
}