-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Review: Replace ArrayList removal with IdentitySet, iterate baseline …
…only once
- Loading branch information
Showing
3 changed files
with
63 additions
and
47 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
44 changes: 44 additions & 0 deletions
44
sarif/src/main/java/com/jetbrains/qodana/sarif/baseline/BaselineCollections.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,44 @@ | ||
package com.jetbrains.qodana.sarif.baseline | ||
|
||
import java.util.* | ||
|
||
internal class MultiMap<K, V> private constructor( | ||
private val underlying: MutableMap<K, MutableList<V>> | ||
) : Iterable<Map.Entry<K, List<V>>> { | ||
constructor() : this(mutableMapOf()) | ||
|
||
fun add(key: K, value: V) { | ||
underlying.compute(key) { _, old -> old?.apply { add(value) } ?: mutableListOf(value) } | ||
} | ||
|
||
fun getOrEmpty(key: K): MutableList<V> = underlying[key] ?: mutableListOf() | ||
|
||
override fun iterator(): Iterator<Map.Entry<K, List<V>>> = underlying.iterator() | ||
} | ||
|
||
internal class IdentitySet<T> private constructor( | ||
private val map: IdentityHashMap<T, UInt> | ||
) : MutableSet<T> by map.keys { | ||
constructor(expectedSize: Int) : this(IdentityHashMap(expectedSize)) | ||
|
||
private var insertCounter = 0u | ||
|
||
override fun add(element: T): Boolean = map.putIfAbsent(element, insertCounter++) == null | ||
override fun addAll(elements: Collection<T>): Boolean = elements.fold(false) { r, e -> add(e) || r } | ||
|
||
override fun iterator(): MutableIterator<T> = object : MutableIterator<T> { | ||
private val underlying = map.entries.sortedBy(MutableMap.MutableEntry<T, UInt>::value).iterator() | ||
|
||
override fun hasNext(): Boolean = underlying.hasNext() | ||
override fun next(): T = underlying.next().key | ||
override fun remove() = throw UnsupportedOperationException("Cannot remove elements from this iterator") | ||
} | ||
} | ||
|
||
internal class Counter<T> { | ||
private val underlying: MutableMap<T, Int> = mutableMapOf() | ||
|
||
operator fun get(key: T) = underlying.getOrDefault(key, 0) | ||
fun increment(key: T) = underlying.compute(key) { _, o -> (o ?: 0).inc() }!! | ||
fun decrement(key: T) = underlying.compute(key) { _, o -> (o ?: 0).dec() }!! | ||
} |
14 changes: 0 additions & 14 deletions
14
sarif/src/main/java/com/jetbrains/qodana/sarif/baseline/MultiMap.kt
This file was deleted.
Oops, something went wrong.