1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 1999,2000 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xerces" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation and was
  52. * originally based on software copyright (c) 1999, International
  53. * Business Machines, Inc., http://www.apache.org. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package org.apache.xml.dtm.ref;
  58. // %REVIEW% Should this be based on SuballocatedIntVector instead?
  59. // (Unclear. Pools will rarely be huge. But if they ever are...)
  60. import org.apache.xml.utils.IntVector;
  61. import java.util.Vector;
  62. /** <p>DTMStringPool is an "interning" mechanism for strings. It will
  63. * create a stable 1:1 mapping between a set of string values and a set of
  64. * integer index values, so the integers can be used to reliably and
  65. * uniquely identify (and when necessary retrieve) the strings.</p>
  66. *
  67. * <p>Design Priorities:
  68. * <ul>
  69. * <li>String-to-index lookup speed is critical.</li>
  70. * <li>Index-to-String lookup speed is slightly less so.</li>
  71. * <li>Threadsafety is not guaranteed at this level.
  72. * Enforce that in the application if needed.</li>
  73. * <li>Storage efficiency is an issue but not a huge one.
  74. * It is expected that string pools won't exceed about 2000 entries.</li>
  75. * </ul>
  76. * </p>
  77. *
  78. * <p>Implementation detail: A standard Hashtable is relatively
  79. * inefficient when looking up primitive int values, especially when
  80. * we're already maintaining an int-to-string vector. So I'm
  81. * maintaining a simple hash chain within this class.</p>
  82. *
  83. * <p>NOTE: There is nothing in the code that has a real dependency upon
  84. * String. It would work with any object type that implements reliable
  85. * .hashCode() and .equals() operations. The API enforces Strings because
  86. * it's safer that way, but this could trivially be turned into a general
  87. * ObjectPool if one was needed.</p>
  88. *
  89. * <p>Status: Passed basic test in main().</p>
  90. * */
  91. public class DTMStringPool
  92. {
  93. Vector m_intToString;
  94. static final int HASHPRIME=101;
  95. int[] m_hashStart=new int[HASHPRIME];
  96. IntVector m_hashChain;
  97. public static final int NULL=-1;
  98. public DTMStringPool()
  99. {
  100. m_intToString=new Vector();
  101. m_hashChain=new IntVector(512);
  102. removeAllElements();
  103. // -sb Add this to force empty strings to be index 0.
  104. stringToIndex("");
  105. }
  106. public void removeAllElements()
  107. {
  108. m_intToString.removeAllElements();
  109. for(int i=0;i<HASHPRIME;++i)
  110. m_hashStart[i]=NULL;
  111. m_hashChain.removeAllElements();
  112. }
  113. /** @return string whose value is uniquely identified by this integer index.
  114. * @throws java.lang.ArrayIndexOutOfBoundsException
  115. * if index doesn't map to a string.
  116. * */
  117. public String indexToString(int i)
  118. throws java.lang.ArrayIndexOutOfBoundsException
  119. {
  120. if(i==NULL) return null;
  121. return (String) m_intToString.elementAt(i);
  122. }
  123. /** @return integer index uniquely identifying the value of this string. */
  124. public int stringToIndex(String s)
  125. {
  126. if(s==null) return NULL;
  127. int hashslot=s.hashCode()%HASHPRIME;
  128. if(hashslot<0) hashslot=-hashslot;
  129. // Is it one we already know?
  130. int hashlast=m_hashStart[hashslot];
  131. int hashcandidate=hashlast;
  132. while(hashcandidate!=NULL)
  133. {
  134. if(m_intToString.elementAt(hashcandidate).equals(s))
  135. return hashcandidate;
  136. hashlast=hashcandidate;
  137. hashcandidate=m_hashChain.elementAt(hashcandidate);
  138. }
  139. // New value. Add to tables.
  140. int newIndex=m_intToString.size();
  141. m_intToString.addElement(s);
  142. m_hashChain.addElement(NULL); // Initialize to no-following-same-hash
  143. if(hashlast==NULL) // First for this hash
  144. m_hashStart[hashslot]=newIndex;
  145. else // Link from previous with same hash
  146. m_hashChain.setElementAt(newIndex,hashlast);
  147. return newIndex;
  148. }
  149. /** Command-line unit test driver. This test relies on the fact that
  150. * this version of the pool assigns indices consecutively, starting
  151. * from zero, as new unique strings are encountered.
  152. */
  153. public static void main(String[] args)
  154. {
  155. String[] word={
  156. "Zero","One","Two","Three","Four","Five",
  157. "Six","Seven","Eight","Nine","Ten",
  158. "Eleven","Twelve","Thirteen","Fourteen","Fifteen",
  159. "Sixteen","Seventeen","Eighteen","Nineteen","Twenty",
  160. "Twenty-One","Twenty-Two","Twenty-Three","Twenty-Four",
  161. "Twenty-Five","Twenty-Six","Twenty-Seven","Twenty-Eight",
  162. "Twenty-Nine","Thirty","Thirty-One","Thirty-Two",
  163. "Thirty-Three","Thirty-Four","Thirty-Five","Thirty-Six",
  164. "Thirty-Seven","Thirty-Eight","Thirty-Nine"};
  165. DTMStringPool pool=new DTMStringPool();
  166. System.out.println("If no complaints are printed below, we passed initial test.");
  167. for(int pass=0;pass<=1;++pass)
  168. {
  169. int i;
  170. for(i=0;i<word.length;++i)
  171. {
  172. int j=pool.stringToIndex(word[i]);
  173. if(j!=i)
  174. System.out.println("\tMismatch populating pool: assigned "+
  175. j+" for create "+i);
  176. }
  177. for(i=0;i<word.length;++i)
  178. {
  179. int j=pool.stringToIndex(word[i]);
  180. if(j!=i)
  181. System.out.println("\tMismatch in stringToIndex: returned "+
  182. j+" for lookup "+i);
  183. }
  184. for(i=0;i<word.length;++i)
  185. {
  186. String w=pool.indexToString(i);
  187. if(!word[i].equals(w))
  188. System.out.println("\tMismatch in indexToString: returned"+
  189. w+" for lookup "+i);
  190. }
  191. pool.removeAllElements();
  192. System.out.println("\nPass "+pass+" complete\n");
  193. } // end pass loop
  194. }
  195. }