Sync progressionLastElement to fix overflow caused by negative step

This commit is contained in:
Pavel Punegov
2018-08-07 12:40:27 +03:00
committed by Pavel Punegov
parent 57d21f9a38
commit 6c4ec4c3f0
@@ -41,24 +41,22 @@ private fun differenceModulo(a: Long, b: Long, c: Long): Long {
* from [start] to [end] in case of a positive [step], or from [end] to [start] in case of a negative * from [start] to [end] in case of a positive [step], or from [end] to [start] in case of a negative
* [step]. * [step].
* *
* No validation on passed parameters is performed. The given parameters should satisfy the condition: either * No validation on passed parameters is performed. The given parameters should satisfy the condition:
* `step > 0` and `start >= end`, or `step < 0` and`start >= end`. *
* - either `step > 0` and `start <= end`,
* - or `step < 0` and `start >= end`.
*
* @param start first element of the progression * @param start first element of the progression
* @param end ending bound for the progression * @param end ending bound for the progression
* @param step increment, or difference of successive elements in the progression * @param step increment, or difference of successive elements in the progression
* @return the final element of the progression * @return the final element of the progression
* @suppress * @suppress
*/ */
internal fun getProgressionLastElement(start: Int, end: Int, step: Int): Int { @PublishedApi
if (step > 0) { internal fun getProgressionLastElement(start: Int, end: Int, step: Int): Int = when {
return end - differenceModulo(end, start, step) step > 0 -> if (start >= end) end else end - differenceModulo(end, start, step)
} step < 0 -> if (start <= end) end else end + differenceModulo(start, end, -step)
else if (step < 0) { else -> throw kotlin.IllegalArgumentException("Step is zero.")
return end + differenceModulo(start, end, -step)
}
else {
throw kotlin.IllegalArgumentException("Step is zero.")
}
} }
/** /**
@@ -66,22 +64,20 @@ internal fun getProgressionLastElement(start: Int, end: Int, step: Int): Int {
* from [start] to [end] in case of a positive [step], or from [end] to [start] in case of a negative * from [start] to [end] in case of a positive [step], or from [end] to [start] in case of a negative
* [step]. * [step].
* *
* No validation on passed parameters is performed. The given parameters should satisfy the condition: either * No validation on passed parameters is performed. The given parameters should satisfy the condition:
* `step > 0` and `start >= end`, or `step < 0` and`start >= end`. *
* - either `step > 0` and `start <= end`,
* - or `step < 0` and `start >= end`.
*
* @param start first element of the progression * @param start first element of the progression
* @param end ending bound for the progression * @param end ending bound for the progression
* @param step increment, or difference of successive elements in the progression * @param step increment, or difference of successive elements in the progression
* @return the final element of the progression * @return the final element of the progression
* @suppress * @suppress
*/ */
internal fun getProgressionLastElement(start: Long, end: Long, step: Long): Long { @PublishedApi
if (step > 0) { internal fun getProgressionLastElement(start: Long, end: Long, step: Long): Long = when {
return end - differenceModulo(end, start, step) step > 0 -> if (start >= end) end else end - differenceModulo(end, start, step)
} step < 0 -> if (start <= end) end else end + differenceModulo(start, end, -step)
else if (step < 0) { else -> throw kotlin.IllegalArgumentException("Step is zero.")
return end + differenceModulo(start, end, -step)
}
else {
throw kotlin.IllegalArgumentException("Step is zero.")
}
} }