1. /*
  2. * @(#)CompactByteArray.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 CompactCharArray
  41. * @see CompactIntArray
  42. * @see CompactShortArray
  43. * @see CompactStringArray
  44. * @version 1.15 01/19/00
  45. * @author Helena Shih
  46. */
  47. final class CompactByteArray implements Cloneable {
  48. /**
  49. * The total number of Unicode characters.
  50. */
  51. public static final int UNICODECOUNT =65536;
  52. /**
  53. * Default constructor for CompactByteArray, the default value of the
  54. * compact array is 0.
  55. */
  56. public CompactByteArray()
  57. {
  58. this((byte)0);
  59. }
  60. /**
  61. * Constructor for CompactByteArray.
  62. * @param defaultValue the default value of the compact array.
  63. */
  64. public CompactByteArray(byte defaultValue)
  65. {
  66. int i;
  67. values = new byte[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 CompactByteArray.
  81. * @param indexArray the indicies of the compact array.
  82. * @param newValues the values of the compact array.
  83. * @exception IllegalArgumentException If index is out of range.
  84. */
  85. public CompactByteArray(short indexArray[],
  86. byte 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 byte 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, byte value)
  117. {
  118. if (isCompact)
  119. expand();
  120. values[(int)index] = value;
  121. touchBlock(index >> BLOCKSHIFT, value);
  122. }
  123. /**
  124. * Set new values for a range of Unicode character.
  125. * @param start the starting offset o of the range
  126. * @param end the ending offset of the range
  127. * @param value the new mapped value
  128. */
  129. public void setElementAt(char start, char end, byte value)
  130. {
  131. int i;
  132. if (isCompact) {
  133. expand();
  134. }
  135. for (i = start; i <= end; ++i) {
  136. values[i] = value;
  137. touchBlock(i >> BLOCKSHIFT, value);
  138. }
  139. }
  140. /**
  141. *Compact the array.
  142. */
  143. public void compact()
  144. {
  145. if (!isCompact) {
  146. int limitCompacted = 0;
  147. int iBlockStart = 0;
  148. short iUntouched = -1;
  149. for (int i = 0; i < indices.length; ++i, iBlockStart += BLOCKCOUNT) {
  150. indices[i] = -1;
  151. boolean touched = blockTouched(i);
  152. if (!touched && iUntouched != -1) {
  153. // If no values in this block were set, we can just set its
  154. // index to be the same as some other block with no values
  155. // set, assuming we've seen one yet.
  156. indices[i] = iUntouched;
  157. } else {
  158. int jBlockStart = 0;
  159. int j = 0;
  160. for (j = 0; j < limitCompacted;
  161. ++j, jBlockStart += BLOCKCOUNT) {
  162. if (hashes[i] == hashes[j] &&
  163. arrayRegionMatches(values, iBlockStart,
  164. values, jBlockStart, BLOCKCOUNT)) {
  165. indices[i] = (short)jBlockStart;
  166. break;
  167. }
  168. }
  169. if (indices[i] == -1) {
  170. // we didn't match, so copy & update
  171. System.arraycopy(values, iBlockStart,
  172. values, jBlockStart, BLOCKCOUNT);
  173. indices[i] = (short)jBlockStart;
  174. hashes[j] = hashes[i];
  175. ++limitCompacted;
  176. if (!touched) {
  177. // If this is the first untouched block we've seen,
  178. // remember its index.
  179. iUntouched = (short)jBlockStart;
  180. }
  181. }
  182. }
  183. }
  184. // we are done compacting, so now make the array shorter
  185. int newSize = limitCompacted*BLOCKCOUNT;
  186. byte[] result = new byte[newSize];
  187. System.arraycopy(values, 0, result, 0, newSize);
  188. values = result;
  189. isCompact = true;
  190. hashes = null;
  191. }
  192. }
  193. /**
  194. * Convenience utility to compare two arrays of doubles.
  195. * @param len the length to compare.
  196. * The start indices and start+len must be valid.
  197. */
  198. final static boolean arrayRegionMatches(byte[] source, int sourceStart,
  199. byte[] target, int targetStart,
  200. int len)
  201. {
  202. int sourceEnd = sourceStart + len;
  203. int delta = targetStart - sourceStart;
  204. for (int i = sourceStart; i < sourceEnd; i++) {
  205. if (source[i] != target[i + delta])
  206. return false;
  207. }
  208. return true;
  209. }
  210. /**
  211. * Remember that a specified block was "touched", i.e. had a value set.
  212. * Untouched blocks can be skipped when compacting the array
  213. */
  214. private final void touchBlock(int i, int value) {
  215. hashes[i] = (hashes[i] + (value<<1)) | 1;
  216. }
  217. /**
  218. * Query whether a specified block was "touched", i.e. had a value set.
  219. * Untouched blocks can be skipped when compacting the array
  220. */
  221. private final boolean blockTouched(int i) {
  222. return hashes[i] != 0;
  223. }
  224. /** For internal use only. Do not modify the result, the behavior of
  225. * modified results are undefined.
  226. */
  227. public short getIndexArray()[]
  228. {
  229. return indices;
  230. }
  231. /** For internal use only. Do not modify the result, the behavior of
  232. * modified results are undefined.
  233. */
  234. public byte getStringArray()[]
  235. {
  236. return values;
  237. }
  238. /**
  239. * Overrides Cloneable
  240. */
  241. public Object clone()
  242. {
  243. try {
  244. CompactByteArray other = (CompactByteArray) super.clone();
  245. other.values = (byte[])values.clone();
  246. other.indices = (short[])indices.clone();
  247. if (hashes != null) other.hashes = (int[])hashes.clone();
  248. return other;
  249. } catch (CloneNotSupportedException e) {
  250. throw new InternalError();
  251. }
  252. }
  253. /**
  254. * Compares the equality of two compact array objects.
  255. * @param obj the compact array object to be compared with this.
  256. * @return true if the current compact array object is the same
  257. * as the compact array object obj; false otherwise.
  258. */
  259. public boolean equals(Object obj) {
  260. if (obj == null) return false;
  261. if (this == obj) // quick check
  262. return true;
  263. if (getClass() != obj.getClass()) // same class?
  264. return false;
  265. CompactByteArray other = (CompactByteArray) obj;
  266. for (int i = 0; i < UNICODECOUNT; i++) {
  267. // could be sped up later
  268. if (elementAt((char)i) != other.elementAt((char)i))
  269. return false;
  270. }
  271. return true; // we made it through the guantlet.
  272. }
  273. /**
  274. * Generates the hash code for the compact array object
  275. */
  276. public int hashCode() {
  277. int result = 0;
  278. int increment = Math.min(3, values.length16);
  279. for (int i = 0; i < values.length; i+= increment) {
  280. result = result * 37 + values[i];
  281. }
  282. return result;
  283. }
  284. // --------------------------------------------------------------
  285. // package private
  286. // --------------------------------------------------------------
  287. /**
  288. * Expanding takes the array back to a 65536 element array.
  289. */
  290. private void expand()
  291. {
  292. int i;
  293. if (isCompact) {
  294. byte[] tempArray;
  295. hashes = new int[INDEXCOUNT];
  296. tempArray = new byte[UNICODECOUNT];
  297. for (i = 0; i < UNICODECOUNT; ++i) {
  298. byte value = elementAt((char)i);
  299. tempArray[i] = value;
  300. touchBlock(i >> BLOCKSHIFT, value);
  301. }
  302. for (i = 0; i < INDEXCOUNT; ++i) {
  303. indices[i] = (short)(i<<BLOCKSHIFT);
  304. }
  305. values = null;
  306. values = tempArray;
  307. isCompact = false;
  308. }
  309. }
  310. private byte[] getArray()
  311. {
  312. return values;
  313. }
  314. private static final int BLOCKSHIFT =7;
  315. private static final int BLOCKCOUNT =(1<<BLOCKSHIFT);
  316. private static final int INDEXSHIFT =(16-BLOCKSHIFT);
  317. private static final int INDEXCOUNT =(1<<INDEXSHIFT);
  318. private static final int BLOCKMASK = BLOCKCOUNT - 1;
  319. private byte[] values; // char -> short (char parameterized short)
  320. private short indices[];
  321. private boolean isCompact;
  322. private int[] hashes;
  323. };