1 16 19 20 package org.apache.xml.dtm.ref; 21 22 import java.util.Vector ; 23 24 import org.apache.xml.utils.IntVector; 25 26 55 public class DTMStringPool 56 { 57 Vector m_intToString; 58 static final int HASHPRIME=101; 59 int[] m_hashStart=new int[HASHPRIME]; 60 IntVector m_hashChain; 61 public static final int NULL=-1; 62 63 68 public DTMStringPool(int chainSize) 69 { 70 m_intToString=new Vector (); 71 m_hashChain=new IntVector(chainSize); 72 removeAllElements(); 73 74 stringToIndex(""); 76 } 77 78 public DTMStringPool() 79 { 80 this(512); 81 } 82 83 public void removeAllElements() 84 { 85 m_intToString.removeAllElements(); 86 for(int i=0;i<HASHPRIME;++i) 87 m_hashStart[i]=NULL; 88 m_hashChain.removeAllElements(); 89 } 90 91 95 public String indexToString(int i) 96 throws java.lang.ArrayIndexOutOfBoundsException 97 { 98 if(i==NULL) return null; 99 return (String ) m_intToString.elementAt(i); 100 } 101 102 103 public int stringToIndex(String s) 104 { 105 if(s==null) return NULL; 106 107 int hashslot=s.hashCode()%HASHPRIME; 108 if(hashslot<0) hashslot=-hashslot; 109 110 int hashlast=m_hashStart[hashslot]; 112 int hashcandidate=hashlast; 113 while(hashcandidate!=NULL) 114 { 115 if(m_intToString.elementAt(hashcandidate).equals(s)) 116 return hashcandidate; 117 118 hashlast=hashcandidate; 119 hashcandidate=m_hashChain.elementAt(hashcandidate); 120 } 121 122 int newIndex=m_intToString.size(); 124 m_intToString.addElement(s); 125 126 m_hashChain.addElement(NULL); if(hashlast==NULL) m_hashStart[hashslot]=newIndex; 129 else m_hashChain.setElementAt(newIndex,hashlast); 131 132 return newIndex; 133 } 134 135 139 public static void main(String [] args) 140 { 141 String [] word={ 142 "Zero","One","Two","Three","Four","Five", 143 "Six","Seven","Eight","Nine","Ten", 144 "Eleven","Twelve","Thirteen","Fourteen","Fifteen", 145 "Sixteen","Seventeen","Eighteen","Nineteen","Twenty", 146 "Twenty-One","Twenty-Two","Twenty-Three","Twenty-Four", 147 "Twenty-Five","Twenty-Six","Twenty-Seven","Twenty-Eight", 148 "Twenty-Nine","Thirty","Thirty-One","Thirty-Two", 149 "Thirty-Three","Thirty-Four","Thirty-Five","Thirty-Six", 150 "Thirty-Seven","Thirty-Eight","Thirty-Nine"}; 151 152 DTMStringPool pool=new DTMStringPool(); 153 154 System.out.println("If no complaints are printed below, we passed initial test."); 155 156 for(int pass=0;pass<=1;++pass) 157 { 158 int i; 159 160 for(i=0;i<word.length;++i) 161 { 162 int j=pool.stringToIndex(word[i]); 163 if(j!=i) 164 System.out.println("\tMismatch populating pool: assigned "+ 165 j+" for create "+i); 166 } 167 168 for(i=0;i<word.length;++i) 169 { 170 int j=pool.stringToIndex(word[i]); 171 if(j!=i) 172 System.out.println("\tMismatch in stringToIndex: returned "+ 173 j+" for lookup "+i); 174 } 175 176 for(i=0;i<word.length;++i) 177 { 178 String w=pool.indexToString(i); 179 if(!word[i].equals(w)) 180 System.out.println("\tMismatch in indexToString: returned"+ 181 w+" for lookup "+i); 182 } 183 184 pool.removeAllElements(); 185 186 System.out.println("\nPass "+pass+" complete\n"); 187 } } 189 } 190 | Popular Tags |