Skip to content

Commit

Permalink
Add Macrocell serializer
Browse files Browse the repository at this point in the history
Fixes #1728
  • Loading branch information
alexvanyo committed Jan 6, 2025
1 parent d3ebfc8 commit 85a3cee
Show file tree
Hide file tree
Showing 18 changed files with 22,524 additions and 18 deletions.
9 changes: 9 additions & 0 deletions algorithm/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ android {
minSdk = 21
}
configureGradleManagedDevices(enumValues<FormFactor>().toSet(), this)
// Add test pattern files in resources
sourceSets {
getByName("test") {
resources.srcDirs("src/jvmTest/resources")
}
getByName("androidTest") {
resources.srcDirs("src/jvmTest/resources")
}
}
}

kotlin {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ actual class CellStateParser(
if (items.isEmpty()) {
return DeserializationResult.Unsuccessful(
warnings = emptyList(),
errors = listOf(EmptyInput()),
errors = listOf(EmptyInput),
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,27 @@ actual fun UnexpectedBlankLineMessage(
lineIndex,
)

actual fun UnexpectedEmptyFileMessage(): ParameterizedString =
actual val UnexpectedEmptyFileMessage: ParameterizedString =
ParameterizedString(R.string.unexpected_empty_file)

actual fun RuleNotSupportedMessage(): ParameterizedString =
actual val RuleNotSupportedMessage: ParameterizedString =
ParameterizedString(R.string.rule_not_supported)

actual fun DuplicateTopLeftCoordinateMessage(
overwritingOffset: IntOffset,
): ParameterizedString =
ParameterizedString(R.string.duplicate_top_left_coordinate, overwritingOffset.x, overwritingOffset.y)

actual fun EmptyInput(): ParameterizedString =
actual val EmptyInput: ParameterizedString =
ParameterizedString(R.string.empty_input)

actual fun UnexpectedNodeIdMessage(
lineIndex: Int,
characterIndices: IntRange,
): ParameterizedString =
ParameterizedString(
R.string.unexpected_node_id,
lineIndex,
characterIndices.first,
characterIndices.last,
)
1 change: 1 addition & 0 deletions algorithm/src/androidMain/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@
<string name="unexpected_input">Unexpected input "%1$s" starting on line %2$d, character %3$d</string>
<string name="duplicate_top_left_coordinate">Duplicate top-left coordinate instruction, overwriting with (%1$d, %2$d)</string>
<string name="empty_input">Input was empty, could not parse</string>
<string name="unexpected_node_id">Invalid node id on line %1$d, characters %2$d-%3$d</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ sealed interface CellStateFormat {
@Serializable
data object RunLengthEncoding : FixedFormat

@Serializable
data object Macrocell : FixedFormat

@GenSealedEnum(generateEnum = true)
companion object
}
Expand All @@ -57,5 +60,6 @@ fun CellStateFormat.Companion.fromFileExtension(fileExtension: String?): CellSta
"cells" -> CellStateFormat.FixedFormat.Plaintext
"lif", "life" -> CellStateFormat.Life
"rle" -> CellStateFormat.FixedFormat.RunLengthEncoding
"mc" -> CellStateFormat.FixedFormat.Macrocell
else -> CellStateFormat.Unknown
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ suspend fun CellStateParser.parseCellState(text: CharSequence?): Deserialization
if (text.isNullOrEmpty()) {
DeserializationResult.Unsuccessful(
warnings = emptyList(),
errors = listOf(EmptyInput()),
errors = listOf(EmptyInput),
)
} else {
flexibleCellStateSerializer.deserializeToCellState(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class FlexibleCellStateSerializer(
CellStateFormat.FixedFormat.Life105 -> Life105CellStateSerializer
CellStateFormat.FixedFormat.RunLengthEncoding -> RunLengthEncodedCellStateSerializer
CellStateFormat.FixedFormat.Life106 -> Life106CellStateSerializer
CellStateFormat.FixedFormat.Macrocell -> MacrocellCellStateSerializer
CellStateFormat.Life,
CellStateFormat.Unknown,
-> null
Expand Down Expand Up @@ -83,5 +84,6 @@ class FlexibleCellStateSerializer(
CellStateFormat.FixedFormat.Life105 -> Life105CellStateSerializer
CellStateFormat.FixedFormat.RunLengthEncoding -> RunLengthEncodedCellStateSerializer
CellStateFormat.FixedFormat.Life106 -> Life106CellStateSerializer
CellStateFormat.FixedFormat.Macrocell -> MacrocellCellStateSerializer
}.serializeToString(cellState)
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ object Life105CellStateSerializer : FixedFormatCellStateSerializer {
val iterator = lines.iterator()

if (!iterator.hasNext()) {
warnings.add(UnexpectedEmptyFileMessage())
warnings.add(UnexpectedEmptyFileMessage)

return DeserializationResult.Successful(
warnings = warnings,
Expand Down Expand Up @@ -98,7 +98,7 @@ object Life105CellStateSerializer : FixedFormatCellStateSerializer {
if (survival != setOf(2, 3) || birth != setOf(3)) {
return DeserializationResult.Unsuccessful(
warnings = warnings,
errors = listOf(RuleNotSupportedMessage()),
errors = listOf(RuleNotSupportedMessage),
)
} else {
lineIndex++
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ object Life106CellStateSerializer : FixedFormatCellStateSerializer {
val iterator = lines.iterator()

if (!iterator.hasNext()) {
warnings.add(UnexpectedEmptyFileMessage())
warnings.add(UnexpectedEmptyFileMessage)

return DeserializationResult.Successful(
warnings = warnings,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ fun MacroCell.iterator(
* This runs in O(level) time.
*/
@Suppress("NestedBlockDepth")
tailrec fun MacroCell.contains(target: IntOffset): Boolean =
tailrec operator fun MacroCell.contains(target: IntOffset): Boolean =
if (target.x !in 0 until (1 shl level) || target.y !in 0 until (1 shl level)) {
false
} else {
Expand Down
Loading

0 comments on commit 85a3cee

Please sign in to comment.