From 20767a3b35f5fce0b2e7b16d4bf4d0daa11472f6 Mon Sep 17 00:00:00 2001 From: Iaroslav Postovalov Date: Wed, 6 Jan 2021 22:46:32 +0700 Subject: [PATCH] Add convenience scope functions for GSL contexts --- .../kscience/kmath/gsl/GslMatrixContext.kt | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/GslMatrixContext.kt b/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/GslMatrixContext.kt index a1f3445ca..1df21c4b1 100644 --- a/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/GslMatrixContext.kt +++ b/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/GslMatrixContext.kt @@ -2,6 +2,7 @@ package kscience.kmath.gsl import kotlinx.cinterop.CStructVar import kotlinx.cinterop.DeferScope +import kotlinx.cinterop.memScoped import kotlinx.cinterop.pointed import kscience.kmath.linear.MatrixContext import kscience.kmath.linear.Point @@ -23,7 +24,7 @@ internal inline fun GslVector.fill(initializer: * Represents matrix context implementing where all the operations are delegated to GSL. */ public abstract class GslMatrixContext internal constructor( - internal val scope: DeferScope + internal val scope: DeferScope, ) : MatrixContext> { init { ensureHasGslErrorHandler() @@ -100,6 +101,12 @@ public class GslRealMatrixContext(scope: DeferScope) : GslMatrixContext GslRealMatrixContext(block: GslRealMatrixContext.() -> R): R = + memScoped { GslRealMatrixContext(this).block() } + /** * Represents [Float] matrix context implementing where all the operations are delegated to GSL. */ @@ -146,6 +153,12 @@ public class GslFloatMatrixContext(scope: DeferScope) : } } +/** + * Invokes [block] inside newly created [GslFloatMatrixContext] which is disposed when the block is invoked. + */ +public fun GslFloatMatrixContext(block: GslFloatMatrixContext.() -> R): R = + memScoped { GslFloatMatrixContext(this).block() } + /** * Represents [Complex] matrix context implementing where all the operations are delegated to GSL. */ @@ -194,3 +207,9 @@ public class GslComplexMatrixContext(scope: DeferScope) : return g1 } } + +/** + * Invokes [block] inside newly created [GslComplexMatrixContext] which is disposed when the block is invoked. + */ +public fun GslComplexMatrixContext(block: GslComplexMatrixContext.() -> R): R = + memScoped { GslComplexMatrixContext(this).block() }