Blame view

tools/mate-tools/src/is2/data/DPSTree.java 2.24 KB
Jan Lupa authored
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
 * 
 */
package is2.data;

import is2.util.DB;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Stack;

/**
 * @author Dr. Bernd Bohnet, 17.01.2011
 * 
 * Dynamic phrase structure tree.
 */
public class DPSTree {


	private int size=0;

	public int[] heads;
	public int[] labels;

	public DPSTree() {
		this(30);
	}

	public DPSTree(int initialCapacity) {
		heads = new int[initialCapacity];
		labels = new int[initialCapacity];
	}


	/**
     * Increases the capacity of this <tt>Graph</tt> instance, if
     * necessary, to ensure  that it can hold at least the number of nodes
     * specified by the minimum capacity argument. 
     *
     * @param   minCapacity   the desired minimum capacity.
     */
    private void ensureCapacity(int minCapacity) {


    	if (minCapacity > heads.length) {

    		int newCapacity =minCapacity + 1;

    		if (newCapacity < minCapacity) newCapacity = minCapacity;
    		int oldIndex[] = heads;
    		heads = new int[newCapacity];
    		System.arraycopy(oldIndex, 0, heads, 0, oldIndex.length);

    		oldIndex = labels;
    		labels = new int[newCapacity];
    		System.arraycopy(oldIndex, 0, labels, 0, oldIndex.length);

    	}
    }


    final public int size() {
	  return size;
    }


    final public boolean isEmpty() {
  	  return size == 0;
      }

  	final public void clear() {
      	size = 0;
     }

  	final public void createTerminals(int terminals) {
  		ensureCapacity(terminals+1); 
  		size= terminals+1;
  	}

     final public int create(int phrase) {

      	ensureCapacity(size+1); 
      	labels[size] =phrase;
      	size++;
      	return size-1;
      }

 	public int create(int phrase, int nodeId) {

 		if (nodeId<0) return this.create(phrase);
// 		DB.println("create phrase "+nodeId+"  label "+phrase);
      	ensureCapacity(nodeId+1); 
		labels[nodeId] =phrase;	
		if (size<nodeId) size=nodeId+1;
		return nodeId;
	}

	public void createEdge(int i, int j) {
		heads[i] =j;	
//		DB.println("create edge "+i+"\t "+j);
	}	

	public DPSTree clone() {
		DPSTree ps = new DPSTree(this.size+1);

		for(int k=0;k<size;k++) { 
			ps.heads[k] = heads[k]; 
			ps.labels[k] = labels[k]; 
		}
		ps.size=size;
		return ps;

	}

}