Rewrite ProgressionUtil to Kotlin, move to "kotlin.internal"

Use reflection in ProgressionUtilTest instead of calling methods on the class
from the class path, because in the latter case we were just testing
ProgressionUtil from bootstrap runtime (which, presumably, always works
correctly since it's a bootstrap distribution)

With this change, compilable built-ins (under core/builtins/src) are fully
written in Kotlin
This commit is contained in:
Alexander Udalov
2014-02-14 20:32:38 +04:00
parent 4afdac3efa
commit 0490d4c989
7 changed files with 124 additions and 106 deletions
@@ -18,11 +18,11 @@
package jet
import jet.runtime.ProgressionUtil
import kotlin.internal.getProgressionFinalElement
class ByteProgressionIterator(start: Byte, end: Byte, val increment: Int) : ByteIterator() {
private var next = start.toInt()
private val finalElement: Byte = ProgressionUtil.getProgressionFinalElement(start.toInt(), end.toInt(), increment).toByte()
private val finalElement: Byte = getProgressionFinalElement(start.toInt(), end.toInt(), increment).toByte()
private var hasNext: Boolean = if (increment > 0) start <= end else start >= end
override fun hasNext(): Boolean = hasNext
@@ -41,7 +41,7 @@ class ByteProgressionIterator(start: Byte, end: Byte, val increment: Int) : Byte
class CharProgressionIterator(start: Char, end: Char, val increment: Int) : CharIterator() {
private var next = start.toInt()
private val finalElement: Char = ProgressionUtil.getProgressionFinalElement(start.toInt(), end.toInt(), increment).toChar()
private val finalElement: Char = getProgressionFinalElement(start.toInt(), end.toInt(), increment).toChar()
private var hasNext: Boolean = if (increment > 0) start <= end else start >= end
override fun hasNext(): Boolean = hasNext
@@ -60,7 +60,7 @@ class CharProgressionIterator(start: Char, end: Char, val increment: Int) : Char
class ShortProgressionIterator(start: Short, end: Short, val increment: Int) : ShortIterator() {
private var next = start.toInt()
private val finalElement: Short = ProgressionUtil.getProgressionFinalElement(start.toInt(), end.toInt(), increment).toShort()
private val finalElement: Short = getProgressionFinalElement(start.toInt(), end.toInt(), increment).toShort()
private var hasNext: Boolean = if (increment > 0) start <= end else start >= end
override fun hasNext(): Boolean = hasNext
@@ -79,7 +79,7 @@ class ShortProgressionIterator(start: Short, end: Short, val increment: Int) : S
class IntProgressionIterator(start: Int, end: Int, val increment: Int) : IntIterator() {
private var next = start
private val finalElement: Int = ProgressionUtil.getProgressionFinalElement(start, end, increment)
private val finalElement: Int = getProgressionFinalElement(start, end, increment)
private var hasNext: Boolean = if (increment > 0) start <= end else start >= end
override fun hasNext(): Boolean = hasNext
@@ -98,7 +98,7 @@ class IntProgressionIterator(start: Int, end: Int, val increment: Int) : IntIter
class LongProgressionIterator(start: Long, end: Long, val increment: Long) : LongIterator() {
private var next = start
private val finalElement: Long = ProgressionUtil.getProgressionFinalElement(start, end, increment)
private val finalElement: Long = getProgressionFinalElement(start, end, increment)
private var hasNext: Boolean = if (increment > 0) start <= end else start >= end
override fun hasNext(): Boolean = hasNext
@@ -1,85 +0,0 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package jet.runtime;
public class ProgressionUtil {
private ProgressionUtil() {
}
// a mod b (in arithmetical sense)
private static int mod(int a, int b) {
int mod = a % b;
return mod >= 0 ? mod : mod + b;
}
private static long mod(long a, long b) {
long mod = a % b;
return mod >= 0 ? mod : mod + b;
}
// (a - b) mod c
private static int differenceModulo(int a, int b, int c) {
return mod(mod(a, c) - mod(b, c), c);
}
private static long differenceModulo(long a, long b, long c) {
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 {@code start} to {@code end} in case of a positive {@code increment}, or from {@code end} to {@code start} in case of a negative
* increment.
*
* <p>No validation on passed parameters is performed. The given parameters should satisfy the condition: either
* {@code increment&nbsp;&gt; 0} and {@code start&nbsp;&lt;= end}, or {@code increment&nbsp;&lt; 0} and {@code start&nbsp;&gt;= end}.
* @param start first element of the progression
* @param end ending bound for the progression
* @param increment increment, or difference of successive elements in the progression
* @return the final element of the progression
*/
public static int getProgressionFinalElement(int start, int end, int increment) {
if (increment > 0) {
return end - differenceModulo(end, start, increment);
}
else {
return end + differenceModulo(start, end, -increment);
}
}
/**
* Calculates the final element of a bounded arithmetic progression, i.e. the last element of the progression which is in the range
* from {@code start} to {@code end} in case of a positive {@code increment}, or from {@code end} to {@code start} in case of a negative
* increment.
*
* <p>No validation on passed parameters is performed. The given parameters should satisfy the condition: either
* {@code increment&nbsp;&gt; 0} and {@code start&nbsp;&lt;= end}, or {@code increment&nbsp;&lt; 0} and {@code start&nbsp;&gt;= end}.
* @param start first element of the progression
* @param end ending bound for the progression
* @param increment increment, or difference of successive elements in the progression
* @return the final element of the progression
*/
public static long getProgressionFinalElement(long start, long end, long increment) {
if (increment > 0) {
return end - differenceModulo(end, start, increment);
}
else {
return end + differenceModulo(start, end, -increment);
}
}
}
@@ -0,0 +1,80 @@
/*
* Copyright 2010-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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 {@code start} to {@code end} in case of a positive {@code increment}, or from {@code end} to {@code start} in case of a negative
* increment.
*
* <p>No validation on passed parameters is performed. The given parameters should satisfy the condition: either
* {@code increment&nbsp;&gt; 0} and {@code start&nbsp;&lt;= end}, or {@code increment&nbsp;&lt; 0} and {@code start&nbsp;&gt;= end}.
* @param start first element of the progression
* @param end ending bound for the progression
* @param increment increment, or difference of successive elements in the progression
* @return the final element of the progression
*/
public fun getProgressionFinalElement(start: Int, end: Int, increment: Int): Int {
if (increment > 0) {
return end - differenceModulo(end, start, increment)
}
else {
return end + differenceModulo(start, end, -increment)
}
}
/**
* Calculates the final element of a bounded arithmetic progression, i.e. the last element of the progression which is in the range
* from {@code start} to {@code end} in case of a positive {@code increment}, or from {@code end} to {@code start} in case of a negative
* increment.
*
* <p>No validation on passed parameters is performed. The given parameters should satisfy the condition: either
* {@code increment&nbsp;&gt; 0} and {@code start&nbsp;&lt;= end}, or {@code increment&nbsp;&lt; 0} and {@code start&nbsp;&gt;= end}.
* @param start first element of the progression
* @param end ending bound for the progression
* @param increment increment, or difference of successive elements in the progression
* @return the final element of the progression
*/
public fun getProgressionFinalElement(start: Long, end: Long, increment: Long): Long {
if (increment > 0) {
return end - differenceModulo(end, start, increment)
}
else {
return end + differenceModulo(start, end, -increment)
}
}