View Javadoc

1   /*
2    * #%L
3    * prolobjectlink-jpi-jtrolog
4    * %%
5    * Copyright (C) 2012 - 2018 WorkLogic Project
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as
9    * published by the Free Software Foundation, either version 2.1 of the
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-2.1.html>.
20   * #L%
21   */
22  package jTrolog.engine;
23  
24  import jTrolog.terms.Term;
25  
26  /**
27   * @author ivar.orstavik@hist.no
28   */
29  class LinkTable {
30  
31  	private int depth = 8;
32  	private int width = Engine.STARTUP_STACK_SIZE;
33  
34  	// Links are stored in a set of two dimensional arrays.
35  	// The terms and their ctx id are stored as individual dimensions.
36  	// The negative duplicates are used for terms with ctx id that are generated
37  	// by library methods.
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  		// mark vNr and vCtx under the given execCtx
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 }