Skip to content

Add kotlin-map to ton-maps and back convertion support #41

@90K2

Description

@90K2

usecase: batch mint nfts required dict of { index: Cell }

reference: tonlib ts

Our efforts with @AntonMeep (it is seems exactly like tonwhales ts logic but doesn't work unfortunatelly)

fun <T> HashMapNodeOf(it: Map<BitString, T>, n: Int): HashMapNode<T> {
    require(it.isNotEmpty())

    return if (it.size == 1) {
        HashMapNodeLeaf(it.values.first())
    } else {
        HashMapNodeFork(
            // Left has implicit '0' appended to the label
            left = HashMapEdgeOf(it.filterKeys { !it.first() }.mapKeys { it.key.drop(1).toBitString() }, n - 1),
            // Right has implicit '1' appended to the label
            right = HashMapEdgeOf(it.filterKeys { it.first() }.mapKeys { it.key.drop(1).toBitString() }, n - 1),
        )
    }
}

fun <T> HashMapEdgeOf(it: Map<BitString, T>, n: Int): HashMapEdge<T> {
    require(it.isNotEmpty())
    val keys = it.keys
    val commonPrefix = it.keys
        .reduce { acc, bitString ->
            acc.zip(bitString)
                .takeWhile { it.first == it.second }
                .map { it.first }
                .toBitString()
        }
    val label = HashMapLabel.of(commonPrefix)
    return HashMapEdge(
        label = label,
        node = HashMapNodeOf(it.mapKeys { it.key.drop(commonPrefix.size).toBitString() }, n - commonPrefix.size)
    )
}

fun pad(src: BitString, size: Int): BitString {
    var t = src
    while (t.size < size)
        t = BitString.of(false).plus(t)
    return t
}

fun <T> HashMapEOf(src: Map<BitString, T>, n: Int): HashMapE<T> = if (src.isEmpty()) {
    HashMapE.of()
} else {

    RootHashMapE(HashMapEdgeOf(src.keys.map {
        pad(it, n) to src.get(it)!!
    }.toMap(), n))

//    RootHashMapE(HashMapEdgeOf(src, n))
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions