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