Skip to content

Instantly share code, notes, and snippets.

@Cadiboo
Last active January 9, 2020 15:19
Show Gist options
  • Save Cadiboo/df386a2688edd4ee893ae3f79139d02a to your computer and use it in GitHub Desktop.
Save Cadiboo/df386a2688edd4ee893ae3f79139d02a to your computer and use it in GitHub Desktop.
Test: get
5, 6, 4, 7, 3, 8, 2, 9, 1, 10, 0,
8, 9, 7, 10, 6,
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
3, 4, 2, 5, 1, 6, 0, 7, 8, 9, 10,
7, 8, 6, 9, 5, 10, 4, 11, 3, 12, 2, 13, 1, 14, 0, 15, 16, 17, 18, 19, 20,
15, 16, 14, 17, 13, 18, 12, 19, 11, 20, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,
Test: get2
5, 6, 4, 7, 3, 8, 2, 9, 1, 10, 0,
8, 9, 7, 10, 6,
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
3, 4, 2, 5, 1, 6, 0, 7, 8, 9, 10,
7, 8, 6, 9, 5, 10, 4, 11, 3, 12, 2, 13, 1, 14, 0, 15, 16, 17, 18, 19, 20,
15, 16, 14, 17, 13, 18, 12, 19, 11, 20, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,
Test: getCandidates
6, 7, 8, 9, 10,
9, 10,
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
4, 5, 6, 7, 8, 9, 10,
8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
16, 17, 18, 19, 20,
package net.minecraftforge.fml.client.config.entry;
import java.io.PrintStream;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.IntConsumer;
import java.util.stream.IntStream;
import java.util.stream.StreamSupport;
/**
* @author Cadiboo
*/
public class Test {
int minInclusive;
int maxInclusive;
int start;
public Test(final int minInclusive, final int maxInclusive, final int start) {
this.minInclusive = minInclusive;
this.maxInclusive = maxInclusive;
this.start = start;
}
public static void main(String... args) {
Test[] tests = {
new Test(0, 10, 5),
new Test(6, 10, 8),
new Test(10, 20, 10),
new Test(0, 10, 3),
new Test(0, 20, 7),
new Test(0, 20, 15),
};
final PrintStream stream = System.out;
stream.println("Test: get");
for (final Test test : tests) {
final int maxInc = test.maxInclusive + 1;
for (int i = 0; i < maxInc - test.minInclusive; ++i) {
stream.print(get(test.minInclusive, maxInc, test.start, i) + ", ");
}
stream.println();
}
stream.println();
stream.println("Test: get2");
for (final Test test : tests) {
final int maxInc = test.maxInclusive + 1;
for (int i = 0; i < maxInc - test.minInclusive; ++i) {
stream.print(get2(test.minInclusive, maxInc, test.start, i) + ", ");
}
stream.println();
}
stream.println();
stream.println("Test: getCandidates");
for (final Test test : tests) {
getCandidates(test.start, test.minInclusive, test.maxInclusive).forEach(c -> stream.print(c + ", "));
stream.println();
}
stream.println();
}
/**
* Counts expanding around a number.
* Examples (i counts up from 0 to maxInclusive + 1):
* (0, 20, 7, i) = [7, 8, 6, 9, 5, 10, 4, 11, 3, 12, 2, 13, 1, 14, 0, 15, 16, 17, 18, 19, 20]
* (0, 20, 15, i) = [15, 16, 14, 17, 13, 18, 12, 19, 11, 20, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
*
* @param minInclusive Minimum value, inclusive
* @param maxExclusive Maximum value, exclusive
* @see "https://discordapp.com/channels/176780432371744769/179315645005955072/664840964208197633"
* @author 2xsaiko#6495
*/
public static int get(int minInclusive, int maxExclusive, int start, int index) {
int minIndex = (start - minInclusive) * 2;
int maxIndex = (maxExclusive - start) * 2 - 1;
if (index > minIndex && index >= maxIndex) {
throw new IllegalStateException("" + index);
} else if (index > minIndex) {
return start + (minIndex / 2) + (index - minIndex);
} else if (index >= maxIndex) {
return start - (maxIndex / 2) - (index - maxIndex) - 1;
} else {
return start + (index % 2 == 1 ? (index + 1) / 2 : -index / 2);
}
}
/**
* Counts expanding around a number.
* Examples (i counts up from 0 to maxInclusive + 1):
* (0, 20, 7, i) = [7, 8, 6, 9, 5, 10, 4, 11, 3, 12, 2, 13, 1, 14, 0, 15, 16, 17, 18, 19, 20]
* (0, 20, 15, i) = [15, 16, 14, 17, 13, 18, 12, 19, 11, 20, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
*
* @param minInclusive Minimum value, inclusive
* @param maxExclusive Maximum value, exclusive
* @see "https://discordapp.com/channels/176780432371744769/179315645005955072/664845850954039306"
* @author 2xsaiko#6495
*/
public static int get2(int minInclusive, int maxExclusive, int start, int index) {
int minIndex = (start - minInclusive) * 2;
int maxIndex = (maxExclusive - start) * 2 - 1;
if (index > minIndex) {
int r = start + (minIndex / 2) + (index - minIndex);
if (r >= maxExclusive)
throw new IndexOutOfBoundsException("" + index);
return r;
} else if (index >= maxIndex) {
int r = start - (maxIndex / 2) - (index - maxIndex) - 1;
if (r < minInclusive)
throw new IndexOutOfBoundsException("" + index);
return r;
} else {
return start + (index % 2 == 1 ? (index + 1) / 2 : -index / 2);
}
}
/**
* @see "https://discordapp.com/channels/176780432371744769/179315645005955072/664841505264893973"
* @author gigaherz
*/
public static IntStream getCandidates(int first, int minInclusive, int maxInclusive) {
if (first == maxInclusive)
return IntStream.rangeClosed(first, minInclusive);
if (first == minInclusive)
return IntStream.rangeClosed(minInclusive, maxInclusive);
return interleave(IntStream.rangeClosed(first, minInclusive), IntStream.rangeClosed(first + 1, maxInclusive));
}
public static IntStream interleave(IntStream a, IntStream b) {
Spliterator.OfInt spA = a.spliterator(), spB = b.spliterator();
long estimatedSize = spA.estimateSize() + spB.estimateSize();
if (estimatedSize < 0) estimatedSize = Long.MAX_VALUE;
int characteristics = (spA.characteristics() & spB.characteristics() & (Spliterator.NONNULL | Spliterator.SIZED)) | Spliterator.ORDERED;
return StreamSupport.intStream(new Spliterators.AbstractIntSpliterator(estimatedSize, characteristics) {
Spliterator.OfInt sp1 = spA, sp2 = spB;
@Override
public boolean tryAdvance(IntConsumer action) {
Spliterator.OfInt sp = sp1;
if (sp.tryAdvance(action)) {
sp1 = sp2;
sp2 = sp;
return true;
}
return sp2.tryAdvance(action);
}
}, false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment