Make two overloads of copyOf to preserve nullness when size doesn't change
This commit is contained in:
committed by
Andrey Breslav
parent
108fdc0097
commit
ceb2aa57f9
@@ -64,65 +64,128 @@ public fun ShortArray.binarySearch(element: Short, fromIndex: Int = 0, toIndex:
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns new array which is a copy of the riginal array
|
* Returns new array which is a copy of the original array
|
||||||
*/
|
*/
|
||||||
public fun <T> Array<out T>.copyOf(newSize: Int = size) : Array<T> {
|
public fun <T> Array<out T>.copyOf() : Array<T> {
|
||||||
return Arrays.copyOf(this, newSize) as Array<T>
|
return Arrays.copyOf(this, size) as Array<T>
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns new array which is a copy of the riginal array
|
* Returns new array which is a copy of the original array
|
||||||
*/
|
*/
|
||||||
public fun BooleanArray.copyOf(newSize: Int = size) : BooleanArray {
|
public fun BooleanArray.copyOf() : BooleanArray {
|
||||||
|
return Arrays.copyOf(this, size)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns new array which is a copy of the original array
|
||||||
|
*/
|
||||||
|
public fun ByteArray.copyOf() : ByteArray {
|
||||||
|
return Arrays.copyOf(this, size)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns new array which is a copy of the original array
|
||||||
|
*/
|
||||||
|
public fun CharArray.copyOf() : CharArray {
|
||||||
|
return Arrays.copyOf(this, size)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns new array which is a copy of the original array
|
||||||
|
*/
|
||||||
|
public fun DoubleArray.copyOf() : DoubleArray {
|
||||||
|
return Arrays.copyOf(this, size)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns new array which is a copy of the original array
|
||||||
|
*/
|
||||||
|
public fun FloatArray.copyOf() : FloatArray {
|
||||||
|
return Arrays.copyOf(this, size)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns new array which is a copy of the original array
|
||||||
|
*/
|
||||||
|
public fun IntArray.copyOf() : IntArray {
|
||||||
|
return Arrays.copyOf(this, size)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns new array which is a copy of the original array
|
||||||
|
*/
|
||||||
|
public fun LongArray.copyOf() : LongArray {
|
||||||
|
return Arrays.copyOf(this, size)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns new array which is a copy of the original array
|
||||||
|
*/
|
||||||
|
public fun ShortArray.copyOf() : ShortArray {
|
||||||
|
return Arrays.copyOf(this, size)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns new array which is a copy of the original array
|
||||||
|
*/
|
||||||
|
public fun <T> Array<out T>.copyOf(newSize: Int) : Array<T?> {
|
||||||
|
return Arrays.copyOf(this, newSize) as Array<T?>
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns new array which is a copy of the original array
|
||||||
|
*/
|
||||||
|
public fun BooleanArray.copyOf(newSize: Int) : BooleanArray {
|
||||||
return Arrays.copyOf(this, newSize)
|
return Arrays.copyOf(this, newSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns new array which is a copy of the riginal array
|
* Returns new array which is a copy of the original array
|
||||||
*/
|
*/
|
||||||
public fun ByteArray.copyOf(newSize: Int = size) : ByteArray {
|
public fun ByteArray.copyOf(newSize: Int) : ByteArray {
|
||||||
return Arrays.copyOf(this, newSize)
|
return Arrays.copyOf(this, newSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns new array which is a copy of the riginal array
|
* Returns new array which is a copy of the original array
|
||||||
*/
|
*/
|
||||||
public fun CharArray.copyOf(newSize: Int = size) : CharArray {
|
public fun CharArray.copyOf(newSize: Int) : CharArray {
|
||||||
return Arrays.copyOf(this, newSize)
|
return Arrays.copyOf(this, newSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns new array which is a copy of the riginal array
|
* Returns new array which is a copy of the original array
|
||||||
*/
|
*/
|
||||||
public fun DoubleArray.copyOf(newSize: Int = size) : DoubleArray {
|
public fun DoubleArray.copyOf(newSize: Int) : DoubleArray {
|
||||||
return Arrays.copyOf(this, newSize)
|
return Arrays.copyOf(this, newSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns new array which is a copy of the riginal array
|
* Returns new array which is a copy of the original array
|
||||||
*/
|
*/
|
||||||
public fun FloatArray.copyOf(newSize: Int = size) : FloatArray {
|
public fun FloatArray.copyOf(newSize: Int) : FloatArray {
|
||||||
return Arrays.copyOf(this, newSize)
|
return Arrays.copyOf(this, newSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns new array which is a copy of the riginal array
|
* Returns new array which is a copy of the original array
|
||||||
*/
|
*/
|
||||||
public fun IntArray.copyOf(newSize: Int = size) : IntArray {
|
public fun IntArray.copyOf(newSize: Int) : IntArray {
|
||||||
return Arrays.copyOf(this, newSize)
|
return Arrays.copyOf(this, newSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns new array which is a copy of the riginal array
|
* Returns new array which is a copy of the original array
|
||||||
*/
|
*/
|
||||||
public fun LongArray.copyOf(newSize: Int = size) : LongArray {
|
public fun LongArray.copyOf(newSize: Int) : LongArray {
|
||||||
return Arrays.copyOf(this, newSize)
|
return Arrays.copyOf(this, newSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns new array which is a copy of the riginal array
|
* Returns new array which is a copy of the original array
|
||||||
*/
|
*/
|
||||||
public fun ShortArray.copyOf(newSize: Int = size) : ShortArray {
|
public fun ShortArray.copyOf(newSize: Int) : ShortArray {
|
||||||
return Arrays.copyOf(this, newSize)
|
return Arrays.copyOf(this, newSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,16 +14,29 @@ fun specialJVM(): List<GenericFunction> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: when newLength is larger than size, we can get null items
|
templates add f("copyOf()") {
|
||||||
templates add f("copyOf(newSize: Int = size)") {
|
|
||||||
only(ArraysOfObjects, ArraysOfPrimitives)
|
only(ArraysOfObjects, ArraysOfPrimitives)
|
||||||
doc { "Returns new array which is a copy of the riginal array" }
|
doc { "Returns new array which is a copy of the original array" }
|
||||||
|
returns("SELF")
|
||||||
|
body {
|
||||||
|
"return Arrays.copyOf(this, size)"
|
||||||
|
}
|
||||||
|
body(ArraysOfObjects) {
|
||||||
|
"return Arrays.copyOf(this, size) as Array<T>"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// This overload can cause nulls if array size is expanding, hence different return overload
|
||||||
|
templates add f("copyOf(newSize: Int)") {
|
||||||
|
only(ArraysOfObjects, ArraysOfPrimitives)
|
||||||
|
doc { "Returns new array which is a copy of the original array" }
|
||||||
returns("SELF")
|
returns("SELF")
|
||||||
body {
|
body {
|
||||||
"return Arrays.copyOf(this, newSize)"
|
"return Arrays.copyOf(this, newSize)"
|
||||||
}
|
}
|
||||||
|
returns(ArraysOfObjects) { "Array<T?>" }
|
||||||
body(ArraysOfObjects) {
|
body(ArraysOfObjects) {
|
||||||
"return Arrays.copyOf(this, newSize) as Array<T>"
|
"return Arrays.copyOf(this, newSize) as Array<T?>"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user