Skip to content

Commit

Permalink
Update ktlint & reformat code
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewbailey committed Jan 26, 2025
1 parent 82f7212 commit 37065e8
Show file tree
Hide file tree
Showing 14 changed files with 271 additions and 238 deletions.
8 changes: 8 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
root = true

[*.{kt,kts}]
ij_kotlin_allow_trailing_comma_on_call_site=false
ij_kotlin_allow_trailing_comma=false
ktlint_code_style = android_studio
ktlint_class_signature_rule_force_multiline_when_parameter_count_greater_or_equal_than=2
ktlint_function_signature_rule_force_multiline_when_parameter_count_greater_or_equal_than=3
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
/captures
.externalNativeBuild
.cxx
*.klib
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,7 @@ class DiffBenchmarkTest {
}
}

private fun generateList(
numberOfItems: Int,
seed: Long
): List<Int> {
private fun generateList(numberOfItems: Int, seed: Long): List<Int> {
val random = Random(seed)
return List(numberOfItems) { random.nextInt() }
}
Expand Down Expand Up @@ -147,5 +144,4 @@ class DiffBenchmarkTest {

return modifiedList
}

}
10 changes: 6 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ buildscript {
classpath 'com.android.tools.build:gradle:8.8.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'androidx.benchmark:benchmark-gradle-plugin:1.3.3'
classpath "org.jlleitschuh.gradle:ktlint-gradle:9.1.1"
classpath "org.jlleitschuh.gradle:ktlint-gradle:12.1.2"
classpath "org.jetbrains.dokka:org.jetbrains.dokka.gradle.plugin:2.0.0"
}
}
Expand All @@ -29,12 +29,14 @@ allprojects {
}

ktlint {
version = "1.5.0"
android = true
verbose = true
additionalEditorconfig = [
"max_line_length": "100"
]
disabledRules = [
"no-empty-class-body",
"no-blank-line-before-rbrace",
"no-wildcard-imports",

]
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package dev.andrewbailey.diff

import dev.andrewbailey.diff.DiffOperation.*
import dev.andrewbailey.diff.DiffOperation.Add
import dev.andrewbailey.diff.DiffOperation.AddAll
import dev.andrewbailey.diff.DiffOperation.Move
import dev.andrewbailey.diff.DiffOperation.MoveRange
import dev.andrewbailey.diff.DiffOperation.Remove
import dev.andrewbailey.diff.DiffOperation.RemoveRange
import dev.andrewbailey.diff.impl.MyersDiffAlgorithm
import dev.andrewbailey.diff.impl.MyersDiffOperation.*
import dev.andrewbailey.diff.impl.MyersDiffOperation.Delete
import dev.andrewbailey.diff.impl.MyersDiffOperation.Insert
import dev.andrewbailey.diff.impl.MyersDiffOperation.Skip

internal object DiffGenerator {

Expand Down Expand Up @@ -83,7 +90,8 @@ internal object DiffGenerator {
var endIndexDifference = 0

while (indexOfOppositeAction < operations.size &&
!canBeReducedToMove(operation, operations[indexOfOppositeAction])) {
!canBeReducedToMove(operation, operations[indexOfOppositeAction])
) {
val rejectedOperation = operations[indexOfOppositeAction]
if (rejectedOperation is Add<T>) {
endIndexDifference++
Expand Down Expand Up @@ -123,12 +131,10 @@ internal object DiffGenerator {
private fun <T> canBeReducedToMove(
operation1: DiffOperation<T>,
operation2: DiffOperation<T>
): Boolean {
return when (operation1) {
is Add<T> -> operation2 is Remove<T> && operation1.item == operation2.item
is Remove<T> -> operation2 is Add<T> && operation1.item == operation2.item
else -> false
}
): Boolean = when (operation1) {
is Add<T> -> operation2 is Remove<T> && operation1.item == operation2.item
is Remove<T> -> operation2 is Add<T> && operation1.item == operation2.item
else -> false
}

private fun <T> reduceSequences(
Expand All @@ -142,7 +148,8 @@ internal object DiffGenerator {
var sequenceEndIndex = index + 1
var sequenceLength = 1
while (sequenceEndIndex < operations.size &&
operationToReduce.canBeCombinedWith(operations[sequenceEndIndex], sequenceLength)) {
operationToReduce.canBeCombinedWith(operations[sequenceEndIndex], sequenceLength)
) {
sequenceEndIndex++
sequenceLength++
}
Expand All @@ -167,63 +174,64 @@ internal object DiffGenerator {
val sequenceLength = sequenceEndIndex - sequenceStartIndex
return if (sequenceLength == 1) {
operations[sequenceStartIndex]
} else when (val startOperation = operations[sequenceStartIndex]) {
is Remove -> {
RemoveRange(
startIndex = startOperation.index,
endIndex = startOperation.index + sequenceLength
)
}
is Add -> {
AddAll(
index = startOperation.index,
items = operations.subList(sequenceStartIndex, sequenceEndIndex)
.asSequence()
.map { operation ->
require(operation is Add<T>) {
"Cannot reduce $operation as part of an insert sequence because " +
"it is not an add action."
} else {
when (val startOperation = operations[sequenceStartIndex]) {
is Remove -> {
RemoveRange(
startIndex = startOperation.index,
endIndex = startOperation.index + sequenceLength
)
}
is Add -> {
AddAll(
index = startOperation.index,
items = operations.subList(sequenceStartIndex, sequenceEndIndex)
.asSequence()
.map { operation ->
require(operation is Add<T>) {
"Cannot reduce $operation as part of an insert sequence " +
"because it is not an add action."
}

operation.item
}

operation.item
}
.toList()
)
}
is Move -> {
MoveRange(
fromIndex = startOperation.fromIndex,
toIndex = startOperation.toIndex,
itemCount = sequenceLength
.toList()
)
}
is Move -> {
MoveRange(
fromIndex = startOperation.fromIndex,
toIndex = startOperation.toIndex,
itemCount = sequenceLength
)
}
else -> throw IllegalArgumentException(
"Cannot reduce sequence starting with $startOperation"
)
}
else -> throw IllegalArgumentException(
"Cannot reduce sequence starting with $startOperation"
)
}
}

private fun <T> DiffOperation<T>.canBeCombinedWith(
otherOperation: DiffOperation<T>,
offset: Int
): Boolean {
return when (this) {
is Remove -> otherOperation is Remove && index == otherOperation.index
is Add -> otherOperation is Add && index + offset == otherOperation.index
is Move -> otherOperation is Move && when {
toIndex < fromIndex -> {
// Move backwards case
toIndex + offset == otherOperation.toIndex &&
): Boolean = when (this) {
is Remove -> otherOperation is Remove && index == otherOperation.index
is Add -> otherOperation is Add && index + offset == otherOperation.index
is Move ->
otherOperation is Move &&
when {
toIndex < fromIndex -> {
// Move backwards case
toIndex + offset == otherOperation.toIndex &&
fromIndex + offset == otherOperation.fromIndex
}
else -> {
// Move forwards case
toIndex == otherOperation.toIndex &&
}
else -> {
// Move forwards case
toIndex == otherOperation.toIndex &&
fromIndex == otherOperation.fromIndex
}
}
}
else -> false
}
else -> false
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,4 @@ sealed class DiffOperation<T> {
val toIndex: Int,
val itemCount: Int
) : DiffOperation<T>()

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package dev.andrewbailey.diff

class DiffResult<T> internal constructor(
val operations: List<DiffOperation<T>>
) {
class DiffResult<T> internal constructor(val operations: List<DiffOperation<T>>) {

inline fun applyDiff(
crossinline remove: (index: Int) -> Unit,
Expand Down Expand Up @@ -72,13 +70,9 @@ class DiffResult<T> internal constructor(
}
}

override fun equals(other: Any?) =
other is DiffResult<*> && other.operations == operations
override fun equals(other: Any?) = other is DiffResult<*> && other.operations == operations

override fun hashCode() =
operations.hashCode()

override fun toString() =
"DiffResult(operations = $operations)"
override fun hashCode() = operations.hashCode()

override fun toString() = "DiffResult(operations = $operations)"
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package dev.andrewbailey.diff.impl

internal inline class CircularIntArray(
val array: IntArray
) {
import kotlin.jvm.JvmInline

@JvmInline
internal value class CircularIntArray(private val array: IntArray) {

constructor(size: Int) : this(IntArray(size))

operator fun get(index: Int): Int {
return array[toInternalIndex(index)]
}
operator fun get(index: Int): Int = array[toInternalIndex(index)]

operator fun set(index: Int, value: Int) {
array[toInternalIndex(index)] = value
Expand All @@ -22,5 +21,4 @@ internal inline class CircularIntArray(
moddedIndex
}
}

}
Loading

0 comments on commit 37065e8

Please sign in to comment.