override fun put()

in environment/src/main/kotlin/jetbrains/exodus/tree/patricia/PatriciaTreeMutable.kt [50:102]


    override fun put(key: ByteIterable, value: ByteIterable): Boolean {
        val it = key.iterator()
        var node: MutableNode = root
        var prev: MutableNode? = null
        var prevFirstByte = 0.toByte()
        while (true) {
            val matchResult = node.matchesKeySequence(it)
            val matchingLength = NodeBase.MatchResult.getMatchingLength(matchResult)
            if (matchingLength < 0) {
                val prefix = node.splitKey(-matchingLength - 1, NodeBase.MatchResult.getKeyByte(matchResult))
                if (NodeBase.MatchResult.hasNext(matchResult)) {
                    prefix.hang(NodeBase.MatchResult.getNextByte(matchResult), it).setValue(value)
                } else {
                    prefix.setValue(value)
                }
                if (prev == null) {
                    root = MutableRoot(prefix, root.sourceAddress)
                } else {
                    prev.setChild(prevFirstByte, prefix)
                }
                ++size
                return true
            }
            if (!it.hasNext()) {
                val oldValue = node.getValue()
                node.setValue(value)
                if (oldValue == null) {
                    ++size
                    return true
                }
                return oldValue != value
            }
            val nextByte = it.next()
            val child = node.getChild(this, nextByte)
            if (child == null) {
                if (node.hasChildren() || node.hasKey() || node.hasValue()) {
                    node.hang(nextByte, it).setValue(value)
                } else {
                    node.setKeySequence(ArrayByteIterable(nextByte, it))
                    node.setValue(value)
                }
                ++size
                return true
            }
            prev = node
            prevFirstByte = nextByte
            val mutableChild = child.getMutableCopy(this)
            if (!child.isMutable) {
                node.setChild(nextByte, mutableChild)
            }
            node = mutableChild
        }
    }