View Javadoc

1   package net.obsearch.example.ted;
2   
3   import java.util.HashMap;
4   
5   import net.obsearch.index.utils.IntegerHolder;
6   
7   
8   
9   public class ShashaAndZhangReferenceImpl extends AbstractTED{
10  	
11  	
12  	
13  	
14      private HashMap<String, IntegerHolder> cache;
15  	
16  	public ShashaAndZhangReferenceImpl(){
17  		init();
18  	}
19  	
20  	protected void init(){
21  		cache = new HashMap<String, IntegerHolder>();
22  		// we don't want to be filling in the default case lots of times...
23  		put(SliceFactory.createEmptySliceForest(), SliceFactory.createEmptySliceForest(), 0);
24  	}
25  	
26  	protected void init(int n, int m){		
27  		cache = new HashMap<String, IntegerHolder>(n * m);
28  		// we don't want to be filling in the default case lots of times...
29  		put(SliceFactory.createEmptySliceForest(), SliceFactory.createEmptySliceForest(), 0);
30  	}
31  	
32  	/** 
33  	 * puts the given value into the cache
34  	 * @param a
35  	 * @param b
36  	 * @param value
37  	 */
38  	protected final  void put(final SliceForest a, final SliceForest b, int value){		
39  		put(makeKey(a,b), value);
40  	}
41  	protected final void put(String k, int value){
42  		cache.put(k, new IntegerHolder(value));
43  	}
44  	
45  	/**
46  	 * returns an integer with the given value of the cache, otherwise returns -1
47  	 * @param k
48  	 * @return
49  	 */
50  	protected final int  get(String k){
51  		IntegerHolder r =  cache.get(k);
52  		if(r == null){
53  			return -1;
54  		}else{
55  			return r.getValue();
56  		}
57  	}
58  	
59  	protected final String makeKey(final SliceForest a, final SliceForest b){
60  		StringBuilder str = new StringBuilder(a.hashString());
61  		str.append(",");
62  		str.append(b.hashString());
63  		return str.toString();
64  	}
65  	
66  	public int ted(final SliceForest a, final SliceForest b){
67  		init(a.getSize(), b.getSize());
68  		return tedAux(a,b);
69  	}
70  	
71  	public int tedAux(final SliceForest a, final SliceForest b){
72  		int res;
73  		String key = makeKey(a, b);
74  		int v = get(key); // get catched value. I was catched. no need to do anything.
75  		if(v != -1){
76  			res = v;
77  		}else if(a.isNull() && b.isNull()){
78  			res = 0;
79  		}else if(!a.isNull() && b.isNull()){
80  			res = tedAux(a.deleteRightTreeNode(), b ) + DeleteCost;
81  		}else if(a.isNull() && ! b.isNull()){
82  			res = tedAux(a, b.deleteRightTreeNode()) + DeleteCost;
83  		}else{
84  			int v1 = tedAux(a.deleteRightTreeNode(), b) + DeleteCost;
85  			int v2 = tedAux(a, b.deleteRightTreeNode()) + DeleteCost ;
86  			int v3 = tedAux(a.deleteRootOnRightTreeAndGetRightTree(), b.deleteRootOnRightTreeAndGetRightTree()) + 
87  			tedAux(a.deleteRightTree() , b.deleteRightTree()) + renameCost(a.getRightTree(), b.getRightTree());	
88  			res = min(v1,v2,v3);
89  		}
90  		// I am not catched, store the catched value.
91  		if(v == -1){
92  			put(key, res);
93  		}
94  		return res;
95  	}
96  	
97  	public int tedSliceAST(SliceAST a, SliceAST b) throws Exception{
98  		throw new Exception("cannot call this method in this class");
99  	}
100 }