TreeMap in Common library


#1

I am building a multi-platform library for graphs and I’d really need a TreeMap to sort the frontier. I was trying to rewrite it as is in java.utils but its implementation is so rooted in the JDK that is insane (if not illegal) to translate all the files required from Java to Kotlin just for the class I need.

I never realized how hard is to manipulate complex data without java.utils

Anyone knows an intelligent way to get a TreeMap up and running other then studying it and manually implementing it from scratch?

An alternative would be to use a wrapped LinkedHashMap where .put() creates a new map with the new entry in the correct position.


#2

I think that general solution is to find a job implementation and wrap it in ‘actual’ declaration. But it is probably better to also write a feature request for standard library. I would support it.


#3

Can you link me the proper website where is should make the feature request? I believe it’s a better solution as well.


#4

In most cases it can be replaced by List.binarySearchBy


#6

Still not efficient as a TreeMap would be but I guess it could do the trick with sortBy. Thanks :slight_smile:


#7

Like this:

    private fun findPiece(cp: Int): Int {
        val n = piece.binarySearchBy(cp) { it.charPos }
        return if (n >= 0) n + 1 else -n - 1
    }