Skip to content

Navigation Menu

Sign inAppearance settings
Appearance settings

Commit b4ba00c

Browse filesBrowse the repository at this point in the historyBrowse files
committed
Document and test NaN propagation of maxOf/minOf
Also simplify minOf/maxOf implementations #KT-38708
1 parent 7b68de3 commit b4ba00c
Copy full SHA for b4ba00c

10 files changed

+514-1,232Lines changed: 514 additions & 1232 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎libraries/stdlib/common/src/generated/_Arrays.kt‎

Copy file name to clipboardExpand all lines: libraries/stdlib/common/src/generated/_Arrays.kt
+216-720Lines changed: 216 additions & 720 deletions
Large diffs are not rendered by default.
Collapse file

‎libraries/stdlib/common/src/generated/_Collections.kt‎

Copy file name to clipboardExpand all lines: libraries/stdlib/common/src/generated/_Collections.kt
+24-44Lines changed: 24 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1797,6 +1797,8 @@ public inline fun <T, R : Comparable<R>> Iterable<T>.maxBy(selector: (T) -> R):
17971797
* Returns the largest value among all values produced by [selector] function
17981798
* applied to each element in the collection.
17991799
*
1800+
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
1801+
*
18001802
* @throws NoSuchElementException if the collection is empty.
18011803
*/
18021804
@SinceKotlin("1.4")
@@ -1807,13 +1809,9 @@ public inline fun <T> Iterable<T>.maxOf(selector: (T) -> Double): Double {
18071809
val iterator = iterator()
18081810
if (!iterator.hasNext()) throw NoSuchElementException()
18091811
var maxValue = selector(iterator.next())
1810-
if (maxValue.isNaN()) return maxValue
18111812
while (iterator.hasNext()) {
18121813
val v = selector(iterator.next())
1813-
if (v.isNaN()) return v
1814-
if (maxValue < v) {
1815-
maxValue = v
1816-
}
1814+
maxValue = maxOf(maxValue, v)
18171815
}
18181816
return maxValue
18191817
}
@@ -1822,6 +1820,8 @@ public inline fun <T> Iterable<T>.maxOf(selector: (T) -> Double): Double {
18221820
* Returns the largest value among all values produced by [selector] function
18231821
* applied to each element in the collection.
18241822
*
1823+
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
1824+
*
18251825
* @throws NoSuchElementException if the collection is empty.
18261826
*/
18271827
@SinceKotlin("1.4")
@@ -1832,13 +1832,9 @@ public inline fun <T> Iterable<T>.maxOf(selector: (T) -> Float): Float {
18321832
val iterator = iterator()
18331833
if (!iterator.hasNext()) throw NoSuchElementException()
18341834
var maxValue = selector(iterator.next())
1835-
if (maxValue.isNaN()) return maxValue
18361835
while (iterator.hasNext()) {
18371836
val v = selector(iterator.next())
1838-
if (v.isNaN()) return v
1839-
if (maxValue < v) {
1840-
maxValue = v
1841-
}
1837+
maxValue = maxOf(maxValue, v)
18421838
}
18431839
return maxValue
18441840
}
@@ -1859,7 +1855,6 @@ public inline fun <T, R : Comparable<R>> Iterable<T>.maxOf(selector: (T) -> R):
18591855
var maxValue = selector(iterator.next())
18601856
while (iterator.hasNext()) {
18611857
val v = selector(iterator.next())
1862-
18631858
if (maxValue < v) {
18641859
maxValue = v
18651860
}
@@ -1870,6 +1865,8 @@ public inline fun <T, R : Comparable<R>> Iterable<T>.maxOf(selector: (T) -> R):
18701865
/**
18711866
* Returns the largest value among all values produced by [selector] function
18721867
* applied to each element in the collection or `null` if there are no elements.
1868+
*
1869+
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
18731870
*/
18741871
@SinceKotlin("1.4")
18751872
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@@ -1879,20 +1876,18 @@ public inline fun <T> Iterable<T>.maxOfOrNull(selector: (T) -> Double): Double?
18791876
val iterator = iterator()
18801877
if (!iterator.hasNext()) return null
18811878
var maxValue = selector(iterator.next())
1882-
if (maxValue.isNaN()) return maxValue
18831879
while (iterator.hasNext()) {
18841880
val v = selector(iterator.next())
1885-
if (v.isNaN()) return v
1886-
if (maxValue < v) {
1887-
maxValue = v
1888-
}
1881+
maxValue = maxOf(maxValue, v)
18891882
}
18901883
return maxValue
18911884
}
18921885

18931886
/**
18941887
* Returns the largest value among all values produced by [selector] function
18951888
* applied to each element in the collection or `null` if there are no elements.
1889+
*
1890+
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
18961891
*/
18971892
@SinceKotlin("1.4")
18981893
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@@ -1902,13 +1897,9 @@ public inline fun <T> Iterable<T>.maxOfOrNull(selector: (T) -> Float): Float? {
19021897
val iterator = iterator()
19031898
if (!iterator.hasNext()) return null
19041899
var maxValue = selector(iterator.next())
1905-
if (maxValue.isNaN()) return maxValue
19061900
while (iterator.hasNext()) {
19071901
val v = selector(iterator.next())
1908-
if (v.isNaN()) return v
1909-
if (maxValue < v) {
1910-
maxValue = v
1911-
}
1902+
maxValue = maxOf(maxValue, v)
19121903
}
19131904
return maxValue
19141905
}
@@ -1927,7 +1918,6 @@ public inline fun <T, R : Comparable<R>> Iterable<T>.maxOfOrNull(selector: (T) -
19271918
var maxValue = selector(iterator.next())
19281919
while (iterator.hasNext()) {
19291920
val v = selector(iterator.next())
1930-
19311921
if (maxValue < v) {
19321922
maxValue = v
19331923
}
@@ -2071,6 +2061,8 @@ public inline fun <T, R : Comparable<R>> Iterable<T>.minBy(selector: (T) -> R):
20712061
* Returns the smallest value among all values produced by [selector] function
20722062
* applied to each element in the collection.
20732063
*
2064+
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
2065+
*
20742066
* @throws NoSuchElementException if the collection is empty.
20752067
*/
20762068
@SinceKotlin("1.4")
@@ -2081,13 +2073,9 @@ public inline fun <T> Iterable<T>.minOf(selector: (T) -> Double): Double {
20812073
val iterator = iterator()
20822074
if (!iterator.hasNext()) throw NoSuchElementException()
20832075
var minValue = selector(iterator.next())
2084-
if (minValue.isNaN()) return minValue
20852076
while (iterator.hasNext()) {
20862077
val v = selector(iterator.next())
2087-
if (v.isNaN()) return v
2088-
if (minValue > v) {
2089-
minValue = v
2090-
}
2078+
minValue = minOf(minValue, v)
20912079
}
20922080
return minValue
20932081
}
@@ -2096,6 +2084,8 @@ public inline fun <T> Iterable<T>.minOf(selector: (T) -> Double): Double {
20962084
* Returns the smallest value among all values produced by [selector] function
20972085
* applied to each element in the collection.
20982086
*
2087+
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
2088+
*
20992089
* @throws NoSuchElementException if the collection is empty.
21002090
*/
21012091
@SinceKotlin("1.4")
@@ -2106,13 +2096,9 @@ public inline fun <T> Iterable<T>.minOf(selector: (T) -> Float): Float {
21062096
val iterator = iterator()
21072097
if (!iterator.hasNext()) throw NoSuchElementException()
21082098
var minValue = selector(iterator.next())
2109-
if (minValue.isNaN()) return minValue
21102099
while (iterator.hasNext()) {
21112100
val v = selector(iterator.next())
2112-
if (v.isNaN()) return v
2113-
if (minValue > v) {
2114-
minValue = v
2115-
}
2101+
minValue = minOf(minValue, v)
21162102
}
21172103
return minValue
21182104
}
@@ -2133,7 +2119,6 @@ public inline fun <T, R : Comparable<R>> Iterable<T>.minOf(selector: (T) -> R):
21332119
var minValue = selector(iterator.next())
21342120
while (iterator.hasNext()) {
21352121
val v = selector(iterator.next())
2136-
21372122
if (minValue > v) {
21382123
minValue = v
21392124
}
@@ -2144,6 +2129,8 @@ public inline fun <T, R : Comparable<R>> Iterable<T>.minOf(selector: (T) -> R):
21442129
/**
21452130
* Returns the smallest value among all values produced by [selector] function
21462131
* applied to each element in the collection or `null` if there are no elements.
2132+
*
2133+
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
21472134
*/
21482135
@SinceKotlin("1.4")
21492136
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@@ -2153,20 +2140,18 @@ public inline fun <T> Iterable<T>.minOfOrNull(selector: (T) -> Double): Double?
21532140
val iterator = iterator()
21542141
if (!iterator.hasNext()) return null
21552142
var minValue = selector(iterator.next())
2156-
if (minValue.isNaN()) return minValue
21572143
while (iterator.hasNext()) {
21582144
val v = selector(iterator.next())
2159-
if (v.isNaN()) return v
2160-
if (minValue > v) {
2161-
minValue = v
2162-
}
2145+
minValue = minOf(minValue, v)
21632146
}
21642147
return minValue
21652148
}
21662149

21672150
/**
21682151
* Returns the smallest value among all values produced by [selector] function
21692152
* applied to each element in the collection or `null` if there are no elements.
2153+
*
2154+
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
21702155
*/
21712156
@SinceKotlin("1.4")
21722157
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@@ -2176,13 +2161,9 @@ public inline fun <T> Iterable<T>.minOfOrNull(selector: (T) -> Float): Float? {
21762161
val iterator = iterator()
21772162
if (!iterator.hasNext()) return null
21782163
var minValue = selector(iterator.next())
2179-
if (minValue.isNaN()) return minValue
21802164
while (iterator.hasNext()) {
21812165
val v = selector(iterator.next())
2182-
if (v.isNaN()) return v
2183-
if (minValue > v) {
2184-
minValue = v
2185-
}
2166+
minValue = minOf(minValue, v)
21862167
}
21872168
return minValue
21882169
}
@@ -2201,7 +2182,6 @@ public inline fun <T, R : Comparable<R>> Iterable<T>.minOfOrNull(selector: (T) -
22012182
var minValue = selector(iterator.next())
22022183
while (iterator.hasNext()) {
22032184
val v = selector(iterator.next())
2204-
22052185
if (minValue > v) {
22062186
minValue = v
22072187
}
Collapse file

‎libraries/stdlib/common/src/generated/_Maps.kt‎

Copy file name to clipboardExpand all lines: libraries/stdlib/common/src/generated/_Maps.kt
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ public inline fun <K, V, R : Comparable<R>> Map<out K, V>.maxBy(selector: (Map.E
193193
* Returns the largest value among all values produced by [selector] function
194194
* applied to each entry in the map.
195195
*
196+
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
197+
*
196198
* @throws NoSuchElementException if the map is empty.
197199
*/
198200
@SinceKotlin("1.4")
@@ -207,6 +209,8 @@ public inline fun <K, V> Map<out K, V>.maxOf(selector: (Map.Entry<K, V>) -> Doub
207209
* Returns the largest value among all values produced by [selector] function
208210
* applied to each entry in the map.
209211
*
212+
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
213+
*
210214
* @throws NoSuchElementException if the map is empty.
211215
*/
212216
@SinceKotlin("1.4")
@@ -234,6 +238,8 @@ public inline fun <K, V, R : Comparable<R>> Map<out K, V>.maxOf(selector: (Map.E
234238
/**
235239
* Returns the largest value among all values produced by [selector] function
236240
* applied to each entry in the map or `null` if there are no entries.
241+
*
242+
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
237243
*/
238244
@SinceKotlin("1.4")
239245
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@@ -246,6 +252,8 @@ public inline fun <K, V> Map<out K, V>.maxOfOrNull(selector: (Map.Entry<K, V>) -
246252
/**
247253
* Returns the largest value among all values produced by [selector] function
248254
* applied to each entry in the map or `null` if there are no entries.
255+
*
256+
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
249257
*/
250258
@SinceKotlin("1.4")
251259
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@@ -314,6 +322,8 @@ public inline fun <K, V, R : Comparable<R>> Map<out K, V>.minBy(selector: (Map.E
314322
* Returns the smallest value among all values produced by [selector] function
315323
* applied to each entry in the map.
316324
*
325+
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
326+
*
317327
* @throws NoSuchElementException if the map is empty.
318328
*/
319329
@SinceKotlin("1.4")
@@ -328,6 +338,8 @@ public inline fun <K, V> Map<out K, V>.minOf(selector: (Map.Entry<K, V>) -> Doub
328338
* Returns the smallest value among all values produced by [selector] function
329339
* applied to each entry in the map.
330340
*
341+
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
342+
*
331343
* @throws NoSuchElementException if the map is empty.
332344
*/
333345
@SinceKotlin("1.4")
@@ -355,6 +367,8 @@ public inline fun <K, V, R : Comparable<R>> Map<out K, V>.minOf(selector: (Map.E
355367
/**
356368
* Returns the smallest value among all values produced by [selector] function
357369
* applied to each entry in the map or `null` if there are no entries.
370+
*
371+
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
358372
*/
359373
@SinceKotlin("1.4")
360374
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@@ -367,6 +381,8 @@ public inline fun <K, V> Map<out K, V>.minOfOrNull(selector: (Map.Entry<K, V>) -
367381
/**
368382
* Returns the smallest value among all values produced by [selector] function
369383
* applied to each entry in the map or `null` if there are no entries.
384+
*
385+
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
370386
*/
371387
@SinceKotlin("1.4")
372388
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.