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

/*
 * 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.Transform
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: Transform?
    ): ClosedRange {
        val lower = lowerLimit ?: dataRange.lowerEnd
        val upper = upperLimit ?: dataRange.upperEnd
        val limits = listOf(lower, upper)
        return ClosedRange.encloseAll(trans?.apply(limits) ?: limits)
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy