1. /*
  2. * @(#)CompactShortArray.java 1.16 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. * @(#)CompactShortArray.java 1.16 01/11/29
  9. *
  10. * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
  11. * (C) Copyright IBM Corp. 1996-1998 - 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. * @see CompactByteArray
  54. * @see CompactIntArray
  55. * @see CompactCharArray
  56. * @see CompactStringArray
  57. * @version 1.16 11/29/01
  58. * @author Helena Shih
  59. */
  60. final class CompactShortArray implements Cloneable {
  61. /**
  62. * The total number of Unicode characters.
  63. */
  64. public static final int UNICODECOUNT =65536;
  65. /**
  66. * Default constructor for CompactShortArray, the default value of the
  67. * compact array is 0.
  68. */
  69. public CompactShortArray()
  70. {
  71. this((short)0);
  72. }
  73. /**
  74. * Constructor for CompactShortArray.
  75. * @param defaultValue the default value of the compact array.
  76. */
  77. public CompactShortArray(short defaultValue)
  78. {
  79. int i;
  80. values = new short[UNICODECOUNT];
  81. indices = new short[INDEXCOUNT];
  82. hashes = new int[INDEXCOUNT];
  83. for (i = 0; i < UNICODECOUNT; ++i) {
  84. values[i] = defaultValue;
  85. }
  86. for (i = 0; i < INDEXCOUNT; ++i) {
  87. indices[i] = (short)(i<<BLOCKSHIFT);
  88. hashes[i] = 0;
  89. }
  90. isCompact = false;
  91. this.defaultValue = defaultValue;
  92. }
  93. /**
  94. * Constructor for CompactShortArray.
  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 CompactShortArray(short indexArray[],
  100. short newValues[],
  101. short defaultValue)
  102. {
  103. int i;
  104. if (indexArray.length != INDEXCOUNT)
  105. throw new IllegalArgumentException("Index out of bounds.");
  106. for (i = 0; i < INDEXCOUNT; ++i) {
  107. short index = indexArray[i];
  108. if ((index < 0) || (index >= newValues.length+BLOCKCOUNT))
  109. throw new IllegalArgumentException("Index out of bounds.");
  110. }
  111. indices = indexArray;
  112. values = newValues;
  113. isCompact = true;
  114. this.defaultValue = defaultValue;
  115. }
  116. /**
  117. * Get the mapped value of a Unicode character.
  118. * @param index the character to get the mapped value with
  119. * @return the mapped value of the given character
  120. */
  121. public short elementAt(char index) // parameterized on short
  122. {
  123. return (values[(indices[index >> BLOCKSHIFT] & 0xFFFF)
  124. + (index & BLOCKMASK)]);
  125. }
  126. /**
  127. * Set a new value for a Unicode character.
  128. * Set automatically expands the array if it is compacted.
  129. * @param index the character to set the mapped value with
  130. * @param value the new mapped value
  131. */
  132. public void setElementAt(char index, short value)
  133. {
  134. if (isCompact)
  135. expand();
  136. values[(int)index] = value;
  137. touchBlock(index >> BLOCKSHIFT, value);
  138. }
  139. /**
  140. * Set new values for a range of Unicode character.
  141. * @param start the starting offset of the range
  142. * @param end the ending offset of the range
  143. * @param value the new mapped value
  144. */
  145. public void setElementAt(char start, char end, short value)
  146. {
  147. int i;
  148. if (isCompact) {
  149. expand();
  150. }
  151. for (i = start; i <= end; ++i) {
  152. values[i] = value;
  153. touchBlock(i >> BLOCKSHIFT, value);
  154. }
  155. }
  156. /**
  157. *Compact the array.
  158. */
  159. public void compact()
  160. {
  161. if (!isCompact) {
  162. int limitCompacted = 0;
  163. int iBlockStart = 0;
  164. short iUntouched = -1;
  165. for (int i = 0; i < indices.length; ++i, iBlockStart += BLOCKCOUNT) {
  166. indices[i] = -1;
  167. boolean touched = blockTouched(i);
  168. if (!touched && iUntouched != -1) {
  169. // If no values in this block were set, we can just set its
  170. // index to be the same as some other block with no values
  171. // set, assuming we've seen one yet.
  172. indices[i] = iUntouched;
  173. } else {
  174. int jBlockStart = 0;
  175. int j = 0;
  176. for (j = 0; j < limitCompacted;
  177. ++j, jBlockStart += BLOCKCOUNT) {
  178. if (hashes[i] == hashes[j] &&
  179. arrayRegionMatches(values, iBlockStart,
  180. values, jBlockStart, BLOCKCOUNT)) {
  181. indices[i] = (short)jBlockStart;
  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. short[] result = new short[newSize];
  202. System.arraycopy(values, 0, result, 0, newSize);
  203. values = result;
  204. isCompact = true;
  205. hashes = null;
  206. }
  207. }
  208. /**
  209. * Convenience utility to compare two arrays of doubles.
  210. * @param len the length to compare.
  211. * The start indices and start+len must be valid.
  212. */
  213. final static boolean arrayRegionMatches(short[] source, int sourceStart,
  214. short[] target, int targetStart,
  215. int len)
  216. {
  217. int sourceEnd = sourceStart + len;
  218. int delta = targetStart - sourceStart;
  219. for (int i = sourceStart; i < sourceEnd; i++) {
  220. if (source[i] != target[i + delta])
  221. return false;
  222. }
  223. return true;
  224. }
  225. /**
  226. * Remember that a specified block was "touched", i.e. had a value set.
  227. * Untouched blocks can be skipped when compacting the array
  228. */
  229. private final void touchBlock(int i, int value) {
  230. hashes[i] = (hashes[i] + (value<<1)) | 1;
  231. }
  232. /**
  233. * Query whether a specified block was "touched", i.e. had a value set.
  234. * Untouched blocks can be skipped when compacting the array
  235. */
  236. private final boolean blockTouched(int i) {
  237. return hashes[i] != 0;
  238. }
  239. /** For internal use only. Do not modify the result, the behavior of
  240. * modified results are undefined.
  241. */
  242. public short getIndexArray()[]
  243. {
  244. return indices;
  245. }
  246. /** For internal use only. Do not modify the result, the behavior of
  247. * modified results are undefined.
  248. */
  249. public short getStringArray()[]
  250. {
  251. return values;
  252. }
  253. /**
  254. * Overrides Cloneable
  255. */
  256. public Object clone()
  257. {
  258. try {
  259. CompactShortArray other = (CompactShortArray) super.clone();
  260. other.values = (short[])values.clone();
  261. other.indices = (short[])indices.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. CompactShortArray other = (CompactShortArray) 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. /**
  300. * An iterator over the indices and values in this compact array,
  301. * The next() method returns each successive index that was used to store
  302. * a value in the array, and the shortValue() method returns each
  303. * corresponding value.
  304. */
  305. public class Iterator {
  306. Iterator() {
  307. compact();
  308. }
  309. // Find the next character in the array
  310. public boolean hasNext() {
  311. nextIndex = index;
  312. boolean done = true;
  313. if (index != nextIndex) {
  314. // hasNext has already been called, and there's a new
  315. // index waiting to be returned
  316. return true;
  317. }
  318. while (++nextIndex < Character.MAX_VALUE) {
  319. //
  320. // See if we're at the start of a new block. If so, there are
  321. // some optimizations we can try
  322. //
  323. if ((nextIndex & BLOCKMASK) == 0) {
  324. int blockIndex = nextIndex >> BLOCKSHIFT;
  325. if (indices[blockIndex] == iUntouched) {
  326. // This block wasn't touched; we can skip it and go
  327. // to the beginning of the next one. The -1 is to
  328. // compensate for the ++nextIndex in the loop condition
  329. //System.out.println("skipping block " + blockIndex);
  330. nextIndex += BLOCKCOUNT - 1;
  331. continue;
  332. }
  333. else if (iUntouched == -1 && !touched) {
  334. // Remember the index of the first untouched block we
  335. // find, so we can skip any others with the same index
  336. iUntouched = indices[blockIndex - 1];
  337. //System.out.println("Block " + (blockIndex-1) + " was untouched");
  338. } else {
  339. // Keep track of whether the next block was touched at all
  340. touched = false;
  341. }
  342. }
  343. nextValue = elementAt((char)nextIndex);
  344. if (nextValue != defValue) {
  345. touched = true; // Remember this block was touched
  346. break; // Return all non-default values
  347. }
  348. }
  349. return nextIndex < Character.MAX_VALUE;
  350. }
  351. public char next() {
  352. if (index == nextIndex && !hasNext()) {
  353. throw new ArrayIndexOutOfBoundsException();
  354. }
  355. index = nextIndex;
  356. value = nextValue;
  357. return (char)index;
  358. }
  359. public short shortValue() {
  360. return value;
  361. }
  362. // Privates....
  363. int nextIndex = -1;
  364. int index = -1;
  365. short nextValue;
  366. short value;
  367. short iUntouched = -1;
  368. boolean touched = true;
  369. short defValue = defaultValue;
  370. }
  371. /**
  372. * Return an iterator over all of the indices and values for the data
  373. * in this compact array
  374. */
  375. public Iterator getIterator() {
  376. return new Iterator();
  377. }
  378. // --------------------------------------------------------------
  379. // private
  380. // --------------------------------------------------------------
  381. /**
  382. * Expanding takes the array back to a 65536 element array.
  383. */
  384. private void expand()
  385. {
  386. int i;
  387. if (isCompact) {
  388. short[] tempArray;
  389. tempArray = new short[UNICODECOUNT];
  390. for (i = 0; i < UNICODECOUNT; ++i) {
  391. tempArray[i] = elementAt((char)i);
  392. }
  393. for (i = 0; i < INDEXCOUNT; ++i) {
  394. indices[i] = (short)(i<<BLOCKSHIFT);
  395. }
  396. values = null;
  397. values = tempArray;
  398. isCompact = false;
  399. }
  400. }
  401. static final int BLOCKSHIFT =7;
  402. static final int BLOCKCOUNT =(1<<BLOCKSHIFT);
  403. static final int INDEXSHIFT =(16-BLOCKSHIFT);
  404. static final int INDEXCOUNT =(1<<INDEXSHIFT);
  405. static final int BLOCKMASK = BLOCKCOUNT - 1;
  406. private short values[]; // char -> short (char parameterized short)
  407. private short indices[];
  408. private int[] hashes;
  409. private boolean isCompact;
  410. short defaultValue;
  411. };