mirror of
https://github.com/haraldk/TwelveMonkeys.git
synced 2025-08-06 04:55:30 -04:00
Code clean-up.
This commit is contained in:
parent
ee81e8ca31
commit
9ca76ec128
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2014, Harald Kuhr
|
* Copyright (c) 2015, Harald Kuhr
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
@ -58,11 +58,9 @@ final class LZWEncoder implements Encoder {
|
|||||||
|
|
||||||
private static final int TABLE_SIZE = 1 << MAX_BITS;
|
private static final int TABLE_SIZE = 1 << MAX_BITS;
|
||||||
|
|
||||||
private int remaining;
|
|
||||||
|
|
||||||
private final LZWString[] table = new LZWString[TABLE_SIZE];
|
private final LZWString[] table = new LZWString[TABLE_SIZE];
|
||||||
// private final Map<LZWString, Integer> reverseTable = new HashMap<>(TABLE_SIZE - 256); // This is foobar
|
|
||||||
private final Map<LZWString, Integer> reverseTable = new TreeMap<>(); // This is foobar
|
private final Map<LZWString, Integer> reverseTable = new TreeMap<>(); // This is foobar
|
||||||
|
// private final Map<LZWString, Integer> reverseTable = new HashMap<>(TABLE_SIZE); // This is foobar
|
||||||
private int tableLength;
|
private int tableLength;
|
||||||
LZWString omega = LZWString.EMPTY;
|
LZWString omega = LZWString.EMPTY;
|
||||||
|
|
||||||
@ -71,8 +69,12 @@ final class LZWEncoder implements Encoder {
|
|||||||
private int maxCode;
|
private int maxCode;
|
||||||
int bitMask;
|
int bitMask;
|
||||||
|
|
||||||
int bits;
|
// Buffer for partial codes
|
||||||
int bitPos;
|
private int bits = 0;
|
||||||
|
private int bitPos = 0;
|
||||||
|
|
||||||
|
// Keep track of how many bytes we will write, to make sure we write EOI at correct position
|
||||||
|
private long remaining;
|
||||||
|
|
||||||
protected LZWEncoder(final int length) {
|
protected LZWEncoder(final int length) {
|
||||||
this.remaining = length;
|
this.remaining = length;
|
||||||
@ -85,23 +87,14 @@ final class LZWEncoder implements Encoder {
|
|||||||
init();
|
init();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int bitmaskFor(final int bits) {
|
|
||||||
return (1 << bits) - 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void init() {
|
private void init() {
|
||||||
tableLength = 258;
|
tableLength = 258;
|
||||||
bitsPerCode = MIN_BITS;
|
bitsPerCode = MIN_BITS;
|
||||||
bitMask = bitmaskFor(bitsPerCode);
|
bitMask = bitmaskFor(bitsPerCode);
|
||||||
maxCode = maxCode();
|
maxCode = maxCode();
|
||||||
// omega = LZWString.EMPTY;
|
|
||||||
reverseTable.clear();
|
reverseTable.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected int maxCode() {
|
|
||||||
return bitMask;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void encode(final OutputStream stream, final ByteBuffer buffer) throws IOException {
|
public void encode(final OutputStream stream, final ByteBuffer buffer) throws IOException {
|
||||||
// InitializeStringTable();
|
// InitializeStringTable();
|
||||||
// WriteCode(ClearCode);
|
// WriteCode(ClearCode);
|
||||||
@ -173,19 +166,9 @@ final class LZWEncoder implements Encoder {
|
|||||||
|
|
||||||
Integer index = reverseTable.get(string);
|
Integer index = reverseTable.get(string);
|
||||||
return index != null ? index : -1;
|
return index != null ? index : -1;
|
||||||
|
|
||||||
// TODO: Needs optimization :-)
|
|
||||||
// for (int i = 258; i < tableLength; i++) {
|
|
||||||
// if (table[i].equals(string)) {
|
|
||||||
// return i;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private int addStringToTable(final LZWString string) {
|
private int addStringToTable(final LZWString string) {
|
||||||
// System.err.println("LZWEncoder.addStringToTable: " + string);
|
|
||||||
final int index = tableLength++;
|
final int index = tableLength++;
|
||||||
table[index] = string;
|
table[index] = string;
|
||||||
reverseTable.put(string, index);
|
reverseTable.put(string, index);
|
||||||
@ -201,10 +184,6 @@ final class LZWEncoder implements Encoder {
|
|||||||
maxCode = maxCode();
|
maxCode = maxCode();
|
||||||
}
|
}
|
||||||
|
|
||||||
// if (string.length > maxString) {
|
|
||||||
// maxString = string.length;
|
|
||||||
// }
|
|
||||||
|
|
||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -222,4 +201,12 @@ final class LZWEncoder implements Encoder {
|
|||||||
|
|
||||||
bits &= bitmaskFor(bitPos);
|
bits &= bitmaskFor(bitPos);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static int bitmaskFor(final int bits) {
|
||||||
|
return (1 << bits) - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected int maxCode() {
|
||||||
|
return bitMask;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user