|
| 1 | +package org.jetbrains.kotlinx.dataframe.impl |
| 2 | + |
| 3 | +import org.jetbrains.kotlinx.dataframe.impl.api.createConverter |
| 4 | +import org.jetbrains.kotlinx.dataframe.impl.commonNumberType |
| 5 | +import java.math.BigDecimal |
| 6 | +import java.math.BigInteger |
| 7 | +import kotlin.reflect.KClass |
| 8 | +import kotlin.reflect.KType |
| 9 | +import kotlin.reflect.full.withNullability |
| 10 | +import kotlin.reflect.typeOf |
| 11 | + |
| 12 | +/** |
| 13 | + * Number type graph, structured in terms of number complexity. |
| 14 | + * A number can always be expressed lossless by a number of a more complex type (any of its parents). |
| 15 | + * |
| 16 | + * ``` |
| 17 | + * BigDecimal |
| 18 | + * / \\ |
| 19 | + * BigInteger | |
| 20 | + * | | |
| 21 | + * ULong | |
| 22 | + * | | |
| 23 | + * Long Double |
| 24 | + * \\ / | |
| 25 | + * UInt | |
| 26 | + * | | |
| 27 | + * Int Float |
| 28 | + * | / |
| 29 | + * UShort |
| 30 | + * | |
| 31 | + * Short |
| 32 | + * | |
| 33 | + * UByte |
| 34 | + * | |
| 35 | + * Byte |
| 36 | + * ``` |
| 37 | + * |
| 38 | + * For any two numbers, we can find the nearest common ancestor in this graph |
| 39 | + * by calling [DirectedAcyclicGraph.findNearestCommonVertex]. |
| 40 | + * @see getCommonNumberClass |
| 41 | + * @see commonNumberClass |
| 42 | + */ |
| 43 | +internal val numberTypeGraph: DirectedAcyclicGraph<KType> by lazy { |
| 44 | + dagOf( |
| 45 | + typeOf<BigDecimal>() to typeOf<BigInteger>(), |
| 46 | + typeOf<BigDecimal>() to typeOf<Double>(), |
| 47 | + typeOf<BigInteger>() to typeOf<ULong>(), |
| 48 | + typeOf<ULong>() to typeOf<Long>(), |
| 49 | + typeOf<Long>() to typeOf<UInt>(), |
| 50 | + typeOf<Double>() to typeOf<UInt>(), |
| 51 | + typeOf<Double>() to typeOf<Float>(), |
| 52 | + typeOf<UInt>() to typeOf<Int>(), |
| 53 | + typeOf<Int>() to typeOf<UShort>(), |
| 54 | + typeOf<Float>() to typeOf<UShort>(), |
| 55 | + typeOf<UShort>() to typeOf<Short>(), |
| 56 | + typeOf<Short>() to typeOf<UByte>(), |
| 57 | + typeOf<UByte>() to typeOf<Byte>(), |
| 58 | + ) |
| 59 | +} |
| 60 | + |
| 61 | +/** @include [numberTypeGraph] */ |
| 62 | +internal val numberClassGraph: DirectedAcyclicGraph<KClass<*>> by lazy { |
| 63 | + numberTypeGraph.map { it.classifier as KClass<*> } |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * Determines the nearest common numeric type, in terms of complexity, between two given classes/types. |
| 68 | + * |
| 69 | + * Unsigned types are supported too even though they are not a [Number] instance, |
| 70 | + * but unless an unsigned type is provided in the input, it will never be returned. |
| 71 | + * Meaning, given two [Number] inputs, the output will always be a [Number]. |
| 72 | + * |
| 73 | + * @param first The first numeric type to compare. Can be null, in which case the second to is returned. |
| 74 | + * @param second The second numeric to compare. Cannot be null. |
| 75 | + * @return The nearest common numeric type between the two input classes. |
| 76 | + * If no common class is found, [IllegalStateException] is thrown. |
| 77 | + * @see numberTypeGraph |
| 78 | + */ |
| 79 | +internal fun getCommonNumberType(first: KType?, second: KType): KType { |
| 80 | + if (first == null) return second |
| 81 | + |
| 82 | + val firstWithoutNullability = first.withNullability(false) |
| 83 | + val secondWithoutNullability = second.withNullability(false) |
| 84 | + |
| 85 | + val result = if (firstWithoutNullability == secondWithoutNullability) { |
| 86 | + firstWithoutNullability |
| 87 | + } else { |
| 88 | + numberTypeGraph.findNearestCommonVertex(firstWithoutNullability, secondWithoutNullability) |
| 89 | + ?: error("Can not find common number type for $first and $second") |
| 90 | + } |
| 91 | + |
| 92 | + return if (first.isMarkedNullable || second.isMarkedNullable) result.withNullability(true) else result |
| 93 | +} |
| 94 | + |
| 95 | +/** @include [getCommonNumberType] */ |
| 96 | +@Suppress("IntroduceWhenSubject") |
| 97 | +internal fun getCommonNumberClass(first: KClass<*>?, second: KClass<*>): KClass<*> = |
| 98 | + when { |
| 99 | + first == null -> second |
| 100 | + |
| 101 | + first == second -> first |
| 102 | + |
| 103 | + else -> numberClassGraph.findNearestCommonVertex(first, second) |
| 104 | + ?: error("Can not find common number type for $first and $second") |
| 105 | + } |
| 106 | + |
| 107 | +/** |
| 108 | + * Determines the nearest common numeric type, in terms of complexity, all types in [this]. |
| 109 | + * |
| 110 | + * Unsigned types are supported too even though they are not a [Number] instance, |
| 111 | + * but unless an unsigned type is provided in the input, it will never be returned. |
| 112 | + * Meaning, given just [Number] inputs, the output will always be a [Number]. |
| 113 | + * |
| 114 | + * @return The nearest common numeric type between the input types. |
| 115 | + * If no common type is found, it returns [Number]. |
| 116 | + * @see numberTypeGraph |
| 117 | + */ |
| 118 | +internal fun Iterable<KType>.commonNumberType(): KType = fold(null as KType?, ::getCommonNumberType) ?: typeOf<Number>() |
| 119 | + |
| 120 | +/** @include [commonNumberType] */ |
| 121 | +internal fun Iterable<KClass<*>>.commonNumberClass(): KClass<*> = |
| 122 | + fold(null as KClass<*>?, ::getCommonNumberClass) ?: Number::class |
| 123 | + |
| 124 | +/** |
| 125 | + * Converts the elements of the given iterable of numbers into a common numeric type based on complexity. |
| 126 | + * The common numeric type is determined using the provided [commonNumberType] parameter |
| 127 | + * or calculated with [Iterable.commonNumberType] from the iterable's elements if not explicitly specified. |
| 128 | + * |
| 129 | + * @param commonNumberType The desired common numeric type to convert the elements to. |
| 130 | + * This is determined by default using the types of the elements in the iterable. |
| 131 | + * @return A new iterable of numbers where each element is converted to the specified or inferred common number type. |
| 132 | + * @throws IllegalStateException if an element cannot be converted to the common number type. |
| 133 | + * @see Iterable.commonNumberType |
| 134 | + */ |
| 135 | +@Suppress("UNCHECKED_CAST") |
| 136 | +internal fun Iterable<Number>.convertToCommonNumberType( |
| 137 | + commonNumberType: KType = this.types().commonNumberType(), |
| 138 | +): Iterable<Number> { |
| 139 | + val converter = createConverter(typeOf<Number>(), commonNumberType)!! as (Number) -> Number? |
| 140 | + return map { |
| 141 | + converter(it) ?: error("Can not convert $it to $commonNumberType") |
| 142 | + } |
| 143 | +} |
0 commit comments