286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/A/*
286N/A * Copyright 1999-2004 The Apache Software Foundation.
286N/A *
286N/A * Licensed under the Apache License, Version 2.0 (the "License");
286N/A * you may not use this file except in compliance with the License.
286N/A * You may obtain a copy of the License at
286N/A *
286N/A * http://www.apache.org/licenses/LICENSE-2.0
286N/A *
286N/A * Unless required by applicable law or agreed to in writing, software
286N/A * distributed under the License is distributed on an "AS IS" BASIS,
286N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
286N/A * See the License for the specific language governing permissions and
286N/A * limitations under the License.
286N/A */
286N/A/*
286N/A * $Id: IntVector.java,v 1.2.4.1 2005/09/15 08:15:45 suresh_emailid Exp $
286N/A */
286N/Apackage com.sun.org.apache.xml.internal.utils;
286N/A
286N/A/**
286N/A * A very simple table that stores a list of int.
286N/A *
286N/A * This version is based on a "realloc" strategy -- a simle array is
286N/A * used, and when more storage is needed, a larger array is obtained
286N/A * and all existing data is recopied into it. As a result, read/write
286N/A * access to existing nodes is O(1) fast but appending may be O(N**2)
286N/A * slow. See also SuballocatedIntVector.
286N/A * @xsl.usage internal
286N/A */
286N/Apublic class IntVector implements Cloneable
286N/A{
286N/A
286N/A /** Size of blocks to allocate */
286N/A protected int m_blocksize;
286N/A
286N/A /** Array of ints */
286N/A protected int m_map[]; // IntStack is trying to see this directly
286N/A
286N/A /** Number of ints in array */
286N/A protected int m_firstFree = 0;
286N/A
286N/A /** Size of array */
286N/A protected int m_mapSize;
286N/A
286N/A /**
286N/A * Default constructor. Note that the default
286N/A * block size is very small, for small lists.
286N/A */
286N/A public IntVector()
286N/A {
286N/A
286N/A m_blocksize = 32;
286N/A m_mapSize = m_blocksize;
286N/A m_map = new int[m_blocksize];
286N/A }
286N/A
286N/A /**
286N/A * Construct a IntVector, using the given block size.
286N/A *
286N/A * @param blocksize Size of block to allocate
286N/A */
286N/A public IntVector(int blocksize)
286N/A {
286N/A
286N/A m_blocksize = blocksize;
286N/A m_mapSize = blocksize;
286N/A m_map = new int[blocksize];
286N/A }
286N/A
286N/A /**
286N/A * Construct a IntVector, using the given block size.
286N/A *
286N/A * @param blocksize Size of block to allocate
286N/A */
286N/A public IntVector(int blocksize, int increaseSize)
286N/A {
286N/A
286N/A m_blocksize = increaseSize;
286N/A m_mapSize = blocksize;
286N/A m_map = new int[blocksize];
286N/A }
286N/A
286N/A /**
286N/A * Copy constructor for IntVector
286N/A *
286N/A * @param v Existing IntVector to copy
286N/A */
286N/A public IntVector(IntVector v)
286N/A {
286N/A m_map = new int[v.m_mapSize];
286N/A m_mapSize = v.m_mapSize;
286N/A m_firstFree = v.m_firstFree;
286N/A m_blocksize = v.m_blocksize;
286N/A System.arraycopy(v.m_map, 0, m_map, 0, m_firstFree);
286N/A }
286N/A
286N/A /**
286N/A * Get the length of the list.
286N/A *
286N/A * @return length of the list
286N/A */
286N/A public final int size()
286N/A {
286N/A return m_firstFree;
286N/A }
286N/A
286N/A /**
286N/A * Get the length of the list.
286N/A *
286N/A * @return length of the list
286N/A */
286N/A public final void setSize(int sz)
286N/A {
286N/A m_firstFree = sz;
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Append a int onto the vector.
286N/A *
286N/A * @param value Int to add to the list
286N/A */
286N/A public final void addElement(int value)
286N/A {
286N/A
286N/A if ((m_firstFree + 1) >= m_mapSize)
286N/A {
286N/A m_mapSize += m_blocksize;
286N/A
286N/A int newMap[] = new int[m_mapSize];
286N/A
286N/A System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
286N/A
286N/A m_map = newMap;
286N/A }
286N/A
286N/A m_map[m_firstFree] = value;
286N/A
286N/A m_firstFree++;
286N/A }
286N/A
286N/A /**
286N/A * Append several int values onto the vector.
286N/A *
286N/A * @param value Int to add to the list
286N/A */
286N/A public final void addElements(int value, int numberOfElements)
286N/A {
286N/A
286N/A if ((m_firstFree + numberOfElements) >= m_mapSize)
286N/A {
286N/A m_mapSize += (m_blocksize+numberOfElements);
286N/A
286N/A int newMap[] = new int[m_mapSize];
286N/A
286N/A System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
286N/A
286N/A m_map = newMap;
286N/A }
286N/A
286N/A for (int i = 0; i < numberOfElements; i++)
286N/A {
286N/A m_map[m_firstFree] = value;
286N/A m_firstFree++;
286N/A }
286N/A }
286N/A
286N/A /**
286N/A * Append several slots onto the vector, but do not set the values.
286N/A *
286N/A * @param numberOfElements Int to add to the list
286N/A */
286N/A public final void addElements(int numberOfElements)
286N/A {
286N/A
286N/A if ((m_firstFree + numberOfElements) >= m_mapSize)
286N/A {
286N/A m_mapSize += (m_blocksize+numberOfElements);
286N/A
286N/A int newMap[] = new int[m_mapSize];
286N/A
286N/A System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
286N/A
286N/A m_map = newMap;
286N/A }
286N/A
286N/A m_firstFree += numberOfElements;
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Inserts the specified node in this vector at the specified index.
286N/A * Each component in this vector with an index greater or equal to
286N/A * the specified index is shifted upward to have an index one greater
286N/A * than the value it had previously.
286N/A *
286N/A * @param value Int to insert
286N/A * @param at Index of where to insert
286N/A */
286N/A public final void insertElementAt(int value, int at)
286N/A {
286N/A
286N/A if ((m_firstFree + 1) >= m_mapSize)
286N/A {
286N/A m_mapSize += m_blocksize;
286N/A
286N/A int newMap[] = new int[m_mapSize];
286N/A
286N/A System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
286N/A
286N/A m_map = newMap;
286N/A }
286N/A
286N/A if (at <= (m_firstFree - 1))
286N/A {
286N/A System.arraycopy(m_map, at, m_map, at + 1, m_firstFree - at);
286N/A }
286N/A
286N/A m_map[at] = value;
286N/A
286N/A m_firstFree++;
286N/A }
286N/A
286N/A /**
286N/A * Inserts the specified node in this vector at the specified index.
286N/A * Each component in this vector with an index greater or equal to
286N/A * the specified index is shifted upward to have an index one greater
286N/A * than the value it had previously.
286N/A */
286N/A public final void removeAllElements()
286N/A {
286N/A
286N/A for (int i = 0; i < m_firstFree; i++)
286N/A {
286N/A m_map[i] = java.lang.Integer.MIN_VALUE;
286N/A }
286N/A
286N/A m_firstFree = 0;
286N/A }
286N/A
286N/A /**
286N/A * Removes the first occurrence of the argument from this vector.
286N/A * If the object is found in this vector, each component in the vector
286N/A * with an index greater or equal to the object's index is shifted
286N/A * downward to have an index one smaller than the value it had
286N/A * previously.
286N/A *
286N/A * @param s Int to remove from array
286N/A *
286N/A * @return True if the int was removed, false if it was not found
286N/A */
286N/A public final boolean removeElement(int s)
286N/A {
286N/A
286N/A for (int i = 0; i < m_firstFree; i++)
286N/A {
286N/A if (m_map[i] == s)
286N/A {
286N/A if ((i + 1) < m_firstFree)
286N/A System.arraycopy(m_map, i + 1, m_map, i - 1, m_firstFree - i);
286N/A else
286N/A m_map[i] = java.lang.Integer.MIN_VALUE;
286N/A
286N/A m_firstFree--;
286N/A
286N/A return true;
286N/A }
286N/A }
286N/A
286N/A return false;
286N/A }
286N/A
286N/A /**
286N/A * Deletes the component at the specified index. Each component in
286N/A * this vector with an index greater or equal to the specified
286N/A * index is shifted downward to have an index one smaller than
286N/A * the value it had previously.
286N/A *
286N/A * @param i index of where to remove and int
286N/A */
286N/A public final void removeElementAt(int i)
286N/A {
286N/A
286N/A if (i > m_firstFree)
286N/A System.arraycopy(m_map, i + 1, m_map, i, m_firstFree);
286N/A else
286N/A m_map[i] = java.lang.Integer.MIN_VALUE;
286N/A
286N/A m_firstFree--;
286N/A }
286N/A
286N/A /**
286N/A * Sets the component at the specified index of this vector to be the
286N/A * specified object. The previous component at that position is discarded.
286N/A *
286N/A * The index must be a value greater than or equal to 0 and less
286N/A * than the current size of the vector.
286N/A *
286N/A * @param value object to set
286N/A * @param index Index of where to set the object
286N/A */
286N/A public final void setElementAt(int value, int index)
286N/A {
286N/A m_map[index] = value;
286N/A }
286N/A
286N/A /**
286N/A * Get the nth element.
286N/A *
286N/A * @param i index of object to get
286N/A *
286N/A * @return object at given index
286N/A */
286N/A public final int elementAt(int i)
286N/A {
286N/A return m_map[i];
286N/A }
286N/A
286N/A /**
286N/A * Tell if the table contains the given node.
286N/A *
286N/A * @param s object to look for
286N/A *
286N/A * @return true if the object is in the list
286N/A */
286N/A public final boolean contains(int s)
286N/A {
286N/A
286N/A for (int i = 0; i < m_firstFree; i++)
286N/A {
286N/A if (m_map[i] == s)
286N/A return true;
286N/A }
286N/A
286N/A return false;
286N/A }
286N/A
286N/A /**
286N/A * Searches for the first occurence of the given argument,
286N/A * beginning the search at index, and testing for equality
286N/A * using the equals method.
286N/A *
286N/A * @param elem object to look for
286N/A * @param index Index of where to begin search
286N/A * @return the index of the first occurrence of the object
286N/A * argument in this vector at position index or later in the
286N/A * vector; returns -1 if the object is not found.
286N/A */
286N/A public final int indexOf(int elem, int index)
286N/A {
286N/A
286N/A for (int i = index; i < m_firstFree; i++)
286N/A {
286N/A if (m_map[i] == elem)
286N/A return i;
286N/A }
286N/A
286N/A return java.lang.Integer.MIN_VALUE;
286N/A }
286N/A
286N/A /**
286N/A * Searches for the first occurence of the given argument,
286N/A * beginning the search at index, and testing for equality
286N/A * using the equals method.
286N/A *
286N/A * @param elem object to look for
286N/A * @return the index of the first occurrence of the object
286N/A * argument in this vector at position index or later in the
286N/A * vector; returns -1 if the object is not found.
286N/A */
286N/A public final int indexOf(int elem)
286N/A {
286N/A
286N/A for (int i = 0; i < m_firstFree; i++)
286N/A {
286N/A if (m_map[i] == elem)
286N/A return i;
286N/A }
286N/A
286N/A return java.lang.Integer.MIN_VALUE;
286N/A }
286N/A
286N/A /**
286N/A * Searches for the first occurence of the given argument,
286N/A * beginning the search at index, and testing for equality
286N/A * using the equals method.
286N/A *
286N/A * @param elem Object to look for
286N/A * @return the index of the first occurrence of the object
286N/A * argument in this vector at position index or later in the
286N/A * vector; returns -1 if the object is not found.
286N/A */
286N/A public final int lastIndexOf(int elem)
286N/A {
286N/A
286N/A for (int i = (m_firstFree - 1); i >= 0; i--)
286N/A {
286N/A if (m_map[i] == elem)
286N/A return i;
286N/A }
286N/A
286N/A return java.lang.Integer.MIN_VALUE;
286N/A }
286N/A
286N/A /**
286N/A * Returns clone of current IntVector
286N/A *
286N/A * @return clone of current IntVector
286N/A */
286N/A public Object clone()
286N/A throws CloneNotSupportedException
286N/A {
286N/A return new IntVector(this);
286N/A }
286N/A
286N/A}