|
|
@@ -8,8 +8,6 @@ import java.io.IOException;
|
|
|
import java.io.ObjectInputStream;
|
|
|
import java.io.ObjectOutputStream;
|
|
|
|
|
|
-//import java.util.zip.*;
|
|
|
-
|
|
|
/** The tokenlist */ // TODO PB: The name 'Structure' is very generic and should be changed to something more descriptive.
|
|
|
public class Structure implements TokenConstants {
|
|
|
public Token[] tokens = new Token[0];
|
|
|
@@ -17,17 +15,15 @@ public class Structure implements TokenConstants {
|
|
|
int hash_length = -1;
|
|
|
|
|
|
int files; // number of END_FILE tokens
|
|
|
- private int anzahl;
|
|
|
-
|
|
|
- //private final int increment = 300;
|
|
|
+ private int numberOfTokens;
|
|
|
|
|
|
public Structure() {
|
|
|
tokens = new Token[400];
|
|
|
- files = anzahl = 0;
|
|
|
+ files = numberOfTokens = 0;
|
|
|
}
|
|
|
|
|
|
public final int size() {
|
|
|
- return anzahl;
|
|
|
+ return numberOfTokens;
|
|
|
}
|
|
|
|
|
|
public final void ensureCapacity(int minCapacity) {
|
|
|
@@ -39,56 +35,55 @@ public class Structure implements TokenConstants {
|
|
|
newCapacity = minCapacity;
|
|
|
}
|
|
|
tokens = new Token[newCapacity];
|
|
|
- System.arraycopy(oldTokens, 0, tokens, 0, anzahl);
|
|
|
+ System.arraycopy(oldTokens, 0, tokens, 0, numberOfTokens);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public final void addToken(Token token) {
|
|
|
- ensureCapacity(anzahl + 1);
|
|
|
- if (anzahl > 0 && tokens[anzahl - 1].file.equals(token.file))
|
|
|
- token.file = tokens[anzahl - 1].file; // To save memory ...
|
|
|
- if ((anzahl > 0) && (token.getLine() < tokens[anzahl - 1].getLine()) && (token.file.equals(tokens[anzahl - 1].file)))
|
|
|
- token.setLine(tokens[anzahl - 1].getLine());
|
|
|
+ ensureCapacity(numberOfTokens + 1);
|
|
|
+ if (numberOfTokens > 0 && tokens[numberOfTokens - 1].file.equals(token.file))
|
|
|
+ token.file = tokens[numberOfTokens - 1].file; // To save memory ...
|
|
|
+ if ((numberOfTokens > 0) && (token.getLine() < tokens[numberOfTokens - 1].getLine()) && (token.file.equals(tokens[numberOfTokens - 1].file)))
|
|
|
+ token.setLine(tokens[numberOfTokens - 1].getLine());
|
|
|
// just to make sure
|
|
|
|
|
|
- tokens[anzahl++] = token;
|
|
|
+ tokens[numberOfTokens++] = token;
|
|
|
if (token.type == FILE_END)
|
|
|
files++;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public final String toString() {
|
|
|
- StringBuffer buf = new StringBuffer();
|
|
|
+ StringBuffer buffer = new StringBuffer();
|
|
|
|
|
|
try {
|
|
|
- for (int i = 0; i < anzahl; i++) {
|
|
|
- String s = tokens[i].toString();
|
|
|
- buf.append(i);
|
|
|
- buf.append("\t");
|
|
|
- buf.append(s);
|
|
|
- if (i < anzahl - 1) {
|
|
|
- buf.append("\n");
|
|
|
+ for (int i = 0; i < numberOfTokens; i++) {
|
|
|
+ buffer.append(i);
|
|
|
+ buffer.append("\t");
|
|
|
+ buffer.append(tokens[i].toString());
|
|
|
+ if (i < numberOfTokens - 1) {
|
|
|
+ buffer.append("\n");
|
|
|
}
|
|
|
}
|
|
|
} catch (OutOfMemoryError e) {
|
|
|
- return "Tokenlist to large for output: " + (anzahl) + " Tokens";
|
|
|
+ return "Tokenlist to large for output: " + (numberOfTokens) + " Tokens";
|
|
|
}
|
|
|
- return buf.toString();
|
|
|
+ return buffer.toString();
|
|
|
}
|
|
|
|
|
|
public void save(File file) {
|
|
|
|
|
|
try {
|
|
|
- ObjectOutputStream p = new ObjectOutputStream(/* new GZIPOutputStream */(new FileOutputStream(file)));
|
|
|
+ ObjectOutputStream input = new ObjectOutputStream((new FileOutputStream(file)));
|
|
|
|
|
|
- p.writeInt(anzahl);
|
|
|
- p.writeInt(hash_length);
|
|
|
- p.writeInt(files);
|
|
|
+ input.writeInt(numberOfTokens);
|
|
|
+ input.writeInt(hash_length);
|
|
|
+ input.writeInt(files);
|
|
|
|
|
|
- for (int i = 0; i < anzahl; i++)
|
|
|
- p.writeObject(tokens[i]);
|
|
|
- p.flush();
|
|
|
- p.close();
|
|
|
+ for (int i = 0; i < numberOfTokens; i++)
|
|
|
+ input.writeObject(tokens[i]);
|
|
|
+ input.flush();
|
|
|
+ input.close();
|
|
|
} catch (IOException e) {
|
|
|
System.out.println("Error writing file: " + file.toString());
|
|
|
}
|
|
|
@@ -97,22 +92,17 @@ public class Structure implements TokenConstants {
|
|
|
/* returns "true" when successful */
|
|
|
public boolean load(File file) {
|
|
|
try {
|
|
|
- ObjectInputStream p = new ObjectInputStream(/* new GZIPInputStream */(new FileInputStream(file)));
|
|
|
-
|
|
|
- int newAnzahl = p.readInt();
|
|
|
- hash_length = p.readInt();
|
|
|
- files = p.readInt();
|
|
|
- ensureCapacity(newAnzahl);
|
|
|
- anzahl = newAnzahl;
|
|
|
- for (int i = 0; i < anzahl; i++) {
|
|
|
- tokens[i] = (Token) p.readObject();
|
|
|
- // special case for text tokens:
|
|
|
- // if (tokens[i] instanceof jplag.text.TextToken) {
|
|
|
- // jplag.text.TextToken token = (jplag.text.TextToken)tokens[i];
|
|
|
- // jplag.text.TextToken.put(token.getText(), token.type);
|
|
|
- // }
|
|
|
+ ObjectInputStream input = new ObjectInputStream((new FileInputStream(file)));
|
|
|
+
|
|
|
+ int newNumberOfTokens = input.readInt();
|
|
|
+ hash_length = input.readInt();
|
|
|
+ files = input.readInt();
|
|
|
+ ensureCapacity(newNumberOfTokens);
|
|
|
+ numberOfTokens = newNumberOfTokens;
|
|
|
+ for (int i = 0; i < numberOfTokens; i++) {
|
|
|
+ tokens[i] = (Token) input.readObject();
|
|
|
}
|
|
|
- p.close();
|
|
|
+ input.close();
|
|
|
table = null;
|
|
|
} catch (FileNotFoundException e) {
|
|
|
System.out.println("File not found: " + file.toString());
|