-
Notifications
You must be signed in to change notification settings - Fork 79
/
BitIterator.java
126 lines (97 loc) · 1.95 KB
/
BitIterator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package mod.chiselsandbits.chiseledblock.data;
import net.minecraft.util.math.BlockPos;
public class BitIterator
{
private static final int zInc = 1 << 8;
private static final int yInc = 1 << 4;
private final int xMin;
private final int yMin;
private final int yMinOffset;
private final int xMax;
private final int yMax;
private final int zMax;
private int zOffset = 0;
private int yOffset = 0;
private int combined = 0;
private int bit;
// read-only outputs.
public int x = -1;
public int y;
public int z;
public BitIterator()
{
yMinOffset = 0;
xMin = 0;
yMin = 0;
xMax = VoxelBlob.dim;
yMax = yInc * VoxelBlob.dim;
zMax = zInc * VoxelBlob.dim;
}
public BitIterator(
final BlockPos a,
final BlockPos b )
{
xMin = x = clampToRange( Math.min( a.getX(), b.getX() ) );
yMin = y = clampToRange( Math.min( a.getY(), b.getY() ) );
z = clampToRange( Math.min( a.getZ(), b.getZ() ) );
x -= 1;
yOffset = yMinOffset = yInc * y;
zOffset = zInc * z;
xMax = clampToRange( Math.max( a.getX(), b.getX() ) ) + 1;
yMax = yInc * ( clampToRange( Math.max( a.getY(), b.getY() ) ) + 1 );
zMax = zInc * ( clampToRange( Math.max( a.getZ(), b.getZ() ) ) + 1 );
combined = zOffset | yOffset;
}
public int clampToRange(
final int a )
{
return Math.max( 0, Math.min( 15, a ) );
}
protected void yPlus()
{
x = xMin;
++y;
yOffset += yInc;
}
protected void zPlus()
{
y = yMin;
yOffset = yMinOffset;
++z;
zOffset += zInc;
}
public boolean hasNext()
{
++x;
if ( x >= xMax )
{
yPlus();
if ( yOffset >= yMax )
{
zPlus();
if ( zOffset >= zMax )
{
done();
return false;
}
}
combined = zOffset | yOffset;
}
bit = combined | x;
return true;
}
protected void done()
{
}
public int getNext(
final VoxelBlob blob )
{
return blob.getBit( bit );
}
public void setNext(
final VoxelBlob blob,
final int value )
{
blob.putBit( bit, value );
}
}