suballocatedintvector.java
来自「JAVA 所有包」· Java 代码 · 共 556 行 · 第 1/2 页
JAVA
556 行
int offset=at&m_MASK; int push; // ***** Easier to work down from top? while(index<=maxindex) { int copylen=m_blocksize-offset-1; int[] block=m_map[index]; if(null==block) { push=0; block=m_map[index]=new int[m_blocksize]; } else { push=block[m_blocksize-1]; System.arraycopy(block, offset , block, offset+1, copylen); } block[offset]=value; value=push; offset=0; ++index; } } } /** * Wipe it out. Currently defined as equivalent to setSize(0). */ public void removeAllElements() { m_firstFree = 0; m_buildCache = m_map0; m_buildCacheStartIndex = 0; } /** * Removes the first occurrence of the argument from this vector. * If the object is found in this vector, each component in the vector * with an index greater or equal to the object's index is shifted * downward to have an index one smaller than the value it had * previously. * * @param s Int to remove from array * * @return True if the int was removed, false if it was not found */ private boolean removeElement(int s) { int at=indexOf(s,0); if(at<0) return false; removeElementAt(at); return true; } /** * Deletes the component at the specified index. Each component in * this vector with an index greater or equal to the specified * index is shifted downward to have an index one smaller than * the value it had previously. * * @param at index of where to remove and int */ private void removeElementAt(int at) { // No point in removing elements that "don't exist"... if(at<m_firstFree) { int index=at>>>m_SHIFT; int maxindex=m_firstFree>>>m_SHIFT; int offset=at&m_MASK; while(index<=maxindex) { int copylen=m_blocksize-offset-1; int[] block=m_map[index]; if(null==block) block=m_map[index]=new int[m_blocksize]; else System.arraycopy(block, offset+1, block, offset, copylen); if(index<maxindex) { int[] next=m_map[index+1]; if(next!=null) block[m_blocksize-1]=(next!=null) ? next[0] : 0; } else block[m_blocksize-1]=0; offset=0; ++index; } } --m_firstFree; } /** * Sets the component at the specified index of this vector to be the * specified object. The previous component at that position is discarded. * * The index must be a value greater than or equal to 0 and less * than the current size of the vector. * * @param value object to set * @param at Index of where to set the object */ public void setElementAt(int value, int at) { if(at<m_blocksize) m_map0[at]=value; else { int index=at>>>m_SHIFT; int offset=at&m_MASK; if(index>=m_map.length) { int newsize=index+m_numblocks; int[][] newMap=new int[newsize][]; System.arraycopy(m_map, 0, newMap, 0, m_map.length); m_map=newMap; } int[] block=m_map[index]; if(null==block) block=m_map[index]=new int[m_blocksize]; block[offset]=value; } if(at>=m_firstFree) m_firstFree=at+1; } /** * Get the nth element. This is often at the innermost loop of an * application, so performance is critical. * * @param i index of value to get * * @return value at given index. If that value wasn't previously set, * the result is undefined for performance reasons. It may throw an * exception (see below), may return zero, or (if setSize has previously * been used) may return stale data. * * @throws ArrayIndexOutOfBoundsException if the index was _clearly_ * unreasonable (negative, or past the highest block). * * @throws NullPointerException if the index points to a block that could * have existed (based on the highest index used) but has never had anything * set into it. * %REVIEW% Could add a catch to create the block in that case, or return 0. * Try/Catch is _supposed_ to be nearly free when not thrown to. Do we * believe that? Should we have a separate safeElementAt? */ public int elementAt(int i) { // This is actually a significant optimization! if(i<m_blocksize) return m_map0[i]; return m_map[i>>>m_SHIFT][i&m_MASK]; } /** * Tell if the table contains the given node. * * @param s object to look for * * @return true if the object is in the list */ private boolean contains(int s) { return (indexOf(s,0) >= 0); } /** * Searches for the first occurence of the given argument, * beginning the search at index, and testing for equality * using the equals method. * * @param elem object to look for * @param index Index of where to begin search * @return the index of the first occurrence of the object * argument in this vector at position index or later in the * vector; returns -1 if the object is not found. */ public int indexOf(int elem, int index) { if(index>=m_firstFree) return -1; int bindex=index>>>m_SHIFT; int boffset=index&m_MASK; int maxindex=m_firstFree>>>m_SHIFT; int[] block; for(;bindex<maxindex;++bindex) { block=m_map[bindex]; if(block!=null) for(int offset=boffset;offset<m_blocksize;++offset) if(block[offset]==elem) return offset+bindex*m_blocksize; boffset=0; // after first } // Last block may need to stop before end int maxoffset=m_firstFree&m_MASK; block=m_map[maxindex]; for(int offset=boffset;offset<maxoffset;++offset) if(block[offset]==elem) return offset+maxindex*m_blocksize; return -1; } /** * Searches for the first occurence of the given argument, * beginning the search at index, and testing for equality * using the equals method. * * @param elem object to look for * @return the index of the first occurrence of the object * argument in this vector at position index or later in the * vector; returns -1 if the object is not found. */ public int indexOf(int elem) { return indexOf(elem,0); } /** * Searches for the first occurence of the given argument, * beginning the search at index, and testing for equality * using the equals method. * * @param elem Object to look for * @return the index of the first occurrence of the object * argument in this vector at position index or later in the * vector; returns -1 if the object is not found. */ private int lastIndexOf(int elem) { int boffset=m_firstFree&m_MASK; for(int index=m_firstFree>>>m_SHIFT; index>=0; --index) { int[] block=m_map[index]; if(block!=null) for(int offset=boffset; offset>=0; --offset) if(block[offset]==elem) return offset+index*m_blocksize; boffset=0; // after first } return -1; } /** * Return the internal m_map0 array * @return the m_map0 array */ public final int[] getMap0() { return m_map0; } /** * Return the m_map double array * @return the internal map of array of arrays */ public final int[][] getMap() { return m_map; } }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?