diff --git a/settings.gradle b/settings.gradle deleted file mode 100644 index dc3d3c0..0000000 --- a/settings.gradle +++ /dev/null @@ -1 +0,0 @@ -rootProject.name = 'kotlin-wheels' diff --git a/settings.gradle.kts b/settings.gradle.kts new file mode 100644 index 0000000..d66c596 --- /dev/null +++ b/settings.gradle.kts @@ -0,0 +1,5 @@ +pluginManagement { + val kotlinVersion: String by settings +} + +rootProject.name = "kotlin-wheels" diff --git a/src/commonMain/kotlin/ru/spbstu/wheels/Heap.kt b/src/commonMain/kotlin/ru/spbstu/wheels/Heap.kt index 2344903..dd29220 100644 --- a/src/commonMain/kotlin/ru/spbstu/wheels/Heap.kt +++ b/src/commonMain/kotlin/ru/spbstu/wheels/Heap.kt @@ -107,10 +107,13 @@ class BinaryHeap(override val comparator: Comparator): Heap, AbstractMu /* Queue implementation */ override fun put(value: T) { add(value) } - override fun take(): T = current?.also { removeAt(0) } ?: throw IllegalArgumentException("Heap is empty") + override fun take(): T { + require(size != 0) { "Heap is empty" } + return uncheckedCast(data[0].also { removeAt(0) }) + } @Suppress(Warnings.UNCHECKED_CAST) override val current: T? - get() = data[0] + get() = runIf(size != 0) { data[0] } override fun isEmpty(): Boolean = size == 0 } diff --git a/src/commonMain/kotlin/ru/spbstu/wheels/Misc.kt b/src/commonMain/kotlin/ru/spbstu/wheels/Misc.kt index 184bda4..263ae53 100644 --- a/src/commonMain/kotlin/ru/spbstu/wheels/Misc.kt +++ b/src/commonMain/kotlin/ru/spbstu/wheels/Misc.kt @@ -2,9 +2,18 @@ package ru.spbstu.wheels import kotlin.contracts.ExperimentalContracts +import kotlin.contracts.InvocationKind import kotlin.contracts.contract inline infix fun Int.times(body: (Int) -> Unit) { contract { callsInPlace(body) } repeat(this, body) } + +inline fun runIf(condition: Boolean, body: () -> T): T? { + contract { + returnsNotNull() implies condition + callsInPlace(body, InvocationKind.AT_MOST_ONCE) + } + return if (condition) body() else null +} diff --git a/src/commonTest/kotlin/ru/spbstu/wheels/HeapTest.kt b/src/commonTest/kotlin/ru/spbstu/wheels/HeapTest.kt index 3344c7c..f358b6a 100644 --- a/src/commonTest/kotlin/ru/spbstu/wheels/HeapTest.kt +++ b/src/commonTest/kotlin/ru/spbstu/wheels/HeapTest.kt @@ -3,9 +3,16 @@ package ru.spbstu.wheels import kotlin.test.Test import kotlin.random.Random import kotlin.test.assertEquals +import kotlin.test.assertTrue class HeapTest { + @Test + fun emptyHeapTest() { + val h = heap() + assertEquals(null, h.current) + } + @Test fun heapSortTest() { diff --git a/src/commonTest/kotlin/ru/spbstu/wheels/MiscTest.kt b/src/commonTest/kotlin/ru/spbstu/wheels/MiscTest.kt new file mode 100644 index 0000000..65bf7e6 --- /dev/null +++ b/src/commonTest/kotlin/ru/spbstu/wheels/MiscTest.kt @@ -0,0 +1,25 @@ +package ru.spbstu.wheels + +import kotlin.test.Test +import kotlin.test.assertEquals + +class MiscTest { + @Test + fun repeat() { + var i = 0 + 3 times { + ++i + } + assertEquals(3, i) + } + + @Test + fun makeIf() { + val numbers = 1..80 + + assertEquals( + (3..80 step 3).toList(), + numbers.mapNotNull { runIf(it % 3 == 0) { it } } + ) + } +} \ No newline at end of file