Skip to content

Commit

Permalink
jacoco support
Browse files Browse the repository at this point in the history
  • Loading branch information
belyaev-mikhail committed Jul 22, 2021
1 parent ba0cfe4 commit bf4dac9
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 13 deletions.
44 changes: 31 additions & 13 deletions src/commonMain/kotlin/ru/spbstu/wheels/Breakable.kt
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
@file:OptIn(ExperimentalContracts::class)
package ru.spbstu.wheels

import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract

object Break : NoStackThrowable("break")
object Continue : NoStackThrowable("continue")

object BreakableContext {
inline val break_: Nothing get() = throw Break
inline val continue_: Nothing get() = throw Continue

inline fun <T> iteration(body: BreakableContext.() -> T): T? =
try {
this.body()
} catch (_: Continue) {
null
}

inline fun <T> loop(body: BreakableContext.() -> T): T? =
try {
this.body()
} catch (_: Break) {
null
}
inline fun <T> iteration(body: BreakableContext.() -> T): T? {
contract { callsInPlace(body, InvocationKind.EXACTLY_ONCE) }
return try {
this.body()
} catch (_: Continue) {
null
}
}

inline fun <T> loop(body: BreakableContext.() -> T): T? {
contract { callsInPlace(body, InvocationKind.EXACTLY_ONCE) }
return try {
this.body()
} catch (_: Break) {
null
}
}
}

inline fun <T> Iterable<T>.forEachB(body: BreakableContext.(T) -> Unit) {
contract { callsInPlace(body) }
BreakableContext.loop {
forEach {
iteration { body(it) }
Expand All @@ -31,6 +41,7 @@ inline fun <T> Iterable<T>.forEachB(body: BreakableContext.(T) -> Unit) {
}

inline fun <T> Sequence<T>.forEachB(body: BreakableContext.(T) -> Unit) {
contract { callsInPlace(body) }
BreakableContext.loop {
forEach {
iteration { body(it) }
Expand All @@ -39,6 +50,7 @@ inline fun <T> Sequence<T>.forEachB(body: BreakableContext.(T) -> Unit) {
}

inline fun <T> Iterable<T>.forEachIndexedB(body: BreakableContext.(Int, T) -> Unit) {
contract { callsInPlace(body) }
BreakableContext.loop {
forEachIndexed { i, e ->
iteration { body(i, e) }
Expand All @@ -47,14 +59,17 @@ inline fun <T> Iterable<T>.forEachIndexedB(body: BreakableContext.(Int, T) -> Un
}

inline fun <T> Sequence<T>.forEachIndexedB(body: BreakableContext.(Int, T) -> Unit) {
contract { callsInPlace(body) }
BreakableContext.loop {
forEachIndexed { i, e ->
iteration { body(i, e) }
}
}
}


inline fun repeatB(times: Int, body: BreakableContext.(Int) -> Unit) {
contract { callsInPlace(body) }
BreakableContext.loop {
repeat(times) {
iteration { body(it) }
Expand All @@ -63,6 +78,7 @@ inline fun repeatB(times: Int, body: BreakableContext.(Int) -> Unit) {
}

inline fun <T, U, C: MutableCollection<U>> Iterable<T>.mapOrBreakTo(to: C, body: BreakableContext.(T) -> U): C {
contract { callsInPlace(body) }
BreakableContext.loop {
forEach {
iteration { to.add(body(it)) }
Expand All @@ -75,6 +91,7 @@ inline fun <T, U> Iterable<T>.mapOrBreak(body: BreakableContext.(T) -> U): List<
mapOrBreakTo(mutableListOf(), body)

inline fun <T, U, C: MutableCollection<U>> Sequence<T>.mapOrBreakTo(to: C, body: BreakableContext.(T) -> U): C {
contract { callsInPlace(body) }
BreakableContext.loop {
forEach {
iteration { to.add(body(it)) }
Expand All @@ -92,6 +109,7 @@ inline fun <T, U> Sequence<T>.mapOrBreak(crossinline body: BreakableContext.(T)
}

inline fun <T, U, C: MutableCollection<U>> Iterable<T>.mapIndexedOrBreakTo(to: C, body: BreakableContext.(Int, T) -> U): C {
contract { callsInPlace(body) }
BreakableContext.loop {
forEachIndexed { i, it ->
iteration { to.add(body(i, it)) }
Expand Down
13 changes: 13 additions & 0 deletions src/commonMain/kotlin/ru/spbstu/wheels/Collections.kt
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,16 @@ inline fun <T> Iterable<T>.anyIndexed(body: (Int, T) -> Boolean): Boolean {
}
return false
}

inline fun <T, C1: MutableCollection<T>, C2: MutableCollection<T>> Iterable<T>.partitionTo(
c1: C1, c2: C2, body: (T) -> Boolean
): Pair<C1, C2> {
for (e in this) {
if (body(e)) {
c1.add(e)
} else {
c2.add(e)
}
}
return Pair(c1, c2)
}
17 changes: 17 additions & 0 deletions src/commonMain/kotlin/ru/spbstu/wheels/Maps.kt
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,20 @@ fun <A, B, M: MutableMap<A, B>> Iterable<A>.zipTo(that: Iterable<B>, to: M): M {

@Suppress(Warnings.NOTHING_TO_INLINE)
inline fun <K, V> Map<K, V>.asMap(): Map<K, V> = this


inline fun <K, V, C1: MutableMap<K, V>, C2: MutableMap<K, V>> Map<K, V>.partitionTo(
c1: C1, c2: C2, body: (K, V) -> Boolean
): Pair<C1, C2> {
for ((k, v) in this) {
if (body(k, v)) {
c1.put(k, v)
} else {
c2.put(k, v)
}
}
return Pair(c1, c2)
}

inline fun <K, V> Map<K, V>.partition(body: (K, V) -> Boolean): Pair<Map<K, V>, Map<K, V>> =
partitionTo(mutableMapOf(), mutableMapOf(), body)
10 changes: 10 additions & 0 deletions src/commonMain/kotlin/ru/spbstu/wheels/Misc.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@file:OptIn(ExperimentalContracts::class)
package ru.spbstu.wheels

import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.contract

inline infix fun Int.times(body: (Int) -> Unit) {
contract { callsInPlace(body) }
repeat(this, body)
}

0 comments on commit bf4dac9

Please sign in to comment.