Skip to content

Commit

Permalink
Fix another bug in ArrayMap - thanks ChatGPT! (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg authored Dec 20, 2023
2 parents 739d716 + ee5a129 commit 1b597f7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
32 changes: 16 additions & 16 deletions selfie-lib/src/commonMain/kotlin/com/diffplug/selfie/ArrayMap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,29 +49,28 @@ class ArrayMap<K : Comparable<K>, V : Any>(private val data: Array<Any>) : Map<K
}
val newData = arrayOfNulls<Any>(data.size - indicesToRemove.size * 2)
var newDataIdx = 0
var currentDataIdx = 0
var currentPairIdx = 0 // Index for key-value pairs
var toRemoveIdx = 0
while (newDataIdx < newData.size) {
if (toRemoveIdx < indicesToRemove.size && currentDataIdx == indicesToRemove[toRemoveIdx]) {
++toRemoveIdx
++currentDataIdx
while (currentPairIdx < data.size / 2) {
if (toRemoveIdx < indicesToRemove.size && currentPairIdx == indicesToRemove[toRemoveIdx]) {
toRemoveIdx++
} else {
newData[newDataIdx++] = data[2 * currentDataIdx]
newData[newDataIdx++] = data[2 * currentDataIdx + 1]
++currentDataIdx
check(newDataIdx < newData.size) {
"The indices weren't sorted or were >= size=$size: $indicesToRemove"
}
newData[newDataIdx++] = data[2 * currentPairIdx] // Copy key
newData[newDataIdx++] = data[2 * currentPairIdx + 1] // Copy value
}
currentPairIdx++
}
val tailRemoval =
if (indicesToRemove.contains((data.size / 2) - 1)) {
1
} else {
0
}
check(toRemoveIdx + tailRemoval == indicesToRemove.size) {
"The indices weren't sorted or were too big: $indicesToRemove"

// Ensure all indices to remove have been processed
check(toRemoveIdx == indicesToRemove.size) {
"The indices weren't sorted or were >= size($size): $indicesToRemove"
}
return ArrayMap<K, V>(newData as Array<Any>)
}

/**
* Returns a new ArrayMap which has added the given key. Throws an exception if the key already
* exists.
Expand Down Expand Up @@ -169,6 +168,7 @@ class ArrayMap<K : Comparable<K>, V : Any>(private val data: Array<Any>) : Map<K
}
}
override fun hashCode(): Int = entries.hashCode()
override fun toString() = this.toMutableMap().toString()

companion object {
private val EMPTY = ArrayMap<String, Any>(arrayOf())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,18 @@ class ArrayMapTest {

arrayMap.minusSortedIndices(listOf(0, 2)) shouldBe mapOf("second" to "2")

// arrayMap.minusSortedIndices(listOf(1, 0))
assertFails { arrayMap.minusSortedIndices(listOf(1, 0)) }.message shouldBe
"The indices weren't sorted or were too big: [1, 0]"
"The indices weren't sorted or were >= size=3: [1, 0]"
assertFails { arrayMap.minusSortedIndices(listOf(2, 1)) }.message shouldBe
"The indices weren't sorted or were too big: [2, 1]"
"The indices weren't sorted or were >= size=3: [2, 1]"
assertFails { arrayMap.minusSortedIndices(listOf(3)) }.message shouldBe
"The indices weren't sorted or were too big: [3]"
"The indices weren't sorted or were >= size=3: [3]"
}

@Test
fun wasBroken() {
val map = ArrayMap.of(0.rangeTo(8).map { it to it.toString() }.toMutableList())
map.minusSortedIndices(listOf(0, 2, 3, 6, 7, 8)).toString() shouldBe "{1=1, 4=4, 5=5}"
}
}

0 comments on commit 1b597f7

Please sign in to comment.