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