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