Move sources of ranges to common stdlib from builtins
But still cherry-pick them when serializing builtins because they are used in builtins signatures. Merge-request: KT-MR-6488 Merged-by: Ilya Gorbunov <Ilya.Gorbunov@jetbrains.com>
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Copyright 2010-2022 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.
|
||||
*/
|
||||
|
||||
// Auto-generated file. DO NOT EDIT!
|
||||
|
||||
package kotlin.collections
|
||||
|
||||
/** An iterator over a sequence of values of type `Byte`. */
|
||||
public abstract class ByteIterator : Iterator<Byte> {
|
||||
override final fun next() = nextByte()
|
||||
|
||||
/** Returns the next value in the sequence without boxing. */
|
||||
public abstract fun nextByte(): Byte
|
||||
}
|
||||
|
||||
/** An iterator over a sequence of values of type `Char`. */
|
||||
public abstract class CharIterator : Iterator<Char> {
|
||||
override final fun next() = nextChar()
|
||||
|
||||
/** Returns the next value in the sequence without boxing. */
|
||||
public abstract fun nextChar(): Char
|
||||
}
|
||||
|
||||
/** An iterator over a sequence of values of type `Short`. */
|
||||
public abstract class ShortIterator : Iterator<Short> {
|
||||
override final fun next() = nextShort()
|
||||
|
||||
/** Returns the next value in the sequence without boxing. */
|
||||
public abstract fun nextShort(): Short
|
||||
}
|
||||
|
||||
/** An iterator over a sequence of values of type `Int`. */
|
||||
public abstract class IntIterator : Iterator<Int> {
|
||||
override final fun next() = nextInt()
|
||||
|
||||
/** Returns the next value in the sequence without boxing. */
|
||||
public abstract fun nextInt(): Int
|
||||
}
|
||||
|
||||
/** An iterator over a sequence of values of type `Long`. */
|
||||
public abstract class LongIterator : Iterator<Long> {
|
||||
override final fun next() = nextLong()
|
||||
|
||||
/** Returns the next value in the sequence without boxing. */
|
||||
public abstract fun nextLong(): Long
|
||||
}
|
||||
|
||||
/** An iterator over a sequence of values of type `Float`. */
|
||||
public abstract class FloatIterator : Iterator<Float> {
|
||||
override final fun next() = nextFloat()
|
||||
|
||||
/** Returns the next value in the sequence without boxing. */
|
||||
public abstract fun nextFloat(): Float
|
||||
}
|
||||
|
||||
/** An iterator over a sequence of values of type `Double`. */
|
||||
public abstract class DoubleIterator : Iterator<Double> {
|
||||
override final fun next() = nextDouble()
|
||||
|
||||
/** Returns the next value in the sequence without boxing. */
|
||||
public abstract fun nextDouble(): Double
|
||||
}
|
||||
|
||||
/** An iterator over a sequence of values of type `Boolean`. */
|
||||
public abstract class BooleanIterator : Iterator<Boolean> {
|
||||
override final fun next() = nextBoolean()
|
||||
|
||||
/** Returns the next value in the sequence without boxing. */
|
||||
public abstract fun nextBoolean(): Boolean
|
||||
}
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Copyright 2010-2018 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.internal
|
||||
|
||||
// a mod b (in arithmetical sense)
|
||||
private fun mod(a: Int, b: Int): Int {
|
||||
val mod = a % b
|
||||
return if (mod >= 0) mod else mod + b
|
||||
}
|
||||
|
||||
private fun mod(a: Long, b: Long): Long {
|
||||
val mod = a % b
|
||||
return if (mod >= 0) mod else mod + b
|
||||
}
|
||||
|
||||
// (a - b) mod c
|
||||
private fun differenceModulo(a: Int, b: Int, c: Int): Int {
|
||||
return mod(mod(a, c) - mod(b, c), c)
|
||||
}
|
||||
|
||||
private fun differenceModulo(a: Long, b: Long, c: Long): Long {
|
||||
return mod(mod(a, c) - mod(b, c), c)
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the final element of a bounded arithmetic progression, i.e. the last element of the progression which is in the range
|
||||
* from [start] to [end] in case of a positive [step], or from [end] to [start] in case of a negative
|
||||
* [step].
|
||||
*
|
||||
* No validation on passed parameters is performed. The given parameters should satisfy the condition:
|
||||
*
|
||||
* - either `step > 0` and `start <= end`,
|
||||
* - or `step < 0` and `start >= end`.
|
||||
*
|
||||
* @param start first element of the progression
|
||||
* @param end ending bound for the progression
|
||||
* @param step increment, or difference of successive elements in the progression
|
||||
* @return the final element of the progression
|
||||
* @suppress
|
||||
*/
|
||||
@PublishedApi
|
||||
internal fun getProgressionLastElement(start: Int, end: Int, step: Int): Int = when {
|
||||
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 -> throw kotlin.IllegalArgumentException("Step is zero.")
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the final element of a bounded arithmetic progression, i.e. the last element of the progression which is in the range
|
||||
* from [start] to [end] in case of a positive [step], or from [end] to [start] in case of a negative
|
||||
* [step].
|
||||
*
|
||||
* No validation on passed parameters is performed. The given parameters should satisfy the condition:
|
||||
*
|
||||
* - either `step > 0` and `start <= end`,
|
||||
* - or `step < 0` and `start >= end`.
|
||||
*
|
||||
* @param start first element of the progression
|
||||
* @param end ending bound for the progression
|
||||
* @param step increment, or difference of successive elements in the progression
|
||||
* @return the final element of the progression
|
||||
* @suppress
|
||||
*/
|
||||
@PublishedApi
|
||||
internal fun getProgressionLastElement(start: Long, end: Long, step: Long): Long = when {
|
||||
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 -> throw kotlin.IllegalArgumentException("Step is zero.")
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
* Copyright 2010-2022 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.
|
||||
*/
|
||||
|
||||
// Auto-generated file. DO NOT EDIT!
|
||||
|
||||
package kotlin.ranges
|
||||
|
||||
/**
|
||||
* A range of values of type `Char`.
|
||||
*/
|
||||
public class CharRange(start: Char, endInclusive: Char) : CharProgression(start, endInclusive, 1), ClosedRange<Char> {
|
||||
override val start: Char get() = first
|
||||
override val endInclusive: Char get() = last
|
||||
|
||||
override fun contains(value: Char): Boolean = first <= value && value <= last
|
||||
|
||||
/**
|
||||
* Checks whether the range is empty.
|
||||
*
|
||||
* The range is empty if its start value is greater than the end value.
|
||||
*/
|
||||
override fun isEmpty(): Boolean = first > last
|
||||
|
||||
override fun equals(other: Any?): Boolean =
|
||||
other is CharRange && (isEmpty() && other.isEmpty() ||
|
||||
first == other.first && last == other.last)
|
||||
|
||||
override fun hashCode(): Int =
|
||||
if (isEmpty()) -1 else (31 * first.code + last.code)
|
||||
|
||||
override fun toString(): String = "$first..$last"
|
||||
|
||||
companion object {
|
||||
/** An empty range of values of type Char. */
|
||||
public val EMPTY: CharRange = CharRange(1.toChar(), 0.toChar())
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A range of values of type `Int`.
|
||||
*/
|
||||
public class IntRange(start: Int, endInclusive: Int) : IntProgression(start, endInclusive, 1), ClosedRange<Int> {
|
||||
override val start: Int get() = first
|
||||
override val endInclusive: Int get() = last
|
||||
|
||||
override fun contains(value: Int): Boolean = first <= value && value <= last
|
||||
|
||||
/**
|
||||
* Checks whether the range is empty.
|
||||
*
|
||||
* The range is empty if its start value is greater than the end value.
|
||||
*/
|
||||
override fun isEmpty(): Boolean = first > last
|
||||
|
||||
override fun equals(other: Any?): Boolean =
|
||||
other is IntRange && (isEmpty() && other.isEmpty() ||
|
||||
first == other.first && last == other.last)
|
||||
|
||||
override fun hashCode(): Int =
|
||||
if (isEmpty()) -1 else (31 * first + last)
|
||||
|
||||
override fun toString(): String = "$first..$last"
|
||||
|
||||
companion object {
|
||||
/** An empty range of values of type Int. */
|
||||
public val EMPTY: IntRange = IntRange(1, 0)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A range of values of type `Long`.
|
||||
*/
|
||||
public class LongRange(start: Long, endInclusive: Long) : LongProgression(start, endInclusive, 1), ClosedRange<Long> {
|
||||
override val start: Long get() = first
|
||||
override val endInclusive: Long get() = last
|
||||
|
||||
override fun contains(value: Long): Boolean = first <= value && value <= last
|
||||
|
||||
/**
|
||||
* Checks whether the range is empty.
|
||||
*
|
||||
* The range is empty if its start value is greater than the end value.
|
||||
*/
|
||||
override fun isEmpty(): Boolean = first > last
|
||||
|
||||
override fun equals(other: Any?): Boolean =
|
||||
other is LongRange && (isEmpty() && other.isEmpty() ||
|
||||
first == other.first && last == other.last)
|
||||
|
||||
override fun hashCode(): Int =
|
||||
if (isEmpty()) -1 else (31 * (first xor (first ushr 32)) + (last xor (last ushr 32))).toInt()
|
||||
|
||||
override fun toString(): String = "$first..$last"
|
||||
|
||||
companion object {
|
||||
/** An empty range of values of type Long. */
|
||||
public val EMPTY: LongRange = LongRange(1, 0)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright 2010-2022 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.
|
||||
*/
|
||||
|
||||
// Auto-generated file. DO NOT EDIT!
|
||||
|
||||
package kotlin.ranges
|
||||
|
||||
/**
|
||||
* An iterator over a progression of values of type `Char`.
|
||||
* @property step the number by which the value is incremented on each step.
|
||||
*/
|
||||
internal class CharProgressionIterator(first: Char, last: Char, val step: Int) : CharIterator() {
|
||||
private val finalElement: Int = last.code
|
||||
private var hasNext: Boolean = if (step > 0) first <= last else first >= last
|
||||
private var next: Int = if (hasNext) first.code else finalElement
|
||||
|
||||
override fun hasNext(): Boolean = hasNext
|
||||
|
||||
override fun nextChar(): Char {
|
||||
val value = next
|
||||
if (value == finalElement) {
|
||||
if (!hasNext) throw kotlin.NoSuchElementException()
|
||||
hasNext = false
|
||||
}
|
||||
else {
|
||||
next += step
|
||||
}
|
||||
return value.toChar()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* An iterator over a progression of values of type `Int`.
|
||||
* @property step the number by which the value is incremented on each step.
|
||||
*/
|
||||
internal class IntProgressionIterator(first: Int, last: Int, val step: Int) : IntIterator() {
|
||||
private val finalElement: Int = last
|
||||
private var hasNext: Boolean = if (step > 0) first <= last else first >= last
|
||||
private var next: Int = if (hasNext) first else finalElement
|
||||
|
||||
override fun hasNext(): Boolean = hasNext
|
||||
|
||||
override fun nextInt(): Int {
|
||||
val value = next
|
||||
if (value == finalElement) {
|
||||
if (!hasNext) throw kotlin.NoSuchElementException()
|
||||
hasNext = false
|
||||
}
|
||||
else {
|
||||
next += step
|
||||
}
|
||||
return value
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* An iterator over a progression of values of type `Long`.
|
||||
* @property step the number by which the value is incremented on each step.
|
||||
*/
|
||||
internal class LongProgressionIterator(first: Long, last: Long, val step: Long) : LongIterator() {
|
||||
private val finalElement: Long = last
|
||||
private var hasNext: Boolean = if (step > 0) first <= last else first >= last
|
||||
private var next: Long = if (hasNext) first else finalElement
|
||||
|
||||
override fun hasNext(): Boolean = hasNext
|
||||
|
||||
override fun nextLong(): Long {
|
||||
val value = next
|
||||
if (value == finalElement) {
|
||||
if (!hasNext) throw kotlin.NoSuchElementException()
|
||||
hasNext = false
|
||||
}
|
||||
else {
|
||||
next += step
|
||||
}
|
||||
return value
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,197 @@
|
||||
/*
|
||||
* Copyright 2010-2022 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.
|
||||
*/
|
||||
|
||||
// Auto-generated file. DO NOT EDIT!
|
||||
|
||||
package kotlin.ranges
|
||||
|
||||
import kotlin.internal.getProgressionLastElement
|
||||
|
||||
/**
|
||||
* A progression of values of type `Char`.
|
||||
*/
|
||||
public open class CharProgression
|
||||
internal constructor
|
||||
(
|
||||
start: Char,
|
||||
endInclusive: Char,
|
||||
step: Int
|
||||
) : Iterable<Char> {
|
||||
init {
|
||||
if (step == 0) throw kotlin.IllegalArgumentException("Step must be non-zero.")
|
||||
if (step == Int.MIN_VALUE) throw kotlin.IllegalArgumentException("Step must be greater than Int.MIN_VALUE to avoid overflow on negation.")
|
||||
}
|
||||
|
||||
/**
|
||||
* The first element in the progression.
|
||||
*/
|
||||
public val first: Char = start
|
||||
|
||||
/**
|
||||
* The last element in the progression.
|
||||
*/
|
||||
public val last: Char = getProgressionLastElement(start.code, endInclusive.code, step).toChar()
|
||||
|
||||
/**
|
||||
* The step of the progression.
|
||||
*/
|
||||
public val step: Int = step
|
||||
|
||||
override fun iterator(): CharIterator = CharProgressionIterator(first, last, step)
|
||||
|
||||
/**
|
||||
* Checks if the progression is empty.
|
||||
*
|
||||
* Progression with a positive step is empty if its first element is greater than the last element.
|
||||
* Progression with a negative step is empty if its first element is less than the last element.
|
||||
*/
|
||||
public open fun isEmpty(): Boolean = if (step > 0) first > last else first < last
|
||||
|
||||
override fun equals(other: Any?): Boolean =
|
||||
other is CharProgression && (isEmpty() && other.isEmpty() ||
|
||||
first == other.first && last == other.last && step == other.step)
|
||||
|
||||
override fun hashCode(): Int =
|
||||
if (isEmpty()) -1 else (31 * (31 * first.code + last.code) + step)
|
||||
|
||||
override fun toString(): String = if (step > 0) "$first..$last step $step" else "$first downTo $last step ${-step}"
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* Creates CharProgression within the specified bounds of a closed range.
|
||||
*
|
||||
* The progression starts with the [rangeStart] value and goes toward the [rangeEnd] value not excluding it, with the specified [step].
|
||||
* In order to go backwards the [step] must be negative.
|
||||
*
|
||||
* [step] must be greater than `Int.MIN_VALUE` and not equal to zero.
|
||||
*/
|
||||
public fun fromClosedRange(rangeStart: Char, rangeEnd: Char, step: Int): CharProgression = CharProgression(rangeStart, rangeEnd, step)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A progression of values of type `Int`.
|
||||
*/
|
||||
public open class IntProgression
|
||||
internal constructor
|
||||
(
|
||||
start: Int,
|
||||
endInclusive: Int,
|
||||
step: Int
|
||||
) : Iterable<Int> {
|
||||
init {
|
||||
if (step == 0) throw kotlin.IllegalArgumentException("Step must be non-zero.")
|
||||
if (step == Int.MIN_VALUE) throw kotlin.IllegalArgumentException("Step must be greater than Int.MIN_VALUE to avoid overflow on negation.")
|
||||
}
|
||||
|
||||
/**
|
||||
* The first element in the progression.
|
||||
*/
|
||||
public val first: Int = start
|
||||
|
||||
/**
|
||||
* The last element in the progression.
|
||||
*/
|
||||
public val last: Int = getProgressionLastElement(start, endInclusive, step)
|
||||
|
||||
/**
|
||||
* The step of the progression.
|
||||
*/
|
||||
public val step: Int = step
|
||||
|
||||
override fun iterator(): IntIterator = IntProgressionIterator(first, last, step)
|
||||
|
||||
/**
|
||||
* Checks if the progression is empty.
|
||||
*
|
||||
* Progression with a positive step is empty if its first element is greater than the last element.
|
||||
* Progression with a negative step is empty if its first element is less than the last element.
|
||||
*/
|
||||
public open fun isEmpty(): Boolean = if (step > 0) first > last else first < last
|
||||
|
||||
override fun equals(other: Any?): Boolean =
|
||||
other is IntProgression && (isEmpty() && other.isEmpty() ||
|
||||
first == other.first && last == other.last && step == other.step)
|
||||
|
||||
override fun hashCode(): Int =
|
||||
if (isEmpty()) -1 else (31 * (31 * first + last) + step)
|
||||
|
||||
override fun toString(): String = if (step > 0) "$first..$last step $step" else "$first downTo $last step ${-step}"
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* Creates IntProgression within the specified bounds of a closed range.
|
||||
*
|
||||
* The progression starts with the [rangeStart] value and goes toward the [rangeEnd] value not excluding it, with the specified [step].
|
||||
* In order to go backwards the [step] must be negative.
|
||||
*
|
||||
* [step] must be greater than `Int.MIN_VALUE` and not equal to zero.
|
||||
*/
|
||||
public fun fromClosedRange(rangeStart: Int, rangeEnd: Int, step: Int): IntProgression = IntProgression(rangeStart, rangeEnd, step)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A progression of values of type `Long`.
|
||||
*/
|
||||
public open class LongProgression
|
||||
internal constructor
|
||||
(
|
||||
start: Long,
|
||||
endInclusive: Long,
|
||||
step: Long
|
||||
) : Iterable<Long> {
|
||||
init {
|
||||
if (step == 0L) throw kotlin.IllegalArgumentException("Step must be non-zero.")
|
||||
if (step == Long.MIN_VALUE) throw kotlin.IllegalArgumentException("Step must be greater than Long.MIN_VALUE to avoid overflow on negation.")
|
||||
}
|
||||
|
||||
/**
|
||||
* The first element in the progression.
|
||||
*/
|
||||
public val first: Long = start
|
||||
|
||||
/**
|
||||
* The last element in the progression.
|
||||
*/
|
||||
public val last: Long = getProgressionLastElement(start, endInclusive, step)
|
||||
|
||||
/**
|
||||
* The step of the progression.
|
||||
*/
|
||||
public val step: Long = step
|
||||
|
||||
override fun iterator(): LongIterator = LongProgressionIterator(first, last, step)
|
||||
|
||||
/**
|
||||
* Checks if the progression is empty.
|
||||
*
|
||||
* Progression with a positive step is empty if its first element is greater than the last element.
|
||||
* Progression with a negative step is empty if its first element is less than the last element.
|
||||
*/
|
||||
public open fun isEmpty(): Boolean = if (step > 0) first > last else first < last
|
||||
|
||||
override fun equals(other: Any?): Boolean =
|
||||
other is LongProgression && (isEmpty() && other.isEmpty() ||
|
||||
first == other.first && last == other.last && step == other.step)
|
||||
|
||||
override fun hashCode(): Int =
|
||||
if (isEmpty()) -1 else (31 * (31 * (first xor (first ushr 32)) + (last xor (last ushr 32))) + (step xor (step ushr 32))).toInt()
|
||||
|
||||
override fun toString(): String = if (step > 0) "$first..$last step $step" else "$first downTo $last step ${-step}"
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* Creates LongProgression within the specified bounds of a closed range.
|
||||
*
|
||||
* The progression starts with the [rangeStart] value and goes toward the [rangeEnd] value not excluding it, with the specified [step].
|
||||
* In order to go backwards the [step] must be negative.
|
||||
*
|
||||
* [step] must be greater than `Long.MIN_VALUE` and not equal to zero.
|
||||
*/
|
||||
public fun fromClosedRange(rangeStart: Long, rangeEnd: Long, step: Long): LongProgression = LongProgression(rangeStart, rangeEnd, step)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright 2010-2019 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.ranges
|
||||
|
||||
/**
|
||||
* Represents a range of values (for example, numbers or characters).
|
||||
* See the [Kotlin language documentation](https://kotlinlang.org/docs/reference/ranges.html) for more information.
|
||||
*/
|
||||
public interface ClosedRange<T: Comparable<T>> {
|
||||
/**
|
||||
* The minimum value in the range.
|
||||
*/
|
||||
public val start: T
|
||||
|
||||
/**
|
||||
* The maximum value in the range (inclusive).
|
||||
*/
|
||||
public val endInclusive: T
|
||||
|
||||
/**
|
||||
* Checks whether the specified [value] belongs to the range.
|
||||
*/
|
||||
public operator fun contains(value: T): Boolean = value >= start && value <= endInclusive
|
||||
|
||||
/**
|
||||
* Checks whether the range is empty.
|
||||
*
|
||||
* The range is empty if its start value is greater than the end value.
|
||||
*/
|
||||
public fun isEmpty(): Boolean = start > endInclusive
|
||||
}
|
||||
Reference in New Issue
Block a user