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