bin-vector sphere
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.BitSet; | |
import java.util.Objects; | |
import java.util.Scanner; | |
import java.util.function.Supplier; | |
import java.util.stream.Stream; | |
import static java.lang.System.out; | |
public class BinVectorSphere { | |
public static void main(String[] args) { | |
final Scanner scanner = new Scanner(System.in); | |
out.println("Enter length"); | |
final int length = scanner.nextInt(); | |
if (length < 0) throw new IllegalArgumentException("Length cannot be negative"); | |
scanner.nextLine(); | |
out.println("Enter center"); | |
final BitSet center = readBitVector(length, scanner); | |
out.println("Enter radius"); | |
final int radius = scanner.nextInt(); | |
if (radius < 0) throw new IllegalArgumentException("Radius cannot be negative"); | |
out.print("S("); | |
printBits(length, center); | |
out.println(", " + radius + ") = {"); | |
sphere(length, center, radius) | |
.takeWhile(Objects::nonNull) | |
.forEach(vector -> { | |
out.print('\t'); | |
printBits(length, vector); | |
out.println(); | |
}); | |
out.println('}'); | |
} | |
private static BitSet readBitVector(final int length, final Scanner scanner) { | |
final String line = scanner.nextLine(); | |
if (line.length() != length) throw new IllegalArgumentException("vector should be of " + length + " bits"); | |
final BitSet bitSet = new BitSet(length); | |
for (int i = 0; i < length; i++) { | |
final char character = line.charAt(i); | |
if (character == '1') bitSet.set(i); | |
else if (character != '0') throw new IllegalArgumentException("A vector should be written using 0s and 1s"); | |
} | |
return bitSet; | |
} | |
private static void printBits(final int length, final BitSet bitSet) { | |
for (int i = 0; i < length; i++) out.print(bitSet.get(i) ? '1' : '0'); | |
} | |
private static Stream<BitSet> sphere(final int length, final BitSet center, final int radius) { | |
return Stream.generate(SphereGenerator.create(length, center, radius)); | |
} | |
private static final class SphereGenerator implements Supplier<BitSet> { | |
private final int length; | |
private final BitSet center; | |
private BitSet radiusVector; | |
public SphereGenerator(final int length, final BitSet center, final BitSet radiusVector) { | |
this.length = length; | |
this.center = center; | |
this.radiusVector = radiusVector; | |
} | |
@Override | |
public BitSet get() { | |
if (radiusVector == null) return null; | |
final BitSet result = (BitSet) center.clone(); | |
result.xor(radiusVector); | |
incrementRadiusVector(); | |
return result; | |
} | |
private void incrementRadiusVector() { | |
for (int index = length - 2; index >= 0; --index) { | |
if (radiusVector.get(index)) { // try move to the right | |
final int nextIndex; | |
if (radiusVector.get(nextIndex = index + 1)) { | |
continue; // cannot move to the right | |
} | |
radiusVector.clear(index); | |
radiusVector.set(nextIndex); | |
return; | |
} | |
} | |
radiusVector = null; | |
} | |
public static Supplier<BitSet> create(final int length, final BitSet center, final int radius) { | |
if (radius > length) throw new IllegalArgumentException("Radius cannot be greater than n"); | |
BitSet radiusVector = new BitSet(); | |
for (int i = 0; i < radius; i++) radiusVector.set(i); | |
return new SphereGenerator(length, center, radiusVector); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Enter length | |
6 | |
Enter center | |
000000 | |
Enter radius | |
3 | |
S(000000, 3) = { | |
111000 | |
110100 | |
110010 | |
110001 | |
101001 | |
100101 | |
100011 | |
010011 | |
001011 | |
000111 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Enter length | |
10 | |
Enter center | |
1100110011 | |
Enter radius | |
5 | |
S(1100110011, 5) = { | |
0011010011 | |
0011100011 | |
0011111011 | |
0011110111 | |
0011110001 | |
0011110010 | |
0010010010 | |
0010100010 | |
0010111010 | |
0010110110 | |
0010110000 | |
0001110000 | |
0000010000 | |
0000100000 | |
0000111000 | |
0000110100 | |
0110110100 | |
0101110100 | |
0100010100 | |
0100100100 | |
0100111100 | |
1000111100 | |
1110111100 | |
1101111100 | |
1100011100 | |
1100101100 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Enter length | |
20 | |
Enter center | |
10011010100101010110 | |
Enter radius | |
7 | |
S(10011010100101010110, 7) = { | |
01100100100101010110 | |
01100111100101010110 | |
01100110000101010110 | |
01100110110101010110 | |
01100110101101010110 | |
01100110100001010110 | |
01100110100111010110 | |
01100110100100010110 | |
01100110100101110110 | |
01100110100101000110 | |
01100110100101011110 | |
01100110100101010010 | |
01100110100101010100 | |
01100110100101010111 | |
01100000100101010111 | |
01100011100101010111 | |
01100010000101010111 | |
01100010110101010111 | |
01100010101101010111 | |
01100010100001010111 | |
01100010100111010111 | |
01100010100100010111 | |
01100010100101110111 | |
01100010100101000111 | |
01100010100101011111 | |
01100010100101010011 | |
01100010100101010101 | |
01101110100101010101 | |
01101000100101010101 | |
01101011100101010101 | |
01101010000101010101 | |
01101010110101010101 | |
01101010101101010101 | |
01101010100001010101 | |
01101010100111010101 | |
01101010100100010101 | |
01101010100101110101 | |
01101010100101000101 | |
01101010100101011101 | |
01101010100101010001 | |
01110010100101010001 | |
01111110100101010001 | |
01111000100101010001 | |
01111011100101010001 | |
01111010000101010001 | |
01111010110101010001 | |
01111010101101010001 | |
01111010100001010001 | |
01111010100111010001 | |
01111010100100010001 | |
01111010100101110001 | |
01111010100101000001 | |
01111010100101011001 | |
01001010100101011001 | |
01010010100101011001 | |
01011110100101011001 | |
01011000100101011001 | |
01011011100101011001 | |
01011010000101011001 | |
01011010110101011001 | |
01011010101101011001 | |
01011010100001011001 | |
01011010100111011001 | |
01011010100100011001 | |
01011010100101111001 | |
01011010100101001001 | |
00111010100101001001 | |
00001010100101001001 | |
00010010100101001001 | |
00011110100101001001 | |
00011000100101001001 | |
00011011100101001001 | |
00011010000101001001 | |
00011010110101001001 | |
00011010101101001001 | |
00011010100001001001 | |
00011010100111001001 | |
00011010100100001001 | |
00011010100101101001 | |
11011010100101101001 | |
10111010100101101001 | |
10001010100101101001 | |
10010010100101101001 | |
10011110100101101001 | |
10011000100101101001 | |
10011011100101101001 | |
10011010000101101001 | |
10011010110101101001 | |
10011010101101101001 | |
10011010100001101001 | |
10011010100111101001 | |
10011010100100101001 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment