scoop:scoop.core:0.1.0
src/ranges.scoop
Selected production source · src/ranges.scoop
1
/**2
* An integer progression with a fixed direction, positive step, and inclusive or exclusive3
* endpoint. Iteration checks the endpoint before arithmetic can wrap.4
*/5
public class IntRange internal constructor(6
private val first: Int,7
private val endpoint: Int,8
private val stride: Int,9
private val descending: Boolean,10
private val inclusive: Boolean11
) : Iterable<Int>, Contains<Int> {12
/**13
* Creates a fresh cursor over this progression.14
*/15
public override operator fun iterator(): Iterator<Int> =16
IntRangeIterator(first, endpoint, stride, descending, inclusive, false)18
/**19
* Returns a progression with the requested positive step magnitude, preserving direction and20
* endpoint policy. Zero or negative signed steps are invalid.21
*/22
public infix fun step(value: Int): IntRange {23
if (value <= 0) {24
throw IllegalArgumentException()25
}26
return IntRange(first, endpoint, value, descending, inclusive)27
}29
/**30
* Tests membership using both endpoint bounds and alignment with the progression step.31
*/32
public override operator fun contains(value: Int): Boolean {33
if (descending) {34
if (value > first) {35
return false36
}37
if (inclusive) {38
if (value < endpoint) {39
return false40
}41
} else {42
if (value <= endpoint) {43
return false44
}45
}46
val distance = first.toUInt() - value.toUInt()47
return distance % stride.toUInt() == 0u48
}50
if (value < first) {51
return false52
}53
if (inclusive) {54
if (value > endpoint) {55
return false56
}57
} else {58
if (value >= endpoint) {59
return false60
}61
}62
val distance = value.toUInt() - first.toUInt()63
return distance % stride.toUInt() == 0u64
}65
}67
internal class IntRangeIterator internal constructor(68
private var current: Int,69
private val endpoint: Int,70
private val stride: Int,71
private val descending: Boolean,72
private val inclusive: Boolean,73
private var exhausted: Boolean74
) : Iterator<Int> {75
public override fun next(): Option<Int> {76
if (exhausted) {77
return None78
}80
if (descending) {81
if (inclusive) {82
if (current < endpoint) {83
exhausted = true84
return None85
}86
} else {87
if (current <= endpoint) {88
exhausted = true89
return None90
}91
}92
} else {93
if (inclusive) {94
if (current > endpoint) {95
exhausted = true96
return None97
}98
} else {99
if (current >= endpoint) {100
exhausted = true101
return None102
}103
}104
}106
val value = current107
val distance = if (descending) {108
current.toUInt() - endpoint.toUInt()109
} else {110
endpoint.toUInt() - current.toUInt()111
}112
val unsignedStride = stride.toUInt()113
if (inclusive) {114
if (unsignedStride <= distance) {115
if (descending) {116
current = current - stride117
} else {118
current = current + stride119
}120
} else {121
exhausted = true122
}123
} else {124
if (unsignedStride < distance) {125
if (descending) {126
current = current - stride127
} else {128
current = current + stride129
}130
} else {131
exhausted = true132
}133
}134
return Some(value)135
}136
}138
/**139
* An integer progression with a fixed direction, positive step, and inclusive or exclusive140
* endpoint. Iteration checks the endpoint before arithmetic can wrap.141
*/142
public class LongRange internal constructor(143
private val first: Long,144
private val endpoint: Long,145
private val stride: Long,146
private val descending: Boolean,147
private val inclusive: Boolean148
) : Iterable<Long>, Contains<Long> {149
/**150
* Creates a fresh cursor over this progression.151
*/152
public override operator fun iterator(): Iterator<Long> =153
LongRangeIterator(first, endpoint, stride, descending, inclusive, false)155
/**156
* Returns a progression with the requested positive step magnitude, preserving direction and157
* endpoint policy. Zero or negative signed steps are invalid.158
*/159
public infix fun step(value: Long): LongRange {160
if (value <= 0L) {161
throw IllegalArgumentException()162
}163
return LongRange(first, endpoint, value, descending, inclusive)164
}166
/**167
* Tests membership using both endpoint bounds and alignment with the progression step.168
*/169
public override operator fun contains(value: Long): Boolean {170
if (descending) {171
if (value > first) {172
return false173
}174
if (inclusive) {175
if (value < endpoint) {176
return false177
}178
} else {179
if (value <= endpoint) {180
return false181
}182
}183
val distance = first.toULong() - value.toULong()184
return distance % stride.toULong() == 0uL185
}187
if (value < first) {188
return false189
}190
if (inclusive) {191
if (value > endpoint) {192
return false193
}194
} else {195
if (value >= endpoint) {196
return false197
}198
}199
val distance = value.toULong() - first.toULong()200
return distance % stride.toULong() == 0uL201
}202
}204
internal class LongRangeIterator internal constructor(205
private var current: Long,206
private val endpoint: Long,207
private val stride: Long,208
private val descending: Boolean,209
private val inclusive: Boolean,210
private var exhausted: Boolean211
) : Iterator<Long> {212
public override fun next(): Option<Long> {213
if (exhausted) {214
return None215
}217
if (descending) {218
if (inclusive) {219
if (current < endpoint) {220
exhausted = true221
return None222
}223
} else {224
if (current <= endpoint) {225
exhausted = true226
return None227
}228
}229
} else {230
if (inclusive) {231
if (current > endpoint) {232
exhausted = true233
return None234
}235
} else {236
if (current >= endpoint) {237
exhausted = true238
return None239
}240
}241
}243
val value = current244
val distance = if (descending) {245
current.toULong() - endpoint.toULong()246
} else {247
endpoint.toULong() - current.toULong()248
}249
val unsignedStride = stride.toULong()250
if (inclusive) {251
if (unsignedStride <= distance) {252
if (descending) {253
current = current - stride254
} else {255
current = current + stride256
}257
} else {258
exhausted = true259
}260
} else {261
if (unsignedStride < distance) {262
if (descending) {263
current = current - stride264
} else {265
current = current + stride266
}267
} else {268
exhausted = true269
}270
}271
return Some(value)272
}273
}275
/**276
* An integer progression with a fixed direction, positive step, and inclusive or exclusive277
* endpoint. Iteration checks the endpoint before arithmetic can wrap.278
*/279
public class UIntRange internal constructor(280
private val first: UInt,281
private val endpoint: UInt,282
private val stride: UInt,283
private val descending: Boolean,284
private val inclusive: Boolean285
) : Iterable<UInt>, Contains<UInt> {286
/**287
* Creates a fresh cursor over this progression.288
*/289
public override operator fun iterator(): Iterator<UInt> =290
UIntRangeIterator(first, endpoint, stride, descending, inclusive, false)292
/**293
* Returns a progression with the requested positive step magnitude, preserving direction and294
* endpoint policy. Zero or negative signed steps are invalid.295
*/296
public infix fun step(value: UInt): UIntRange {297
if (value == 0u) {298
throw IllegalArgumentException()299
}300
return UIntRange(first, endpoint, value, descending, inclusive)301
}303
/**304
* Tests membership using both endpoint bounds and alignment with the progression step.305
*/306
public override operator fun contains(value: UInt): Boolean {307
if (descending) {308
if (value > first) {309
return false310
}311
if (inclusive) {312
if (value < endpoint) {313
return false314
}315
} else {316
if (value <= endpoint) {317
return false318
}319
}320
val distance = first - value321
return distance % stride == 0u322
}324
if (value < first) {325
return false326
}327
if (inclusive) {328
if (value > endpoint) {329
return false330
}331
} else {332
if (value >= endpoint) {333
return false334
}335
}336
val distance = value - first337
return distance % stride == 0u338
}339
}341
internal class UIntRangeIterator internal constructor(342
private var current: UInt,343
private val endpoint: UInt,344
private val stride: UInt,345
private val descending: Boolean,346
private val inclusive: Boolean,347
private var exhausted: Boolean348
) : Iterator<UInt> {349
public override fun next(): Option<UInt> {350
if (exhausted) {351
return None352
}354
if (descending) {355
if (inclusive) {356
if (current < endpoint) {357
exhausted = true358
return None359
}360
} else {361
if (current <= endpoint) {362
exhausted = true363
return None364
}365
}366
} else {367
if (inclusive) {368
if (current > endpoint) {369
exhausted = true370
return None371
}372
} else {373
if (current >= endpoint) {374
exhausted = true375
return None376
}377
}378
}380
val value = current381
val distance = if (descending) {382
current - endpoint383
} else {384
endpoint - current385
}386
if (inclusive) {387
if (stride <= distance) {388
if (descending) {389
current = current - stride390
} else {391
current = current + stride392
}393
} else {394
exhausted = true395
}396
} else {397
if (stride < distance) {398
if (descending) {399
current = current - stride400
} else {401
current = current + stride402
}403
} else {404
exhausted = true405
}406
}407
return Some(value)408
}409
}411
/**412
* An integer progression with a fixed direction, positive step, and inclusive or exclusive413
* endpoint. Iteration checks the endpoint before arithmetic can wrap.414
*/415
public class ULongRange internal constructor(416
private val first: ULong,417
private val endpoint: ULong,418
private val stride: ULong,419
private val descending: Boolean,420
private val inclusive: Boolean421
) : Iterable<ULong>, Contains<ULong> {422
/**423
* Creates a fresh cursor over this progression.424
*/425
public override operator fun iterator(): Iterator<ULong> =426
ULongRangeIterator(first, endpoint, stride, descending, inclusive, false)428
/**429
* Returns a progression with the requested positive step magnitude, preserving direction and430
* endpoint policy. Zero or negative signed steps are invalid.431
*/432
public infix fun step(value: ULong): ULongRange {433
if (value == 0uL) {434
throw IllegalArgumentException()435
}436
return ULongRange(first, endpoint, value, descending, inclusive)437
}439
/**440
* Tests membership using both endpoint bounds and alignment with the progression step.441
*/442
public override operator fun contains(value: ULong): Boolean {443
if (descending) {444
if (value > first) {445
return false446
}447
if (inclusive) {448
if (value < endpoint) {449
return false450
}451
} else {452
if (value <= endpoint) {453
return false454
}455
}456
val distance = first - value457
return distance % stride == 0uL458
}460
if (value < first) {461
return false462
}463
if (inclusive) {464
if (value > endpoint) {465
return false466
}467
} else {468
if (value >= endpoint) {469
return false470
}471
}472
val distance = value - first473
return distance % stride == 0uL474
}475
}477
internal class ULongRangeIterator internal constructor(478
private var current: ULong,479
private val endpoint: ULong,480
private val stride: ULong,481
private val descending: Boolean,482
private val inclusive: Boolean,483
private var exhausted: Boolean484
) : Iterator<ULong> {485
public override fun next(): Option<ULong> {486
if (exhausted) {487
return None488
}490
if (descending) {491
if (inclusive) {492
if (current < endpoint) {493
exhausted = true494
return None495
}496
} else {497
if (current <= endpoint) {498
exhausted = true499
return None500
}501
}502
} else {503
if (inclusive) {504
if (current > endpoint) {505
exhausted = true506
return None507
}508
} else {509
if (current >= endpoint) {510
exhausted = true511
return None512
}513
}514
}516
val value = current517
val distance = if (descending) {518
current - endpoint519
} else {520
endpoint - current521
}522
if (inclusive) {523
if (stride <= distance) {524
if (descending) {525
current = current - stride526
} else {527
current = current + stride528
}529
} else {530
exhausted = true531
}532
} else {533
if (stride < distance) {534
if (descending) {535
current = current - stride536
} else {537
current = current + stride538
}539
} else {540
exhausted = true541
}542
}543
return Some(value)544
}545
}