| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
package org.tukaani.xz.lz; |
| 7 |
|
| 8 |
import org.tukaani.xz.ArrayCache; |
| 9 |
|
| 10 |
|
| 11 |
final class BT4 extends LZEncoder { |
| 12 |
private final Hash234 hash; |
| 13 |
private final int[] tree; |
| 14 |
private final Matches matches; |
| 15 |
private final int depthLimit; |
| 16 |
|
| 17 |
private final int cyclicSize; |
| 18 |
private int cyclicPos = -1; |
| 19 |
private int lzPos; |
| 20 |
|
| 21 |
static int getMemoryUsage(int dictSize) { |
| 22 |
return Hash234.getMemoryUsage(dictSize) + dictSize / (1024 / 8) + 10; |
| 23 |
} |
| 24 |
|
| 25 |
BT4(int dictSize, int beforeSizeMin, int readAheadMax, |
| 26 |
int niceLen, int matchLenMax, int depthLimit, |
| 27 |
ArrayCache arrayCache) { |
| 28 |
super(dictSize, beforeSizeMin, readAheadMax, niceLen, matchLenMax, |
| 29 |
arrayCache); |
| 30 |
|
| 31 |
cyclicSize = dictSize + 1; |
| 32 |
lzPos = cyclicSize; |
| 33 |
|
| 34 |
hash = new Hash234(dictSize, arrayCache); |
| 35 |
tree = arrayCache.getIntArray(cyclicSize * 2, false); |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
matches = new Matches(niceLen - 1); |
| 41 |
|
| 42 |
this.depthLimit = depthLimit > 0 ? depthLimit : 16 + niceLen / 2; |
| 43 |
} |
| 44 |
|
| 45 |
@Override |
| 46 |
public void putArraysToCache(ArrayCache arrayCache) { |
| 47 |
arrayCache.putArray(tree); |
| 48 |
hash.putArraysToCache(arrayCache); |
| 49 |
super.putArraysToCache(arrayCache); |
| 50 |
} |
| 51 |
|
| 52 |
private int movePos() { |
| 53 |
int avail = movePos(niceLen, 4); |
| 54 |
|
| 55 |
if (avail != 0) { |
| 56 |
if (++lzPos == Integer.MAX_VALUE) { |
| 57 |
int normalizationOffset = Integer.MAX_VALUE - cyclicSize; |
| 58 |
hash.normalize(normalizationOffset); |
| 59 |
normalize(tree, cyclicSize * 2, normalizationOffset); |
| 60 |
lzPos -= normalizationOffset; |
| 61 |
} |
| 62 |
|
| 63 |
if (++cyclicPos == cyclicSize) |
| 64 |
cyclicPos = 0; |
| 65 |
} |
| 66 |
|
| 67 |
return avail; |
| 68 |
} |
| 69 |
|
| 70 |
@Override |
| 71 |
public Matches getMatches() { |
| 72 |
matches.count = 0; |
| 73 |
|
| 74 |
int matchLenLimit = matchLenMax; |
| 75 |
int niceLenLimit = niceLen; |
| 76 |
int avail = movePos(); |
| 77 |
|
| 78 |
if (avail < matchLenLimit) { |
| 79 |
if (avail == 0) |
| 80 |
return matches; |
| 81 |
|
| 82 |
matchLenLimit = avail; |
| 83 |
if (niceLenLimit > avail) |
| 84 |
niceLenLimit = avail; |
| 85 |
} |
| 86 |
|
| 87 |
hash.calcHashes(buf, readPos); |
| 88 |
int delta2 = lzPos - hash.getHash2Pos(); |
| 89 |
int delta3 = lzPos - hash.getHash3Pos(); |
| 90 |
int currentMatch = hash.getHash4Pos(); |
| 91 |
hash.updateTables(lzPos); |
| 92 |
|
| 93 |
int lenBest = 0; |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
if (delta2 < cyclicSize && buf[readPos - delta2] == buf[readPos]) { |
| 100 |
lenBest = 2; |
| 101 |
matches.len[0] = 2; |
| 102 |
matches.dist[0] = delta2 - 1; |
| 103 |
matches.count = 1; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
if (delta2 != delta3 && delta3 < cyclicSize |
| 111 |
&& buf[readPos - delta3] == buf[readPos]) { |
| 112 |
lenBest = 3; |
| 113 |
matches.dist[matches.count++] = delta3 - 1; |
| 114 |
delta2 = delta3; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
if (matches.count > 0) { |
| 119 |
lenBest = MatchLength.getLen(buf, readPos, delta2, |
| 120 |
lenBest, matchLenLimit); |
| 121 |
matches.len[matches.count - 1] = lenBest; |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
if (lenBest >= niceLenLimit) { |
| 126 |
skip(niceLenLimit, currentMatch); |
| 127 |
return matches; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
if (lenBest < 3) |
| 134 |
lenBest = 3; |
| 135 |
|
| 136 |
int depth = depthLimit; |
| 137 |
|
| 138 |
int ptr0 = (cyclicPos << 1) + 1; |
| 139 |
int ptr1 = cyclicPos << 1; |
| 140 |
int len0 = 0; |
| 141 |
int len1 = 0; |
| 142 |
|
| 143 |
while (true) { |
| 144 |
int delta = lzPos - currentMatch; |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
if (depth-- == 0 || delta >= cyclicSize) { |
| 150 |
tree[ptr0] = 0; |
| 151 |
tree[ptr1] = 0; |
| 152 |
return matches; |
| 153 |
} |
| 154 |
|
| 155 |
int pair = (cyclicPos - delta |
| 156 |
+ (delta > cyclicPos ? cyclicSize : 0)) << 1; |
| 157 |
int len = Math.min(len0, len1); |
| 158 |
|
| 159 |
if (buf[readPos + len - delta] == buf[readPos + len]) { |
| 160 |
len = MatchLength.getLen(buf, readPos, delta, |
| 161 |
len + 1, matchLenLimit); |
| 162 |
|
| 163 |
if (len > lenBest) { |
| 164 |
lenBest = len; |
| 165 |
matches.len[matches.count] = len; |
| 166 |
matches.dist[matches.count] = delta - 1; |
| 167 |
++matches.count; |
| 168 |
|
| 169 |
if (len >= niceLenLimit) { |
| 170 |
tree[ptr1] = tree[pair]; |
| 171 |
tree[ptr0] = tree[pair + 1]; |
| 172 |
return matches; |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
if ((buf[readPos + len - delta] & 0xFF) |
| 178 |
< (buf[readPos + len] & 0xFF)) { |
| 179 |
tree[ptr1] = currentMatch; |
| 180 |
ptr1 = pair + 1; |
| 181 |
currentMatch = tree[ptr1]; |
| 182 |
len1 = len; |
| 183 |
} else { |
| 184 |
tree[ptr0] = currentMatch; |
| 185 |
ptr0 = pair; |
| 186 |
currentMatch = tree[ptr0]; |
| 187 |
len0 = len; |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
private void skip(int niceLenLimit, int currentMatch) { |
| 193 |
int depth = depthLimit; |
| 194 |
|
| 195 |
int ptr0 = (cyclicPos << 1) + 1; |
| 196 |
int ptr1 = cyclicPos << 1; |
| 197 |
int len0 = 0; |
| 198 |
int len1 = 0; |
| 199 |
|
| 200 |
while (true) { |
| 201 |
int delta = lzPos - currentMatch; |
| 202 |
|
| 203 |
if (depth-- == 0 || delta >= cyclicSize) { |
| 204 |
tree[ptr0] = 0; |
| 205 |
tree[ptr1] = 0; |
| 206 |
return; |
| 207 |
} |
| 208 |
|
| 209 |
int pair = (cyclicPos - delta |
| 210 |
+ (delta > cyclicPos ? cyclicSize : 0)) << 1; |
| 211 |
int len = Math.min(len0, len1); |
| 212 |
|
| 213 |
if (buf[readPos + len - delta] == buf[readPos + len]) { |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
len = MatchLength.getLen(buf, readPos, delta, |
| 218 |
len + 1, niceLenLimit); |
| 219 |
if (len == niceLenLimit) { |
| 220 |
tree[ptr1] = tree[pair]; |
| 221 |
tree[ptr0] = tree[pair + 1]; |
| 222 |
return; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
if ((buf[readPos + len - delta] & 0xFF) |
| 227 |
< (buf[readPos + len] & 0xFF)) { |
| 228 |
tree[ptr1] = currentMatch; |
| 229 |
ptr1 = pair + 1; |
| 230 |
currentMatch = tree[ptr1]; |
| 231 |
len1 = len; |
| 232 |
} else { |
| 233 |
tree[ptr0] = currentMatch; |
| 234 |
ptr0 = pair; |
| 235 |
currentMatch = tree[ptr0]; |
| 236 |
len0 = len; |
| 237 |
} |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
@Override |
| 242 |
public void skip(int len) { |
| 243 |
while (len-- > 0) { |
| 244 |
int niceLenLimit = niceLen; |
| 245 |
int avail = movePos(); |
| 246 |
|
| 247 |
if (avail < niceLenLimit) { |
| 248 |
if (avail == 0) |
| 249 |
continue; |
| 250 |
|
| 251 |
niceLenLimit = avail; |
| 252 |
} |
| 253 |
|
| 254 |
hash.calcHashes(buf, readPos); |
| 255 |
int currentMatch = hash.getHash4Pos(); |
| 256 |
hash.updateTables(lzPos); |
| 257 |
|
| 258 |
skip(niceLenLimit, currentMatch); |
| 259 |
} |
| 260 |
} |
| 261 |
} |