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

commonMain.jetbrains.datalore.plot.base.scale.MapperUtil.kt Maven / Gradle / Ivy

There is a newer version: 4.5.3-alpha1
Show newest version
/*
 * Copyright (c) 2019. JetBrains s.r.o.
 * Use of this source code is governed by the MIT license that can be found in the LICENSE file.
 */

package jetbrains.datalore.plot.base.scale

import jetbrains.datalore.base.gcommon.collect.ClosedRange
import jetbrains.datalore.plot.base.ContinuousTransform
import kotlin.math.max
import kotlin.math.min

object MapperUtil {
    fun map(r: ClosedRange, mapper: (Double?) -> Double?): ClosedRange {
        val a = mapper(r.lowerEnd)!!
        val b = mapper(r.upperEnd)!!
        return ClosedRange(min(a, b), max(a, b))
    }

    fun mapDiscreteDomainValuesToNumbers(values: Collection<*>): Map {
        return mapDiscreteDomainValuesToIndices(values)
    }

    private fun mapDiscreteDomainValuesToIndices(values: Collection<*>): Map {
        val result = LinkedHashMap()
        var index = 0
        for (v in values) {
            if (v != null && !result.containsKey(v)) {
                result[v] = index++.toDouble()
            }
        }
        return result
    }

    fun rangeWithLimitsAfterTransform(
        dataRange: ClosedRange,
        lowerLimit: Double?,
        upperLimit: Double?,
        trans: ContinuousTransform
    ): ClosedRange {
        val lower = if (lowerLimit != null && lowerLimit.isFinite()) {
            lowerLimit
        } else {
            dataRange.lowerEnd
        }
        check(trans.isInDomain(lower)) {
            "[${trans::class.simpleName}] Lower end $lower is outside of transform's domain."
        }

        val upper = if (upperLimit != null && upperLimit.isFinite()) {
            upperLimit
        } else {
            dataRange.upperEnd
        }
        check(trans.isInDomain(upper)) {
            "[${trans::class.simpleName}] Lower end $upper is outside of transform's domain."
        }

        val limits = listOf(lower, upper)
        return ClosedRange.encloseAll(trans.apply(limits))
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy