Generate ranges, progressions and progressioniterators for UInt and ULong
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. 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.*
|
||||
|
||||
/**
|
||||
* A range of values of type `UInt`.
|
||||
*/
|
||||
public class UIntRange(start: UInt, endInclusive: UInt) : UIntProgression(start, endInclusive, 1), ClosedRange<UInt> {
|
||||
override val start: UInt get() = first
|
||||
override val endInclusive: UInt get() = last
|
||||
|
||||
override fun contains(value: UInt): Boolean = first <= value && value <= last
|
||||
|
||||
override fun isEmpty(): Boolean = first > last
|
||||
|
||||
override fun equals(other: Any?): Boolean =
|
||||
other is UIntRange && (isEmpty() && other.isEmpty() ||
|
||||
first == other.first && last == other.last)
|
||||
|
||||
override fun hashCode(): Int =
|
||||
if (isEmpty()) -1 else (31 * first.toInt() + last.toInt())
|
||||
|
||||
override fun toString(): String = "$first..$last"
|
||||
|
||||
companion object {
|
||||
/** An empty range of values of type UInt. */
|
||||
public val EMPTY: UIntRange = UIntRange(UInt.MAX_VALUE, UInt.MIN_VALUE)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A progression of values of type `UInt`.
|
||||
*/
|
||||
public open class UIntProgression
|
||||
internal constructor(
|
||||
start: UInt,
|
||||
endInclusive: UInt,
|
||||
step: Int
|
||||
) : Iterable<UInt> {
|
||||
init {
|
||||
if (step == 0.toInt()) throw kotlin.IllegalArgumentException("Step must be non-zero")
|
||||
}
|
||||
|
||||
/**
|
||||
* The first element in the progression.
|
||||
*/
|
||||
public val first: UInt = start
|
||||
|
||||
/**
|
||||
* The last element in the progression.
|
||||
*/
|
||||
public val last: UInt = getProgressionLastElement(start, endInclusive, step)
|
||||
|
||||
/**
|
||||
* The step of the progression.
|
||||
*/
|
||||
public val step: Int = step
|
||||
|
||||
override fun iterator(): UIntIterator = UIntProgressionIterator(first, last, step)
|
||||
|
||||
/** Checks if the progression is empty. */
|
||||
public open fun isEmpty(): Boolean = if (step > 0) first > last else first < last
|
||||
|
||||
override fun equals(other: Any?): Boolean =
|
||||
other is UIntProgression && (isEmpty() && other.isEmpty() ||
|
||||
first == other.first && last == other.last && step == other.step)
|
||||
|
||||
override fun hashCode(): Int =
|
||||
if (isEmpty()) -1 else (31 * (31 * first.toInt() + last.toInt()) + step.toInt())
|
||||
|
||||
override fun toString(): String = if (step > 0) "$first..$last step $step" else "$first downTo $last step ${-step}"
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* Creates UIntProgression 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.
|
||||
*/
|
||||
public fun fromClosedRange(rangeStart: UInt, rangeEnd: UInt, step: Int): UIntProgression = UIntProgression(rangeStart, rangeEnd, step)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* An iterator over a progression of values of type `UInt`.
|
||||
* @property step the number by which the value is incremented on each step.
|
||||
*/
|
||||
private class UIntProgressionIterator(first: UInt, last: UInt, step: Int) : UIntIterator() {
|
||||
private val finalElement = last
|
||||
private var hasNext: Boolean = if (step > 0) first <= last else first >= last
|
||||
private val step = step.toUInt() // use 2-complement math for negative steps
|
||||
private var next = if (hasNext) first else finalElement
|
||||
|
||||
override fun hasNext(): Boolean = hasNext
|
||||
|
||||
override fun nextUInt(): UInt {
|
||||
val value = next
|
||||
if (value == finalElement) {
|
||||
if (!hasNext) throw kotlin.NoSuchElementException()
|
||||
hasNext = false
|
||||
} else {
|
||||
next += step
|
||||
}
|
||||
return value
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user