1. /*
  2. * @(#)CompactCharArray.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 CompactByteArray
  41. * @see CompactIntArray
  42. * @see CompactShortArray
  43. * @see CompactStringArray
  44. * @version 1.15 01/19/00
  45. * @author Helena Shih
  46. */
  47. final class CompactCharArray implements Cloneable{
  48. /**
  49. * The total number of Unicode characters.
  50. */
  51. public static final int UNICODECOUNT =65536;
  52. /**
  53. * Default constructor for CompactCharArray, the default value of the
  54. * compact array is '\u0000'.
  55. */
  56. public CompactCharArray()
  57. {
  58. this('\u0000');
  59. }
  60. /**
  61. * Contructor for CompactCharArray.
  62. * @param defaultValue the default value of the compact array.
  63. */
  64. public CompactCharArray(char defaultValue)
  65. {
  66. int i;
  67. values = new char[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 CompactCharArray.
  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 CompactCharArray(short indexArray[], char newValues[])
  86. {
  87. int i;
  88. if (indexArray.length != INDEXCOUNT)
  89. throw new IllegalArgumentException("Index out of bounds.");
  90. for (i = 0; i < INDEXCOUNT; ++i) {
  91. short index = indexArray[i];
  92. if ((index < 0) || (index >= newValues.length+BLOCKCOUNT))
  93. throw new IllegalArgumentException("Index out of bounds.");
  94. }
  95. indices = indexArray;
  96. values = newValues;
  97. isCompact = true;
  98. }
  99. /**
  100. * Get the mapped value of a Unicode character.
  101. * @param index the character to get the mapped value with
  102. * @return the mapped value of the given character
  103. */
  104. public char elementAt(char index) // parameterized on short
  105. {
  106. return (values[(indices[index >> BLOCKSHIFT] & 0xFFFF)
  107. + (index & BLOCKMASK)]);
  108. }
  109. /**
  110. * Set a new value for a Unicode character.
  111. * Set automatically expands the array if it is compacted.
  112. * @param index the character to set the mapped value with
  113. * @param value the new mapped value
  114. */
  115. public void setElementAt(char index, char value)
  116. {
  117. if (isCompact)
  118. expand();
  119. values[(int)index] = value;
  120. touchBlock(index >> BLOCKSHIFT, value);
  121. }
  122. /**
  123. * Set new values for a range of Unicode character.
  124. * @param start the starting offset of the range
  125. * @param end the endding offset of the range
  126. * @param value the new mapped value
  127. */
  128. public void setElementAt(char start, char end, char value)
  129. {
  130. int i;
  131. if (isCompact) {
  132. expand();
  133. }
  134. for (i = start; i <= end; ++i) {
  135. values[i] = value;
  136. touchBlock(i >> BLOCKSHIFT, value);
  137. }
  138. }
  139. /**
  140. *Compact the array.
  141. */
  142. public void compact()
  143. {
  144. if (!isCompact) {
  145. int limitCompacted = 0;
  146. int iBlockStart = 0;
  147. short iUntouched = -1;
  148. for (int i = 0; i < indices.length; ++i, iBlockStart += BLOCKCOUNT) {
  149. indices[i] = -1;
  150. boolean touched = blockTouched(i);
  151. if (!touched && iUntouched != -1) {
  152. // If no values in this block were set, we can just set its
  153. // index to be the same as some other block with no values
  154. // set, assuming we've seen one yet.
  155. indices[i] = iUntouched;
  156. } else {
  157. int jBlockStart = 0;
  158. int j = 0;
  159. for (j = 0; j < limitCompacted;
  160. ++j, jBlockStart += BLOCKCOUNT) {
  161. if (hashes[i] == hashes[j] &&
  162. arrayRegionMatches(values, iBlockStart,
  163. values, jBlockStart, BLOCKCOUNT)) {
  164. indices[i] = (short)jBlockStart;
  165. }
  166. }
  167. if (indices[i] == -1) {
  168. // we didn't match, so copy & update
  169. System.arraycopy(values, iBlockStart,
  170. values, jBlockStart, BLOCKCOUNT);
  171. indices[i] = (short)jBlockStart;
  172. hashes[j] = hashes[i];
  173. ++limitCompacted;
  174. if (!touched) {
  175. // If this is the first untouched block we've seen,
  176. // remember its index.
  177. iUntouched = (short)jBlockStart;
  178. }
  179. }
  180. }
  181. }
  182. // we are done compacting, so now make the array shorter
  183. int newSize = limitCompacted*BLOCKCOUNT;
  184. char[] result = new char[newSize];
  185. System.arraycopy(values, 0, result, 0, newSize);
  186. values = result;
  187. isCompact = true;
  188. hashes = null;
  189. }
  190. }
  191. /**
  192. * Convenience utility to compare two arrays of doubles.
  193. * @param len the length to compare.
  194. * The start indices and start+len must be valid.
  195. */
  196. final static boolean arrayRegionMatches(char[] source, int sourceStart,
  197. char[] target, int targetStart,
  198. int len)
  199. {
  200. int sourceEnd = sourceStart + len;
  201. int delta = targetStart - sourceStart;
  202. for (int i = sourceStart; i < sourceEnd; i++) {
  203. if (source[i] != target[i + delta])
  204. return false;
  205. }
  206. return true;
  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 char getStringArray()[]
  233. {
  234. return values;
  235. }
  236. /**
  237. * Overrides Cloneable
  238. */
  239. public Object clone()
  240. {
  241. try {
  242. CompactCharArray other = (CompactCharArray) super.clone();
  243. other.values = (char[])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. CompactCharArray other = (CompactCharArray) 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 guantlet.
  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. * Expanding takes the array back to a 65536 element array.
  287. */
  288. private void expand()
  289. {
  290. int i;
  291. if (isCompact) {
  292. char[] tempArray;
  293. tempArray = new char[UNICODECOUNT];
  294. hashes = new int[INDEXCOUNT];
  295. for (i = 0; i < UNICODECOUNT; ++i) {
  296. char 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 = null;
  304. values = tempArray;
  305. isCompact = false;
  306. }
  307. }
  308. private char getArrayValue(int n)
  309. {
  310. return values[n];
  311. }
  312. private short getIndexArrayValue(int n)
  313. {
  314. return indices[n];
  315. }
  316. private static final int BLOCKSHIFT =7;
  317. private static final int BLOCKCOUNT =(1<<BLOCKSHIFT);
  318. private static final int INDEXSHIFT =(16-BLOCKSHIFT);
  319. private static final int INDEXCOUNT =(1<<INDEXSHIFT);
  320. private static final int BLOCKMASK = BLOCKCOUNT - 1;
  321. private char[] values; // char -> short (char parameterized short)
  322. private short indices[];
  323. private int[] hashes;
  324. private boolean isCompact;
  325. };