diff options
| author | Linnea Gräf <nea@nea.moe> | 2025-06-17 20:59:45 +0200 |
|---|---|---|
| committer | Linnea Gräf <nea@nea.moe> | 2025-06-17 21:19:43 +0200 |
| commit | 4b9e966ca7e8a9291f307850f715820e122d69fd (patch) | |
| tree | 94b08c865c882f93ff9c0c6aa7a507d47bcc67bf /src/main/kotlin/util/collections | |
| parent | 775933d516db10dbcc1cbbe405defa7b6e0c5a6a (diff) | |
| download | Firmament-4b9e966ca7e8a9291f307850f715820e122d69fd.tar.gz Firmament-4b9e966ca7e8a9291f307850f715820e122d69fd.tar.bz2 Firmament-4b9e966ca7e8a9291f307850f715820e122d69fd.zip | |
feat: Add macro wheels
Diffstat (limited to 'src/main/kotlin/util/collections')
| -rw-r--r-- | src/main/kotlin/util/collections/RangeUtil.kt | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/main/kotlin/util/collections/RangeUtil.kt b/src/main/kotlin/util/collections/RangeUtil.kt new file mode 100644 index 0000000..a7029ac --- /dev/null +++ b/src/main/kotlin/util/collections/RangeUtil.kt @@ -0,0 +1,40 @@ +package moe.nea.firmament.util.collections + +import kotlin.math.floor + +val ClosedFloatingPointRange<Float>.centre get() = (endInclusive + start) / 2 + +fun ClosedFloatingPointRange<Float>.nonNegligibleSubSectionsAlignedWith( + interval: Float +): Iterable<Float> { + require(interval.isFinite()) + val range = this + return object : Iterable<Float> { + override fun iterator(): Iterator<Float> { + return object : FloatIterator() { + var polledValue: Float = range.start + var lastValue: Float = polledValue + + override fun nextFloat(): Float { + if (!hasNext()) throw NoSuchElementException() + lastValue = polledValue + polledValue = Float.NaN + return lastValue + } + + override fun hasNext(): Boolean { + if (!polledValue.isNaN()) { + return true + } + if (lastValue == range.endInclusive) + return false + polledValue = (floor(lastValue / interval) + 1) * interval + if (polledValue > range.endInclusive) { + polledValue = range.endInclusive + } + return true + } + } + } + } +} |
