Replace remaining usages of copyRangeTo to copyInto and drop copyRangeTo

This commit is contained in:
Ilya Gorbunov
2018-12-28 04:41:40 +03:00
committed by Vasily Levchenko
parent cfa7c12413
commit cfefbc184b
4 changed files with 17 additions and 110 deletions
@@ -200,7 +200,7 @@ actual class ArrayList<E> private constructor(
private fun insertAtInternal(i: Int, n: Int) { private fun insertAtInternal(i: Int, n: Int) {
ensureExtraCapacity(n) ensureExtraCapacity(n)
array.copyRangeTo(array, fromIndex = i, toIndex = offset + length, destinationIndex = i + n) array.copyInto(array, startIndex = i, endIndex = offset + length, destinationOffset = i + n)
length += n length += n
} }
@@ -238,7 +238,7 @@ actual class ArrayList<E> private constructor(
return old return old
} else { } else {
val old = array[i] val old = array[i]
array.copyRangeTo(array, fromIndex = i + 1, toIndex = offset + length, destinationIndex = i) array.copyInto(array, startIndex = i + 1, endIndex = offset + length, destinationOffset = i)
array.resetAt(offset + length - 1) array.resetAt(offset + length - 1)
length-- length--
return old return old
@@ -249,7 +249,7 @@ actual class ArrayList<E> private constructor(
if (backing != null) { if (backing != null) {
backing.removeRangeInternal(rangeOffset, rangeLength) backing.removeRangeInternal(rangeOffset, rangeLength)
} else { } else {
array.copyRangeTo(array, fromIndex = rangeOffset + rangeLength, toIndex = length, destinationIndex = rangeOffset) array.copyInto(array, startIndex = rangeOffset + rangeLength, endIndex = length, destinationOffset = rangeOffset)
array.resetRange(fromIndex = length - rangeLength, toIndex = length) array.resetRange(fromIndex = length - rangeLength, toIndex = length)
} }
length -= rangeLength length -= rangeLength
@@ -272,7 +272,7 @@ actual class ArrayList<E> private constructor(
} }
} }
val removed = rangeLength - j val removed = rangeLength - j
array.copyRangeTo(array, fromIndex = rangeOffset + rangeLength, toIndex = length, destinationIndex = rangeOffset + j) array.copyInto(array, startIndex = rangeOffset + rangeLength, endIndex = length, destinationOffset = rangeOffset + j)
array.resetRange(fromIndex = length - removed, toIndex = length) array.resetRange(fromIndex = length - removed, toIndex = length)
length -= removed length -= removed
return removed return removed
@@ -99,8 +99,7 @@ internal fun IntArray.fill(fromIndex: Int, toIndex: Int, value: Int) {
@SymbolName("Kotlin_Array_copyImpl") @SymbolName("Kotlin_Array_copyImpl")
@PointsTo(0b000100, 0, 0b000001) // <array> points to <destination>, <destination> points to <array>. @PointsTo(0b000100, 0, 0b000001) // <array> points to <destination>, <destination> points to <array>.
internal external fun arrayCopy(array: Array<Any?>, fromIndex: Int, internal external fun arrayCopy(array: Array<Any?>, fromIndex: Int, destination: Array<Any?>, toIndex: Int, count: Int)
destination: Array<Any?>, toIndex: Int, count: Int)
@SymbolName("Kotlin_ByteArray_copyImpl") @SymbolName("Kotlin_ByteArray_copyImpl")
internal external fun arrayCopy(array: ByteArray, fromIndex: Int, destination: ByteArray, toIndex: Int, count: Int) internal external fun arrayCopy(array: ByteArray, fromIndex: Int, destination: ByteArray, toIndex: Int, count: Int)
@@ -117,107 +116,15 @@ internal external fun arrayCopy(array: IntArray, fromIndex: Int, destination: In
@SymbolName("Kotlin_LongArray_copyImpl") @SymbolName("Kotlin_LongArray_copyImpl")
internal external fun arrayCopy(array: LongArray, fromIndex: Int, destination: LongArray, toIndex: Int, count: Int) internal external fun arrayCopy(array: LongArray, fromIndex: Int, destination: LongArray, toIndex: Int, count: Int)
// Note: [arrayCopy] for an unsigned array is bitwise identical to signed type, so
// signed array implementations from runtime are directly reused for unsigned ones.
@ExperimentalUnsignedTypes
@SymbolName("Kotlin_ByteArray_copyImpl")
internal external fun arrayCopy(array: UByteArray, fromIndex: Int, destination: UByteArray, toIndex: Int, count: Int)
@ExperimentalUnsignedTypes
@SymbolName("Kotlin_ShortArray_copyImpl")
internal external fun arrayCopy(array: UShortArray, fromIndex: Int, destination: UShortArray, toIndex: Int, count: Int)
@ExperimentalUnsignedTypes
@SymbolName("Kotlin_IntArray_copyImpl")
internal external fun arrayCopy(array: UIntArray, fromIndex: Int,
destination: UIntArray, toIndex: Int, count: Int)
@ExperimentalUnsignedTypes
@SymbolName("Kotlin_LongArray_copyImpl")
internal external fun arrayCopy(array: ULongArray, fromIndex: Int,
destination: ULongArray, toIndex: Int, count: Int)
@SymbolName("Kotlin_FloatArray_copyImpl") @SymbolName("Kotlin_FloatArray_copyImpl")
internal external fun arrayCopy(array: FloatArray, fromIndex: Int, internal external fun arrayCopy(array: FloatArray, fromIndex: Int, destination: FloatArray, toIndex: Int, count: Int)
destination: FloatArray, toIndex: Int, count: Int)
@SymbolName("Kotlin_DoubleArray_copyImpl") @SymbolName("Kotlin_DoubleArray_copyImpl")
internal external fun arrayCopy(array: DoubleArray, fromIndex: Int, internal external fun arrayCopy(array: DoubleArray, fromIndex: Int, destination: DoubleArray, toIndex: Int, count: Int)
destination: DoubleArray, toIndex: Int, count: Int)
@SymbolName("Kotlin_BooleanArray_copyImpl") @SymbolName("Kotlin_BooleanArray_copyImpl")
internal external fun arrayCopy(array: BooleanArray, fromIndex: Int, internal external fun arrayCopy(array: BooleanArray, fromIndex: Int, destination: BooleanArray, toIndex: Int, count: Int)
destination: BooleanArray, toIndex: Int, count: Int)
/**
* Copies a range of array elements at a specified [fromIndex] (inclusive) to [toIndex] (exclusive) range of indices
* to another [destination] array starting at [destinationIndex].
*/
@PublishedApi
internal fun <E> Array<out E>.copyRangeTo(
destination: Array<in E>, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) {
arrayCopy(@Suppress("UNCHECKED_CAST") (this as Array<Any?>), fromIndex,
@Suppress("UNCHECKED_CAST") (destination as Array<Any?>),
destinationIndex, toIndex - fromIndex)
}
internal fun ByteArray.copyRangeTo(destination: ByteArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) {
arrayCopy(this, fromIndex, destination, destinationIndex, toIndex - fromIndex)
}
internal fun ShortArray.copyRangeTo(destination: ShortArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) {
arrayCopy(this, fromIndex, destination, destinationIndex, toIndex - fromIndex)
}
internal fun CharArray.copyRangeTo(destination: CharArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) {
arrayCopy(this, fromIndex, destination, destinationIndex, toIndex - fromIndex)
}
internal fun IntArray.copyRangeTo(destination: IntArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) {
arrayCopy(this, fromIndex, destination, destinationIndex, toIndex - fromIndex)
}
internal fun LongArray.copyRangeTo(destination: LongArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) {
arrayCopy(this, fromIndex, destination, destinationIndex, toIndex - fromIndex)
}
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
internal fun UByteArray.copyRangeTo(destination: UByteArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) {
arrayCopy(this, fromIndex, destination, destinationIndex, toIndex - fromIndex)
}
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
internal fun UShortArray.copyRangeTo(destination: UShortArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) {
arrayCopy(this, fromIndex, destination, destinationIndex, toIndex - fromIndex)
}
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
internal fun UIntArray.copyRangeTo(destination: UIntArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) {
arrayCopy(this, fromIndex, destination, destinationIndex, toIndex - fromIndex)
}
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
internal fun ULongArray.copyRangeTo(destination: ULongArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) {
arrayCopy(this, fromIndex, destination, destinationIndex, toIndex - fromIndex)
}
internal fun FloatArray.copyRangeTo(destination: FloatArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) {
arrayCopy(this, fromIndex, destination, destinationIndex, toIndex - fromIndex)
}
internal fun DoubleArray.copyRangeTo(destination: DoubleArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) {
arrayCopy(this, fromIndex, destination, destinationIndex, toIndex - fromIndex)
}
internal fun BooleanArray.copyRangeTo(destination: BooleanArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) {
arrayCopy(this, fromIndex, destination, destinationIndex, toIndex - fromIndex)
}
internal fun <E> Collection<E>.collectionToString(): String { internal fun <E> Collection<E>.collectionToString(): String {
val sb = StringBuilder(2 + size * 3) val sb = StringBuilder(2 + size * 3)
@@ -34,7 +34,7 @@ public class MutableData constructor(capacity: Int = 16) {
if (newSize > buffer.size) { if (newSize > buffer.size) {
val actualSize = maxOf(buffer.size * 3 / 2 + 1, newSize) val actualSize = maxOf(buffer.size * 3 / 2 + 1, newSize)
val newBuffer = ByteArray(actualSize) val newBuffer = ByteArray(actualSize)
buffer.copyRangeTo(newBuffer, 0, size, 0) buffer.copyInto(newBuffer, startIndex = 0, endIndex = size)
newBuffer.share() newBuffer.share()
buffer = newBuffer buffer = newBuffer
} }
@@ -73,7 +73,7 @@ public class MutableData constructor(capacity: Int = 16) {
throw IndexOutOfBoundsException("$fromIndex is bigger than $toIndex") throw IndexOutOfBoundsException("$fromIndex is bigger than $toIndex")
if (toIndex == toIndex) return if (toIndex == toIndex) return
val where = resizeDataLocked(this.size + (toIndex - fromIndex)) val where = resizeDataLocked(this.size + (toIndex - fromIndex))
data.copyRangeTo(buffer, fromIndex, toIndex, where) data.copyInto(buffer, where, fromIndex, toIndex)
} }
/** /**
@@ -91,7 +91,7 @@ public class MutableData constructor(capacity: Int = 16) {
* Copies range of mutable data to the byte array. * Copies range of mutable data to the byte array.
*/ */
public fun copyInto(output: ByteArray, destinationIndex: Int, startIndex: Int, endIndex: Int): Unit = locked(lock) { public fun copyInto(output: ByteArray, destinationIndex: Int, startIndex: Int, endIndex: Int): Unit = locked(lock) {
buffer.copyRangeTo(output, startIndex, endIndex, destinationIndex) buffer.copyInto(output, destinationIndex, startIndex, endIndex)
} }
/** /**
@@ -168,7 +168,7 @@ actual class StringBuilder private constructor (
val extraLength = end - start val extraLength = end - start
ensureExtraCapacity(extraLength) ensureExtraCapacity(extraLength)
array.copyRangeTo(array, index, _length, index + extraLength) array.copyInto(array, startIndex = index, endIndex = _length, destinationOffset = index + extraLength)
var from = start var from = start
var to = index var to = index
while (from < end) { while (from < end) {
@@ -183,8 +183,8 @@ actual class StringBuilder private constructor (
checkInsertIndex(index) checkInsertIndex(index)
ensureExtraCapacity(chars.size) ensureExtraCapacity(chars.size)
array.copyRangeTo(array, index, _length, index + chars.size) array.copyInto(array, startIndex = index, endIndex = _length, destinationOffset = index + chars.size)
chars.copyRangeTo(array, 0, chars.size, index) chars.copyInto(array, destinationOffset = index)
_length += chars.size _length += chars.size
return this return this
@@ -193,7 +193,7 @@ actual class StringBuilder private constructor (
fun insert(index: Int, string: String): StringBuilder { fun insert(index: Int, string: String): StringBuilder {
checkInsertIndex(index) checkInsertIndex(index)
ensureExtraCapacity(string.length) ensureExtraCapacity(string.length)
array.copyRangeTo(array, index, _length, index + string.length) array.copyInto(array, startIndex = index, endIndex = _length, destinationOffset = index + string.length)
_length += insertString(array, index, string) _length += insertString(array, index, string)
return this return this
} }
@@ -235,7 +235,7 @@ actual class StringBuilder private constructor (
fun append(it: CharArray): StringBuilder { fun append(it: CharArray): StringBuilder {
ensureExtraCapacity(it.size) ensureExtraCapacity(it.size)
it.copyRangeTo(array, 0, it.size, _length) it.copyInto(array, _length)
_length += it.size _length += it.size
return this return this
} }
@@ -262,7 +262,7 @@ actual class StringBuilder private constructor (
fun deleteCharAt(index: Int) { fun deleteCharAt(index: Int) {
checkIndex(index) checkIndex(index)
array.copyRangeTo(array, index + 1, _length, index) array.copyInto(array, startIndex = index + 1, endIndex = _length, destinationOffset = index)
--_length --_length
} }