standard library: added isEmpty/isNotEmpty functions for primitive arrays

This commit is contained in:
nik
2013-12-01 15:02:45 +04:00
committed by Evgeny Gerashchenko
parent b636538422
commit 45e9211943
12 changed files with 171 additions and 6 deletions
@@ -183,6 +183,20 @@ public inline fun <K> BooleanArray.groupByTo(result: MutableMap<K, MutableList<B
return result
}
/**
* Returns true if the array is empty
*/
public fun BooleanArray.isEmpty() : Boolean {
return size == 0
}
/**
* Returns true if the array is empty
*/
public fun BooleanArray.isNotEmpty() : Boolean {
return !isEmpty()
}
/**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
@@ -183,6 +183,20 @@ public inline fun <K> ByteArray.groupByTo(result: MutableMap<K, MutableList<Byte
return result
}
/**
* Returns true if the array is empty
*/
public fun ByteArray.isEmpty() : Boolean {
return size == 0
}
/**
* Returns true if the array is empty
*/
public fun ByteArray.isNotEmpty() : Boolean {
return !isEmpty()
}
/**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
@@ -183,6 +183,20 @@ public inline fun <K> CharArray.groupByTo(result: MutableMap<K, MutableList<Char
return result
}
/**
* Returns true if the array is empty
*/
public fun CharArray.isEmpty() : Boolean {
return size == 0
}
/**
* Returns true if the array is empty
*/
public fun CharArray.isNotEmpty() : Boolean {
return !isEmpty()
}
/**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
@@ -183,6 +183,20 @@ public inline fun <K> DoubleArray.groupByTo(result: MutableMap<K, MutableList<Do
return result
}
/**
* Returns true if the array is empty
*/
public fun DoubleArray.isEmpty() : Boolean {
return size == 0
}
/**
* Returns true if the array is empty
*/
public fun DoubleArray.isNotEmpty() : Boolean {
return !isEmpty()
}
/**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
@@ -183,6 +183,20 @@ public inline fun <K> FloatArray.groupByTo(result: MutableMap<K, MutableList<Flo
return result
}
/**
* Returns true if the array is empty
*/
public fun FloatArray.isEmpty() : Boolean {
return size == 0
}
/**
* Returns true if the array is empty
*/
public fun FloatArray.isNotEmpty() : Boolean {
return !isEmpty()
}
/**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
@@ -183,6 +183,20 @@ public inline fun <K> IntArray.groupByTo(result: MutableMap<K, MutableList<Int>>
return result
}
/**
* Returns true if the array is empty
*/
public fun IntArray.isEmpty() : Boolean {
return size == 0
}
/**
* Returns true if the array is empty
*/
public fun IntArray.isNotEmpty() : Boolean {
return !isEmpty()
}
/**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
@@ -183,6 +183,20 @@ public inline fun <K> LongArray.groupByTo(result: MutableMap<K, MutableList<Long
return result
}
/**
* Returns true if the array is empty
*/
public fun LongArray.isEmpty() : Boolean {
return size == 0
}
/**
* Returns true if the array is empty
*/
public fun LongArray.isNotEmpty() : Boolean {
return !isEmpty()
}
/**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
@@ -183,6 +183,20 @@ public inline fun <K> ShortArray.groupByTo(result: MutableMap<K, MutableList<Sho
return result
}
/**
* Returns true if the array is empty
*/
public fun ShortArray.isEmpty() : Boolean {
return size == 0
}
/**
* Returns true if the array is empty
*/
public fun ShortArray.isNotEmpty() : Boolean {
return !isEmpty()
}
/**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
+24
View File
@@ -59,6 +59,30 @@ class ArraysJVMTest {
}
}
test fun isEmpty() {
assertTrue(intArray().isEmpty())
assertFalse(intArray(1).isEmpty())
assertTrue(byteArray().isEmpty())
assertFalse(byteArray(1).isEmpty())
assertTrue(shortArray().isEmpty())
assertFalse(shortArray(1).isEmpty())
assertTrue(longArray().isEmpty())
assertFalse(longArray(1).isEmpty())
assertTrue(charArray().isEmpty())
assertFalse(charArray('a').isEmpty())
assertTrue(floatArray().isEmpty())
assertFalse(floatArray(0.1).isEmpty())
assertTrue(doubleArray().isEmpty())
assertFalse(doubleArray(0.1).isEmpty())
assertTrue(booleanArray().isEmpty())
assertFalse(booleanArray(false).isEmpty())
}
test fun isNotEmpty() {
assertFalse(intArray().isNotEmpty())
assertTrue(intArray(1).isNotEmpty())
}
test fun min() {
expect(null, { intArray().min() })
expect(1, { intArray(1).min() })
@@ -32,19 +32,19 @@ fun main(args: Array<String>) {
buildFor(Iterators, null)
}
val iterables = iterables()
val arrays = arrays()
val sumFunctions = PrimitiveType.values().map(::sumFunction).filterNotNull()
(iterables + sumFunctions).writeTo(File(outDir, "_Arrays.kt")) {
(arrays + sumFunctions).writeTo(File(outDir, "_Arrays.kt")) {
buildFor(Arrays, null)
}
for (primitive in PrimitiveType.values()) {
(iterables + sumFunction(primitive)).filterNotNull().writeTo(File(outDir, "_${primitive.name}Arrays.kt")) {
(arrays + sumFunction(primitive)).filterNotNull().writeTo(File(outDir, "_${primitive.name}Arrays.kt")) {
buildFor(PrimitiveArrays, primitive)
}
}
(iterables + sumFunctions).writeTo(File(outDir, "_Iterables.kt")) {
(iterables().sort() + sumFunctions).writeTo(File(outDir, "_Iterables.kt")) {
buildFor(Iterables, null)
}
@@ -0,0 +1,29 @@
package templates
import templates.Family.*
fun arrays(): List<GenericFunction> {
val templates = iterables()
templates add f("isEmpty()") {
absentFor(Arrays)
isInline = false
doc = "Returns true if the array is empty"
returns("Boolean")
body {
"return size == 0"
}
}
templates add f("isNotEmpty()") {
absentFor(Arrays)
isInline = false
doc = "Returns true if the array is empty"
returns("Boolean")
body {
"return !isEmpty()"
}
}
return templates.sort()
}
@@ -3,7 +3,7 @@ package templates
import java.util.ArrayList
import templates.Family.*
fun iterables(): List<GenericFunction> {
fun iterables(): ArrayList<GenericFunction> {
val templates = commons()
@@ -140,5 +140,5 @@ fun iterables(): List<GenericFunction> {
}
}
return templates.sort()
return templates
}