From 079723937d47d980a0cbd5eabca66366de064127 Mon Sep 17 00:00:00 2001 From: norangebit Date: Sat, 13 Apr 2019 19:16:42 +0200 Subject: [PATCH] clean up code --- .../datastructures/queue/priority/BinaryHeap.kt | 16 +++------------- .../algorithms/exercises/OrderedListSorter.kt | 4 ++-- 2 files changed, 5 insertions(+), 15 deletions(-) diff --git a/src/main/kotlin/it/norangeb/algorithms/datastructures/queue/priority/BinaryHeap.kt b/src/main/kotlin/it/norangeb/algorithms/datastructures/queue/priority/BinaryHeap.kt index eebe71c..d453bff 100644 --- a/src/main/kotlin/it/norangeb/algorithms/datastructures/queue/priority/BinaryHeap.kt +++ b/src/main/kotlin/it/norangeb/algorithms/datastructures/queue/priority/BinaryHeap.kt @@ -57,16 +57,8 @@ class BinaryHeap private constructor( return result.toOption() } - private fun print() { - print("|") - array.forEach { - print(" $it |") - } - println() - } - override fun peek(): Option = if (size >= 1) - array[1].toOption() + array[FIRST_ELEMENT].toOption() else None @@ -84,7 +76,6 @@ class BinaryHeap private constructor( exchange(array, k / WAY, k) pushUp(array, k / WAY) } - else -> return } } @@ -109,9 +100,8 @@ class BinaryHeap private constructor( } private fun exchange(array: Array, i: Int, j: Int) { - array[i] = array[j].also { - array[j] = array[i] - } + array[i] = array[j] + .also { array[j] = array[i] } } private fun isFull(): Boolean = size + 1 == array.size diff --git a/src/main/kotlin/it/norangeb/algorithms/exercises/OrderedListSorter.kt b/src/main/kotlin/it/norangeb/algorithms/exercises/OrderedListSorter.kt index aab7296..2d4193e 100644 --- a/src/main/kotlin/it/norangeb/algorithms/exercises/OrderedListSorter.kt +++ b/src/main/kotlin/it/norangeb/algorithms/exercises/OrderedListSorter.kt @@ -60,8 +60,8 @@ object OrderedListSorter { fun > sort(vararg lists: List): List { val heap = BinaryHeap.createMinPriorityQueue, T> { it.value } - lists.forEachIndexed { index, list -> - heap.insert(Node(list.first(), index, 0)) + lists.forEachIndexed { k, list -> + heap.insert(Node(list.first(), k, 0)) } val outList = ArrayList()