forked from JetBrains/kotlin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComparables.kt
402 lines (377 loc) · 13.8 KB
/
Comparables.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package templates
import templates.Family.*
object ComparableOps : TemplateGroupBase() {
private val numericPrimitives = PrimitiveType.numericPrimitives.sortedBy { it.capacity }.toSet()
private val intPrimitives = setOf(PrimitiveType.Int, PrimitiveType.Long)
private val shortIntPrimitives = setOf(PrimitiveType.Byte, PrimitiveType.Short)
val f_coerceAtLeast = fn("coerceAtLeast(minimumValue: SELF)") {
include(Generic)
include(Primitives, numericPrimitives)
} builder {
sourceFile(SourceFile.Ranges)
returns("SELF")
typeParam("T : Comparable<T>")
doc {
"""
Ensures that this value is not less than the specified [minimumValue].
@return this value if it's greater than or equal to the [minimumValue] or the [minimumValue] otherwise.
"""
}
sample("samples.comparisons.ComparableOps.coerceAtLeast${if (f == Generic) "Comparable" else ""}")
body {
"""
return if (this < minimumValue) minimumValue else this
"""
}
}
val f_coerceAtMost = fn("coerceAtMost(maximumValue: SELF)") {
include(Generic)
include(Primitives, numericPrimitives)
} builder {
sourceFile(SourceFile.Ranges)
returns("SELF")
typeParam("T : Comparable<T>")
doc {
"""
Ensures that this value is not greater than the specified [maximumValue].
@return this value if it's less than or equal to the [maximumValue] or the [maximumValue] otherwise.
"""
}
sample("samples.comparisons.ComparableOps.coerceAtMost${if (f == Generic) "Comparable" else ""}")
body {
"""
return if (this > maximumValue) maximumValue else this
"""
}
}
val f_coerceIn_range_primitive = fn("coerceIn(range: ClosedRange<T>)") {
include(Generic)
include(Primitives, intPrimitives)
} builder {
sourceFile(SourceFile.Ranges)
returns("SELF")
typeParam("T : Comparable<T>")
doc {
"""
Ensures that this value lies in the specified [range].
@return this value if it's in the [range], or `range.start` if this value is less than `range.start`, or `range.endInclusive` if this value is greater than `range.endInclusive`.
"""
}
sample("samples.comparisons.ComparableOps.coerceIn${if (f == Generic) "Comparable" else ""}")
body(Generic) {
"""
if (range is ClosedFloatingPointRange) {
return this.coerceIn<T>(range)
}
if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: ${'$'}range.")
return when {
this < range.start -> range.start
this > range.endInclusive -> range.endInclusive
else -> this
}
"""
}
body(Primitives) {
"""
if (range is ClosedFloatingPointRange) {
return this.coerceIn<T>(range)
}
if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: ${'$'}range.")
return when {
this < range.start -> range.start
this > range.endInclusive -> range.endInclusive
else -> this
}
"""
}
}
val f_coerceIn_fpRange = fn("coerceIn(range: ClosedFloatingPointRange<T>)") {
include(Generic)
} builder {
sourceFile(SourceFile.Ranges)
since("1.1")
returns("SELF")
typeParam("T : Comparable<T>")
doc {
"""
Ensures that this value lies in the specified [range].
@return this value if it's in the [range], or `range.start` if this value is less than `range.start`, or `range.endInclusive` if this value is greater than `range.endInclusive`.
"""
}
sample("samples.comparisons.ComparableOps.coerceInFloatingPointRange")
body(Generic) {
"""
if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: ${'$'}range.")
return when {
// this < start equiv to this <= start && !(this >= start)
range.lessThanOrEquals(this, range.start) && !range.lessThanOrEquals(range.start, this) -> range.start
// this > end equiv to this >= end && !(this <= end)
range.lessThanOrEquals(range.endInclusive, this) && !range.lessThanOrEquals(this, range.endInclusive) -> range.endInclusive
else -> this
}
"""
}
}
val f_minOf_2 = fn("minOf(a: T, b: T)") {
include(Generic)
include(Primitives, numericPrimitives)
} builder {
sourceFile(SourceFile.Comparisons)
since("1.1")
typeParam("T : Comparable<T>")
returns("T")
receiver("")
doc {
"""
Returns the smaller of two values.
If values are equal, returns the first one.
"""
}
// TODO: Add a note about NaN propagation for floats.
specialFor(Primitives) {
inlineOnly()
doc {
"""Returns the smaller of two values."""
}
var convertBack = "to$primitive()"
on(Platform.JS) {
convertBack = "unsafeCast<$primitive>()"
}
body {
"return Math.min(a, b)"
}
if (primitive in shortIntPrimitives) {
body { "return Math.min(a.toInt(), b.toInt()).$convertBack" }
}
on(Platform.JS) {
if (primitive == PrimitiveType.Long) {
inline(suppressWarning = true)
body { "return if (a <= b) a else b" }
}
}
}
on(Platform.JS) { /* just to make expect, KT-22520 */ }
body(Generic) {
"return if (a <= b) a else b"
}
}
val f_minOf = fn("minOf(a: T, b: T, c: T)") {
include(Generic)
include(Primitives, numericPrimitives)
} builder {
sourceFile(SourceFile.Comparisons)
since("1.1")
typeParam("T : Comparable<T>")
returns("T")
receiver("")
specialFor(Primitives) { inlineOnly() }
// TODO: Add a note about NaN propagation for floats.
doc {
"""
Returns the smaller of three values.
"""
}
body {
"return minOf(a, minOf(b, c))"
}
on(Platform.JS) { /* just to make expect, KT-22520 */ }
specialFor(Primitives) {
if (primitive in shortIntPrimitives) {
body { "return Math.min(a.toInt(), Math.min(b.toInt(), c.toInt())).to$primitive()" }
on(Platform.JS) {
body { "return Math.min(a.toInt(), b.toInt(), c.toInt()).unsafeCast<$primitive>()" }
}
}
else if (primitive != PrimitiveType.Long) {
on(Platform.JS) {
body { "return Math.min(a, b, c)" }
}
}
}
}
val f_minOf_2_comparator = fn("minOf(a: T, b: T, comparator: Comparator<in T>)") {
include(Generic)
} builder {
sourceFile(SourceFile.Comparisons)
since("1.1")
returns("T")
receiver("")
doc {
"""
Returns the smaller of two values according to the order specified by the given [comparator].
If values are equal, returns the first one.
"""
}
body {
"return if (comparator.compare(a, b) <= 0) a else b"
}
}
val f_minOf_3_comparator = fn("minOf(a: T, b: T, c: T, comparator: Comparator<in T>)") {
include(Generic)
} builder {
sourceFile(SourceFile.Comparisons)
since("1.1")
returns("T")
receiver("")
doc {
"""
Returns the smaller of three values according to the order specified by the given [comparator].
"""
}
body {
"return minOf(a, minOf(b, c, comparator), comparator)"
}
}
val f_maxOf_2 = fn("maxOf(a: T, b: T)") {
include(Generic)
include(Primitives, numericPrimitives)
} builder {
sourceFile(SourceFile.Comparisons)
since("1.1")
typeParam("T : Comparable<T>")
returns("T")
receiver("")
doc {
"""
Returns the greater of two values.
If values are equal, returns the first one.
"""
}
// TODO: Add a note about NaN propagation for floats.
specialFor(Primitives) {
inlineOnly()
doc {
"""Returns the greater of two values."""
}
var convertBack = "to$primitive()"
on(Platform.JS) {
convertBack = "unsafeCast<$primitive>()"
}
body {
"return Math.max(a, b)"
}
if (primitive in shortIntPrimitives) {
body { "return Math.max(a.toInt(), b.toInt()).$convertBack" }
}
on(Platform.JS) {
if (primitive == PrimitiveType.Long) {
inline(suppressWarning = true)
body { "return if (a >= b) a else b" }
}
}
}
on(Platform.JS) { /* just to make expect, KT-22520 */ }
body(Generic) {
"return if (a >= b) a else b"
}
}
val f_maxOf_3 = fn("maxOf(a: T, b: T, c: T)") {
include(Generic)
include(Primitives, numericPrimitives)
} builder {
sourceFile(SourceFile.Comparisons)
since("1.1")
typeParam("T : Comparable<T>")
returns("T")
receiver("")
specialFor(Primitives) { inlineOnly() }
// TODO: Add a note about NaN propagation for floats.
doc {
"""
Returns the greater of three values.
"""
}
body {
"return maxOf(a, maxOf(b, c))"
}
on(Platform.JS) { /* just to make expect, KT-22520 */ }
specialFor(Primitives) {
if (primitive in shortIntPrimitives) {
body { "return Math.max(a.toInt(), Math.max(b.toInt(), c.toInt())).to$primitive()" }
on(Platform.JS) {
body { "return Math.max(a.toInt(), b.toInt(), c.toInt()).unsafeCast<$primitive>()" }
}
}
else if (primitive != PrimitiveType.Long) {
on(Platform.JS) {
body { "return Math.max(a, b, c)" }
}
}
}
}
val f_maxOf_2_comparator = fn("maxOf(a: T, b: T, comparator: Comparator<in T>)") {
include(Generic)
} builder {
sourceFile(SourceFile.Comparisons)
since("1.1")
returns("T")
receiver("")
doc {
"""
Returns the greater of two values according to the order specified by the given [comparator].
If values are equal, returns the first one.
"""
}
body {
"return if (comparator.compare(a, b) >= 0) a else b"
}
}
val f_maxOf_3_comparator = fn("maxOf(a: T, b: T, c: T, comparator: Comparator<in T>)") {
include(Generic)
} builder {
sourceFile(SourceFile.Comparisons)
since("1.1")
returns("T")
receiver("")
doc {
"""
Returns the greater of three values according to the order specified by the given [comparator].
"""
}
body {
"return maxOf(a, maxOf(b, c, comparator), comparator)"
}
}
val f_coerceIn_min_max = fn("coerceIn(minimumValue: SELF, maximumValue: SELF)") {
include(Generic)
include(Primitives, numericPrimitives)
} builder {
sourceFile(SourceFile.Ranges)
specialFor(Generic) { signature("coerceIn(minimumValue: SELF?, maximumValue: SELF?)", notForSorting = true) }
typeParam("T : Comparable<T>")
returns("SELF")
doc {
"""
Ensures that this value lies in the specified range [minimumValue]..[maximumValue].
@return this value if it's in the range, or [minimumValue] if this value is less than [minimumValue], or [maximumValue] if this value is greater than [maximumValue].
"""
}
sample("samples.comparisons.ComparableOps.coerceIn${if (f == Generic) "Comparable" else ""}")
body(Primitives) {
"""
if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum ${'$'}maximumValue is less than minimum ${'$'}minimumValue.")
if (this < minimumValue) return minimumValue
if (this > maximumValue) return maximumValue
return this
"""
}
body(Generic) {
"""
if (minimumValue !== null && maximumValue !== null) {
if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum ${'$'}maximumValue is less than minimum ${'$'}minimumValue.")
if (this < minimumValue) return minimumValue
if (this > maximumValue) return maximumValue
}
else {
if (minimumValue !== null && this < minimumValue) return minimumValue
if (maximumValue !== null && this > maximumValue) return maximumValue
}
return this
"""
}
}
}