Path: blob/master/src/java.base/share/classes/sun/text/UCompactIntArray.java
41152 views
/*1* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.text;2627public final class UCompactIntArray implements Cloneable {28/**29* Default constructor for UCompactIntArray, the default value of the30* compact array is 0.31*/32public UCompactIntArray() {33values = new int[16][];34indices = new short[16][];35blockTouched = new boolean[16][];36planeTouched = new boolean[16];37}3839public UCompactIntArray(int defaultValue) {40this();41this.defaultValue = defaultValue;42}4344/**45* Get the mapped value of a Unicode character.46* @param index the character to get the mapped value with47* @return the mapped value of the given character48*/49public int elementAt(int index) {50int plane = (index & PLANEMASK) >> PLANESHIFT;51if (!planeTouched[plane]) {52return defaultValue;53}54index &= CODEPOINTMASK;55return values[plane][(indices[plane][index >> BLOCKSHIFT] & 0xFFFF)56+ (index & BLOCKMASK)];57}585960/**61* Set a new value for a Unicode character.62* Set automatically expands the array if it is compacted.63* @param index the character to set the mapped value with64* @param value the new mapped value65*/66public void setElementAt(int index, int value) {67if (isCompact) {68expand();69}70int plane = (index & PLANEMASK) >> PLANESHIFT;71if (!planeTouched[plane]) {72initPlane(plane);73}74index &= CODEPOINTMASK;75values[plane][index] = value;76blockTouched[plane][index >> BLOCKSHIFT] = true;77}787980/**81* Compact the array.82*/83public void compact() {84if (isCompact) {85return;86}87for (int plane = 0; plane < PLANECOUNT; plane++) {88if (!planeTouched[plane]) {89continue;90}91int limitCompacted = 0;92int iBlockStart = 0;93short iUntouched = -1;9495for (int i = 0; i < indices[plane].length; ++i, iBlockStart += BLOCKCOUNT) {96indices[plane][i] = -1;97if (!blockTouched[plane][i] && iUntouched != -1) {98// If no values in this block were set, we can just set its99// index to be the same as some other block with no values100// set, assuming we've seen one yet.101indices[plane][i] = iUntouched;102} else {103int jBlockStart = limitCompacted * BLOCKCOUNT;104if (i > limitCompacted) {105System.arraycopy(values[plane], iBlockStart,106values[plane], jBlockStart, BLOCKCOUNT);107}108if (!blockTouched[plane][i]) {109// If this is the first untouched block we've seen, remember it.110iUntouched = (short)jBlockStart;111}112indices[plane][i] = (short)jBlockStart;113limitCompacted++;114}115}116117// we are done compacting, so now make the array shorter118int newSize = limitCompacted * BLOCKCOUNT;119int[] result = new int[newSize];120System.arraycopy(values[plane], 0, result, 0, newSize);121values[plane] = result;122blockTouched[plane] = null;123}124isCompact = true;125}126127128// --------------------------------------------------------------129// private130// --------------------------------------------------------------131/**132* Expanded takes the array back to a 0x10ffff element array133*/134private void expand() {135int i;136if (isCompact) {137int[] tempArray;138for (int plane = 0; plane < PLANECOUNT; plane++) {139if (!planeTouched[plane]) {140continue;141}142blockTouched[plane] = new boolean[INDEXCOUNT];143tempArray = new int[UNICODECOUNT];144for (i = 0; i < UNICODECOUNT; ++i) {145tempArray[i] = values[plane][indices[plane][i >> BLOCKSHIFT]146& 0xffff + (i & BLOCKMASK)];147blockTouched[plane][i >> BLOCKSHIFT] = true;148}149for (i = 0; i < INDEXCOUNT; ++i) {150indices[plane][i] = (short)(i<<BLOCKSHIFT);151}152values[plane] = tempArray;153}154isCompact = false;155}156}157158private void initPlane(int plane) {159values[plane] = new int[UNICODECOUNT];160indices[plane] = new short[INDEXCOUNT];161blockTouched[plane] = new boolean[INDEXCOUNT];162planeTouched[plane] = true;163164if (planeTouched[0] && plane != 0) {165System.arraycopy(indices[0], 0, indices[plane], 0, INDEXCOUNT);166} else {167for (int i = 0; i < INDEXCOUNT; ++i) {168indices[plane][i] = (short)(i<<BLOCKSHIFT);169}170}171for (int i = 0; i < UNICODECOUNT; ++i) {172values[plane][i] = defaultValue;173}174}175176public int getKSize() {177int size = 0;178for (int plane = 0; plane < PLANECOUNT; plane++) {179if (planeTouched[plane]) {180size += (values[plane].length * 4 + indices[plane].length * 2);181}182}183return size / 1024;184}185186private static final int PLANEMASK = 0x30000;187private static final int PLANESHIFT = 16;188private static final int PLANECOUNT = 0x10;189private static final int CODEPOINTMASK = 0xffff;190191private static final int UNICODECOUNT = 0x10000;192private static final int BLOCKSHIFT = 7;193private static final int BLOCKCOUNT = (1<<BLOCKSHIFT);194private static final int INDEXSHIFT = (16-BLOCKSHIFT);195private static final int INDEXCOUNT = (1<<INDEXSHIFT);196private static final int BLOCKMASK = BLOCKCOUNT - 1;197198private int defaultValue;199private int values[][];200private short indices[][];201private boolean isCompact;202private boolean[][] blockTouched;203private boolean[] planeTouched;204};205206207