API Surface (3)
boolean hasNext()
int nextInt()
Iterable<Integer> asIterable(final int numerator, final int denominator)
Fields & Constants (5)
denominator
quotient
mod
returnedParts
remainder
Full Source
Divisor.java52 lines · Minecraft 26.3 · verbatim
| 1 | package com.mojang.math;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import it.unimi.dsi.fastutil.ints.IntIterator;
|
| 5 | import java.util.NoSuchElementException;
|
| 6 |
|
| 7 | public class Divisor implements IntIterator {
|
| 8 | private final int denominator;
|
| 9 | private final int quotient;
|
| 10 | private final int mod;
|
| 11 | private int returnedParts;
|
| 12 | private int remainder;
|
| 13 |
|
| 14 | public Divisor(final int numerator, final int denominator) {
|
| 15 | this.denominator = denominator;
|
| 16 | if (denominator > 0) {
|
| 17 | this.quotient = numerator / denominator;
|
| 18 | this.mod = numerator % denominator;
|
| 19 | } else {
|
| 20 | this.quotient = 0;
|
| 21 | this.mod = 0;
|
| 22 | }
|
| 23 | }
|
| 24 |
|
| 25 | @Override
|
| 26 | public boolean hasNext() {
|
| 27 | return this.returnedParts < this.denominator;
|
| 28 | }
|
| 29 |
|
| 30 | @Override
|
| 31 | public int nextInt() {
|
| 32 | if (!this.hasNext()) {
|
| 33 | throw new NoSuchElementException();
|
| 34 | }
|
| 35 |
|
| 36 | int next = this.quotient;
|
| 37 | this.remainder = this.remainder + this.mod;
|
| 38 | if (this.remainder >= this.denominator) {
|
| 39 | this.remainder = this.remainder - this.denominator;
|
| 40 | next++;
|
| 41 | }
|
| 42 |
|
| 43 | this.returnedParts++;
|
| 44 | return next;
|
| 45 | }
|
| 46 |
|
| 47 | @VisibleForTesting
|
| 48 | public static Iterable<Integer> asIterable(final int numerator, final int denominator) {
|
| 49 | return () -> new Divisor(numerator, denominator);
|
| 50 | }
|
| 51 | }
|
| 52 |
|
Decompiled Minecraft 26.3 source of Divisor (com/mojang/math/Divisor.java) with Mojang official mappings, stored verbatim on the site.