-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLiteralTable.java
More file actions
70 lines (59 loc) · 2.21 KB
/
Copy pathLiteralTable.java
File metadata and controls
70 lines (59 loc) · 2.21 KB
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
import java.util.ArrayList;
class LiteralTable {
// (id is the serial number)
public ArrayList<LiteralTableEntry> table = new ArrayList<>();
public boolean contains(String literal) {
for (LiteralTableEntry entry : table) {
if (entry.literal.equals(literal)) {
return true;
}
}
return false;
}
public boolean containsInCurrentUndonePool(String literal, PoolTable poolTable) {
int indexLiteralTableSearchStart = poolTable.table.get(poolTable.table.size()).literalId + poolTable.table.get(poolTable.table.size()).poolLength;
int indexLiteralTableSearchEnd = table.size();
for (int i = indexLiteralTableSearchStart; i < indexLiteralTableSearchEnd; i++) {
if (table.get(i-1).literal.equals(literal)) {
return true;
}
}
return false;
}
public int getLiteralId(String literal, int currentPoolNumber, PoolTable poolTable) {
int literalIdFirstInPool = poolTable.table.get(currentPoolNumber).literalId;
int lengthCurrentPool = poolTable.table.get(currentPoolNumber).poolLength;
for (int i = literalIdFirstInPool - 1; i < (literalIdFirstInPool + lengthCurrentPool - 1); i++) {
LiteralTableEntry entry = table.get(i);
if (entry.literal.equals(literal)) {
return entry.id;
}
}
return -1;
}
@Override
public String toString() {
StringBuilder buffer = new StringBuilder();
buffer.append("Literal Table:\n");
buffer.append(String.format("%-4s %-10s %-7s\n", "ID", "Literal", "Address"));
for (LiteralTableEntry entry : table) {
buffer.append(String.format("%-4d %-10s %-7d\n", entry.id, entry.literal, entry.address));
}
return buffer.toString();
}
}
class LiteralTableEntry {
int id;
String literal;
int address;
public LiteralTableEntry(int id, String literal, int address) {
this.id = id;
this.literal = literal;
this.address = address;
}
public LiteralTableEntry(int id, String literal) {
this.id = id;
this.literal = literal;
this.address = -1;
}
}