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

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