suballocatedbytevector.java
来自「java jdk 1.4的源码」· Java 代码 · 共 535 行 · 第 1/2 页
JAVA
535 行
/* * The Apache Software License, Version 1.1 * * * Copyright (c) 1999 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Xalan" and "Apache Software Foundation" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", * nor may "Apache" appear in their name, without prior written * permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation and was * originally based on software copyright (c) 1999, Lotus * Development Corporation., http://www.lotus.com. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */package org.apache.xml.utils;/** * <meta name="usage" content="internal"/> * A very simple table that stores a list of byte. Very similar API to our * IntVector class (same API); different internal storage. * * This version uses an array-of-arrays solution. Read/write access is thus * a bit slower than the simple IntVector, and basic storage is a trifle * higher due to the top-level array -- but appending is O(1) fast rather * than O(N**2) slow, which will swamp those costs in situations where * long vectors are being built up. * * Known issues: * * Some methods are private because they haven't yet been tested properly. * * If an element has not been set (because we skipped it), its value will * initially be 0. Shortening the vector does not clear old storage; if you * then skip values and setElementAt a higher index again, you may see old data * reappear in the truncated-and-restored section. Doing anything else would * have performance costs. */public class SuballocatedByteVector{ /** Size of blocks to allocate */ protected int m_blocksize; /** Number of blocks to (over)allocate by */ protected int m_numblocks=32; /** Array of arrays of bytes */ protected byte m_map[][]; /** Number of bytes in array */ protected int m_firstFree = 0; /** "Shortcut" handle to m_map[0] */ protected byte m_map0[]; /** * Default constructor. Note that the default * block size is very small, for small lists. */ public SuballocatedByteVector() { this(2048); } /** * Construct a ByteVector, using the given block size. * * @param blocksize Size of block to allocate */ public SuballocatedByteVector(int blocksize) { m_blocksize = blocksize; m_map0=new byte[blocksize]; m_map = new byte[m_numblocks][]; m_map[0]=m_map0; } /** * Construct a ByteVector, using the given block size. * * @param blocksize Size of block to allocate */ public SuballocatedByteVector(int blocksize, int increaseSize) { // increaseSize not currently used. this(blocksize); } /** * Get the length of the list. * * @return length of the list */ public int size() { return m_firstFree; } /** * Set the length of the list. * * @return length of the list */ private void setSize(int sz) { if(m_firstFree<sz) m_firstFree = sz; } /** * Append a byte onto the vector. * * @param value Byte to add to the list */ public void addElement(byte value) { if(m_firstFree<m_blocksize) m_map0[m_firstFree++]=value; else { int index=m_firstFree/m_blocksize; int offset=m_firstFree%m_blocksize; ++m_firstFree; if(index>=m_map.length) { int newsize=index+m_numblocks; byte[][] newMap=new byte[newsize][]; System.arraycopy(m_map, 0, newMap, 0, m_map.length); m_map=newMap; } byte[] block=m_map[index]; if(null==block) block=m_map[index]=new byte[m_blocksize]; block[offset]=value; } } /** * Append several byte values onto the vector. * * @param value Byte to add to the list */ private void addElements(byte value, int numberOfElements) { if(m_firstFree+numberOfElements<m_blocksize) for (int i = 0; i < numberOfElements; i++) { m_map0[m_firstFree++]=value; } else { int index=m_firstFree/m_blocksize; int offset=m_firstFree%m_blocksize; m_firstFree+=numberOfElements; while( numberOfElements>0) { if(index>=m_map.length) { int newsize=index+m_numblocks; byte[][] newMap=new byte[newsize][]; System.arraycopy(m_map, 0, newMap, 0, m_map.length); m_map=newMap; } byte[] block=m_map[index]; if(null==block) block=m_map[index]=new byte[m_blocksize]; int copied=(m_blocksize-offset < numberOfElements) ? m_blocksize-offset : numberOfElements; numberOfElements-=copied; while(copied-- > 0) block[offset++]=value; ++index;offset=0; } } } /** * Append several slots onto the vector, but do not set the values. * Note: "Not Set" means the value is unspecified. * * @param value Byte to add to the list */ private void addElements(int numberOfElements) { int newlen=m_firstFree+numberOfElements; if(newlen>m_blocksize) { int index=m_firstFree%m_blocksize; int newindex=(m_firstFree+numberOfElements)%m_blocksize; for(int i=index+1;i<=newindex;++i) m_map[i]=new byte[m_blocksize]; } m_firstFree=newlen; } /** * Inserts the specified node in this vector at the specified index. * Each component in this vector with an index greater or equal to * the specified index is shifted upward to have an index one greater * than the value it had previously. * * Insertion may be an EXPENSIVE operation! * * @param value Byte to insert * @param at Index of where to insert */ private void insertElementAt(byte value, int at) { if(at==m_firstFree) addElement(value); else if (at>m_firstFree) { int index=at/m_blocksize; if(index>=m_map.length) { int newsize=index+m_numblocks; byte[][] newMap=new byte[newsize][]; System.arraycopy(m_map, 0, newMap, 0, m_map.length); m_map=newMap; } byte[] block=m_map[index]; if(null==block) block=m_map[index]=new byte[m_blocksize]; int offset=at%m_blocksize;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?