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

commonMain.jetbrains.datalore.plot.base.stat.MultiOrdering.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.stat

import jetbrains.datalore.base.gcommon.base.Preconditions.checkArgument

internal class MultiOrdering>(private val myKeys: List) {
    private val myIndices: MutableList

    init {
        myIndices = ArrayList(myKeys.size)
        for (i in myKeys.indices) {
            myIndices.add(i)
        }

        myIndices.sortWith(Comparator { i: Int?, j: Int? ->
            val keyI = myKeys[i!!]
            val keyJ = myKeys[j!!]
            when {
                keyI === keyJ -> 0
                keyI == null -> -1
                keyJ == null -> 1
                else -> keyI.compareTo(keyJ)
            }
        })
    }

    fun  sortedCopy(l: List): List {
        checkArgument(l.size == myIndices.size,
                "Expected size " + myIndices.size + " but was size " + l.size)
        val copy = ArrayList(myIndices.size)
        for (oldIndex in myIndices) {
            val v = l[oldIndex]
            copy.add(v)
        }
        return copy
    }

    fun sortedCopyOfKeys(): List {
        return sortedCopy(myKeys)
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy