Optimize selection functions, add guard checks.
This commit is contained in:
committed by
Andrey Breslav
parent
a3ad79e5d9
commit
d9d5631a16
File diff suppressed because it is too large
Load Diff
@@ -9,7 +9,17 @@ fun elements(): List<GenericFunction> {
|
|||||||
doc { "Returns true if *element* is found in the collection" }
|
doc { "Returns true if *element* is found in the collection" }
|
||||||
returns("Boolean")
|
returns("Boolean")
|
||||||
body {
|
body {
|
||||||
"return indexOf(element) >= 0"
|
"""
|
||||||
|
if (this is Collection<*>)
|
||||||
|
return contains(element)
|
||||||
|
return indexOf(element) >= 0
|
||||||
|
"""
|
||||||
|
}
|
||||||
|
exclude(Lists, Collections)
|
||||||
|
body(ArraysOfPrimitives, ArraysOfObjects) {
|
||||||
|
"""
|
||||||
|
return indexOf(element) >= 0
|
||||||
|
"""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,7 +84,6 @@ fun elements(): List<GenericFunction> {
|
|||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
|
|
||||||
include(Lists)
|
|
||||||
body(Lists, ArraysOfObjects) {
|
body(Lists, ArraysOfObjects) {
|
||||||
"""
|
"""
|
||||||
if (element == null) {
|
if (element == null) {
|
||||||
@@ -146,14 +155,26 @@ fun elements(): List<GenericFunction> {
|
|||||||
returns("T")
|
returns("T")
|
||||||
body {
|
body {
|
||||||
"""
|
"""
|
||||||
val iterator = iterator()
|
when (this) {
|
||||||
if (!iterator.hasNext())
|
is List<*> -> {
|
||||||
throw IllegalArgumentException("Collection is empty")
|
if (size == 0)
|
||||||
return iterator.next()
|
throw IllegalArgumentException("Collection is empty")
|
||||||
|
else
|
||||||
|
return this[0] as T
|
||||||
|
}
|
||||||
|
else -> {
|
||||||
|
val iterator = iterator()
|
||||||
|
if (!iterator.hasNext())
|
||||||
|
throw IllegalArgumentException("Collection is empty")
|
||||||
|
return iterator.next()
|
||||||
|
}
|
||||||
|
}
|
||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
body(Lists, ArraysOfObjects, ArraysOfPrimitives) {
|
body(Lists, ArraysOfObjects, ArraysOfPrimitives) {
|
||||||
"""
|
"""
|
||||||
|
if (size == 0)
|
||||||
|
throw IllegalArgumentException("Collection is empty")
|
||||||
return this[0]
|
return this[0]
|
||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
@@ -163,10 +184,20 @@ fun elements(): List<GenericFunction> {
|
|||||||
returns("T?")
|
returns("T?")
|
||||||
body {
|
body {
|
||||||
"""
|
"""
|
||||||
val iterator = iterator()
|
when (this) {
|
||||||
if (!iterator.hasNext())
|
is List<*> -> {
|
||||||
return null
|
if (size == 0)
|
||||||
return iterator.next()
|
return null
|
||||||
|
else
|
||||||
|
return this[0] as T
|
||||||
|
}
|
||||||
|
else -> {
|
||||||
|
val iterator = iterator()
|
||||||
|
if (!iterator.hasNext())
|
||||||
|
return null
|
||||||
|
return iterator.next()
|
||||||
|
}
|
||||||
|
}
|
||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
body(Lists, ArraysOfObjects, ArraysOfPrimitives) {
|
body(Lists, ArraysOfObjects, ArraysOfPrimitives) {
|
||||||
@@ -208,7 +239,10 @@ fun elements(): List<GenericFunction> {
|
|||||||
body {
|
body {
|
||||||
"""
|
"""
|
||||||
when (this) {
|
when (this) {
|
||||||
is List<*> -> return this[size - 1] as T
|
is List<*> -> if (size == 0)
|
||||||
|
throw IllegalArgumentException("Collection is empty")
|
||||||
|
else
|
||||||
|
return this[size - 1] as T
|
||||||
else -> {
|
else -> {
|
||||||
val iterator = iterator()
|
val iterator = iterator()
|
||||||
if (!iterator.hasNext())
|
if (!iterator.hasNext())
|
||||||
@@ -258,43 +292,36 @@ fun elements(): List<GenericFunction> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
templates add f("last(predicate: (T) -> Boolean)") {
|
templates add f("last(predicate: (T) -> Boolean)") {
|
||||||
|
inline(true)
|
||||||
doc { "Returns last element matching the given *predicate*" }
|
doc { "Returns last element matching the given *predicate*" }
|
||||||
returns("T")
|
returns("T")
|
||||||
body {
|
body {
|
||||||
"""
|
"""
|
||||||
fun first(it : Iterator<T>) : T {
|
var last: T? = null
|
||||||
for (element in it) if (predicate(element)) return element
|
var found = false
|
||||||
throw IllegalArgumentException("Collection doesn't contain any element matching predicate")
|
for (element in this) {
|
||||||
}
|
if (predicate(element)) {
|
||||||
val iterator = iterator()
|
|
||||||
var last = first(iterator)
|
|
||||||
while (iterator.hasNext()) {
|
|
||||||
val element = iterator.next()
|
|
||||||
if (predicate(element))
|
|
||||||
last = element
|
last = element
|
||||||
|
found = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return last
|
if (!found) throw IllegalArgumentException("Collection doesn't contain any element matching predicate")
|
||||||
|
return last as T
|
||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
templates add f("lastOrNull(predicate: (T) -> Boolean)") {
|
templates add f("lastOrNull(predicate: (T) -> Boolean)") {
|
||||||
|
inline(true)
|
||||||
doc { "Returns last element matching the given *predicate*, or null if element was not found" }
|
doc { "Returns last element matching the given *predicate*, or null if element was not found" }
|
||||||
returns("T?")
|
returns("T?")
|
||||||
body {
|
body {
|
||||||
"""
|
"""
|
||||||
fun first(it : Iterator<T>) : T? {
|
var last: T? = null
|
||||||
for (element in it) if (predicate(element)) return element
|
for (element in this) {
|
||||||
return null
|
if (predicate(element)) {
|
||||||
}
|
|
||||||
val iterator = iterator()
|
|
||||||
var last = first(iterator)
|
|
||||||
if (last == null)
|
|
||||||
return null
|
|
||||||
while (iterator.hasNext()) {
|
|
||||||
val element = iterator.next()
|
|
||||||
if (predicate(element))
|
|
||||||
last = element
|
last = element
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return last
|
return last
|
||||||
"""
|
"""
|
||||||
@@ -321,7 +348,7 @@ fun elements(): List<GenericFunction> {
|
|||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
body(ArraysOfObjects, ArraysOfPrimitives) {
|
body(Lists, ArraysOfObjects, ArraysOfPrimitives) {
|
||||||
"""
|
"""
|
||||||
if (size != 1)
|
if (size != 1)
|
||||||
throw IllegalArgumentException("Collection has ${bucks}size elements")
|
throw IllegalArgumentException("Collection has ${bucks}size elements")
|
||||||
@@ -349,7 +376,7 @@ fun elements(): List<GenericFunction> {
|
|||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
body(ArraysOfObjects, ArraysOfPrimitives) {
|
body(Lists, ArraysOfObjects, ArraysOfPrimitives) {
|
||||||
"""
|
"""
|
||||||
if (size == 0)
|
if (size == 0)
|
||||||
return null
|
return null
|
||||||
@@ -361,44 +388,42 @@ fun elements(): List<GenericFunction> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
templates add f("single(predicate: (T) -> Boolean)") {
|
templates add f("single(predicate: (T) -> Boolean)") {
|
||||||
|
inline(true)
|
||||||
doc { "Returns single element matching the given *predicate*, or throws exception if there is no or more than one element" }
|
doc { "Returns single element matching the given *predicate*, or throws exception if there is no or more than one element" }
|
||||||
returns("T")
|
returns("T")
|
||||||
body {
|
body {
|
||||||
"""
|
"""
|
||||||
fun first(it : Iterator<T>) : T {
|
var single: T? = null
|
||||||
for (element in it) if (predicate(element)) return element
|
var found = false
|
||||||
throw IllegalArgumentException("Collection doesn't have matching element")
|
for (element in this) {
|
||||||
|
if (predicate(element)) {
|
||||||
|
if (found) throw IllegalArgumentException("Collection contains more than one matching element")
|
||||||
|
single = element
|
||||||
|
found = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
val iterator = iterator()
|
if (!found) throw IllegalArgumentException("Collection doesn't contain any element matching predicate")
|
||||||
var single = first(iterator)
|
return single as T
|
||||||
while (iterator.hasNext()) {
|
|
||||||
val element = iterator.next()
|
|
||||||
if (predicate(element))
|
|
||||||
throw IllegalArgumentException("Collection has more than one matching element")
|
|
||||||
}
|
|
||||||
return single
|
|
||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
templates add f("singleOrNull(predicate: (T) -> Boolean)") {
|
templates add f("singleOrNull(predicate: (T) -> Boolean)") {
|
||||||
|
inline(true)
|
||||||
doc { "Returns single element matching the given *predicate*, or null if element was not found or more than one elements were found" }
|
doc { "Returns single element matching the given *predicate*, or null if element was not found or more than one elements were found" }
|
||||||
returns("T?")
|
returns("T?")
|
||||||
body {
|
body {
|
||||||
"""
|
"""
|
||||||
fun first(it : Iterator<T>) : T? {
|
var single: T? = null
|
||||||
for (element in it) if (predicate(element)) return element
|
var found = false
|
||||||
return null
|
for (element in this) {
|
||||||
}
|
if (predicate(element)) {
|
||||||
val iterator = iterator()
|
if (found) throw IllegalArgumentException("Collection contains more than one matching element")
|
||||||
var single = first(iterator)
|
single = element
|
||||||
if (single == null)
|
found = true
|
||||||
return null
|
}
|
||||||
while (iterator.hasNext()) {
|
|
||||||
val element = iterator.next()
|
|
||||||
if (predicate(element))
|
|
||||||
throw IllegalArgumentException("Collection has more than one matching element")
|
|
||||||
}
|
}
|
||||||
|
if (!found) return null
|
||||||
return single
|
return single
|
||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user