compactstringarray.java

来自「《移动Agent技术》一书的所有章节源代码。」· Java 代码 · 共 451 行 · 第 1/2 页

JAVA
451
字号
            for (iIndex = 0; iIndex < tempIndexCount; ++iIndex) {
                tempArray[iIndex] = values[tempIndex[iIndex]];
            }
            values = null;
            values = tempArray;
            isCompact = true;
        } // endif (isCompact != false)
    }
    /** For internal use only.  Do not modify the result, the behavior of
     * modified results are undefined.
     */
    public short getIndexArray()[]
    {
        return indices;
    }
    /** For internal use only.  Do not modify the result, the behavior of
      * modified results are undefined.
      */
    public char getStringArray()[]
    {
        return values;
    }
    /**
     * Overrides Cloneable
     */
    public Object clone()
    {
        try {
            CompactStringArray other = (CompactStringArray) super.clone();
            other.values = (char[])values.clone();
            other.indices = (short[])indices.clone();
            other.exceptions = new StringBuffer(exceptions.toString());
            return other;
        } catch (CloneNotSupportedException e) {
            throw new InternalError();
        }
    }
    /**
     * Compares the equality of two compact array objects.
     * @param obj the compact array object to be compared with this.
     * @return true if the current compact array object is the same
     * as the compact array object obj; false otherwise.
     */
    public boolean equals(Object obj) {
        if (obj == null) return false;
        if (this == obj)                      // quick check
            return true;
        if (getClass() != obj.getClass())         // same class?
            return false;
        CompactStringArray other = (CompactStringArray) obj;
        for (int i = 0; i < UNICODECOUNT; i++) {
            // could be sped up later
            if (elementAt((char)i) != other.elementAt((char)i))
                return false;
        }
        return true; // we made it through the guantlet.
    }
    /**
     * Generates the hash code for the compact array object
     */

    public int hashCode() {
        int result = 0;
        int increment = Math.min(3, values.length/16);
        for (int i = 0; i < values.length; i+= increment) {
            result = result * 37 + values[i];
        }
        return result;
    }
    /**
      * package private : for internal use only
      */
    void writeArrays()
    {
        int i;
        int cnt = 0;
        if (values.length > 0)
            cnt = values.length;
        else
            cnt = values.length + UNICODECOUNT;
        System.out.println("{");
        for (i = 0; i < INDEXCOUNT-1; i++)
        {
            System.out.print("(short)" + (int)((getIndexArrayValue(i) >= 0) ?
                (int)getIndexArrayValue(i) :
                (int)(getIndexArrayValue(i)+UNICODECOUNT)) + ", ");
            if (i != 0)
                if (i % 10 == 0)
                    System.out.println();
        }
        System.out.println("(char)" +
                           (int)((getIndexArrayValue(INDEXCOUNT-1) >= 0) ?
                           (int)getIndexArrayValue(i) :
                           (int)(getIndexArrayValue(i)+UNICODECOUNT)) + " }");
        System.out.println("{");
        for (i = 0; i < cnt-1; i++)
        {
            char ch = getArrayValue(i);
            if (ch < 0x20 || (ch > 0x7E && ch < 0xA0) || ch > 0x100)
                System.out.print("(char)0x" +
                    Integer.toString((int)ch,16).toUpperCase() + ",");
            else System.out.print("\'" + ch + "\',");
            if (i != 0)
                if (i % 10 == 0)
                    System.out.println();
        }
        System.out.println("(char)" + (int)getArrayValue(cnt-1) + " }");
        System.out.println("\"" + exceptions.toString() + "\"");
    }
    // Print char Array  : Debug only
    void printIndex(char start, short count)
    {
        int i;
        for (i = start; i < count; ++i)
        {
            System.out.println(i + " -> : " +
                               (int)((indices[i] >= 0) ?
                                     indices[i] : indices[i] + UNICODECOUNT));
        }
        System.out.println();
    }
    void printPlainArray(int start,int count, char[] tempIndex)
    {
        int iIndex;
        if (tempIndex != null)
        {
            for (iIndex     = start; iIndex < start + count; ++iIndex)
            {
                System.out.print(" " + (int)getArrayValue(tempIndex[iIndex]));
            }
        }
        else
        {
            for (iIndex = start; iIndex < start + count; ++iIndex)
            {
                System.out.print(" " + (int)getArrayValue(iIndex));
            }
        }
        System.out.println("    Range: start " + start + " , count " + count);
    }
    /**
     * private functions
     */
    /**
      * Expanded takes the array back to a 65536 element array
      */
    private void expand()
    {
        int i;
        if (isCompact) {
            char[]  tempArray;
            tempArray = new char[UNICODECOUNT];
            for (i = 0; i < UNICODECOUNT; ++i) {
                tempArray[i] =(values[((int)indices[i>>BLOCKSHIFT] & 0xFFFF) +
                                     (i & BLOCKMASK)]);;
            }
            for (i = 0; i < INDEXCOUNT; ++i) {
                indices[i] = (short)(i<<BLOCKSHIFT);
            }
            values = null;
            values = tempArray;
            isCompact = false;
        }
    }
    // # of elements in the indexed array
    private short capacity()
    {
        return (short)values.length;
    }
    private char getArrayValue(int n)
    {
        return values[n];
    }
    private short getIndexArrayValue(int n)
    {
        return indices[n];
    }
    private int
    FindOverlappingPosition(int start, char[] tempIndex, int tempIndexCount)
    {
        int i;
        short j;
        short currentCount;

        if (DEBUGOVERLAP && start < DEBUGSHOWOVERLAPLIMIT) {
            printPlainArray(start, BLOCKCOUNT, null);
            printPlainArray(0, tempIndexCount, tempIndex);
        }
        for (i = 0; i < tempIndexCount; i += BLOCKCOUNT) {
            currentCount = (short)BLOCKCOUNT;
            if (i + BLOCKCOUNT > tempIndexCount) {
                currentCount = (short)(tempIndexCount - i);
            }
            for (j = 0; j < currentCount; ++j) {
                if (values[start + j] != values[tempIndex[i + j]]) break;
            }
            if (j == currentCount) break;
        }
        if (DEBUGOVERLAP && start < DEBUGSHOWOVERLAPLIMIT) {
            for (j = 1; j < i; ++j) {
                System.out.print(" ");
            }
            printPlainArray(start, BLOCKCOUNT, null);
            System.out.println("    Found At: " + i);
        }
        return i;
    }

    private static  final int DEBUGSHOWOVERLAPLIMIT = 100;
    private static  final boolean DEBUGTRACE = false;
    private static  final boolean DEBUGSMALL = false;
    private static  final boolean DEBUGOVERLAP = false;
    private static  final int DEBUGSMALLLIMIT = 30000;
    private static  final int BLOCKSHIFT =7;
    private static  final int BLOCKCOUNT =(1<<BLOCKSHIFT);
    private static  final int INDEXSHIFT =(16-BLOCKSHIFT);
    private static  final int INDEXCOUNT =(1<<INDEXSHIFT);
    private static  final int BLOCKMASK = BLOCKCOUNT - 1;

    private char[] values;  // char -> short (char parameterized short)
    private short indices[];
    private StringBuffer exceptions = new StringBuffer();
    private boolean isCompact;
};

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?