1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package jTrolog.engine;
23
24 import jTrolog.terms.Term;
25
26
27
28
29 class LinkTable {
30
31 private int depth = 8;
32 private int width = Engine.STARTUP_STACK_SIZE;
33
34
35
36
37
38 private Term[][] linkToTerm = new Term[depth][width];
39 private int[][] linkToCtx = new int[depth][width];
40
41 LinkTable() {
42 for (int i = 0; i < depth; i++) {
43 linkToTerm[i] = new Term[width];
44 linkToCtx[i] = new int[width];
45 }
46 }
47
48 final Term getTerm(final int vNr, final int vCtx) {
49 return vNr >= depth || vCtx >= width ? null : linkToTerm[vNr][vCtx];
50 }
51
52 final int getCtx(final int vNr, final int vCtx) {
53 return linkToCtx[vNr][vCtx];
54 }
55
56 final void put(final int vNr, int vCtx, final Term link, final int linkCtx) {
57 if (vNr >= depth)
58 expandLinkTableDepth(vNr * 2);
59
60
61 linkToTerm[vNr][vCtx] = link;
62 linkToCtx[vNr][vCtx] = linkCtx;
63 }
64
65 private void expandLinkTableDepth(final int newSize) {
66 linkToTerm = LinkTable.expandMap(linkToTerm, newSize);
67 linkToCtx = LinkTable.expandMap(linkToCtx, newSize);
68 for (int i = depth; i < newSize; i++) {
69 linkToTerm[i] = new Term[width];
70 linkToCtx[i] = new int[width];
71 }
72 depth = newSize;
73 }
74
75 final void reset(int vNr, int vCtx) {
76 linkToTerm[vNr][vCtx] = null;
77 linkToCtx[vNr][vCtx] = 0;
78 }
79
80 final void doubleWidth(final int newSize) {
81 for (int i = 0; i < depth; i++) {
82 linkToTerm[i] = LinkTable.expandArray(linkToTerm[i], newSize);
83 linkToCtx[i] = LinkTable.expandArray(linkToCtx[i], newSize);
84 }
85 width = newSize;
86 }
87
88 public int getWidth() {
89 return width;
90 }
91
92 static int[][] expandMap(final int[][] array, final int newSize) {
93 int[][] newArray = new int[newSize][];
94 System.arraycopy(array, 0, newArray, 0, array.length);
95 return newArray;
96 }
97
98 static int[] expandArray(int[] array, final int newSize) {
99 int[] newArray = new int[newSize];
100 System.arraycopy(array, 0, newArray, 0, array.length);
101 return newArray;
102 }
103
104 static Term[][] expandMap(final Term[][] array, final int newSize) {
105 Term[][] newArray = new Term[newSize][];
106 System.arraycopy(array, 0, newArray, 0, array.length);
107 return newArray;
108 }
109
110 static Term[] expandArray(Term[] array, final int newSize) {
111 Term[] newArray = new Term[newSize];
112 System.arraycopy(array, 0, newArray, 0, array.length);
113 return newArray;
114 }
115 }