diff options
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/utils')
| -rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/CollectionUtils.kt | 35 | ||||
| -rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/LorenzVec.kt | 2 |
2 files changed, 36 insertions, 1 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/CollectionUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/CollectionUtils.kt index 87261b6d0..d540daffe 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/CollectionUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/CollectionUtils.kt @@ -92,6 +92,16 @@ object CollectionUtils { } } + /** Returns a map containing the count of occurrences of each distinct result of the [selector] function. */ + inline fun <T, K> Iterable<T>.countBy(selector: (T) -> K): Map<K, Int> { + val map = mutableMapOf<K, Int>() + for (item in this) { + val key = selector(item) + map[key] = map.getOrDefault(key, 0) + 1 + } + return map + } + fun List<String>.nextAfter(after: String, skip: Int = 1) = nextAfter({ it == after }, skip) fun List<String>.nextAfter(after: (String) -> Boolean, skip: Int = 1): String? { @@ -210,6 +220,31 @@ object CollectionUtils { return collection } + /** Removes the first element that matches the given [predicate] in the list. */ + fun <T> List<T>.removeFirst(predicate: (T) -> Boolean): List<T> { + val mutableList = this.toMutableList() + val iterator = mutableList.iterator() + while (iterator.hasNext()) { + if (predicate(iterator.next())) { + iterator.remove() + break + } + } + return mutableList.toList() + } + + /** Removes the first element that matches the given [predicate] in the map. */ + fun <K, V> Map<K, V>.removeFirst(predicate: (Map.Entry<K, V>) -> Boolean): Map<K, V> { + val mutableMap = this.toMutableMap() + val iterator = mutableMap.entries.iterator() + while (iterator.hasNext()) { + if (predicate(iterator.next())) { + iterator.remove() + break + } + } + return mutableMap.toMap() + } /** Updates a value if it is present in the set (equals), useful if the newValue is not reference equal with the value in the set */ inline fun <reified T> MutableSet<T>.refreshReference(newValue: T) = if (this.contains(newValue)) { diff --git a/src/main/java/at/hannibal2/skyhanni/utils/LorenzVec.kt b/src/main/java/at/hannibal2/skyhanni/utils/LorenzVec.kt index e369a69ca..474d6dee5 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/LorenzVec.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/LorenzVec.kt @@ -121,7 +121,7 @@ data class LorenzVec( } } - fun toCleanString(): String = "$x $y $z" + fun toCleanString(separator: String = ", "): String = listOf(x, y, z).joinToString(separator) fun lengthSquared(): Double = x * x + y * y + z * z fun length(): Double = sqrt(this.lengthSquared()) |
