1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * $Id: DTMStringPool.java,v 1.8 2004/02/16 23:06:11 minchau Exp $
  18. */
  19. package com.sun.org.apache.xml.internal.dtm.ref;
  20. import java.util.Vector;
  21. import com.sun.org.apache.xml.internal.utils.IntVector;
  22. /** <p>DTMStringPool is an "interning" mechanism for strings. It will
  23. * create a stable 1:1 mapping between a set of string values and a set of
  24. * integer index values, so the integers can be used to reliably and
  25. * uniquely identify (and when necessary retrieve) the strings.</p>
  26. *
  27. * <p>Design Priorities:
  28. * <ul>
  29. * <li>String-to-index lookup speed is critical.</li>
  30. * <li>Index-to-String lookup speed is slightly less so.</li>
  31. * <li>Threadsafety is not guaranteed at this level.
  32. * Enforce that in the application if needed.</li>
  33. * <li>Storage efficiency is an issue but not a huge one.
  34. * It is expected that string pools won't exceed about 2000 entries.</li>
  35. * </ul>
  36. * </p>
  37. *
  38. * <p>Implementation detail: A standard Hashtable is relatively
  39. * inefficient when looking up primitive int values, especially when
  40. * we're already maintaining an int-to-string vector. So I'm
  41. * maintaining a simple hash chain within this class.</p>
  42. *
  43. * <p>NOTE: There is nothing in the code that has a real dependency upon
  44. * String. It would work with any object type that implements reliable
  45. * .hashCode() and .equals() operations. The API enforces Strings because
  46. * it's safer that way, but this could trivially be turned into a general
  47. * ObjectPool if one was needed.</p>
  48. *
  49. * <p>Status: Passed basic test in _main().</p>
  50. * */
  51. public class DTMStringPool
  52. {
  53. Vector m_intToString;
  54. static final int HASHPRIME=101;
  55. int[] m_hashStart=new int[HASHPRIME];
  56. IntVector m_hashChain;
  57. public static final int NULL=-1;
  58. /**
  59. * Create a DTMStringPool using the given chain size
  60. *
  61. * @param chainSize The size of the hash chain vector
  62. */
  63. public DTMStringPool(int chainSize)
  64. {
  65. m_intToString=new Vector();
  66. m_hashChain=new IntVector(chainSize);
  67. removeAllElements();
  68. // -sb Add this to force empty strings to be index 0.
  69. stringToIndex("");
  70. }
  71. public DTMStringPool()
  72. {
  73. this(512);
  74. }
  75. public void removeAllElements()
  76. {
  77. m_intToString.removeAllElements();
  78. for(int i=0;i<HASHPRIME;++i)
  79. m_hashStart[i]=NULL;
  80. m_hashChain.removeAllElements();
  81. }
  82. /** @return string whose value is uniquely identified by this integer index.
  83. * @throws java.lang.ArrayIndexOutOfBoundsException
  84. * if index doesn't map to a string.
  85. * */
  86. public String indexToString(int i)
  87. throws java.lang.ArrayIndexOutOfBoundsException
  88. {
  89. if(i==NULL) return null;
  90. return (String) m_intToString.elementAt(i);
  91. }
  92. /** @return integer index uniquely identifying the value of this string. */
  93. public int stringToIndex(String s)
  94. {
  95. if(s==null) return NULL;
  96. int hashslot=s.hashCode()%HASHPRIME;
  97. if(hashslot<0) hashslot=-hashslot;
  98. // Is it one we already know?
  99. int hashlast=m_hashStart[hashslot];
  100. int hashcandidate=hashlast;
  101. while(hashcandidate!=NULL)
  102. {
  103. if(m_intToString.elementAt(hashcandidate).equals(s))
  104. return hashcandidate;
  105. hashlast=hashcandidate;
  106. hashcandidate=m_hashChain.elementAt(hashcandidate);
  107. }
  108. // New value. Add to tables.
  109. int newIndex=m_intToString.size();
  110. m_intToString.addElement(s);
  111. m_hashChain.addElement(NULL); // Initialize to no-following-same-hash
  112. if(hashlast==NULL) // First for this hash
  113. m_hashStart[hashslot]=newIndex;
  114. else // Link from previous with same hash
  115. m_hashChain.setElementAt(newIndex,hashlast);
  116. return newIndex;
  117. }
  118. /** Command-line unit test driver. This test relies on the fact that
  119. * this version of the pool assigns indices consecutively, starting
  120. * from zero, as new unique strings are encountered.
  121. */
  122. public static void _main(String[] args)
  123. {
  124. String[] word={
  125. "Zero","One","Two","Three","Four","Five",
  126. "Six","Seven","Eight","Nine","Ten",
  127. "Eleven","Twelve","Thirteen","Fourteen","Fifteen",
  128. "Sixteen","Seventeen","Eighteen","Nineteen","Twenty",
  129. "Twenty-One","Twenty-Two","Twenty-Three","Twenty-Four",
  130. "Twenty-Five","Twenty-Six","Twenty-Seven","Twenty-Eight",
  131. "Twenty-Nine","Thirty","Thirty-One","Thirty-Two",
  132. "Thirty-Three","Thirty-Four","Thirty-Five","Thirty-Six",
  133. "Thirty-Seven","Thirty-Eight","Thirty-Nine"};
  134. DTMStringPool pool=new DTMStringPool();
  135. System.out.println("If no complaints are printed below, we passed initial test.");
  136. for(int pass=0;pass<=1;++pass)
  137. {
  138. int i;
  139. for(i=0;i<word.length;++i)
  140. {
  141. int j=pool.stringToIndex(word[i]);
  142. if(j!=i)
  143. System.out.println("\tMismatch populating pool: assigned "+
  144. j+" for create "+i);
  145. }
  146. for(i=0;i<word.length;++i)
  147. {
  148. int j=pool.stringToIndex(word[i]);
  149. if(j!=i)
  150. System.out.println("\tMismatch in stringToIndex: returned "+
  151. j+" for lookup "+i);
  152. }
  153. for(i=0;i<word.length;++i)
  154. {
  155. String w=pool.indexToString(i);
  156. if(!word[i].equals(w))
  157. System.out.println("\tMismatch in indexToString: returned"+
  158. w+" for lookup "+i);
  159. }
  160. pool.removeAllElements();
  161. System.out.println("\nPass "+pass+" complete\n");
  162. } // end pass loop
  163. }
  164. }