Skip to content

Commit

Permalink
m
Browse files Browse the repository at this point in the history
  • Loading branch information
belyaev-mikhail committed Aug 4, 2021
1 parent f405c26 commit f4a4ba2
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 3 deletions.
1 change: 0 additions & 1 deletion settings.gradle

This file was deleted.

5 changes: 5 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pluginManagement {
val kotlinVersion: String by settings
}

rootProject.name = "kotlin-wheels"
7 changes: 5 additions & 2 deletions src/commonMain/kotlin/ru/spbstu/wheels/Heap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,13 @@ class BinaryHeap<T>(override val comparator: Comparator<T>): Heap<T>, 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
}
Expand Down
9 changes: 9 additions & 0 deletions src/commonMain/kotlin/ru/spbstu/wheels/Misc.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T> runIf(condition: Boolean, body: () -> T): T? {
contract {
returnsNotNull() implies condition
callsInPlace(body, InvocationKind.AT_MOST_ONCE)
}
return if (condition) body() else null
}
7 changes: 7 additions & 0 deletions src/commonTest/kotlin/ru/spbstu/wheels/HeapTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int>()
assertEquals(null, h.current)
}

@Test
fun heapSortTest() {

Expand Down
25 changes: 25 additions & 0 deletions src/commonTest/kotlin/ru/spbstu/wheels/MiscTest.kt
Original file line number Diff line number Diff line change
@@ -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 } }
)
}
}

0 comments on commit f4a4ba2

Please sign in to comment.