129 lines
3.6 KiB
Kotlin
129 lines
3.6 KiB
Kotlin
/*
|
|
* MIT License
|
|
*
|
|
* Copyright (c) 2019 norangebit
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
* copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
* SOFTWARE.
|
|
*
|
|
*/
|
|
|
|
package it.norangeb.algorithms.exam
|
|
|
|
import arrow.core.None
|
|
import arrow.core.Option
|
|
import arrow.core.toOption
|
|
|
|
class ThreeWayHeap<T> private constructor(
|
|
private val shouldExchange: (T, T) -> Boolean
|
|
) {
|
|
private val array: Array<T?> = arrayOfNulls<Any>(1000) as Array<T?>
|
|
private var size = 0
|
|
|
|
fun insert(elem: T) {
|
|
array[++size] = elem
|
|
|
|
pushUp(size)
|
|
}
|
|
|
|
fun pop(): Option<T> {
|
|
if (size <= 0)
|
|
return None
|
|
|
|
val elem = array[FIRST_ELEMENT]
|
|
exchange(FIRST_ELEMENT, size)
|
|
array[size--] = null
|
|
pullDown(FIRST_ELEMENT)
|
|
|
|
return elem.toOption()
|
|
}
|
|
|
|
fun peek(): Option<T> {
|
|
if (size <= 0)
|
|
return None
|
|
|
|
return array[FIRST_ELEMENT].toOption()
|
|
}
|
|
|
|
private fun pushUp(k: Int) {
|
|
val parent = k.parent()
|
|
when {
|
|
k <= FIRST_ELEMENT -> return
|
|
shouldExchange(
|
|
array[parent] ?: return,
|
|
array[k] ?: return
|
|
) -> {
|
|
exchange(k, parent)
|
|
pushUp(parent)
|
|
}
|
|
}
|
|
}
|
|
|
|
private fun pullDown(k: Int) {
|
|
var i = k
|
|
var child = child(i)
|
|
|
|
while (child >= 0) {
|
|
exchange(i, child)
|
|
i = child
|
|
child = child(i)
|
|
}
|
|
|
|
pushUp(i)
|
|
}
|
|
|
|
private fun child(k: Int): Int {
|
|
val leftChild = array[k.leftChild()] ?: return -1
|
|
val child = array[k.child()] ?: return k.leftChild()
|
|
val rightChild = array[k.rightChild()] ?: return if (
|
|
shouldExchange(leftChild, child)
|
|
) k.child() else k.leftChild()
|
|
|
|
return when {
|
|
shouldExchange(leftChild, child) -> if (
|
|
shouldExchange(child, rightChild)
|
|
) k.rightChild() else k.child()
|
|
else -> if (shouldExchange(leftChild, rightChild))
|
|
k.rightChild() else k.leftChild()
|
|
}
|
|
}
|
|
|
|
private fun exchange(i: Int, j: Int) {
|
|
array[i] = array[j]
|
|
.also { array[j] = array[i] }
|
|
}
|
|
|
|
companion object {
|
|
|
|
fun <T : Comparable<T>> createMinHeap() = ThreeWayHeap<T> { t1, t2 ->
|
|
t1 > t2
|
|
}
|
|
|
|
private fun Int.child() = this * THREE_WAY
|
|
|
|
private fun Int.leftChild() = this * THREE_WAY - 1
|
|
|
|
private fun Int.rightChild() = this * THREE_WAY + 1
|
|
|
|
private fun Int.parent(): Int = (this + 1) / 3
|
|
|
|
const val THREE_WAY = 3
|
|
const val FIRST_ELEMENT = 1
|
|
}
|
|
}
|