-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Serialize nested collections in column major order
- Loading branch information
Showing
8 changed files
with
210 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/main/kotlin/dev/bright/vb6serializer/CollectionLikeSerializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package dev.bright.vb6serializer | ||
|
||
import kotlinx.serialization.InternalSerializationApi | ||
import kotlinx.serialization.SerializationStrategy | ||
import kotlinx.serialization.internal.AbstractCollectionSerializer | ||
|
||
@OptIn(InternalSerializationApi::class) | ||
internal object CollectionLikeSerializer { | ||
private val elementSerializer = | ||
javaClass.classLoader.loadClass("kotlinx.serialization.internal.CollectionLikeSerializer") | ||
.getDeclaredField("elementSerializer").apply { isAccessible = true } | ||
|
||
fun <T> elementSerializer(serializer: AbstractCollectionSerializer<T, *, *>): SerializationStrategy<*> = | ||
elementSerializer.get(serializer) as SerializationStrategy<*> | ||
} | ||
|
||
@OptIn(InternalSerializationApi::class) | ||
internal fun <T> AbstractCollectionSerializer<T, *, *>.elementSerializer( | ||
): SerializationStrategy<*> { | ||
return CollectionLikeSerializer.elementSerializer(this) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/main/kotlin/dev/bright/vb6serializer/TransposeInPlace.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package dev.bright.vb6serializer | ||
|
||
internal fun transposeInPlace(serialized: ByteArray, rows: Int, cols: Int, elementByteSize: Int) { | ||
fun swapElement(i: Int, j: Int) { | ||
val iByteSizeIndex = i * elementByteSize | ||
val jByteSizeIndex = j * elementByteSize | ||
|
||
for (x in 0 until elementByteSize) { | ||
val temp = serialized[iByteSizeIndex + x] | ||
serialized[iByteSizeIndex + x] = serialized[jByteSizeIndex + x] | ||
serialized[jByteSizeIndex + x] = temp | ||
} | ||
} | ||
// https://stackoverflow.com/questions/9227747/in-place-transposition-of-a-matrix | ||
val lastIndex = rows * cols - 1 | ||
val visited = BooleanArray(rows * cols) | ||
|
||
for (ix in 1 until lastIndex) {// 0 and lastIndex shouldn't move | ||
if (visited[ix]) { | ||
continue | ||
} | ||
|
||
var cycleStartIndex = ix | ||
do { | ||
cycleStartIndex = if (cycleStartIndex == lastIndex) lastIndex else (rows * cycleStartIndex) % lastIndex | ||
// Swap arr[a] and arr[cycleIndex] | ||
swapElement(cycleStartIndex, ix) | ||
visited[cycleStartIndex] = true | ||
} while (cycleStartIndex != ix) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters