|
| 1 | +import java.io.Serializable; |
| 2 | + |
| 3 | +/** |
| 4 | + * Class that fits 8 bit values (reached or not reached) |
| 5 | + * into each byte in a list of bytes. |
| 6 | + * Minimises memory by using one long array (only 8bytes setup cost) |
| 7 | + * at the cost of increased complexity of implementation |
| 8 | + * @author Ranulf Green |
| 9 | + * |
| 10 | + */ |
| 11 | +public class DistancePruningTable implements Serializable{ |
| 12 | + /* |
| 13 | + * NOTE: |
| 14 | + * never attempt to run both tables at the same time since this will lead to |
| 15 | + * a huge memory requirment >400Mbytes |
| 16 | + */ |
| 17 | + long[] list; |
| 18 | + int width; |
| 19 | + int height; |
| 20 | + int length; |
| 21 | + int depth; |
| 22 | + /** |
| 23 | + * design for phase 2 pruning table with only two dimentions |
| 24 | + * @param width |
| 25 | + * @param height |
| 26 | + */ |
| 27 | + public DistancePruningTable(int width, int height){ |
| 28 | + long l = (width*height)/8; |
| 29 | + long m = (width*height)%8; |
| 30 | + if( m==0){ |
| 31 | + length = (int) l; |
| 32 | + }else{ |
| 33 | + length = (int) (l+1); |
| 34 | + } |
| 35 | + list = new long[length]; |
| 36 | + this.width = width; |
| 37 | + this.height = height; |
| 38 | + |
| 39 | + } |
| 40 | + /** |
| 41 | + * design for phase1 pruning table with three dimensions. |
| 42 | + * uses 240Mbytes of memory so will require extended heap space |
| 43 | + * @param width |
| 44 | + * @param height |
| 45 | + * @param depth |
| 46 | + */ |
| 47 | + public DistancePruningTable(long width, long height, long depth){ |
| 48 | + long l = (width*height*depth)/8; |
| 49 | + long m = (width*height*depth)%8; |
| 50 | + if(m==0){ |
| 51 | + length = (int) l; |
| 52 | + }else{ |
| 53 | + length = (int) (l+1); |
| 54 | + } |
| 55 | + list = new long[length]; |
| 56 | + this.width = (int) width; |
| 57 | + this.height = (int) height; |
| 58 | + this.depth = (int) depth; |
| 59 | + } |
| 60 | + public DistancePruningTable(long[] list, int width, int height, int depth){ |
| 61 | + this.list = list; |
| 62 | + this.width = (int) width; |
| 63 | + this.height = (int) height; |
| 64 | + this.depth = (int) depth; |
| 65 | + |
| 66 | + } |
| 67 | + public DistancePruningTable(long[] list, int width, int height){ |
| 68 | + this.list = list; |
| 69 | + this.width = (int) width; |
| 70 | + this.height = (int) height; |
| 71 | + |
| 72 | + |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | + public static int computeMask(long ww){ |
| 82 | + int bit = (int) (ww%8); |
| 83 | + switch (bit){ |
| 84 | + case 0: |
| 85 | + return 1; |
| 86 | + case 1: |
| 87 | + return 2; |
| 88 | + case 2: |
| 89 | + return 4; |
| 90 | + case 3: |
| 91 | + return 8; |
| 92 | + case 4: |
| 93 | + return 16; |
| 94 | + case 5: |
| 95 | + return 32; |
| 96 | + case 6: |
| 97 | + return 64; |
| 98 | + case 7: |
| 99 | + return 128; |
| 100 | + |
| 101 | + |
| 102 | + } |
| 103 | + |
| 104 | + return 99999; |
| 105 | + |
| 106 | + } |
| 107 | + |
| 108 | + |
| 109 | + private void st(long windowplace, int distance){ |
| 110 | + |
| 111 | + int w = (int) (windowplace/8); |
| 112 | + long window = list[w]; |
| 113 | + // each window (long) is a 64 bit mask and we need to split into 6 bit chunks so we can record a distance in each one rather than a simple match |
| 114 | + |
| 115 | + |
| 116 | + list[w] = (byte) (window | computeMask(windowplace)); |
| 117 | + } |
| 118 | + |
| 119 | + public void setReached(int hPlace, int vPlace, int dPlace, int distance){ // EO CO UD |
| 120 | + long ww = ((long)(width*height)*dPlace + width*vPlace + hPlace); |
| 121 | + st(ww, distance); |
| 122 | + } |
| 123 | + |
| 124 | + public void setReached(int hPlace, int vPlace, int distance){ |
| 125 | + long ww = (width*vPlace + hPlace); |
| 126 | + st(ww, distance); |
| 127 | + } |
| 128 | + |
| 129 | + private boolean gt(long windowplace){ |
| 130 | + int w = (int) (windowplace/8); |
| 131 | + long window = list[w]; |
| 132 | + |
| 133 | + |
| 134 | + int mask = computeMask(windowplace); |
| 135 | + if ((window & mask) == mask){ |
| 136 | + return true; |
| 137 | + }else{ |
| 138 | + return false; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + public boolean getReached(int hPlace, int vPlace, int dPlace){ // EO CO UD |
| 143 | + long ww = ((long)(width*height)*dPlace + width*vPlace + hPlace); |
| 144 | + return gt(ww); |
| 145 | + |
| 146 | + } |
| 147 | + |
| 148 | + public boolean getReached(int hPlace, int vPlace){ |
| 149 | + long ww = (width*vPlace + hPlace); |
| 150 | + return gt(ww); |
| 151 | + |
| 152 | + } |
| 153 | + |
| 154 | + public void printTable(){ |
| 155 | + for(int i=0;i<list.length;i++){ |
| 156 | + if(list[i]<0){ |
| 157 | + System.out.print((256+list[i]) + " "); |
| 158 | + }else{ |
| 159 | + System.out.print((list[i]) + " "); |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + public long get(int index){ |
| 164 | + return list[index]; |
| 165 | + } |
| 166 | + public void set(int index, byte value){ |
| 167 | + list[index] = value; |
| 168 | + } |
| 169 | + public int getSize(){ |
| 170 | + return list.length; |
| 171 | + } |
| 172 | + public long[] getTable(){ |
| 173 | + return list; |
| 174 | + } |
| 175 | + |
| 176 | +} |
0 commit comments