kotlin.reflect.jvm.internal.impl.utils.MemoryOptimizedCollectionUtil.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlin-reflect Show documentation
Show all versions of kotlin-reflect Show documentation
Kotlin Full Reflection Library
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package kotlin.reflect.jvm.internal.impl.utils
import java.util.*
import kotlin.math.min
/**
* A memory-optimized version of [Iterable.map].
* @see Iterable.map
*/
inline fun Collection.memoryOptimizedMap(transform: (T) -> R): List {
return mapTo(ArrayList(size), transform).compactIfPossible()
}
/**
* A memory-optimized version of [Iterable.mapIndexed].
* @see Iterable.mapIndexed
*/
inline fun Collection.memoryOptimizedMapIndexed(transform: (index: Int, T) -> R): List {
return mapIndexedTo(ArrayList(size), transform).compactIfPossible()
}
/**
* A memory-optimized version of [Iterable.mapNotNull].
* @see Iterable.mapNotNull
*/
inline fun Collection.memoryOptimizedMapNotNull(transform: (T) -> R?): List {
return mapNotNullTo(ArrayList(), transform).compactIfPossible()
}
/**
* A memory-optimized version of [Iterable.flatMap].
* @see Iterable.flatMap
*/
inline fun Collection.memoryOptimizedFlatMap(transform: (T) -> Iterable): List {
return flatMapTo(ArrayList(), transform).compactIfPossible()
}
/**
* A memory-optimized version of [Iterable.filter].
* @see Iterable.filter
*/
inline fun Collection.memoryOptimizedFilter(predicate: (T) -> Boolean): List {
return filterTo(ArrayList(), predicate).compactIfPossible()
}
/**
* A memory-optimized version of [Iterable.filterNot].
* @see Iterable.filterNot
*/
inline fun Collection.memoryOptimizedFilterNot(predicate: (T) -> Boolean): List {
return filterNotTo(ArrayList(), predicate).compactIfPossible()
}
/**
* A memory-optimized version of [Iterable.filterIsInstance].
* @see Iterable.filterIsInstance
*/
inline fun Collection<*>.memoryOptimizedFilterIsInstance(): List {
return filterIsInstanceTo(ArrayList()).compactIfPossible()
}
/**
* A memory-optimized version of [Iterable.plus].
* @see Iterable.plus
*/
infix fun List.memoryOptimizedPlus(elements: List): List =
when (val resultSize = size + elements.size) {
0 -> emptyList()
1 -> Collections.singletonList(if (isEmpty()) elements.first() else first())
else -> ArrayList(resultSize).also {
it.addAll(this)
it.addAll(elements)
}
}
/**
* A memory-optimized version of [Iterable.plus].
* @see Iterable.plus
*/
infix fun List.memoryOptimizedPlus(element: T): List =
when (size) {
0 -> Collections.singletonList(element)
else -> ArrayList(size + 1).also {
it.addAll(this)
it.add(element)
}
}
/**
* A memory-optimized version of [Iterable.zip].
* @see Iterable.zip
*/
infix fun Collection.memoryOptimizedZip(other: Collection): List> {
return when {
isEmpty() || other.isEmpty() -> emptyList()
min(size, other.size) == 1 -> listOf(first() to other.first())
else -> zip(other) { t1, t2 -> t1 to t2 }
}
}
/**
* [Sequence] variant of [kotlin.reflect.jvm.internal.impl.backend.common.atMostOne]
* So, when:
* - there is no element then `null` will be returned
* - there is a single element then the element will be returned
* - there is more than one element then the error will be thrown
* @see kotlin.reflect.jvm.internal.impl.backend.common.atMostOne
*/
fun Sequence.atMostOne(): T? {
val iterator = iterator()
if (!iterator.hasNext()) return null
val single = iterator.next()
if (iterator.hasNext()) {
throw IllegalArgumentException("Sequence has more than one element.")
}
return single
}
/**
* The variant of [kotlin.reflect.jvm.internal.impl.util.collectionUtils.filterIsInstanceAnd] extension function but to find the first element
* which is an instance of type [T] and satisfies [predicate] condition
* @see kotlin.reflect.jvm.internal.impl.util.collectionUtils.filterIsInstanceAnd
*/
inline fun Iterable<*>.findIsInstanceAnd(predicate: (T) -> Boolean): T? {
for (element in this) {
if (element is T && predicate(element)) {
return element
}
}
return null
}
/**
* The same as [Collection.toMutableList] extension function, but it returns a SmartList which is better with in sense of memory consumption
* @see Collection.toMutableList
*/
fun Collection.toSmartList(): List = SmartList(this)