Refactor: replace Math.min with minOf in common code.

Add temporary import of kotlin.comparisons where required.
This commit is contained in:
Ilya Gorbunov
2016-11-16 07:49:14 +03:00
parent 51c24a0c3c
commit 844c68286a
11 changed files with 74 additions and 78 deletions
@@ -74,14 +74,6 @@ internal inline header fun <T> Array<out T>.copyToArrayOfAny(isVarargs: Boolean)
internal header interface Serializable
// temporary
internal header object Math {
fun max(a: Int, b: Int): Int
fun min(a: Int, b: Int): Int
}
// From numbers.kt
header fun Double.isNaN(): Boolean
+26 -26
View File
@@ -11684,7 +11684,7 @@ public infix fun <R> CharArray.zip(other: Array<out R>): List<Pair<Char, R>> {
* Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection.
*/
public inline fun <T, R, V> Array<out T>.zip(other: Array<out R>, transform: (T, R) -> V): List<V> {
val size = Math.min(size, other.size)
val size = minOf(size, other.size)
val list = ArrayList<V>(size)
for (i in 0..size-1) {
list.add(transform(this[i], other[i]))
@@ -11696,7 +11696,7 @@ public inline fun <T, R, V> Array<out T>.zip(other: Array<out R>, transform: (T,
* Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection.
*/
public inline fun <R, V> ByteArray.zip(other: Array<out R>, transform: (Byte, R) -> V): List<V> {
val size = Math.min(size, other.size)
val size = minOf(size, other.size)
val list = ArrayList<V>(size)
for (i in 0..size-1) {
list.add(transform(this[i], other[i]))
@@ -11708,7 +11708,7 @@ public inline fun <R, V> ByteArray.zip(other: Array<out R>, transform: (Byte, R)
* Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection.
*/
public inline fun <R, V> ShortArray.zip(other: Array<out R>, transform: (Short, R) -> V): List<V> {
val size = Math.min(size, other.size)
val size = minOf(size, other.size)
val list = ArrayList<V>(size)
for (i in 0..size-1) {
list.add(transform(this[i], other[i]))
@@ -11720,7 +11720,7 @@ public inline fun <R, V> ShortArray.zip(other: Array<out R>, transform: (Short,
* Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection.
*/
public inline fun <R, V> IntArray.zip(other: Array<out R>, transform: (Int, R) -> V): List<V> {
val size = Math.min(size, other.size)
val size = minOf(size, other.size)
val list = ArrayList<V>(size)
for (i in 0..size-1) {
list.add(transform(this[i], other[i]))
@@ -11732,7 +11732,7 @@ public inline fun <R, V> IntArray.zip(other: Array<out R>, transform: (Int, R) -
* Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection.
*/
public inline fun <R, V> LongArray.zip(other: Array<out R>, transform: (Long, R) -> V): List<V> {
val size = Math.min(size, other.size)
val size = minOf(size, other.size)
val list = ArrayList<V>(size)
for (i in 0..size-1) {
list.add(transform(this[i], other[i]))
@@ -11744,7 +11744,7 @@ public inline fun <R, V> LongArray.zip(other: Array<out R>, transform: (Long, R)
* Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection.
*/
public inline fun <R, V> FloatArray.zip(other: Array<out R>, transform: (Float, R) -> V): List<V> {
val size = Math.min(size, other.size)
val size = minOf(size, other.size)
val list = ArrayList<V>(size)
for (i in 0..size-1) {
list.add(transform(this[i], other[i]))
@@ -11756,7 +11756,7 @@ public inline fun <R, V> FloatArray.zip(other: Array<out R>, transform: (Float,
* Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection.
*/
public inline fun <R, V> DoubleArray.zip(other: Array<out R>, transform: (Double, R) -> V): List<V> {
val size = Math.min(size, other.size)
val size = minOf(size, other.size)
val list = ArrayList<V>(size)
for (i in 0..size-1) {
list.add(transform(this[i], other[i]))
@@ -11768,7 +11768,7 @@ public inline fun <R, V> DoubleArray.zip(other: Array<out R>, transform: (Double
* Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection.
*/
public inline fun <R, V> BooleanArray.zip(other: Array<out R>, transform: (Boolean, R) -> V): List<V> {
val size = Math.min(size, other.size)
val size = minOf(size, other.size)
val list = ArrayList<V>(size)
for (i in 0..size-1) {
list.add(transform(this[i], other[i]))
@@ -11780,7 +11780,7 @@ public inline fun <R, V> BooleanArray.zip(other: Array<out R>, transform: (Boole
* Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection.
*/
public inline fun <R, V> CharArray.zip(other: Array<out R>, transform: (Char, R) -> V): List<V> {
val size = Math.min(size, other.size)
val size = minOf(size, other.size)
val list = ArrayList<V>(size)
for (i in 0..size-1) {
list.add(transform(this[i], other[i]))
@@ -11856,7 +11856,7 @@ public infix fun <R> CharArray.zip(other: Iterable<R>): List<Pair<Char, R>> {
*/
public inline fun <T, R, V> Array<out T>.zip(other: Iterable<R>, transform: (T, R) -> V): List<V> {
val arraySize = size
val list = ArrayList<V>(Math.min(other.collectionSizeOrDefault(10), arraySize))
val list = ArrayList<V>(minOf(other.collectionSizeOrDefault(10), arraySize))
var i = 0
for (element in other) {
if (i >= arraySize) break
@@ -11870,7 +11870,7 @@ public inline fun <T, R, V> Array<out T>.zip(other: Iterable<R>, transform: (T,
*/
public inline fun <R, V> ByteArray.zip(other: Iterable<R>, transform: (Byte, R) -> V): List<V> {
val arraySize = size
val list = ArrayList<V>(Math.min(other.collectionSizeOrDefault(10), arraySize))
val list = ArrayList<V>(minOf(other.collectionSizeOrDefault(10), arraySize))
var i = 0
for (element in other) {
if (i >= arraySize) break
@@ -11884,7 +11884,7 @@ public inline fun <R, V> ByteArray.zip(other: Iterable<R>, transform: (Byte, R)
*/
public inline fun <R, V> ShortArray.zip(other: Iterable<R>, transform: (Short, R) -> V): List<V> {
val arraySize = size
val list = ArrayList<V>(Math.min(other.collectionSizeOrDefault(10), arraySize))
val list = ArrayList<V>(minOf(other.collectionSizeOrDefault(10), arraySize))
var i = 0
for (element in other) {
if (i >= arraySize) break
@@ -11898,7 +11898,7 @@ public inline fun <R, V> ShortArray.zip(other: Iterable<R>, transform: (Short, R
*/
public inline fun <R, V> IntArray.zip(other: Iterable<R>, transform: (Int, R) -> V): List<V> {
val arraySize = size
val list = ArrayList<V>(Math.min(other.collectionSizeOrDefault(10), arraySize))
val list = ArrayList<V>(minOf(other.collectionSizeOrDefault(10), arraySize))
var i = 0
for (element in other) {
if (i >= arraySize) break
@@ -11912,7 +11912,7 @@ public inline fun <R, V> IntArray.zip(other: Iterable<R>, transform: (Int, R) ->
*/
public inline fun <R, V> LongArray.zip(other: Iterable<R>, transform: (Long, R) -> V): List<V> {
val arraySize = size
val list = ArrayList<V>(Math.min(other.collectionSizeOrDefault(10), arraySize))
val list = ArrayList<V>(minOf(other.collectionSizeOrDefault(10), arraySize))
var i = 0
for (element in other) {
if (i >= arraySize) break
@@ -11926,7 +11926,7 @@ public inline fun <R, V> LongArray.zip(other: Iterable<R>, transform: (Long, R)
*/
public inline fun <R, V> FloatArray.zip(other: Iterable<R>, transform: (Float, R) -> V): List<V> {
val arraySize = size
val list = ArrayList<V>(Math.min(other.collectionSizeOrDefault(10), arraySize))
val list = ArrayList<V>(minOf(other.collectionSizeOrDefault(10), arraySize))
var i = 0
for (element in other) {
if (i >= arraySize) break
@@ -11940,7 +11940,7 @@ public inline fun <R, V> FloatArray.zip(other: Iterable<R>, transform: (Float, R
*/
public inline fun <R, V> DoubleArray.zip(other: Iterable<R>, transform: (Double, R) -> V): List<V> {
val arraySize = size
val list = ArrayList<V>(Math.min(other.collectionSizeOrDefault(10), arraySize))
val list = ArrayList<V>(minOf(other.collectionSizeOrDefault(10), arraySize))
var i = 0
for (element in other) {
if (i >= arraySize) break
@@ -11954,7 +11954,7 @@ public inline fun <R, V> DoubleArray.zip(other: Iterable<R>, transform: (Double,
*/
public inline fun <R, V> BooleanArray.zip(other: Iterable<R>, transform: (Boolean, R) -> V): List<V> {
val arraySize = size
val list = ArrayList<V>(Math.min(other.collectionSizeOrDefault(10), arraySize))
val list = ArrayList<V>(minOf(other.collectionSizeOrDefault(10), arraySize))
var i = 0
for (element in other) {
if (i >= arraySize) break
@@ -11968,7 +11968,7 @@ public inline fun <R, V> BooleanArray.zip(other: Iterable<R>, transform: (Boolea
*/
public inline fun <R, V> CharArray.zip(other: Iterable<R>, transform: (Char, R) -> V): List<V> {
val arraySize = size
val list = ArrayList<V>(Math.min(other.collectionSizeOrDefault(10), arraySize))
val list = ArrayList<V>(minOf(other.collectionSizeOrDefault(10), arraySize))
var i = 0
for (element in other) {
if (i >= arraySize) break
@@ -12037,7 +12037,7 @@ public infix fun CharArray.zip(other: CharArray): List<Pair<Char, Char>> {
* Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection.
*/
public inline fun <V> ByteArray.zip(other: ByteArray, transform: (Byte, Byte) -> V): List<V> {
val size = Math.min(size, other.size)
val size = minOf(size, other.size)
val list = ArrayList<V>(size)
for (i in 0..size-1) {
list.add(transform(this[i], other[i]))
@@ -12049,7 +12049,7 @@ public inline fun <V> ByteArray.zip(other: ByteArray, transform: (Byte, Byte) ->
* Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection.
*/
public inline fun <V> ShortArray.zip(other: ShortArray, transform: (Short, Short) -> V): List<V> {
val size = Math.min(size, other.size)
val size = minOf(size, other.size)
val list = ArrayList<V>(size)
for (i in 0..size-1) {
list.add(transform(this[i], other[i]))
@@ -12061,7 +12061,7 @@ public inline fun <V> ShortArray.zip(other: ShortArray, transform: (Short, Short
* Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection.
*/
public inline fun <V> IntArray.zip(other: IntArray, transform: (Int, Int) -> V): List<V> {
val size = Math.min(size, other.size)
val size = minOf(size, other.size)
val list = ArrayList<V>(size)
for (i in 0..size-1) {
list.add(transform(this[i], other[i]))
@@ -12073,7 +12073,7 @@ public inline fun <V> IntArray.zip(other: IntArray, transform: (Int, Int) -> V):
* Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection.
*/
public inline fun <V> LongArray.zip(other: LongArray, transform: (Long, Long) -> V): List<V> {
val size = Math.min(size, other.size)
val size = minOf(size, other.size)
val list = ArrayList<V>(size)
for (i in 0..size-1) {
list.add(transform(this[i], other[i]))
@@ -12085,7 +12085,7 @@ public inline fun <V> LongArray.zip(other: LongArray, transform: (Long, Long) ->
* Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection.
*/
public inline fun <V> FloatArray.zip(other: FloatArray, transform: (Float, Float) -> V): List<V> {
val size = Math.min(size, other.size)
val size = minOf(size, other.size)
val list = ArrayList<V>(size)
for (i in 0..size-1) {
list.add(transform(this[i], other[i]))
@@ -12097,7 +12097,7 @@ public inline fun <V> FloatArray.zip(other: FloatArray, transform: (Float, Float
* Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection.
*/
public inline fun <V> DoubleArray.zip(other: DoubleArray, transform: (Double, Double) -> V): List<V> {
val size = Math.min(size, other.size)
val size = minOf(size, other.size)
val list = ArrayList<V>(size)
for (i in 0..size-1) {
list.add(transform(this[i], other[i]))
@@ -12109,7 +12109,7 @@ public inline fun <V> DoubleArray.zip(other: DoubleArray, transform: (Double, Do
* Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection.
*/
public inline fun <V> BooleanArray.zip(other: BooleanArray, transform: (Boolean, Boolean) -> V): List<V> {
val size = Math.min(size, other.size)
val size = minOf(size, other.size)
val list = ArrayList<V>(size)
for (i in 0..size-1) {
list.add(transform(this[i], other[i]))
@@ -12121,7 +12121,7 @@ public inline fun <V> BooleanArray.zip(other: BooleanArray, transform: (Boolean,
* Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection.
*/
public inline fun <V> CharArray.zip(other: CharArray, transform: (Char, Char) -> V): List<V> {
val size = Math.min(size, other.size)
val size = minOf(size, other.size)
val list = ArrayList<V>(size)
for (i in 0..size-1) {
list.add(transform(this[i], other[i]))
@@ -1882,7 +1882,7 @@ public infix fun <T, R> Iterable<T>.zip(other: Array<out R>): List<Pair<T, R>> {
*/
public inline fun <T, R, V> Iterable<T>.zip(other: Array<out R>, transform: (T, R) -> V): List<V> {
val arraySize = other.size
val list = ArrayList<V>(Math.min(collectionSizeOrDefault(10), arraySize))
val list = ArrayList<V>(minOf(collectionSizeOrDefault(10), arraySize))
var i = 0
for (element in this) {
if (i >= arraySize) break
@@ -1904,7 +1904,7 @@ public infix fun <T, R> Iterable<T>.zip(other: Iterable<R>): List<Pair<T, R>> {
public inline fun <T, R, V> Iterable<T>.zip(other: Iterable<R>, transform: (T, R) -> V): List<V> {
val first = iterator()
val second = other.iterator()
val list = ArrayList<V>(Math.min(collectionSizeOrDefault(10), other.collectionSizeOrDefault(10)))
val list = ArrayList<V>(minOf(collectionSizeOrDefault(10), other.collectionSizeOrDefault(10)))
while (first.hasNext() && second.hasNext()) {
list.add(transform(first.next(), second.next()))
}
+1 -1
View File
@@ -1144,7 +1144,7 @@ public infix fun CharSequence.zip(other: CharSequence): List<Pair<Char, Char>> {
* Returns a list of values built from characters of both char sequences with same indexes using provided [transform]. List has length of shortest char sequence.
*/
public inline fun <V> CharSequence.zip(other: CharSequence, transform: (Char, Char) -> V): List<V> {
val length = Math.min(this.length, other.length)
val length = minOf(this.length, other.length)
val list = ArrayList<V>(length)
for (i in 0..length-1) {
list.add(transform(this[i], other[i]))
@@ -5,6 +5,7 @@ package kotlin.io
import java.io.*
import java.util.*
import kotlin.comparisons.*
/**
* Creates an empty directory in the specified [directory], using the given [prefix] and [suffix] to generate its name.
@@ -120,7 +121,7 @@ private fun File.toRelativeStringOrNull(base: File): String? {
val sameCount = run countSame@ {
var i = 0
val maxSameCount = Math.min(thisCount, baseCount)
val maxSameCount = minOf(thisCount, baseCount)
while (i < maxSameCount && thisComponents.segments[i] == baseComponents.segments[i])
i++
return@countSame i
+6 -4
View File
@@ -20,6 +20,8 @@
package kotlin.text
import kotlin.comparisons.*
/**
* Returns a sub sequence of this char sequence having leading and trailing characters matching the [predicate] trimmed.
@@ -747,7 +749,7 @@ public fun CharSequence.endsWith(suffix: CharSequence, ignoreCase: Boolean = fal
* @param ignoreCase `true` to ignore character case when matching a character. By default `false`.
*/
public fun CharSequence.commonPrefixWith(other: CharSequence, ignoreCase: Boolean = false): String {
val shortestLength = Math.min(this.length, other.length)
val shortestLength = minOf(this.length, other.length)
var i = 0
while (i < shortestLength && this[i].equals(other[i], ignoreCase = ignoreCase)) {
@@ -769,7 +771,7 @@ public fun CharSequence.commonPrefixWith(other: CharSequence, ignoreCase: Boolea
public fun CharSequence.commonSuffixWith(other: CharSequence, ignoreCase: Boolean = false): String {
val thisLength = this.length
val otherLength = other.length
val shortestLength = Math.min(thisLength, otherLength)
val shortestLength = minOf(thisLength, otherLength)
var i = 0
while (i < shortestLength && this[thisLength - i - 1].equals(other[otherLength - i - 1], ignoreCase = ignoreCase)) {
@@ -791,7 +793,7 @@ private fun CharSequence.findAnyOf(chars: CharArray, startIndex: Int, ignoreCase
return if (index < 0) null else index to char
}
val indices = if (!last) Math.max(startIndex, 0)..lastIndex else Math.min(startIndex, lastIndex) downTo 0
val indices = if (!last) startIndex.coerceAtLeast(0)..lastIndex else startIndex.coerceAtMost(lastIndex) downTo 0
for (index in indices) {
val charAtIndex = get(index)
val matchingCharIndex = chars.indexOfFirst { it.equals(charAtIndex, ignoreCase) }
@@ -1028,7 +1030,7 @@ private class DelimitedRangesSequence(private val input: CharSequence, private v
override fun iterator(): Iterator<IntRange> = object : Iterator<IntRange> {
var nextState: Int = -1 // -1 for unknown, 0 for done, 1 for continue
var currentStartIndex: Int = Math.min(Math.max(startIndex, 0), input.length)
var currentStartIndex: Int = startIndex.coerceIn(0, input.length)
var nextSearchIndex: Int = currentStartIndex
var nextItem: IntRange? = null
var counter: Int = 0
@@ -1,6 +1,7 @@
package test.ranges
import org.junit.Test
import kotlin.comparisons.*
import kotlin.test.assertEquals
@@ -72,7 +73,7 @@ class ProgressionLastElementTest {
var x = start
while (true) {
val next = x + increment
if (next < Math.min(start, end) || next > Math.max(start, end)) break
if (next !in minOf(start, end)..maxOf(start, end)) break
x = next
}