This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.vivint | |
| enum class Color(val code: Long) { | |
| RED(0xff0000L), | |
| GREEN(0x00ff00L), | |
| BLUE(0x0000ffL) | |
| } | |
| fun main() { | |
| // 1. Stable replacement of the enum class values function |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| override fun delete(key: String) { | |
| key.fold(root) { node, char -> | |
| node.children[char] ?: return | |
| }.value = null | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| override fun delete(key: String) { | |
| // search leaf | |
| key.fold(root) { node, char -> | |
| node.children[char] ?: return | |
| }.let { leaf -> | |
| leaf.value = null | |
| key.foldRight(leaf) { char, current -> | |
| if (current.children.isEmpty() && current.value == null) { | |
| current.parent?.apply { children.remove(char) } ?: return | |
| } else return |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| override fun delete(key: String) { | |
| delete(key, 0, root) | |
| } | |
| private fun delete(key: String, index: Int, node: Node<Value>) { | |
| if (index == key.length) | |
| node.value = null | |
| else node.children[key[index]]?.run { | |
| delete(key, index + 1, this) | |
| if (children.isEmpty() && value == null) node.children.remove(key[index]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| override fun delete(key: String) { | |
| delete(key, 0, root) | |
| } | |
| private fun delete(key: String, index: Int, node: Node<Value>): Trie.Node<Value>? { | |
| if (index == key.length) { | |
| node.value = null | |
| if (node.children.isEmpty()) return null | |
| return node | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| override fun search(key: String) = key.fold(root) { node, char -> | |
| node.children[char] ?: return null | |
| }.value |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| override fun search(key: String): Value? { | |
| var currentNode = root | |
| for (char in key) { | |
| if (currentNode.children[char] == null) { | |
| return null | |
| } | |
| currentNode = currentNode.children[char]!! | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fun insert(key: String, value: Value) { | |
| key.fold(root) { node, char -> | |
| node.children[char] | |
| }.value = value | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fun insert(key: String, value: Value) { | |
| key.fold(root) { node, char -> | |
| node.children[char] ?: Node<Value>().also { node.children[char] = it } | |
| }.value = value | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fun insert(key: String, value: Value) { | |
| var currentNode = root | |
| for (char in key) { | |
| if (currentNode.children[char] == null) { | |
| currentNode.children[char] = Node() | |
| } | |
| currentNode = currentNode.children[char]!! | |
| } | |
NewerOlder