jdk/src/share/classes/sun/text/CompactByteArray.java
author sherman
Tue, 30 Aug 2011 11:53:11 -0700
changeset 10419 12c063b39232
parent 5506 202f599c92aa
child 11136 f0f53bbe5bd1
permissions -rw-r--r--
7084245: Update usages of InternalError to use exception chaining Summary: to use new InternalError constructor with cause chainning Reviewed-by: alanb, ksrini, xuelei, neugens Contributed-by: sebastian.sickelmann@gmx.de
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1996, 2005, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * (C) Copyright IBM Corp. 1996 - All Rights Reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *   The original version of this source code and documentation is copyrighted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * materials are provided under terms of a License Agreement between Taligent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * and Sun. This technology is protected by multiple US and International
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * patents. This notice and attribution to Taligent may not be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *   Taligent is a registered trademark of Taligent, Inc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
package sun.text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * class CompactATypeArray : use only on primitive data types
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * Provides a compact way to store information that is indexed by Unicode
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * values, such as character properties, types, keyboard values, etc.This
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * is very useful when you have a block of Unicode data that contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * significant values while the rest of the Unicode data is unused in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * application or when you have a lot of redundance, such as where all 21,000
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * Han ideographs have the same value.  However, lookup is much faster than a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * hash table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * A compact array of any primitive data type serves two purposes:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <UL type = round>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *     <LI>Fast access of the indexed values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *     <LI>Smaller memory footprint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * </UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * A compact array is composed of a index array and value array.  The index
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * array contains the indicies of Unicode characters to the value array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @see                CompactIntArray
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @see                CompactShortArray
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @author             Helena Shih
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
public final class CompactByteArray implements Cloneable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * The total number of Unicode characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public static  final int UNICODECOUNT =65536;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * Constructor for CompactByteArray.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * @param defaultValue the default value of the compact array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public CompactByteArray(byte defaultValue)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        values = new byte[UNICODECOUNT];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        indices = new short[INDEXCOUNT];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        hashes = new int[INDEXCOUNT];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        for (i = 0; i < UNICODECOUNT; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            values[i] = defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        for (i = 0; i < INDEXCOUNT; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            indices[i] = (short)(i<<BLOCKSHIFT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            hashes[i] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        isCompact = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Constructor for CompactByteArray.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @param indexArray the indicies of the compact array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @param newValues the values of the compact array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @exception IllegalArgumentException If index is out of range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     public CompactByteArray(short indexArray[],
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                            byte newValues[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if (indexArray.length != INDEXCOUNT)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            throw new IllegalArgumentException("Index out of bounds!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        for (i = 0; i < INDEXCOUNT; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            short index = indexArray[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            if ((index < 0) || (index >= newValues.length+BLOCKCOUNT))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                throw new IllegalArgumentException("Index out of bounds!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        indices = indexArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        values = newValues;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        isCompact = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * Get the mapped value of a Unicode character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @param index the character to get the mapped value with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @return the mapped value of the given character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public byte elementAt(char index)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        return (values[(indices[index >> BLOCKSHIFT] & 0xFFFF)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                       + (index & BLOCKMASK)]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Set a new value for a Unicode character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * Set automatically expands the array if it is compacted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @param index the character to set the mapped value with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @param value the new mapped value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    public void setElementAt(char index, byte value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        if (isCompact)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            expand();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        values[(int)index] = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        touchBlock(index >> BLOCKSHIFT, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Set new values for a range of Unicode character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @param start the starting offset o of the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @param end the ending offset of the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @param value the new mapped value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public void setElementAt(char start, char end, byte value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if (isCompact) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            expand();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        for (i = start; i <= end; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            values[i] = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            touchBlock(i >> BLOCKSHIFT, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
      *Compact the array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    public void compact()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (!isCompact) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            int limitCompacted = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            int iBlockStart = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            short iUntouched = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            for (int i = 0; i < indices.length; ++i, iBlockStart += BLOCKCOUNT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                indices[i] = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                boolean touched = blockTouched(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                if (!touched && iUntouched != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    // If no values in this block were set, we can just set its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    // index to be the same as some other block with no values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                    // set, assuming we've seen one yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    indices[i] = iUntouched;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    int jBlockStart = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    int j = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    for (j = 0; j < limitCompacted;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                            ++j, jBlockStart += BLOCKCOUNT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                        if (hashes[i] == hashes[j] &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                                arrayRegionMatches(values, iBlockStart,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                                values, jBlockStart, BLOCKCOUNT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                            indices[i] = (short)jBlockStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                    if (indices[i] == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                        // we didn't match, so copy & update
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                        System.arraycopy(values, iBlockStart,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                            values, jBlockStart, BLOCKCOUNT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                        indices[i] = (short)jBlockStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                        hashes[j] = hashes[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                        ++limitCompacted;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                        if (!touched) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                            // If this is the first untouched block we've seen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                            // remember its index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                            iUntouched = (short)jBlockStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            // we are done compacting, so now make the array shorter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            int newSize = limitCompacted*BLOCKCOUNT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            byte[] result = new byte[newSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            System.arraycopy(values, 0, result, 0, newSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            values = result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            isCompact = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            hashes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * Convenience utility to compare two arrays of doubles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @param len the length to compare.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * The start indices and start+len must be valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    final static boolean arrayRegionMatches(byte[] source, int sourceStart,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                                            byte[] target, int targetStart,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                                            int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        int sourceEnd = sourceStart + len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        int delta = targetStart - sourceStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        for (int i = sourceStart; i < sourceEnd; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            if (source[i] != target[i + delta])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * Remember that a specified block was "touched", i.e. had a value set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * Untouched blocks can be skipped when compacting the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    private final void touchBlock(int i, int value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        hashes[i] = (hashes[i] + (value<<1)) | 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * Query whether a specified block was "touched", i.e. had a value set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * Untouched blocks can be skipped when compacting the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    private final boolean blockTouched(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        return hashes[i] != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    /** For internal use only.  Do not modify the result, the behavior of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
      * modified results are undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    public short getIndexArray()[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        return indices;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    /** For internal use only.  Do not modify the result, the behavior of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
      * modified results are undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    public byte getStringArray()[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        return values;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * Overrides Cloneable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    public Object clone()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            CompactByteArray other = (CompactByteArray) super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            other.values = (byte[])values.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            other.indices = (short[])indices.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            if (hashes != null) other.hashes = (int[])hashes.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            return other;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        } catch (CloneNotSupportedException e) {
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 5506
diff changeset
   272
            throw new InternalError(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * Compares the equality of two compact array objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @param obj the compact array object to be compared with this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @return true if the current compact array object is the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * as the compact array object obj; false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        if (obj == null) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        if (this == obj)                      // quick check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        if (getClass() != obj.getClass())         // same class?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        CompactByteArray other = (CompactByteArray) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        for (int i = 0; i < UNICODECOUNT; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            // could be sped up later
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            if (elementAt((char)i) != other.elementAt((char)i))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        return true; // we made it through the guantlet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * Generates the hash code for the compact array object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        int result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        int increment = Math.min(3, values.length/16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        for (int i = 0; i < values.length; i+= increment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            result = result * 37 + values[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    // --------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    // package private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    // --------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
      * Expanding takes the array back to a 65536 element array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    private void expand()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        if (isCompact) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            byte[]  tempArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            hashes = new int[INDEXCOUNT];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            tempArray = new byte[UNICODECOUNT];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            for (i = 0; i < UNICODECOUNT; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                byte value = elementAt((char)i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                tempArray[i] = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                touchBlock(i >> BLOCKSHIFT, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            for (i = 0; i < INDEXCOUNT; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                indices[i] = (short)(i<<BLOCKSHIFT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            values = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            values = tempArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            isCompact = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    private byte[] getArray()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        return values;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    private static  final int BLOCKSHIFT =7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    private static  final int BLOCKCOUNT =(1<<BLOCKSHIFT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    private static  final int INDEXSHIFT =(16-BLOCKSHIFT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    private static  final int INDEXCOUNT =(1<<INDEXSHIFT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    private static  final int BLOCKMASK = BLOCKCOUNT - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    private byte[] values;  // char -> short (char parameterized short)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    private short indices[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    private boolean isCompact;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    private int[] hashes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
};