All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fuookami.ospf.kotlin.utils.math.ComparisonOperator.kt Maven / Gradle / Ivy

There is a newer version: 1.0.29
Show newest version
package fuookami.ospf.kotlin.utils.math

import fuookami.ospf.kotlin.utils.operator.*

data class Equal(
    val precision: U?
) where T : Arithmetic, T : Invariant, T : Minus, U : Abs, U : Ord {
    companion object {
        operator fun  invoke(precision: U): Equal
                where T : Arithmetic, T : Invariant, U : FloatingNumber, T : Minus = Equal(precision)

        operator fun  invoke(constants: RealNumberConstants): Equal
                where T : Arithmetic, T : Invariant, U : RealNumber, T : Minus =
            Equal(constants.decimalPrecision)
    }

    operator fun invoke(lhs: T, rhs: T) = when (precision) {
        null -> lhs.value() eq rhs.value()
        else -> abs((lhs - rhs).value()) <= precision
    }
}

data class Unequal(
    val precision: U?
) where T : Arithmetic, T : Invariant, T : Minus, U : Abs, U : Ord {
    companion object {
        operator fun  invoke(precision: U): Unequal
                where T : Arithmetic, T : Invariant, U : FloatingNumber, T : Minus = Unequal(precision)

        operator fun  invoke(constants: RealNumberConstants): Unequal
                where T : Arithmetic, T : Invariant, U : RealNumber, T : Minus =
            Unequal(constants.decimalPrecision)
    }

    operator fun invoke(lhs: T, rhs: T) = when (precision) {
        null -> lhs.value() neq rhs.value()
        else -> abs((lhs - rhs).value()) >= precision
    }
}

data class Less(
    val precision: U?
) where T : Arithmetic, T : Invariant, T : Minus, U : Ord, U : Neg {
    companion object {
        operator fun  invoke(precision: U): Less
                where T : Arithmetic, T : Invariant, U : FloatingNumber, T : Minus, U : Neg =
            Less(precision)

        operator fun  invoke(constants: RealNumberConstants): Less
                where T : Arithmetic, T : Invariant, U : RealNumber, T : Minus, U : Neg =
            Less(constants.decimalPrecision)
    }

    operator fun invoke(lhs: T, rhs: T) = when (precision) {
        null -> lhs.value() < rhs.value()
        else -> (lhs - rhs).value() <= -precision
    }
}

data class LessEqual(
    val precision: U?
) where T : Arithmetic, T : Invariant, T : Minus, U : Ord {
    companion object {
        operator fun  invoke(precision: U): LessEqual
                where T : Arithmetic, T : Invariant, U : FloatingNumber, T : Minus = LessEqual(precision)

        operator fun  invoke(constants: RealNumberConstants): LessEqual
                where T : Arithmetic, T : Invariant, U : RealNumber, T : Minus =
            LessEqual(constants.decimalPrecision)
    }

    operator fun invoke(lhs: T, rhs: T) = when (precision) {
        null -> lhs.value() <= rhs.value()
        else -> (lhs - rhs).value() <= precision
    }
}

data class Greater(
    val precision: U?
) where T : Arithmetic, T : Invariant, T : Minus, U : Ord {
    companion object {
        operator fun  invoke(precision: U): Greater
                where T : Arithmetic, T : Invariant, U : FloatingNumber, T : Minus = Greater(precision)

        operator fun  invoke(constants: RealNumberConstants): Greater
                where T : Arithmetic, T : Invariant, U : RealNumber, T : Minus =
            Greater(constants.decimalPrecision)
    }

    operator fun invoke(lhs: T, rhs: T) = when (precision) {
        null -> lhs.value() > rhs.value()
        else -> (lhs - rhs).value() >= precision
    }
}

data class GreaterEqual(
    val precision: U?
) where T : Arithmetic, T : Invariant, T : Minus, U : Ord, U : Neg {
    companion object {
        operator fun  invoke(precision: U): GreaterEqual
                where T : Arithmetic, T : Invariant, U : FloatingNumber, T : Minus =
            GreaterEqual(precision)

        operator fun  invoke(constants: RealNumberConstants): GreaterEqual
                where T : Arithmetic, T : Invariant, U : RealNumber, T : Minus, U : Neg =
            GreaterEqual(constants.decimalPrecision)
    }

    operator fun invoke(lhs: T, rhs: T) = when (precision) {
        null -> lhs.value() >= rhs.value()
        else -> (lhs - rhs).value() >= -precision
    }
}

data class ComparisonOperator(
    val precision: U?
) where T : Arithmetic, T : Invariant, T : Minus, U : Abs, U : Ord, U : Neg {
    infix fun T.eq(rhs: T): Boolean {
        val op = Equal(precision)
        return op(this, rhs)
    }

    infix fun T.neq(rhs: T): Boolean {
        val op = Unequal(precision)
        return op(this, rhs)
    }

    infix fun T.ls(rhs: T): Boolean {
        val op = Less(precision)
        return op(this, rhs)
    }

    infix fun T.leq(rhs: T): Boolean {
        val op = LessEqual(precision)
        return op(this, rhs)
    }

    infix fun T.gr(rhs: T): Boolean {
        val op = Greater(precision)
        return op(this, rhs)
    }

    infix fun T.geq(rhs: T): Boolean {
        val op = GreaterEqual(precision)
        return op(this, rhs)
    }
}