Skip to content

Commit 483e730

Browse files
committed
fix: Fix ConcurrentModificationException occurring when calling GroupingBlockIndex#removeAll
1 parent 587babf commit 483e730

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

src/main/kotlin/com/xpdustry/nohorny/geometry/GroupingBlockIndex.kt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,7 @@ public interface GroupingBlockIndex<T : Any> {
8181
@Suppress("UnstableApiUsage")
8282
internal class GroupingBlockIndexImpl<T : Any>(private val group: GroupingFunction<T>) : GroupingBlockIndex<T> {
8383
private val index = IntMap<IndexBlock<T>>()
84-
internal val graph =
85-
GraphBuilder.undirected()
86-
.nodeOrder(ElementOrder.unordered<Int>())
87-
.build<Int>()
84+
internal var graph = createGraph()
8885

8986
override fun select(
9087
x: Int,
@@ -156,7 +153,7 @@ internal class GroupingBlockIndexImpl<T : Any>(private val group: GroupingFuncti
156153

157154
override fun removeAll() {
158155
index.clear()
159-
graph.nodes().forEach { graph.removeNode(it) }
156+
graph = createGraph()
160157
}
161158

162159
override fun neighbors(
@@ -231,4 +228,10 @@ internal class GroupingBlockIndexImpl<T : Any>(private val group: GroupingFuncti
231228
}
232229
return result.values().toSet()
233230
}
231+
232+
private fun createGraph() =
233+
GraphBuilder
234+
.undirected()
235+
.nodeOrder(ElementOrder.unordered<Int>())
236+
.build<Int>()
234237
}

src/test/kotlin/com/xpdustry/nohorny/geometry/GroupingBlockIndexImplTest.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,5 +202,24 @@ class GroupingBlockIndexImplTest {
202202
Assertions.assertEquals(4, index.groups().size)
203203
}
204204

205+
@Test
206+
fun `test remove all`() {
207+
val index = createIndex()
208+
repeat(5) { x ->
209+
index.insert(x, 0, 1, Unit)
210+
}
211+
212+
Assertions.assertEquals(1, index.groups().size)
213+
Assertions.assertEquals(5, index.groups().toList()[0].blocks.size)
214+
Assertions.assertEquals(5, index.graph.nodes().size)
215+
Assertions.assertEquals(4, index.graph.edges().size)
216+
217+
index.removeAll()
218+
219+
Assertions.assertEquals(0, index.groups().size)
220+
Assertions.assertEquals(0, index.graph.nodes().size)
221+
Assertions.assertEquals(0, index.graph.edges().size)
222+
}
223+
205224
private fun createIndex() = GroupingBlockIndex.create<Unit>() as GroupingBlockIndexImpl<Unit>
206225
}

0 commit comments

Comments
 (0)