home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / CompactCharArray.java < prev    next >
Text File  |  1997-05-20  |  15KB  |  420 lines

  1. /*
  2.  * @(#)CompactCharArray.java    1.8 97/01/27
  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 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.  * class CompactATypeArray : use only on primitive data types
  35.  * Provides a compact way to store information that is indexed by Unicode
  36.  * values, such as character properties, types, keyboard values, etc.This
  37.  * is very useful when you have a block of Unicode data that contains
  38.  * significant values while the rest of the Unicode data is unused in the
  39.  * application or when you have a lot of redundance, such as where all 21,000
  40.  * Han ideographs have the same value.  However, lookup is much faster than a
  41.  * hash table.
  42.  * A compact array of any primitive data type serves two purposes:
  43.  * <UL type = round>
  44.  *     <LI>Fast access of the indexed values.
  45.  *     <LI>Smaller memory footprint.
  46.  * </UL>
  47.  * A compact array is composed of a index array and value array.  The index
  48.  * array contains the indicies of Unicode characters to the value array.
  49.  *
  50.  * @see        CompactByteArray
  51.  * @see        CompactIntArray
  52.  * @see        CompactShortArray
  53.  * @see        CompactStringArray
  54.  * @version    1.8 01/27/97
  55.  * @author     Helena Shih
  56.  */
  57. final class CompactCharArray implements Cloneable{
  58.  
  59.     /**
  60.      * The total number of Unicode characters.
  61.      */
  62.     public static  final int UNICODECOUNT =65536;
  63.  
  64.     /**
  65.      * Default constructor for CompactCharArray, the default value of the
  66.      * compact array is '\u0000'.
  67.      */
  68.     public CompactCharArray()
  69.     {
  70.         this('\u0000');
  71.     }
  72.  
  73.     /**
  74.      * Contructor for CompactCharArray.
  75.      * @param defaultValue the default value of the compact array.
  76.      */
  77.     public CompactCharArray(char defaultValue)
  78.     {
  79.         int i;
  80.         values = new char[UNICODECOUNT];
  81.         indices = new short[INDEXCOUNT];
  82.         for (i = 0; i < UNICODECOUNT; ++i) {
  83.             values[i] = defaultValue;
  84.         }
  85.         for (i = 0; i < INDEXCOUNT; ++i) {
  86.             indices[i] = (short)(i<<BLOCKSHIFT);
  87.         }
  88.         isCompact = false;
  89.     }
  90.  
  91.     /**
  92.      * Constructor for CompactCharArray.
  93.      * @param indexArray the indicies of the compact array.
  94.      * @param newValues the values of the compact array.
  95.      * @exception IllegalArgumentException If the index is out of range.
  96.      */
  97.     public CompactCharArray(short indexArray[], char newValues[])
  98.     {
  99.         int i;
  100.         if (indexArray.length != INDEXCOUNT)
  101.             throw new IllegalArgumentException("Index out of bounds.");
  102.         for (i = 0; i < INDEXCOUNT; ++i) {
  103.             short index = indexArray[i];
  104.             if ((index < 0) || (index >= newValues.length+BLOCKCOUNT))
  105.                 throw new IllegalArgumentException("Index out of bounds.");
  106.         }
  107.         indices = indexArray;
  108.         values = newValues;
  109.     }
  110.  
  111.     /**
  112.      * Get the mapped value of a Unicode character.
  113.      * @param index the character to get the mapped value with
  114.      * @return the mapped value of the given character
  115.      */
  116.    public char elementAt(char index) // parameterized on short
  117.     {
  118.         return (values[(indices[index >> BLOCKSHIFT] & 0xFFFF)
  119.                        + (index & BLOCKMASK)]);
  120.     }
  121.  
  122.     /**
  123.      * Set a new value for a Unicode character.
  124.      * Set automatically expands the array if it is compacted.
  125.      * @param index the character to set the mapped value with
  126.      * @param value the new mapped value
  127.      */
  128.     public void setElementAt(char index, char value)
  129.     {
  130.         if (isCompact)
  131.             expand();
  132.         values[(int)index] = value;
  133.     }
  134.  
  135.     /**
  136.      * Set new values for a range of Unicode character.
  137.      * @param start the starting offset of the range
  138.      * @param end the ending offset of the range
  139.      * @param value the new mapped value
  140.      */
  141.     public void setElementAt(char start, char end, char value)
  142.     {
  143.         int i;
  144.         if (isCompact) {
  145.             expand();
  146.         }
  147.         for (i = start; i <= end; ++i) {
  148.             values[i] = value;
  149.         }
  150.     }
  151.  
  152.     /**
  153.       *Compact the array.
  154.       */
  155.     public void compact()
  156.     {
  157.         if (isCompact == false) {
  158.             char[] tempIndex;
  159.             int    tempIndexCount;
  160.             char[] tempArray;
  161.             short  iBlock, iIndex;
  162.  
  163.             // make temp storage, larger than we need
  164.             tempIndex = new char[UNICODECOUNT];
  165.             // set up first block.
  166.             tempIndexCount = BLOCKCOUNT;
  167.             for (iIndex = 0; iIndex < BLOCKCOUNT; ++iIndex) {
  168.                 tempIndex[iIndex] = (char)iIndex;
  169.             }; // endfor (iIndex = 0; .....)
  170.             indices[0] = (short)0;
  171.  
  172.             // for each successive block, find out its first
  173.             // position in the compacted array
  174.             for (iBlock = 1; iBlock < INDEXCOUNT; ++iBlock) {
  175.                 int     newCount, firstPosition, block;
  176.                 block = iBlock<<BLOCKSHIFT;
  177.                 if (DEBUGSMALL) if (block > DEBUGSMALLLIMIT) break;
  178.                 firstPosition = FindOverlappingPosition(block, tempIndex,
  179.                                                         tempIndexCount);
  180.  
  181.                 newCount = firstPosition + BLOCKCOUNT;
  182.                 if (newCount > tempIndexCount) {
  183.                     for (iIndex = (short)tempIndexCount;
  184.                          iIndex < newCount;
  185.                          ++iIndex) {
  186.                         tempIndex[iIndex] = (char)
  187.                                             (iIndex - firstPosition + block);
  188.                     } // endfor (iIndex = tempIndexCount....)
  189.                     tempIndexCount = newCount;
  190.                 } // endif (newCount > tempIndexCount)
  191.                 indices[iBlock] = (short)firstPosition;
  192.             } // endfor (iBlock = 1.....)
  193.  
  194.             // now allocate and copy the items into the array
  195.             tempArray = new char[tempIndexCount];
  196.             for (iIndex = 0; iIndex < tempIndexCount; ++iIndex) {
  197.                 tempArray[iIndex] = values[tempIndex[iIndex]];
  198.             }
  199.             values = null;
  200.             values = tempArray;
  201.             isCompact = true;
  202.         } // endif (isCompact != false)
  203.     }
  204.  
  205.     /** For internal use only.  Do not modify the result, the behavior of
  206.       * modified results are undefined.
  207.       */
  208.     public short getIndexArray()[]
  209.     {
  210.         return indices;
  211.     }
  212.     /** For internal use only.  Do not modify the result, the behavior of
  213.       * modified results are undefined.
  214.       */
  215.     public char getStringArray()[]
  216.     {
  217.         return values;
  218.     }
  219.     /**
  220.      * Overrides Cloneable
  221.      */
  222.     public Object clone()
  223.     {
  224.         try {
  225.             CompactCharArray other = (CompactCharArray) super.clone();
  226.             other.values = (char[])values.clone();
  227.             other.indices = (short[])indices.clone();
  228.             return other;
  229.         } catch (CloneNotSupportedException e) {
  230.             throw new InternalError();
  231.         }
  232.     }
  233.     /**
  234.      * Compares the equality of two compact array objects.
  235.      * @param obj the compact array object to be compared with this.
  236.      * @return true if the current compact array object is the same
  237.      * as the compact array object obj; false otherwise.
  238.      */
  239.     public boolean equals(Object obj) {
  240.         if (this == obj)                      // quick check
  241.             return true;
  242.         if (getClass() != obj.getClass())         // same class?
  243.             return false;
  244.         CompactCharArray other = (CompactCharArray) obj;
  245.         for (int i = 0; i < UNICODECOUNT; i++) {
  246.             // could be sped up later
  247.             if (elementAt((char)i) != other.elementAt((char)i))
  248.                 return false;
  249.         }
  250.         return true; // we made it through the guantlet.
  251.     }
  252.     /**
  253.      * Generates the hash code for the compact array object
  254.      */
  255.  
  256.     public int hashCode() {
  257.         int result = 0;
  258.         int increment = Math.min(3, values.length/16);
  259.         for (int i = 0; i < values.length; i+= increment) {
  260.             result = result * 37 + values[i];
  261.         }
  262.         return result;
  263.     }
  264.     // --------------------------------------------------------------
  265.     // package private
  266.     // --------------------------------------------------------------
  267.     public void writeArrays()
  268.     {
  269.         int i;
  270.         int cnt = ((values.length > 0) ?
  271.                    values.length :
  272.                    (values.length + UNICODECOUNT));
  273.         System.out.println("{");
  274.         for (i = 0; i < INDEXCOUNT-1; i++)
  275.         {
  276.             System.out.print("(short)"
  277.                              + (int)((getIndexArrayValue(i) >= 0) ?
  278.                                      (int)getIndexArrayValue(i) :
  279.                                      (int)(getIndexArrayValue(i)+UNICODECOUNT))
  280.                              + ", ");
  281.             if (i != 0)
  282.                 if (i % 10 == 0)
  283.                     System.out.println();
  284.         }
  285.         System.out.println("(short)" +
  286.                            (int)((getIndexArrayValue(INDEXCOUNT-1) >= 0) ?
  287.                                  (int)getIndexArrayValue(i) :
  288.                                  (int)(getIndexArrayValue(i)+UNICODECOUNT)) +
  289.                            " }");
  290.         System.out.println("{");
  291.         for (i = 0; i < cnt-1; i++)
  292.         {
  293.             System.out.print("(char)" + (int)getArrayValue(i) + ", ");
  294.             if (i != 0)
  295.                 if (i % 10 == 0)
  296.                     System.out.println();
  297.         }
  298.         System.out.println("(char)" + (int)getArrayValue(cnt-1) + " }");
  299.     }
  300.  
  301.     // Print char Array  : Debug only
  302.     public void printIndex(short start, short count)
  303.     {
  304.         int i;
  305.         for (i = start; i < count; ++i)
  306.         {
  307.             System.out.println(i + " -> : "
  308.                                + (int)((indices[i] >= 0) ? indices[i]
  309.                                        : indices[i] + UNICODECOUNT));
  310.         }
  311.         System.out.println();
  312.     }
  313.  
  314.     public void printPlainArray(int start,int count, char[] tempIndex)
  315.     {
  316.         int iIndex;
  317.         if (tempIndex != null)
  318.         {
  319.             for (iIndex     = start; iIndex < start + count; ++iIndex)
  320.             {
  321.                 System.out.print(" " + (int)getArrayValue(tempIndex[iIndex]));
  322.             }
  323.         }
  324.         else
  325.         {
  326.             for (iIndex = start; iIndex < start + count; ++iIndex)
  327.             {
  328.                 System.out.print(" " + (int)getArrayValue(iIndex));
  329.             }
  330.         }
  331.         System.out.println("    Range: start " + start + " , count " + count);
  332.     }
  333.  
  334.     // --------------------------------------------------------------
  335.     // private
  336.     // --------------------------------------------------------------
  337.     /**
  338.       * Expanding takes the array back to a 65536 element array.
  339.       */
  340.     private void expand()
  341.     {
  342.         int i;
  343.         if (isCompact) {
  344.             char[]  tempArray;
  345.             tempArray = new char[UNICODECOUNT];
  346.             for (i = 0; i < UNICODECOUNT; ++i) {
  347.                 tempArray[i] = elementAt((char)i);
  348.             }
  349.             for (i = 0; i < INDEXCOUNT; ++i) {
  350.                 indices[i] = (short)(i<<BLOCKSHIFT);
  351.             }
  352.             values = null;
  353.             values = tempArray;
  354.             isCompact = false;
  355.         }
  356.     }
  357.  
  358.     // # of elements in the indexed array
  359.     private short capacity()
  360.     {
  361.         return (short)values.length;
  362.     }
  363.  
  364.     private char getArrayValue(int n)
  365.     {
  366.         return values[n];
  367.     }
  368.  
  369.     private short getIndexArrayValue(int n)
  370.     {
  371.         return indices[n];
  372.     }
  373.  
  374.     private int
  375.     FindOverlappingPosition(int start, char[] tempIndex, int tempIndexCount)
  376.     {
  377.         int i;
  378.         short j;
  379.         short currentCount;
  380.  
  381.         if (DEBUGOVERLAP && start < DEBUGSHOWOVERLAPLIMIT) {
  382.             printPlainArray(start, BLOCKCOUNT, null);
  383.             printPlainArray(0, tempIndexCount, tempIndex);
  384.         }
  385.         for (i = 0; i < tempIndexCount; i += BLOCKCOUNT) {
  386.             currentCount = (short)BLOCKCOUNT;
  387.             if (i + BLOCKCOUNT > tempIndexCount) {
  388.                 currentCount = (short)(tempIndexCount - i);
  389.             }
  390.             for (j = 0; j < currentCount; ++j) {
  391.                 if (values[start + j] != values[tempIndex[i + j]]) break;
  392.             }
  393.             if (j == currentCount) break;
  394.         }
  395.         if (DEBUGOVERLAP && start < DEBUGSHOWOVERLAPLIMIT) {
  396.             for (j = 1; j < i; ++j) {
  397.                 System.out.print(" ");
  398.             }
  399.             printPlainArray(start, BLOCKCOUNT, null);
  400.             System.out.println("    Found At: " + i);
  401.         }
  402.         return i;
  403.     }
  404.  
  405.     private static  final int DEBUGSHOWOVERLAPLIMIT = 100;
  406.     private static  final boolean DEBUGTRACE = false;
  407.     private static  final boolean DEBUGSMALL = false;
  408.     private static  final boolean DEBUGOVERLAP = false;
  409.     private static  final int DEBUGSMALLLIMIT = 30000;
  410.     private static  final int BLOCKSHIFT =7;
  411.     private static  final int BLOCKCOUNT =(1<<BLOCKSHIFT);
  412.     private static  final int INDEXSHIFT =(16-BLOCKSHIFT);
  413.     private static  final int INDEXCOUNT =(1<<INDEXSHIFT);
  414.     private static  final int BLOCKMASK = BLOCKCOUNT - 1;
  415.  
  416.     private char[] values;  // char -> short (char parameterized short)
  417.     private short indices[];
  418.     private boolean isCompact;
  419. };
  420.