1. /*
  2. * @(#)CompactIntArray.java 1.15 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. /*
  8. * @(#)CompactIntArray.java 1.15 01/11/29
  9. *
  10. * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
  11. * (C) Copyright IBM Corp. 1996 - All Rights Reserved
  12. *
  13. * Portions copyright (c) 1996-1998 Sun Microsystems, Inc. All Rights Reserved.
  14. *
  15. * The original version of this source code and documentation is copyrighted
  16. * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
  17. * materials are provided under terms of a License Agreement between Taligent
  18. * and Sun. This technology is protected by multiple US and International
  19. * patents. This notice and attribution to Taligent may not be removed.
  20. * Taligent is a registered trademark of Taligent, Inc.
  21. *
  22. * Permission to use, copy, modify, and distribute this software
  23. * and its documentation for NON-COMMERCIAL purposes and without
  24. * fee is hereby granted provided that this copyright notice
  25. * appears in all copies. Please refer to the file "copyright.html"
  26. * for further important copyright and licensing information.
  27. *
  28. * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  29. * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  30. * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  31. * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  32. * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  33. * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  34. *
  35. */
  36. package java.text;
  37. /**
  38. * class CompactATypeArray : use only on primitive data types
  39. * Provides a compact way to store information that is indexed by Unicode
  40. * values, such as character properties, types, keyboard values, etc.This
  41. * is very useful when you have a block of Unicode data that contains
  42. * significant values while the rest of the Unicode data is unused in the
  43. * application or when you have a lot of redundance, such as where all 21,000
  44. * Han ideographs have the same value. However, lookup is much faster than a
  45. * hash table.
  46. * A compact array of any primitive data type serves two purposes:
  47. * <UL type = round>
  48. * <LI>Fast access of the indexed values.
  49. * <LI>Smaller memory footprint.
  50. * </UL>
  51. * A compact array is composed of a index array and value array. The index
  52. * array contains the indicies of Unicode characters to the value array.
  53. *
  54. * @see CompactShortArray
  55. * @see CompactByteArray
  56. * @see CompactCharArray
  57. * @see CompactStringArray
  58. * @version 1.15 11/29/01
  59. * @author Helena Shih
  60. */
  61. final class CompactIntArray implements Cloneable {
  62. /**
  63. * The total number of Unicode characters.
  64. */
  65. public static final int UNICODECOUNT =65536;
  66. /**
  67. * Default constructor for CompactIntArray, the default value of the
  68. * compact array is 0.
  69. */
  70. public CompactIntArray()
  71. {
  72. this(0);
  73. }
  74. /**
  75. * Constructor for CompactIntArray.
  76. * @param defaultValue the default value of the compact array.
  77. */
  78. public CompactIntArray(int defaultValue)
  79. {
  80. int i;
  81. values = new int[UNICODECOUNT];
  82. indices = new short[INDEXCOUNT];
  83. hashes = new int[INDEXCOUNT];
  84. for (i = 0; i < UNICODECOUNT; ++i) {
  85. values[i] = defaultValue;
  86. }
  87. for (i = 0; i < INDEXCOUNT; ++i) {
  88. indices[i] = (short)(i<<BLOCKSHIFT);
  89. hashes[i] = 0;
  90. }
  91. isCompact = false;
  92. }
  93. /**
  94. * Constructor for CompactIntArray.
  95. * @param indexArray the indicies of the compact array.
  96. * @param newValues the values of the compact array.
  97. * @exception IllegalArgumentException If the index is out of range.
  98. */
  99. public CompactIntArray(short indexArray[],
  100. int newValues[])
  101. {
  102. int i;
  103. if (indexArray.length != INDEXCOUNT)
  104. throw new IllegalArgumentException("Index out of bounds.");
  105. for (i = 0; i < INDEXCOUNT; ++i) {
  106. short index = indexArray[i];
  107. if ((index < 0) || (index >= newValues.length+BLOCKCOUNT))
  108. throw new IllegalArgumentException("Index out of bounds.");
  109. }
  110. indices = indexArray;
  111. values = newValues;
  112. isCompact = true;
  113. }
  114. /**
  115. * Get the mapped value of a Unicode character.
  116. * @param index the character to get the mapped value with
  117. * @return the mapped value of the given character
  118. */
  119. public int elementAt(char index)
  120. {
  121. return (values[(indices[index >> BLOCKSHIFT] & 0xFFFF)
  122. + (index & BLOCKMASK)]);
  123. }
  124. /**
  125. * Set a new value for a Unicode character.
  126. * Set automatically expands the array if it is compacted.
  127. * @param index the character to set the mapped value with
  128. * @param value the new mapped value
  129. */
  130. public void setElementAt(char index, int value)
  131. {
  132. if (isCompact) {
  133. expand();
  134. }
  135. values[(int)index] = value;
  136. touchBlock(index >> BLOCKSHIFT, value);
  137. }
  138. /**
  139. * Set new values for a range of Unicode character.
  140. * @param start the startting offset of the range
  141. * @param end the ending offset of the range
  142. * @param value the new mapped value
  143. */
  144. public void setElementAt(char start, char end, int value)
  145. {
  146. int i;
  147. if (isCompact) {
  148. expand();
  149. }
  150. for (i = start; i <= end; ++i) {
  151. values[i] = value;
  152. touchBlock(i >> BLOCKSHIFT, value);
  153. }
  154. }
  155. /**
  156. * Compact the array.
  157. */
  158. public void compact()
  159. {
  160. if (!isCompact) {
  161. int limitCompacted = 0;
  162. int iBlockStart = 0;
  163. short iUntouched = -1;
  164. for (int i = 0; i < indices.length; ++i, iBlockStart += BLOCKCOUNT) {
  165. indices[i] = -1;
  166. boolean touched = blockTouched(i);
  167. if (!touched && iUntouched != -1) {
  168. // If no values in this block were set, we can just set its
  169. // index to be the same as some other block with no values
  170. // set, assuming we've seen one yet.
  171. indices[i] = iUntouched;
  172. } else {
  173. int jBlockStart = 0;
  174. int j = 0;
  175. for (j=0; j < limitCompacted;
  176. ++j, jBlockStart += BLOCKCOUNT) {
  177. if (hashes[i] == hashes[j] &&
  178. Utility.arrayRegionMatches(values, iBlockStart,
  179. values, jBlockStart, BLOCKCOUNT)) {
  180. indices[i] = (short)jBlockStart;
  181. break;
  182. }
  183. }
  184. if (indices[i] == -1) {
  185. // we didn't match, so copy & update
  186. System.arraycopy(values, iBlockStart,
  187. values, jBlockStart, BLOCKCOUNT);
  188. indices[i] = (short)jBlockStart;
  189. hashes[j] = hashes[i];
  190. ++limitCompacted;
  191. if (!touched) {
  192. // If this is the first untouched block we've seen,
  193. // remember its index.
  194. iUntouched = (short)jBlockStart;
  195. }
  196. }
  197. }
  198. }
  199. // we are done compacting, so now make the array shorter
  200. int newSize = limitCompacted * BLOCKCOUNT;
  201. int[] result = new int[newSize];
  202. System.arraycopy(values, 0, result, 0, newSize);
  203. values = result;
  204. isCompact = true;
  205. hashes = null;
  206. }
  207. }
  208. /**
  209. * Remember that a specified block was "touched", i.e. had a value set.
  210. * Untouched blocks can be skipped when compacting the array
  211. */
  212. private final void touchBlock(int i, int value) {
  213. hashes[i] = (hashes[i] + (value<<1)) | 1;
  214. }
  215. /**
  216. * Query whether a specified block was "touched", i.e. had a value set.
  217. * Untouched blocks can be skipped when compacting the array
  218. */
  219. private final boolean blockTouched(int i) {
  220. return hashes[i] != 0;
  221. }
  222. /** For internal use only. Do not modify the result, the behavior of
  223. * modified results are undefined.
  224. */
  225. public short getIndexArray()[]
  226. {
  227. return indices;
  228. }
  229. /** For internal use only. Do not modify the result, the behavior of
  230. * modified results are undefined.
  231. */
  232. public int getStringArray()[]
  233. {
  234. return values;
  235. }
  236. /**
  237. * Overrides Cloneable
  238. */
  239. public Object clone()
  240. {
  241. try {
  242. CompactIntArray other = (CompactIntArray) super.clone();
  243. other.values = (int[])values.clone();
  244. other.indices = (short[])indices.clone();
  245. if (hashes != null) other.hashes = (int[])hashes.clone();
  246. return other;
  247. } catch (CloneNotSupportedException e) {
  248. throw new InternalError();
  249. }
  250. }
  251. /**
  252. * Compares the equality of two compact array objects.
  253. * @param obj the compact array object to be compared with this.
  254. * @return true if the current compact array object is the same
  255. * as the compact array object obj; false otherwise.
  256. */
  257. public boolean equals(Object obj) {
  258. if (obj == null) return false;
  259. if (this == obj) // quick check
  260. return true;
  261. if (getClass() != obj.getClass()) // same class?
  262. return false;
  263. CompactIntArray other = (CompactIntArray) obj;
  264. for (int i = 0; i < UNICODECOUNT; i++) {
  265. // could be sped up later
  266. if (elementAt((char)i) != other.elementAt((char)i))
  267. return false;
  268. }
  269. return true; // we made it through the gauntlet.
  270. }
  271. /**
  272. * Generates the hash code for the compact array object
  273. */
  274. public int hashCode() {
  275. int result = 0;
  276. int increment = Math.min(3, values.length16);
  277. for (int i = 0; i < values.length; i+= increment) {
  278. result = result * 37 + values[i];
  279. }
  280. return result;
  281. }
  282. // --------------------------------------------------------------
  283. // private
  284. // --------------------------------------------------------------
  285. /**
  286. * Expanded takes the array back to a 65536 element array
  287. */
  288. private void expand()
  289. {
  290. int i;
  291. if (isCompact) {
  292. int[] tempArray;
  293. hashes = new int[INDEXCOUNT];
  294. tempArray = new int[UNICODECOUNT];
  295. for (i = 0; i < UNICODECOUNT; ++i) {
  296. int value = elementAt((char)i);
  297. tempArray[i] = value;
  298. touchBlock(i >> BLOCKSHIFT, value);
  299. }
  300. for (i = 0; i < INDEXCOUNT; ++i) {
  301. indices[i] = (short)(i<<BLOCKSHIFT);
  302. }
  303. values = tempArray;
  304. isCompact = false;
  305. }
  306. }
  307. private static final int BLOCKSHIFT =7;
  308. private static final int BLOCKCOUNT =(1<<BLOCKSHIFT);
  309. private static final int INDEXSHIFT =(16-BLOCKSHIFT);
  310. private static final int INDEXCOUNT =(1<<INDEXSHIFT);
  311. private static final int BLOCKMASK = BLOCKCOUNT - 1;
  312. private int[] values; // char -> int (char parameterized int)
  313. private short indices[];
  314. private boolean isCompact;
  315. private int[] hashes;
  316. };