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: incremental spacing resource generation #5040

Merged
merged 1 commit into from
Sep 14, 2023
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 @@ -34,6 +34,7 @@ dependencies {
testImplementation libs.kotlinx.coroutines.test

implementation project(':kt-osrd-utils')
implementation project(':kt-fast-collections')
implementation project(':kt-osrd-signaling')
implementation project(":kt-osrd-sim-interlocking")
implementation project(":kt-osrd-sim-infra")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,10 @@ private fun CollectionItemType.generateRingBuffer(context: GeneratorContext, cur
val simpleName = type.simpleName
val paramsDecl = type.paramsDecl
val paramsUse = type.paramsUse
val paramsStar = type.paramsStar
val fileName = "${simpleName}RingBuffer"
val bufferType = "Mutable${simpleName}Array${paramsUse}"
val itemListType = "${simpleName}List${paramsUse}"
val storageType = storageType!!
val primitiveZero = storageType.primitiveZero()
val toPrimitive = storageType.toPrimitive
val wrapperZero = storageType.fromPrimitive(primitiveZero)
val file = context.codeGenerator.createNewFile(Dependencies(true, currentFile), generatedPackage, fileName)
file.appendText("""
Expand All @@ -45,6 +42,12 @@ private fun CollectionItemType.generateRingBuffer(context: GeneratorContext, cur

val size get() = _size

fun isEmpty() = size == 0
fun isNotEmpty() = size != 0

val beginIndex get() = if (isEmpty()) -1 else startIndex
val endIndex get() = if (isEmpty()) -1 else startIndex + size

/** GENERATED CODE */
override fun iterator(): Iterator<$type> {
return object : Iterator<$type> {
Expand Down Expand Up @@ -167,6 +170,18 @@ private fun CollectionItemType.generateRingBuffer(context: GeneratorContext, cur
return oldValue
}

/** GENERATED CODE */
fun removeFrontUntil(cutoffIndex: Int) {
val removedCount = cutoffIndex - startIndex
if (removedCount == 0)
return
assert(removedCount > 0)
assert(removedCount <= size)
_size -= removedCount
startIndex += removedCount
offset = (offset + removedCount).mod(capacity)
}

/** GENERATED CODE */
fun clone() : Mutable${simpleName}RingBuffer${paramsUse} {
return Mutable${simpleName}RingBuffer${paramsUse}(size, buffer.copyOf())
Expand Down
16 changes: 16 additions & 0 deletions core/kt-fast-collections/src/test/kotlin/TestRingBuffer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,20 @@ class RingBufferTest {
assertEquals(-2, deque.addFront(5))
assertEquals(listOf(5, 3, 1, 2, 4), deque.toList())
}

@Test
fun `test removeFrontUntil`() {
val deque = MutableIntRingBuffer()
assertEquals(0, deque.addFront(1))
assertEquals(1, deque.addBack(2))
assertEquals(-1, deque.addFront(3))
assertEquals(2, deque.addBack(4))
assertEquals(-2, deque.addFront(5))
assertEquals(listOf(5, 3, 1, 2, 4), deque.toList())
deque.removeFrontUntil(1) // 1 is the index of "2"
assertEquals(2, deque[1])
assertEquals(listOf(2, 4), deque.toList())
deque.removeFrontUntil(3) // 3 is the index of "2"
assertEquals(listOf(), deque.toList())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ interface ReservationInfra : LocationInfra {
fun getZonePathChunks(zonePath: ZonePathId): DirStaticIdxList<TrackChunk>
}

fun ReservationInfra.getZonePathZone(zonePath: ZonePathId): ZoneId {
return getNextZone(getZonePathEntry(zonePath))!!
}

/** A zone path is a path inside a zone */
sealed interface ZonePath
typealias ZonePathId = StaticIdx<ZonePath>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface BlockInfraBuilder {
}


class BlockInfraBuilderImpl : BlockInfraBuilder {
class BlockInfraBuilderImpl(val loadedSignalInfra: LoadedSignalInfra, val rawInfra: RawInfra) : BlockInfraBuilder {
private val blockSet = mutableMapOf<BlockDescriptor, BlockId>()
private val blockPool = StaticPool<Block, BlockDescriptor>()
override fun block(
Expand All @@ -27,11 +27,16 @@ class BlockInfraBuilderImpl : BlockInfraBuilder {
signalsDistances: OffsetList<Block>,
): BlockId {
assert(path.size != 0)
val newBlock = BlockDescriptor(startAtBufferStop, stopsAtBufferStop, path, signals, signalsDistances)

var length = Length<Block>(0.meters)
for (zonePath in path)
length += rawInfra.getZonePathLength(zonePath).distance

val newBlock = BlockDescriptor(length, startAtBufferStop, stopsAtBufferStop, path, signals, signalsDistances)
return blockSet.getOrPut(newBlock) { blockPool.add(newBlock) }
}

fun build(loadedSignalInfra: LoadedSignalInfra, rawInfra: RawInfra): BlockInfra {
fun build(): BlockInfra {
return BlockInfraImpl(blockPool, loadedSignalInfra, rawInfra)
}
}
Expand All @@ -42,7 +47,7 @@ fun blockInfraBuilder(
rawInfra: RawInfra,
init: BlockInfraBuilder.() -> Unit,
): BlockInfra {
val builder = BlockInfraBuilderImpl()
val builder = BlockInfraBuilderImpl(loadedSignalInfra, rawInfra)
builder.init()
return builder.build(loadedSignalInfra, rawInfra)
return builder.build()
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import fr.sncf.osrd.utils.indexing.*
import fr.sncf.osrd.utils.units.*

class BlockDescriptor(
val length: Length<Block>,
val startAtBufferStop: Boolean,
val stopsAtBufferStop: Boolean,
val path: StaticIdxList<ZonePath>,
Expand Down Expand Up @@ -117,10 +118,6 @@ class BlockInfraImpl(
}

override fun getBlockLength(block: BlockId): Length<Block> {
var length = Length<Block>(0.meters)
for (path in blockPool[block].path) {
length += rawInfra.getZonePathLength(path).distance
}
return length
return blockPool[block].length
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import fr.sncf.osrd.utils.DistanceRangeMap
import fr.sncf.osrd.utils.distanceRangeMapOf
import fr.sncf.osrd.utils.indexing.DirStaticIdxList
import fr.sncf.osrd.utils.units.Distance
import fr.sncf.osrd.utils.units.Offset
import fr.sncf.osrd.utils.units.meters
import java.lang.RuntimeException

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
primitive = UInt::class,
fromPrimitive = "StaticIdx(%s)",
toPrimitive = "%s.index",
collections = ["Array", "ArrayList", "ArraySortedSet"],
collections = ["Array", "ArrayList", "ArraySortedSet", "RingBuffer"],
)

package fr.sncf.osrd.utils.indexing
Expand Down Expand Up @@ -187,4 +187,4 @@ class VirtualStaticPool<IndexT>(private var _size: UInt) : StaticIdxIterable<Ind
fun space(): StaticIdxSpace<IndexT> {
return StaticIdxSpace(size)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
primitive = Long::class,
fromPrimitive = "Distance(%s)",
toPrimitive = "%s.millimeters",
collections = ["Array", "ArrayList"],
collections = ["Array", "ArrayList", "RingBuffer"],
)

@file:PrimitiveWrapperCollections(
wrapper = Offset::class,
primitive = Long::class,
fromPrimitive = "Offset(Distance(%s))",
toPrimitive = "%s.distance.millimeters",
collections = ["Array", "ArrayList"],
collections = ["Array", "ArrayList", "RingBuffer"],
)

package fr.sncf.osrd.utils.units
Expand Down
Loading