Scoop API

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 exclusive
3 * endpoint. Iteration checks the endpoint before arithmetic can wrap.
4 */
5public 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: Boolean
11) : 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 and
20 * 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 false
36 }
37 if (inclusive) {
38 if (value < endpoint) {
39 return false
40 }
41 } else {
42 if (value <= endpoint) {
43 return false
44 }
45 }
46 val distance = first.toUInt() - value.toUInt()
47 return distance % stride.toUInt() == 0u
48 }
50 if (value < first) {
51 return false
52 }
53 if (inclusive) {
54 if (value > endpoint) {
55 return false
56 }
57 } else {
58 if (value >= endpoint) {
59 return false
60 }
61 }
62 val distance = value.toUInt() - first.toUInt()
63 return distance % stride.toUInt() == 0u
64 }
67internal 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: Boolean
74) : Iterator<Int> {
75 public override fun next(): Option<Int> {
76 if (exhausted) {
77 return None
78 }
80 if (descending) {
81 if (inclusive) {
82 if (current < endpoint) {
83 exhausted = true
84 return None
85 }
86 } else {
87 if (current <= endpoint) {
88 exhausted = true
89 return None
90 }
91 }
92 } else {
93 if (inclusive) {
94 if (current > endpoint) {
95 exhausted = true
96 return None
97 }
98 } else {
99 if (current >= endpoint) {
100 exhausted = true
101 return None
102 }
103 }
104 }
106 val value = current
107 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 - stride
117 } else {
118 current = current + stride
119 }
120 } else {
121 exhausted = true
122 }
123 } else {
124 if (unsignedStride < distance) {
125 if (descending) {
126 current = current - stride
127 } else {
128 current = current + stride
129 }
130 } else {
131 exhausted = true
132 }
133 }
134 return Some(value)
135 }
138/**
139 * An integer progression with a fixed direction, positive step, and inclusive or exclusive
140 * endpoint. Iteration checks the endpoint before arithmetic can wrap.
141 */
142public 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: Boolean
148) : 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 and
157 * 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 false
173 }
174 if (inclusive) {
175 if (value < endpoint) {
176 return false
177 }
178 } else {
179 if (value <= endpoint) {
180 return false
181 }
182 }
183 val distance = first.toULong() - value.toULong()
184 return distance % stride.toULong() == 0uL
185 }
187 if (value < first) {
188 return false
189 }
190 if (inclusive) {
191 if (value > endpoint) {
192 return false
193 }
194 } else {
195 if (value >= endpoint) {
196 return false
197 }
198 }
199 val distance = value.toULong() - first.toULong()
200 return distance % stride.toULong() == 0uL
201 }
204internal 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: Boolean
211) : Iterator<Long> {
212 public override fun next(): Option<Long> {
213 if (exhausted) {
214 return None
215 }
217 if (descending) {
218 if (inclusive) {
219 if (current < endpoint) {
220 exhausted = true
221 return None
222 }
223 } else {
224 if (current <= endpoint) {
225 exhausted = true
226 return None
227 }
228 }
229 } else {
230 if (inclusive) {
231 if (current > endpoint) {
232 exhausted = true
233 return None
234 }
235 } else {
236 if (current >= endpoint) {
237 exhausted = true
238 return None
239 }
240 }
241 }
243 val value = current
244 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 - stride
254 } else {
255 current = current + stride
256 }
257 } else {
258 exhausted = true
259 }
260 } else {
261 if (unsignedStride < distance) {
262 if (descending) {
263 current = current - stride
264 } else {
265 current = current + stride
266 }
267 } else {
268 exhausted = true
269 }
270 }
271 return Some(value)
272 }
275/**
276 * An integer progression with a fixed direction, positive step, and inclusive or exclusive
277 * endpoint. Iteration checks the endpoint before arithmetic can wrap.
278 */
279public 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: Boolean
285) : 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 and
294 * 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 false
310 }
311 if (inclusive) {
312 if (value < endpoint) {
313 return false
314 }
315 } else {
316 if (value <= endpoint) {
317 return false
318 }
319 }
320 val distance = first - value
321 return distance % stride == 0u
322 }
324 if (value < first) {
325 return false
326 }
327 if (inclusive) {
328 if (value > endpoint) {
329 return false
330 }
331 } else {
332 if (value >= endpoint) {
333 return false
334 }
335 }
336 val distance = value - first
337 return distance % stride == 0u
338 }
341internal 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: Boolean
348) : Iterator<UInt> {
349 public override fun next(): Option<UInt> {
350 if (exhausted) {
351 return None
352 }
354 if (descending) {
355 if (inclusive) {
356 if (current < endpoint) {
357 exhausted = true
358 return None
359 }
360 } else {
361 if (current <= endpoint) {
362 exhausted = true
363 return None
364 }
365 }
366 } else {
367 if (inclusive) {
368 if (current > endpoint) {
369 exhausted = true
370 return None
371 }
372 } else {
373 if (current >= endpoint) {
374 exhausted = true
375 return None
376 }
377 }
378 }
380 val value = current
381 val distance = if (descending) {
382 current - endpoint
383 } else {
384 endpoint - current
385 }
386 if (inclusive) {
387 if (stride <= distance) {
388 if (descending) {
389 current = current - stride
390 } else {
391 current = current + stride
392 }
393 } else {
394 exhausted = true
395 }
396 } else {
397 if (stride < distance) {
398 if (descending) {
399 current = current - stride
400 } else {
401 current = current + stride
402 }
403 } else {
404 exhausted = true
405 }
406 }
407 return Some(value)
408 }
411/**
412 * An integer progression with a fixed direction, positive step, and inclusive or exclusive
413 * endpoint. Iteration checks the endpoint before arithmetic can wrap.
414 */
415public 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: Boolean
421) : 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 and
430 * 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 false
446 }
447 if (inclusive) {
448 if (value < endpoint) {
449 return false
450 }
451 } else {
452 if (value <= endpoint) {
453 return false
454 }
455 }
456 val distance = first - value
457 return distance % stride == 0uL
458 }
460 if (value < first) {
461 return false
462 }
463 if (inclusive) {
464 if (value > endpoint) {
465 return false
466 }
467 } else {
468 if (value >= endpoint) {
469 return false
470 }
471 }
472 val distance = value - first
473 return distance % stride == 0uL
474 }
477internal 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: Boolean
484) : Iterator<ULong> {
485 public override fun next(): Option<ULong> {
486 if (exhausted) {
487 return None
488 }
490 if (descending) {
491 if (inclusive) {
492 if (current < endpoint) {
493 exhausted = true
494 return None
495 }
496 } else {
497 if (current <= endpoint) {
498 exhausted = true
499 return None
500 }
501 }
502 } else {
503 if (inclusive) {
504 if (current > endpoint) {
505 exhausted = true
506 return None
507 }
508 } else {
509 if (current >= endpoint) {
510 exhausted = true
511 return None
512 }
513 }
514 }
516 val value = current
517 val distance = if (descending) {
518 current - endpoint
519 } else {
520 endpoint - current
521 }
522 if (inclusive) {
523 if (stride <= distance) {
524 if (descending) {
525 current = current - stride
526 } else {
527 current = current + stride
528 }
529 } else {
530 exhausted = true
531 }
532 } else {
533 if (stride < distance) {
534 if (descending) {
535 current = current - stride
536 } else {
537 current = current + stride
538 }
539 } else {
540 exhausted = true
541 }
542 }
543 return Some(value)
544 }