Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: directly load infrastructure from RailJSON #7248

Merged
merged 4 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ dependencies {
implementation project(':kt-osrd-signaling')
implementation project(":kt-osrd-sim-interlocking")
implementation project(":kt-osrd-sim-infra")
implementation project(":kt-osrd-rjs-parser")
implementation project(":kt-osrd-sncf-signaling")

// base primitives
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,46 @@ private fun CollectionItemType.generateArray(context: GeneratorContext, currentF
set(index.toInt(), value)
}
/**
* Array must be sorted.
* If the array contains the value, return its index.
* Otherwise returns -(insertion offset) - 1, where insertion offset is
* where to insert the new value to keep the array sorted.
*/
fun binarySearch(
value: $type,
comparator: ${simpleName}Comparator${paramsUse},
fromIndex: Int,
toIndex: Int,
): Int {
var low = fromIndex
var high = toIndex - 1
while (low <= high) {
val mid = (low + high) ushr 1
val midVal = data[mid]
val cmp = comparator.compare(${storageType.fromPrimitive("midVal")}, value)
if (cmp < 0)
low = mid + 1
else if (cmp > 0)
high = mid - 1
else
return mid
}
return -(low + 1)
}
/**
* Array must be sorted.
* If the array contains the value, return its index.
* Otherwise returns -(insertion offset) - 1, where insertion offset is
* where to insert the new value to keep the array sorted.
*/
fun binarySearch(value: $type, comparator: ${simpleName}Comparator${paramsUse}): Int {
return binarySearch(value, comparator, 0, data.size)
}
/** Creates an iterator over the elements of the array. */
operator fun iterator(): Iterator<$type> {
return object : Iterator<$type> {
Expand Down Expand Up @@ -145,6 +185,33 @@ private fun CollectionItemType.generateArray(context: GeneratorContext, currentF
fun copyOf(size: Int): ${simpleName}Array${paramsUse} {
return ${simpleName}Array${paramsUse}(data.copyOf(size))
}
fun binarySearch(
value: $type,
comparator: ${simpleName}Comparator${paramsUse},
fromIndex: Int,
toIndex: Int,
): Int {
var low = fromIndex
var high = toIndex - 1
while (low <= high) {
val mid = (low + high) ushr 1
val midVal = data[mid]
val cmp = comparator.compare(${storageType.fromPrimitive("midVal")}, value)
if (cmp < 0)
low = mid + 1
else if (cmp > 0)
high = mid - 1
else
return mid
}
return -(low + 1)
}
fun binarySearch(value: $type, comparator: ${simpleName}Comparator${paramsUse}): Int {
return binarySearch(value, comparator, 0, data.size)
}
/** Creates an iterator over the elements of the array. */
operator fun iterator(): Iterator<$type> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ private fun CollectionItemType.generateInterfaces(context: GeneratorContext, cur
val size: Int
}
fun interface ${simpleName}Comparator${paramsDecl} {
fun compare(a: $type, b: $type): Int
}
/** GENERATED CODE */
@Suppress("INAPPLICABLE_JVM_NAME")
interface ${simpleName}List${paramsDecl} : ${simpleName}Collection${paramsUse} {
Expand Down
51 changes: 51 additions & 0 deletions core/kt-osrd-rjs-parser/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
plugins {
alias(libs.plugins.kotlin.jvm)
alias(libs.plugins.ksp)
id 'jacoco'
}

repositories {
mavenCentral()
}

java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}

dependencies {
// PLEASE ADD AND UPDATE DEPENDENCIES USING libs.versions.toml
implementation project(':kt-fast-collections')
ksp project(':kt-fast-collections-generator')
implementation project(':osrd-geom')
implementation libs.guava
implementation libs.kotlin.stdlib
implementation libs.kotlin.logging

api project(':kt-osrd-sim-infra')
api project(":kt-osrd-utils")
testImplementation libs.kotlin.test

// Use JUnit Jupiter API for testing.
testImplementation libs.junit.jupiter.api
}

// to get KSP generated-stuff to be recognised
kotlin {
sourceSets {
main.kotlin.srcDirs += 'build/generated/ksp/main/kotlin'
test.kotlin.srcDirs += 'build/generated/ksp/test/kotlin'
}
}

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
kotlinOptions {
freeCompilerArgs += [
"-opt-in=kotlin.RequiresOptIn",
"-opt-in=kotlin.ExperimentalUnsignedTypes",
"-opt-in=kotlin.time.ExperimentalTime",
"-opt-in=kotlin.contracts.ExperimentalContracts",
]
}
}
Loading
Loading