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: NodeVector.java,v 1.2.4.1 2005/09/15 08:15:50 suresh_emailid Exp $
286N/A */
286N/Apackage com.sun.org.apache.xml.internal.utils;
286N/A
286N/Aimport java.io.Serializable;
286N/A
286N/Aimport com.sun.org.apache.xml.internal.dtm.DTM;
286N/A
286N/A/**
286N/A * A very simple table that stores a list of Nodes.
286N/A * @xsl.usage internal
286N/A */
286N/Apublic class NodeVector implements Serializable, Cloneable
286N/A{
286N/A static final long serialVersionUID = -713473092200731870L;
286N/A
286N/A /**
286N/A * Size of blocks to allocate.
286N/A * @serial
286N/A */
286N/A private int m_blocksize;
286N/A
286N/A /**
286N/A * Array of nodes this points to.
286N/A * @serial
286N/A */
286N/A private int m_map[];
286N/A
286N/A /**
286N/A * Number of nodes in this NodeVector.
286N/A * @serial
286N/A */
286N/A protected int m_firstFree = 0;
286N/A
286N/A /**
286N/A * Size of the array this points to.
286N/A * @serial
286N/A */
286N/A private int m_mapSize; // lazy initialization
286N/A
286N/A /**
286N/A * Default constructor.
286N/A */
286N/A public NodeVector()
286N/A {
286N/A m_blocksize = 32;
286N/A m_mapSize = 0;
286N/A }
286N/A
286N/A /**
286N/A * Construct a NodeVector, using the given block size.
286N/A *
286N/A * @param blocksize Size of blocks to allocate
286N/A */
286N/A public NodeVector(int blocksize)
286N/A {
286N/A m_blocksize = blocksize;
286N/A m_mapSize = 0;
286N/A }
286N/A
286N/A /**
286N/A * Get a cloned LocPathIterator.
286N/A *
286N/A * @return A clone of this
286N/A *
286N/A * @throws CloneNotSupportedException
286N/A */
286N/A public Object clone() throws CloneNotSupportedException
286N/A {
286N/A
286N/A NodeVector clone = (NodeVector) super.clone();
286N/A
286N/A if ((null != this.m_map) && (this.m_map == clone.m_map))
286N/A {
286N/A clone.m_map = new int[this.m_map.length];
286N/A
286N/A System.arraycopy(this.m_map, 0, clone.m_map, 0, this.m_map.length);
286N/A }
286N/A
286N/A return clone;
286N/A }
286N/A
286N/A /**
286N/A * Get the length of the list.
286N/A *
286N/A * @return Number of nodes in this NodeVector
286N/A */
286N/A public int size()
286N/A {
286N/A return m_firstFree;
286N/A }
286N/A
286N/A /**
286N/A * Append a Node onto the vector.
286N/A *
286N/A * @param value Node to add to the vector
286N/A */
286N/A public void addElement(int value)
286N/A {
286N/A
286N/A if ((m_firstFree + 1) >= m_mapSize)
286N/A {
286N/A if (null == m_map)
286N/A {
286N/A m_map = new int[m_blocksize];
286N/A m_mapSize = m_blocksize;
286N/A }
286N/A else
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
286N/A m_map[m_firstFree] = value;
286N/A
286N/A m_firstFree++;
286N/A }
286N/A
286N/A /**
286N/A * Append a Node onto the vector.
286N/A *
286N/A * @param value Node to add to the vector
286N/A */
286N/A public final void push(int value)
286N/A {
286N/A
286N/A int ff = m_firstFree;
286N/A
286N/A if ((ff + 1) >= m_mapSize)
286N/A {
286N/A if (null == m_map)
286N/A {
286N/A m_map = new int[m_blocksize];
286N/A m_mapSize = m_blocksize;
286N/A }
286N/A else
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, ff + 1);
286N/A
286N/A m_map = newMap;
286N/A }
286N/A }
286N/A
286N/A m_map[ff] = value;
286N/A
286N/A ff++;
286N/A
286N/A m_firstFree = ff;
286N/A }
286N/A
286N/A /**
286N/A * Pop a node from the tail of the vector and return the result.
286N/A *
286N/A * @return the node at the tail of the vector
286N/A */
286N/A public final int pop()
286N/A {
286N/A
286N/A m_firstFree--;
286N/A
286N/A int n = m_map[m_firstFree];
286N/A
286N/A m_map[m_firstFree] = DTM.NULL;
286N/A
286N/A return n;
286N/A }
286N/A
286N/A /**
286N/A * Pop a node from the tail of the vector and return the
286N/A * top of the stack after the pop.
286N/A *
286N/A * @return The top of the stack after it's been popped
286N/A */
286N/A public final int popAndTop()
286N/A {
286N/A
286N/A m_firstFree--;
286N/A
286N/A m_map[m_firstFree] = DTM.NULL;
286N/A
286N/A return (m_firstFree == 0) ? DTM.NULL : m_map[m_firstFree - 1];
286N/A }
286N/A
286N/A /**
286N/A * Pop a node from the tail of the vector.
286N/A */
286N/A public final void popQuick()
286N/A {
286N/A
286N/A m_firstFree--;
286N/A
286N/A m_map[m_firstFree] = DTM.NULL;
286N/A }
286N/A
286N/A /**
286N/A * Return the node at the top of the stack without popping the stack.
286N/A * Special purpose method for TransformerImpl, pushElemTemplateElement.
286N/A * Performance critical.
286N/A *
286N/A * @return Node at the top of the stack or null if stack is empty.
286N/A */
286N/A public final int peepOrNull()
286N/A {
286N/A return ((null != m_map) && (m_firstFree > 0))
286N/A ? m_map[m_firstFree - 1] : DTM.NULL;
286N/A }
286N/A
286N/A /**
286N/A * Push a pair of nodes into the stack.
286N/A * Special purpose method for TransformerImpl, pushElemTemplateElement.
286N/A * Performance critical.
286N/A *
286N/A * @param v1 First node to add to vector
286N/A * @param v2 Second node to add to vector
286N/A */
286N/A public final void pushPair(int v1, int v2)
286N/A {
286N/A
286N/A if (null == m_map)
286N/A {
286N/A m_map = new int[m_blocksize];
286N/A m_mapSize = m_blocksize;
286N/A }
286N/A else
286N/A {
286N/A if ((m_firstFree + 2) >= 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);
286N/A
286N/A m_map = newMap;
286N/A }
286N/A }
286N/A
286N/A m_map[m_firstFree] = v1;
286N/A m_map[m_firstFree + 1] = v2;
286N/A m_firstFree += 2;
286N/A }
286N/A
286N/A /**
286N/A * Pop a pair of nodes from the tail of the stack.
286N/A * Special purpose method for TransformerImpl, pushElemTemplateElement.
286N/A * Performance critical.
286N/A */
286N/A public final void popPair()
286N/A {
286N/A
286N/A m_firstFree -= 2;
286N/A m_map[m_firstFree] = DTM.NULL;
286N/A m_map[m_firstFree + 1] = DTM.NULL;
286N/A }
286N/A
286N/A /**
286N/A * Set the tail of the stack to the given node.
286N/A * Special purpose method for TransformerImpl, pushElemTemplateElement.
286N/A * Performance critical.
286N/A *
286N/A * @param n Node to set at the tail of vector
286N/A */
286N/A public final void setTail(int n)
286N/A {
286N/A m_map[m_firstFree - 1] = n;
286N/A }
286N/A
286N/A /**
286N/A * Set the given node one position from the tail.
286N/A * Special purpose method for TransformerImpl, pushElemTemplateElement.
286N/A * Performance critical.
286N/A *
286N/A * @param n Node to set
286N/A */
286N/A public final void setTailSub1(int n)
286N/A {
286N/A m_map[m_firstFree - 2] = n;
286N/A }
286N/A
286N/A /**
286N/A * Return the node at the tail of the vector without popping
286N/A * Special purpose method for TransformerImpl, pushElemTemplateElement.
286N/A * Performance critical.
286N/A *
286N/A * @return Node at the tail of the vector
286N/A */
286N/A public final int peepTail()
286N/A {
286N/A return m_map[m_firstFree - 1];
286N/A }
286N/A
286N/A /**
286N/A * Return the node one position from the tail without popping.
286N/A * Special purpose method for TransformerImpl, pushElemTemplateElement.
286N/A * Performance critical.
286N/A *
286N/A * @return Node one away from the tail
286N/A */
286N/A public final int peepTailSub1()
286N/A {
286N/A return m_map[m_firstFree - 2];
286N/A }
286N/A
286N/A /**
286N/A * Insert a node in order in the list.
286N/A *
286N/A * @param value Node to insert
286N/A */
286N/A public void insertInOrder(int value)
286N/A {
286N/A
286N/A for (int i = 0; i < m_firstFree; i++)
286N/A {
286N/A if (value < m_map[i])
286N/A {
286N/A insertElementAt(value, i);
286N/A
286N/A return;
286N/A }
286N/A }
286N/A
286N/A addElement(value);
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 Node to insert
286N/A * @param at Position where to insert
286N/A */
286N/A public void insertElementAt(int value, int at)
286N/A {
286N/A
286N/A if (null == m_map)
286N/A {
286N/A m_map = new int[m_blocksize];
286N/A m_mapSize = m_blocksize;
286N/A }
286N/A else 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 * Append the nodes to the list.
286N/A *
286N/A * @param nodes NodeVector to append to this list
286N/A */
286N/A public void appendNodes(NodeVector nodes)
286N/A {
286N/A
286N/A int nNodes = nodes.size();
286N/A
286N/A if (null == m_map)
286N/A {
286N/A m_mapSize = nNodes + m_blocksize;
286N/A m_map = new int[m_mapSize];
286N/A }
286N/A else if ((m_firstFree + nNodes) >= m_mapSize)
286N/A {
286N/A m_mapSize += (nNodes + 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 + nNodes);
286N/A
286N/A m_map = newMap;
286N/A }
286N/A
286N/A System.arraycopy(nodes.m_map, 0, m_map, m_firstFree, nNodes);
286N/A
286N/A m_firstFree += nNodes;
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 void removeAllElements()
286N/A {
286N/A
286N/A if (null == m_map)
286N/A return;
286N/A
286N/A for (int i = 0; i < m_firstFree; i++)
286N/A {
286N/A m_map[i] = DTM.NULL;
286N/A }
286N/A
286N/A m_firstFree = 0;
286N/A }
286N/A
286N/A /**
286N/A * Set the length to zero, but don't clear the array.
286N/A */
286N/A public void RemoveAllNoClear()
286N/A {
286N/A
286N/A if (null == m_map)
286N/A return;
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 Node to remove from the list
286N/A *
286N/A * @return True if the node was successfully removed
286N/A */
286N/A public boolean removeElement(int s)
286N/A {
286N/A
286N/A if (null == m_map)
286N/A return false;
286N/A
286N/A for (int i = 0; i < m_firstFree; i++)
286N/A {
286N/A int node = m_map[i];
286N/A
286N/A if (node == s)
286N/A {
286N/A if (i > 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] = DTM.NULL;
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 node to remove
286N/A */
286N/A public void removeElementAt(int i)
286N/A {
286N/A
286N/A if (null == m_map)
286N/A return;
286N/A
286N/A if (i > 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] = DTM.NULL;
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 node Node to set
286N/A * @param index Index of where to set the node
286N/A */
286N/A public void setElementAt(int node, int index)
286N/A {
286N/A
286N/A if (null == m_map)
286N/A {
286N/A m_map = new int[m_blocksize];
286N/A m_mapSize = m_blocksize;
286N/A }
286N/A
286N/A if(index == -1)
286N/A addElement(node);
286N/A
286N/A m_map[index] = node;
286N/A }
286N/A
286N/A /**
286N/A * Get the nth element.
286N/A *
286N/A * @param i Index of node to get
286N/A *
286N/A * @return Node at specified index
286N/A */
286N/A public int elementAt(int i)
286N/A {
286N/A
286N/A if (null == m_map)
286N/A return DTM.NULL;
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 Node to look for
286N/A *
286N/A * @return True if the given node was found.
286N/A */
286N/A public boolean contains(int s)
286N/A {
286N/A
286N/A if (null == m_map)
286N/A return false;
286N/A
286N/A for (int i = 0; i < m_firstFree; i++)
286N/A {
286N/A int node = m_map[i];
286N/A
286N/A if (node == 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 Node to look for
286N/A * @param index Index of where to start the 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 int indexOf(int elem, int index)
286N/A {
286N/A
286N/A if (null == m_map)
286N/A return -1;
286N/A
286N/A for (int i = index; i < m_firstFree; i++)
286N/A {
286N/A int node = m_map[i];
286N/A
286N/A if (node == elem)
286N/A return i;
286N/A }
286N/A
286N/A return -1;
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 Node 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 int indexOf(int elem)
286N/A {
286N/A
286N/A if (null == m_map)
286N/A return -1;
286N/A
286N/A for (int i = 0; i < m_firstFree; i++)
286N/A {
286N/A int node = m_map[i];
286N/A
286N/A if (node == elem)
286N/A return i;
286N/A }
286N/A
286N/A return -1;
286N/A }
286N/A
286N/A /**
286N/A * Sort an array using a quicksort algorithm.
286N/A *
286N/A * @param a The array to be sorted.
286N/A * @param lo0 The low index.
286N/A * @param hi0 The high index.
286N/A *
286N/A * @throws Exception
286N/A */
286N/A public void sort(int a[], int lo0, int hi0) throws Exception
286N/A {
286N/A
286N/A int lo = lo0;
286N/A int hi = hi0;
286N/A
286N/A // pause(lo, hi);
286N/A if (lo >= hi)
286N/A {
286N/A return;
286N/A }
286N/A else if (lo == hi - 1)
286N/A {
286N/A
286N/A /*
286N/A * sort a two element list by swapping if necessary
286N/A */
286N/A if (a[lo] > a[hi])
286N/A {
286N/A int T = a[lo];
286N/A
286N/A a[lo] = a[hi];
286N/A a[hi] = T;
286N/A }
286N/A
286N/A return;
286N/A }
286N/A
286N/A /*
286N/A * Pick a pivot and move it out of the way
286N/A */
286N/A int pivot = a[(lo + hi) / 2];
286N/A
286N/A a[(lo + hi) / 2] = a[hi];
286N/A a[hi] = pivot;
286N/A
286N/A while (lo < hi)
286N/A {
286N/A
286N/A /*
286N/A * Search forward from a[lo] until an element is found that
286N/A * is greater than the pivot or lo >= hi
286N/A */
286N/A while (a[lo] <= pivot && lo < hi)
286N/A {
286N/A lo++;
286N/A }
286N/A
286N/A /*
286N/A * Search backward from a[hi] until element is found that
286N/A * is less than the pivot, or lo >= hi
286N/A */
286N/A while (pivot <= a[hi] && lo < hi)
286N/A {
286N/A hi--;
286N/A }
286N/A
286N/A /*
286N/A * Swap elements a[lo] and a[hi]
286N/A */
286N/A if (lo < hi)
286N/A {
286N/A int T = a[lo];
286N/A
286N/A a[lo] = a[hi];
286N/A a[hi] = T;
286N/A
286N/A // pause();
286N/A }
286N/A
286N/A // if (stopRequested) {
286N/A // return;
286N/A // }
286N/A }
286N/A
286N/A /*
286N/A * Put the median in the "center" of the list
286N/A */
286N/A a[hi0] = a[hi];
286N/A a[hi] = pivot;
286N/A
286N/A /*
286N/A * Recursive calls, elements a[lo0] to a[lo-1] are less than or
286N/A * equal to pivot, elements a[hi+1] to a[hi0] are greater than
286N/A * pivot.
286N/A */
286N/A sort(a, lo0, lo - 1);
286N/A sort(a, hi + 1, hi0);
286N/A }
286N/A
286N/A /**
286N/A * Sort an array using a quicksort algorithm.
286N/A *
286N/A * @throws Exception
286N/A */
286N/A public void sort() throws Exception
286N/A {
286N/A sort(m_map, 0, m_firstFree - 1);
286N/A }
286N/A}