11package space.kscience.kmath.gsl.codegen
22
33import org.intellij.lang.annotations.Language
4- import org.jetbrains.kotlin.com.intellij.openapi.project.Project
5- import org.jetbrains.kotlin.name.FqName
6- import org.jetbrains.kotlin.psi.KtFile
7- import org.jetbrains.kotlin.psi.KtPsiFactory
8- import org.jetbrains.kotlin.resolve.ImportPath
94import java.io.File
105
11- private fun KtPsiFactory.createMatrixClass (
12- f : KtFile ,
6+ private fun Appendable.createMatrixClass (
137 cTypeName : String ,
148 kotlinTypeName : String ,
15- kotlinTypeAlias : String = kotlinTypeName
169) {
1710 fun fn (pattern : String ) = fn(pattern, cTypeName)
18- val className = " Gsl${kotlinTypeAlias } Matrix"
11+ val className = " Gsl${kotlinTypeName } Matrix"
1912 val structName = sn(" gsl_matrixR" , cTypeName)
2013
2114 @Language(" kotlin" ) val text = """ internal class $className (
@@ -29,18 +22,20 @@ private fun KtPsiFactory.createMatrixClass(
2922 override val colNum: Int
3023 get() = nativeHandle.pointed.size2.toInt()
3124
32- override val rows: Buffer<Buffer<$kotlinTypeName >>
33- get() = VirtualBuffer(rowNum) { r ->
34- Gsl${kotlinTypeAlias} Vector(
25+ @PerformancePitfall
26+ override val rows: List<Buffer<$kotlinTypeName >>
27+ get() = List(rowNum) { r ->
28+ Gsl${kotlinTypeName} Vector(
3529 ${fn(" gsl_matrixRrow" )} (nativeHandle, r.toULong()).placeTo(scope).pointed.vector.ptr,
3630 scope,
3731 false,
3832 )
3933 }
4034
41- override val columns: Buffer<Buffer<$kotlinTypeName >>
42- get() = VirtualBuffer(rowNum) { c ->
43- Gsl${kotlinTypeAlias} Vector(
35+ @PerformancePitfall
36+ override val columns: List<Buffer<$kotlinTypeName >>
37+ get() = List(rowNum) { c ->
38+ Gsl${kotlinTypeName} Vector(
4439 ${fn(" gsl_matrixRcolumn" )} (nativeHandle, c.toULong()).placeTo(scope).pointed.vector.ptr,
4540 scope,
4641 false,
@@ -60,45 +55,31 @@ private fun KtPsiFactory.createMatrixClass(
6055 }
6156
6257 override fun close(): Unit = ${fn(" gsl_matrixRfree" )} (nativeHandle)
63-
64- override fun equals(other: Any?): Boolean {
65- if (other is $className )
66- return ${fn(" gsl_matrixRequal" )} (nativeHandle, other.nativeHandle) == 1
67-
68- return super.equals(other)
69- }
7058}"""
71- f + = createClass (text)
72- f + = createNewLine( 2 )
59+ appendLine (text)
60+ appendLine( )
7361}
7462
7563/* *
7664 * Generates matrices source code for kmath-gsl.
7765 */
78- fun matricesCodegen (outputFile : String , project : Project = createProject()) {
79- val f = KtPsiFactory (project, true ).run {
80- createFile(" " ).also { f ->
81- f + = createPackageDirective(FqName (" space.kscience.kmath.gsl" ))
82- f + = createNewLine(2 )
83- f + = createImportDirective(ImportPath .fromString(" kotlinx.cinterop.*" ))
84- f + = createNewLine(1 )
85- f + = createImportDirective(ImportPath .fromString(" space.kscience.kmath.structures.*" ))
86- f + = createNewLine(1 )
87- f + = createImportDirective(ImportPath .fromString(" org.gnu.gsl.*" ))
88- f + = createNewLine(2 )
89- createMatrixClass(f, " double" , " Double" , " Real" )
90- createMatrixClass(f, " float" , " Float" )
91- createMatrixClass(f, " short" , " Short" )
92- createMatrixClass(f, " ushort" , " UShort" )
93- createMatrixClass(f, " long" , " Long" )
94- createMatrixClass(f, " ulong" , " ULong" )
95- createMatrixClass(f, " int" , " Int" )
96- createMatrixClass(f, " uint" , " UInt" )
97- }
98- }
99-
100- File (outputFile).apply {
101- parentFile.mkdirs()
102- writeText(f.text)
66+ fun matricesCodegen (outputFile : String ): Unit = File (outputFile).run {
67+ parentFile.mkdirs()
68+ writer().use {
69+ it.appendLine(" package space.kscience.kmath.gsl" )
70+ it.appendLine()
71+ it.appendLine(" import kotlinx.cinterop.*" )
72+ it.appendLine(" import org.gnu.gsl.*" )
73+ it.appendLine(" import space.kscience.kmath.misc.PerformancePitfall" )
74+ it.appendLine(" import space.kscience.kmath.structures.*" )
75+ it.appendLine()
76+ it.createMatrixClass(" double" , " Double" )
77+ it.createMatrixClass(" float" , " Float" )
78+ it.createMatrixClass(" short" , " Short" )
79+ it.createMatrixClass(" ushort" , " UShort" )
80+ it.createMatrixClass(" long" , " Long" )
81+ it.createMatrixClass(" ulong" , " ULong" )
82+ it.createMatrixClass(" int" , " Int" )
83+ it.createMatrixClass(" uint" , " UInt" )
10384 }
10485}
0 commit comments