home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / text / CompactByteArray.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  12.0 KB  |  349 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)CompactByteArray.java    1.14 98/06/11
  3.  *
  4.  * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
  5.  * (C) Copyright IBM Corp. 1996 - All Rights Reserved
  6.  *
  7.  * Portions copyright (c) 1996-1998 Sun Microsystems, Inc. All Rights Reserved.
  8.  *
  9.  *   The original version of this source code and documentation is copyrighted
  10.  * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
  11.  * materials are provided under terms of a License Agreement between Taligent
  12.  * and Sun. This technology is protected by multiple US and International
  13.  * patents. This notice and attribution to Taligent may not be removed.
  14.  *   Taligent is a registered trademark of Taligent, Inc.
  15.  *
  16.  * Permission to use, copy, modify, and distribute this software
  17.  * and its documentation for NON-COMMERCIAL purposes and without
  18.  * fee is hereby granted provided that this copyright notice
  19.  * appears in all copies. Please refer to the file "copyright.html"
  20.  * for further important copyright and licensing information.
  21.  *
  22.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  23.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  24.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  25.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  26.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  27.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  28.  *
  29.  */
  30.  
  31. package java.text;
  32.  
  33.  
  34. /**
  35.  * class CompactATypeArray : use only on primitive data types
  36.  * Provides a compact way to store information that is indexed by Unicode
  37.  * values, such as character properties, types, keyboard values, etc.This
  38.  * is very useful when you have a block of Unicode data that contains
  39.  * significant values while the rest of the Unicode data is unused in the
  40.  * application or when you have a lot of redundance, such as where all 21,000
  41.  * Han ideographs have the same value.  However, lookup is much faster than a
  42.  * hash table.
  43.  * A compact array of any primitive data type serves two purposes:
  44.  * <UL type = round>
  45.  *     <LI>Fast access of the indexed values.
  46.  *     <LI>Smaller memory footprint.
  47.  * </UL>
  48.  * A compact array is composed of a index array and value array.  The index
  49.  * array contains the indicies of Unicode characters to the value array.
  50.  *
  51.  * @see                CompactCharArray
  52.  * @see                CompactIntArray
  53.  * @see                CompactShortArray
  54.  * @see                CompactStringArray
  55.  * @version            1.14 06/11/98
  56.  * @author             Helena Shih
  57.  */
  58. final class CompactByteArray implements Cloneable {
  59.  
  60.     /**
  61.      * The total number of Unicode characters.
  62.      */
  63.     public static  final int UNICODECOUNT =65536;
  64.  
  65.     /**
  66.      * Default constructor for CompactByteArray, the default value of the
  67.      * compact array is 0.
  68.      */
  69.     public CompactByteArray()
  70.     {
  71.         this((byte)0);
  72.     }
  73.     /**
  74.      * Constructor for CompactByteArray.
  75.      * @param defaultValue the default value of the compact array.
  76.      */
  77.     public CompactByteArray(byte defaultValue)
  78.     {
  79.         int i;
  80.         values = new byte[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.     }
  92.     /**
  93.      * Constructor for CompactByteArray.
  94.      * @param indexArray the indicies of the compact array.
  95.      * @param newValues the values of the compact array.
  96.      * @exception IllegalArgumentException If index is out of range.
  97.      */
  98.     public CompactByteArray(short indexArray[],
  99.                             byte newValues[])
  100.     {
  101.         int i;
  102.         if (indexArray.length != INDEXCOUNT)
  103.             throw new IllegalArgumentException("Index out of bounds!");
  104.         for (i = 0; i < INDEXCOUNT; ++i) {
  105.             short index = indexArray[i];
  106.             if ((index < 0) || (index >= newValues.length+BLOCKCOUNT))
  107.                 throw new IllegalArgumentException("Index out of bounds!");
  108.         }
  109.         indices = indexArray;
  110.         values = newValues;
  111.         isCompact = true;
  112.     }
  113.     /**
  114.      * Get the mapped value of a Unicode character.
  115.      * @param index the character to get the mapped value with
  116.      * @return the mapped value of the given character
  117.      */
  118.     public byte elementAt(char index)
  119.     {
  120.         return (values[(indices[index >> BLOCKSHIFT] & 0xFFFF)
  121.                        + (index & BLOCKMASK)]);
  122.     }
  123.     /**
  124.      * Set a new value for a Unicode character.
  125.      * Set automatically expands the array if it is compacted.
  126.      * @param index the character to set the mapped value with
  127.      * @param value the new mapped value
  128.      */
  129.     public void setElementAt(char index, byte value)
  130.     {
  131.         if (isCompact)
  132.             expand();
  133.         values[(int)index] = value;
  134.         touchBlock(index >> BLOCKSHIFT, value);
  135.     }
  136.     /**
  137.      * Set new values for a range of Unicode character.
  138.      * @param start the starting offset o of the range
  139.      * @param end the ending offset of the range
  140.      * @param value the new mapped value
  141.      */
  142.     public void setElementAt(char start, char end, byte value)
  143.     {
  144.         int i;
  145.         if (isCompact) {
  146.             expand();
  147.         }
  148.         for (i = start; i <= end; ++i) {
  149.             values[i] = value;
  150.             touchBlock(i >> BLOCKSHIFT, value);
  151.         }
  152.     }
  153.     /**
  154.       *Compact the array.
  155.       */
  156.     public void compact()
  157.     {
  158.         if (!isCompact) {
  159.             int limitCompacted = 0;
  160.             int iBlockStart = 0;
  161.             short iUntouched = -1;
  162.  
  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.  
  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.             byte[] result = new byte[newSize];
  202.             System.arraycopy(values, 0, result, 0, newSize);
  203.             values = result;
  204.             isCompact = true;
  205.             hashes = null;
  206.         }
  207.     }
  208.  
  209.     /**
  210.      * Convenience utility to compare two arrays of doubles.
  211.      * @param len the length to compare.
  212.      * The start indices and start+len must be valid.
  213.      */
  214.     final static boolean arrayRegionMatches(byte[] source, int sourceStart,
  215.                                             byte[] target, int targetStart,
  216.                                             int len)
  217.     {
  218.         int sourceEnd = sourceStart + len;
  219.         int delta = targetStart - sourceStart;
  220.         for (int i = sourceStart; i < sourceEnd; i++) {
  221.             if (source[i] != target[i + delta])
  222.             return false;
  223.         }
  224.         return true;
  225.     }
  226.  
  227.     /**
  228.      * Remember that a specified block was "touched", i.e. had a value set.
  229.      * Untouched blocks can be skipped when compacting the array
  230.      */
  231.     private final void touchBlock(int i, int value) {
  232.         hashes[i] = (hashes[i] + (value<<1)) | 1;
  233.     }
  234.  
  235.     /**
  236.      * Query whether a specified block was "touched", i.e. had a value set.
  237.      * Untouched blocks can be skipped when compacting the array
  238.      */
  239.     private final boolean blockTouched(int i) {
  240.         return hashes[i] != 0;
  241.     }
  242.      
  243.     /** For internal use only.  Do not modify the result, the behavior of
  244.       * modified results are undefined.
  245.       */
  246.     public short getIndexArray()[]
  247.     {
  248.         return indices;
  249.     }
  250.     /** For internal use only.  Do not modify the result, the behavior of
  251.       * modified results are undefined.
  252.       */
  253.     public byte getStringArray()[]
  254.     {
  255.         return values;
  256.     }
  257.     /**
  258.      * Overrides Cloneable
  259.      */
  260.     public Object clone()
  261.     {
  262.         try {
  263.             CompactByteArray other = (CompactByteArray) super.clone();
  264.             other.values = (byte[])values.clone();
  265.             other.indices = (short[])indices.clone();
  266.             if (hashes != null) other.hashes = (int[])hashes.clone();
  267.             return other;
  268.         } catch (CloneNotSupportedException e) {
  269.             throw new InternalError();
  270.         }
  271.     }
  272.     /**
  273.      * Compares the equality of two compact array objects.
  274.      * @param obj the compact array object to be compared with this.
  275.      * @return true if the current compact array object is the same
  276.      * as the compact array object obj; false otherwise.
  277.      */
  278.     public boolean equals(Object obj) {
  279.         if (obj == null) return false;
  280.         if (this == obj)                      // quick check
  281.             return true;
  282.         if (getClass() != obj.getClass())         // same class?
  283.             return false;
  284.         CompactByteArray other = (CompactByteArray) obj;
  285.         for (int i = 0; i < UNICODECOUNT; i++) {
  286.             // could be sped up later
  287.             if (elementAt((char)i) != other.elementAt((char)i))
  288.                 return false;
  289.         }
  290.         return true; // we made it through the guantlet.
  291.     }
  292.  
  293.     /**
  294.      * Generates the hash code for the compact array object
  295.      */
  296.  
  297.     public int hashCode() {
  298.         int result = 0;
  299.         int increment = Math.min(3, values.length/16);
  300.         for (int i = 0; i < values.length; i+= increment) {
  301.             result = result * 37 + values[i];
  302.         }
  303.         return result;
  304.     }
  305.  
  306.     // --------------------------------------------------------------
  307.     // package private
  308.     // --------------------------------------------------------------
  309.     /**
  310.       * Expanding takes the array back to a 65536 element array.
  311.       */
  312.     private void expand()
  313.     {
  314.         int i;
  315.         if (isCompact) {
  316.             byte[]  tempArray;
  317.             hashes = new int[INDEXCOUNT];
  318.             tempArray = new byte[UNICODECOUNT];
  319.             for (i = 0; i < UNICODECOUNT; ++i) {
  320.                 byte value = elementAt((char)i);
  321.                 tempArray[i] = value;
  322.                 touchBlock(i >> BLOCKSHIFT, value);
  323.             }
  324.             for (i = 0; i < INDEXCOUNT; ++i) {
  325.                 indices[i] = (short)(i<<BLOCKSHIFT);
  326.             }
  327.             values = null;
  328.             values = tempArray;
  329.             isCompact = false;
  330.         }
  331.     }
  332.  
  333.     private byte[] getArray()
  334.     {
  335.         return values;
  336.     }
  337.  
  338.     private static  final int BLOCKSHIFT =7;
  339.     private static  final int BLOCKCOUNT =(1<<BLOCKSHIFT);
  340.     private static  final int INDEXSHIFT =(16-BLOCKSHIFT);
  341.     private static  final int INDEXCOUNT =(1<<INDEXSHIFT);
  342.     private static  final int BLOCKMASK = BLOCKCOUNT - 1;
  343.  
  344.     private byte[] values;  // char -> short (char parameterized short)
  345.     private short indices[];
  346.     private boolean isCompact;
  347.     private int[] hashes;
  348. };
  349.