diff options
Diffstat (limited to 'opendc-stdlib/src/main/kotlin/com/atlarge/opendc/simulator')
3 files changed, 0 insertions, 225 deletions
diff --git a/opendc-stdlib/src/main/kotlin/com/atlarge/opendc/simulator/Helpers.kt b/opendc-stdlib/src/main/kotlin/com/atlarge/opendc/simulator/Helpers.kt deleted file mode 100644 index 0f6392ed..00000000 --- a/opendc-stdlib/src/main/kotlin/com/atlarge/opendc/simulator/Helpers.kt +++ /dev/null @@ -1,44 +0,0 @@ -package com.atlarge.opendc.simulator - -import kotlin.coroutines.experimental.intrinsics.suspendCoroutineOrReturn - -/** - * Try to find the [Context] instance associated with the [Process] in the call chain which has (indirectly) invoked the - * caller of this method. - * - * Note however that this method does not guarantee type-safety as this method allows the user to cast to a context - * with different generic type arguments. - * - * @return The context that has been found or `null` if this method is not called in a simulation context. - */ -suspend fun <S, M> contextOrNull(): Context<S, M>? = suspendCoroutineOrReturn { it.context[Context] } - -/** - * Find the [Context] instance associated with the [Process] in the call chain which has (indirectly) invoked the - * caller of this method. - * - * Note however that this method does not guarantee type-safety as this method allows the user to cast to a context - * with different generic type arguments. - * - * @throws IllegalStateException if the context cannot be found. - * @return The context that has been found. - */ -suspend fun <S, M> context(): Context<S, M> = - contextOrNull() ?: throw IllegalStateException("The suspending call does not have an associated process context") - -/** - * Try to find the untyped [Context] instance associated with the [Process] in the call chain which has (indirectly) - * invoked the caller of this method. - * - * @return The untyped context that has been found or `null` if this method is not called in a simulation context. - */ -suspend fun untypedContextOrNull(): Context<*, *>? = contextOrNull<Any?, Any?>() - -/** - * Find the [Context] instance associated with the [Process] in the call chain which has (indirectly) invoked the - * caller of this method. - * - * @throws IllegalStateException if the context cannot be found. - * @return The untyped context that has been found. - */ -suspend fun untypedContext(): Context<*, *> = context<Any?, Any?>() diff --git a/opendc-stdlib/src/main/kotlin/com/atlarge/opendc/simulator/instrumentation/Helpers.kt b/opendc-stdlib/src/main/kotlin/com/atlarge/opendc/simulator/instrumentation/Helpers.kt deleted file mode 100644 index e303133a..00000000 --- a/opendc-stdlib/src/main/kotlin/com/atlarge/opendc/simulator/instrumentation/Helpers.kt +++ /dev/null @@ -1,49 +0,0 @@ -package com.atlarge.opendc.simulator.instrumentation - -import kotlinx.coroutines.experimental.Unconfined -import kotlinx.coroutines.experimental.channels.ReceiveChannel -import kotlinx.coroutines.experimental.channels.consumeEach -import kotlinx.coroutines.experimental.channels.produce -import kotlinx.coroutines.experimental.channels.toChannel -import kotlinx.coroutines.experimental.launch -import kotlin.coroutines.experimental.CoroutineContext -import kotlin.coroutines.experimental.coroutineContext - -/** - * Transform each element in the channel into a [ReceiveChannel] of output elements that is then flattened into the - * output stream by emitting elements from the channels as they become available. - * - * @param context The [CoroutineContext] to run the operation in. - * @param transform The function to transform the elements into channels. - * @return The flattened [ReceiveChannel] of merged elements. - */ -fun <E, R> ReceiveChannel<E>.flatMapMerge(context: CoroutineContext = Unconfined, - transform: suspend (E) -> ReceiveChannel<R>): ReceiveChannel<R> = - produce(context) { - val job = launch(Unconfined) { - consumeEach { - launch(coroutineContext) { - transform(it).toChannel(this@produce) - } - } - } - job.join() - } - -/** - * Merge this channel with the other channel into an output stream by emitting elements from the channels as they - * become available. - * - * @param context The [CoroutineContext] to run the operation in. - * @param other The other channel to merge with. - * @return The [ReceiveChannel] of merged elements. - */ -fun <E, E1: E, E2: E> ReceiveChannel<E1>.merge(context: CoroutineContext = Unconfined, - other: ReceiveChannel<E2>): ReceiveChannel<E> = - produce(context) { - val job = launch(Unconfined) { - launch(coroutineContext) { toChannel(this@produce) } - launch(coroutineContext) { other.toChannel(this@produce) } - } - job.join() - } diff --git a/opendc-stdlib/src/main/kotlin/com/atlarge/opendc/simulator/instrumentation/Interpolation.kt b/opendc-stdlib/src/main/kotlin/com/atlarge/opendc/simulator/instrumentation/Interpolation.kt deleted file mode 100644 index 5a033ff3..00000000 --- a/opendc-stdlib/src/main/kotlin/com/atlarge/opendc/simulator/instrumentation/Interpolation.kt +++ /dev/null @@ -1,132 +0,0 @@ -/* - * MIT License - * - * Copyright (c) 2017 atlarge-research - * - * 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 com.atlarge.opendc.simulator.instrumentation - -import kotlinx.coroutines.experimental.Unconfined -import kotlinx.coroutines.experimental.channels.ReceiveChannel -import kotlinx.coroutines.experimental.channels.consume -import kotlinx.coroutines.experimental.channels.produce -import kotlin.coroutines.experimental.CoroutineContext - -/** - * Interpolate [n] amount of elements between every two occurrences of elements passing through the channel. - * - * The operation is _intermediate_ and _stateless_. - * This function [consumes][consume] all elements of the original [ReceiveChannel]. - * - * @param context The context of the coroutine. - * @param n The amount of elements to interpolate between the actual elements in the channel. - * @param interpolator A function to interpolate between the two element occurrences. - */ -fun <E> ReceiveChannel<E>.interpolate(n: Int, context: CoroutineContext = Unconfined, - interpolator: (Double, E, E) -> E): ReceiveChannel<E> { - require(n >= 0) { "The amount to interpolate must be non-negative" } - - // If we do not want to interpolate any elements, just return the original channel - if (n == 0) { - return this - } - - return produce(context) { - consume { - val iterator = iterator() - - if (!iterator.hasNext()) - return@produce - - var a = iterator.next() - send(a) - - while (iterator.hasNext()) { - val b = iterator.next() - for (i in 1..n) { - send(interpolator(i.toDouble() / (n + 1), a, b)) - } - send(b) - a = b - } - } - } -} - -/** - * Perform a linear interpolation on the given double values. - * - * @param a The start value - * @param b The end value - * @param f The amount to interpolate which represents the position between the two values as a percentage in [0, 1]. - * @return The interpolated double result between the double values. - */ -fun lerp(a: Double, b: Double, f: Double): Double = a + f * (b - a) - -/** - * Perform a linear interpolation on the given float values. - * - * @param a The start value - * @param b The end value - * @param f The amount to interpolate which represents the position between the two values as a percentage in [0, 1]. - * @return The interpolated float result between the float values. - */ -fun lerp(a: Float, b: Float, f: Float): Float = a + f * (b - a) - -/** - * Perform a linear interpolation on the given integer values. - * - * @param a The start value - * @param b The end value - * @param f The amount to interpolate which represents the position between the two values as a percentage in [0, 1]. - * @return The interpolated integer result between the integer values. - */ -fun lerp(a: Int, b: Int, f: Float): Int = lerp(a.toFloat(), b.toFloat(), f).toInt() - -/** - * Perform a linear interpolation on the given integer values. - * - * @param a The start value - * @param b The end value - * @param f The amount to interpolate which represents the position between the two values as a percentage in [0, 1]. - * @return The interpolated integer result between the integer values. - */ -fun lerp(a: Int, b: Int, f: Double): Int = lerp(a.toDouble(), b.toDouble(), f).toInt() - -/** - * Perform a linear interpolation on the given long values. - * - * @param a The start value - * @param b The end value - * @param f The amount to interpolate which represents the position between the two values as a percentage in [0, 1]. - * @return The interpolated long result between the long values. - */ -fun lerp(a: Long, b: Long, f: Double): Long = lerp(a.toDouble(), b.toDouble(), f).toLong() - -/** - * Perform a linear interpolation on the given long values. - * - * @param a The start value - * @param b The end value - * @param f The amount to interpolate which represents the position between the two values as a percentage in [0, 1]. - * @return The interpolated long result between the long values. - */ -fun lerp(a: Long, b: Long, f: Float): Long = lerp(a.toFloat(), b.toFloat(), f).toLong() |
