FastVector.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 10k
Category:

Windows Develop

Development Platform:

Java

  1. /*
  2.  *    This program is free software; you can redistribute it and/or modify
  3.  *    it under the terms of the GNU General Public License as published by
  4.  *    the Free Software Foundation; either version 2 of the License, or
  5.  *    (at your option) any later version.
  6.  *
  7.  *    This program is distributed in the hope that it will be useful,
  8.  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10.  *    GNU General Public License for more details.
  11.  *
  12.  *    You should have received a copy of the GNU General Public License
  13.  *    along with this program; if not, write to the Free Software
  14.  *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15.  */
  16. /*
  17.  *    FastVector.java
  18.  *    Copyright (C) 1999 Eibe Frank
  19.  *
  20.  */
  21. package weka.core;
  22. import java.util.*;
  23. import java.io.*;
  24. /**
  25.  * Implements a fast vector class without synchronized
  26.  * methods. Replaces java.util.Vector. (Synchronized methods tend to
  27.  * be slow.)
  28.  *
  29.  * @author Eibe Frank (eibe@cs.waikato.ac.nz)
  30.  * @version $Revision: 1.7 $ */
  31. public class FastVector implements Copyable, Serializable {
  32.   /**
  33.    * Class for enumerating the vector's elements.
  34.    */
  35.   public class FastVectorEnumeration implements Enumeration {
  36.     /** The counter. */
  37.     private int m_Counter;
  38.     /** The vector. */
  39.     private FastVector m_Vector;
  40.     /** Special element. Skipped during enumeration. */
  41.     private int m_SpecialElement;
  42.     /**
  43.      * Constructs an enumeration.
  44.      *
  45.      * @param vector the vector which is to be enumerated
  46.      */
  47.     public FastVectorEnumeration(FastVector vector) {
  48.       m_Counter = 0;
  49.       m_Vector = vector;
  50.       m_SpecialElement = -1;
  51.     }
  52.     /**
  53.      * Constructs an enumeration with a special element.
  54.      * The special element is skipped during the enumeration.
  55.      *
  56.      * @param vector the vector which is to be enumerated
  57.      * @param special the index of the special element
  58.      */
  59.     public FastVectorEnumeration(FastVector vector, int special) {
  60.       m_Vector = vector;
  61.       m_SpecialElement = special;
  62.       if (special == 0) {
  63. m_Counter = 1;
  64.       } else {
  65. m_Counter = 0;
  66.       }
  67.     }
  68.     /**
  69.      * Tests if there are any more elements to enumerate.
  70.      *
  71.      * @return true if there are some elements left
  72.      */
  73.     public final boolean hasMoreElements() {
  74.       if (m_Counter < m_Vector.size()) {
  75. return true;
  76.       }
  77.       return false;
  78.     }
  79.     /**
  80.      * Returns the next element.
  81.      *
  82.      * @return the next element to be enumerated
  83.      */
  84.     public final Object nextElement() {
  85.   
  86.       Object result = m_Vector.elementAt(m_Counter);
  87.       m_Counter++;
  88.       if (m_Counter == m_SpecialElement) {
  89. m_Counter++;
  90.       }
  91.       return result;
  92.     }
  93.   }
  94.   /** The array of objects. */
  95.   private Object[] m_Objects;
  96.   /** The current size; */
  97.   private int m_Size;
  98.   /** The capacity increment */
  99.   private int m_CapacityIncrement;
  100.   
  101.   /** The capacity multiplier. */
  102.   private double m_CapacityMultiplier;
  103.   /**
  104.    * Constructs an empty vector with initial
  105.    * capacity zero.
  106.    */
  107.   public FastVector() {
  108.   
  109.     m_Objects = new Object[0];
  110.     m_Size = 0;
  111.     m_CapacityIncrement = 1;
  112.     m_CapacityMultiplier = 2;
  113.   }
  114.   /**
  115.    * Constructs a vector with the given capacity.
  116.    *
  117.    * @param capacity the vector's initial capacity
  118.    */
  119.   public FastVector(int capacity) {
  120.     m_Objects = new Object[capacity];
  121.     m_Size = 0;
  122.     m_CapacityIncrement = 1;
  123.     m_CapacityMultiplier = 2;
  124.   }
  125.   /**
  126.    * Constructs a vector with the given capacity, capacity 
  127.    * increment and capacity mulitplier.
  128.    *
  129.    * @param capacity the vector's initial capacity
  130.    */
  131.   public FastVector(int capacity, int capIncrement, 
  132.     double capMultiplier) {
  133.     m_Objects = new Object[capacity];
  134.     m_Size = 0;
  135.     m_CapacityIncrement = capIncrement;
  136.     m_CapacityMultiplier = capMultiplier;
  137.   }
  138.   /**
  139.    * Adds an element to this vector. Increases its
  140.    * capacity if its not large enough.
  141.    *
  142.    * @param element the element to add
  143.    */
  144.   public final void addElement(Object element) {
  145.     Object[] newObjects;
  146.     if (m_Size == m_Objects.length) {
  147.       newObjects = new Object[(int)m_CapacityMultiplier *
  148.      (m_Objects.length +
  149.       m_CapacityIncrement)];
  150.       System.arraycopy(m_Objects, 0, newObjects, 0, m_Size);
  151.       m_Objects = newObjects;
  152.     }
  153.     m_Objects[m_Size] = element;
  154.     m_Size++;
  155.   }
  156.   /**
  157.    * Returns the capacity of the vector.
  158.    *
  159.    * @return the capacity of the vector
  160.    */
  161.   public final int capacity() {
  162.   
  163.     return m_Objects.length;
  164.   }
  165.   /**
  166.    * Produces a shallow copy of this vector.
  167.    *
  168.    * @return the new vector
  169.    */
  170.   public final Object copy() {
  171.     FastVector copy = new FastVector(m_Objects.length, 
  172.      m_CapacityIncrement,
  173.      m_CapacityMultiplier);
  174.     copy.m_Size = m_Size;
  175.     System.arraycopy(m_Objects, 0, copy.m_Objects, 0, m_Size);
  176.     return copy;
  177.   }
  178.   /**
  179.    * Clones the vector and shallow copies all its elements.
  180.    * The elements have to implement the Copyable interface.
  181.    * 
  182.    * @return the new vector
  183.    */
  184.   public final Object copyElements() {
  185.     FastVector copy = new FastVector(m_Objects.length, 
  186.      m_CapacityIncrement,
  187.      m_CapacityMultiplier);
  188.     copy.m_Size = m_Size;
  189.     for (int i = 0; i < m_Size; i++) {
  190.       copy.m_Objects[i] = ((Copyable)m_Objects[i]).copy();
  191.     }
  192.     return copy;
  193.   }
  194.   /**
  195.    * Returns the element at the given position.
  196.    *
  197.    * @param index the element's index
  198.    * @return the element with the given index
  199.    */
  200.   public final Object elementAt(int index) {
  201.     return m_Objects[index];
  202.   }
  203.   /**
  204.    * Returns an enumeration of this vector.
  205.    *
  206.    * @return an enumeration of this vector
  207.    */
  208.   public final Enumeration elements() {
  209.   
  210.     return new FastVectorEnumeration(this);
  211.   }
  212.   /**
  213.    * Returns an enumeration of this vector, skipping the
  214.    * element with the given index.
  215.    *
  216.    * @param index the element to skip
  217.    * @return an enumeration of this vector
  218.    */
  219.   public final Enumeration elements(int index) {
  220.   
  221.     return new FastVectorEnumeration(this, index);
  222.   }
  223.   /**
  224.    * Returns the first element of the vector.
  225.    *
  226.    * @return the first element of the vector
  227.    */
  228.   public final Object firstElement() {
  229.     return m_Objects[0];
  230.   }
  231.   /**
  232.    * Searches for the first occurence of the given argument, 
  233.    * testing for equality using the equals method. 
  234.    *
  235.    * @param element the element to be found
  236.    * @return the index of the first occurrence of the argument 
  237.    * in this vector; returns -1 if the object is not found
  238.    */
  239.   public final int indexOf(Object element) {
  240.     for (int i = 0; i < m_Size; i++) {
  241.       if (element.equals(m_Objects[i])) {
  242. return i;
  243.       }
  244.     }
  245.     return -1;
  246.   }
  247.   /**
  248.    * Inserts an element at the given position.
  249.    *
  250.    * @param element the element to be inserted
  251.    * @param index the element's index
  252.    */
  253.   public final void insertElementAt(Object element, int index) {
  254.     Object[] newObjects;
  255.     if (m_Size < m_Objects.length) {
  256.       System.arraycopy(m_Objects, index, m_Objects, index + 1, 
  257.                        m_Size - index);
  258.       m_Objects[index] = element;
  259.     } else {
  260.       newObjects = new Object[(int)m_CapacityMultiplier *
  261.      (m_Objects.length +
  262.       m_CapacityIncrement)];
  263.       System.arraycopy(m_Objects, 0, newObjects, 0, index);
  264.       newObjects[index] = element;
  265.       System.arraycopy(m_Objects, index, newObjects, index + 1,
  266.        m_Size - index);
  267.       m_Objects = newObjects;
  268.     }
  269.     m_Size++;
  270.   }
  271.   /**
  272.    * Returns the last element of the vector.
  273.    *
  274.    * @return the last element of the vector
  275.    */
  276.   public final Object lastElement() {
  277.     return m_Objects[m_Size - 1];
  278.   }
  279.   /**
  280.    * Deletes an element from this vector.
  281.    *
  282.    * @param index the index of the element to be deleted
  283.    */
  284.   public final void removeElementAt(int index) {
  285.     System.arraycopy(m_Objects, index + 1, m_Objects, index, 
  286.                      m_Size - index - 1);
  287.     m_Size--;
  288.   }
  289.   /**
  290.    * Removes all components from this vector and sets its 
  291.    * size to zero. 
  292.    */
  293.   public final void removeAllElements() {
  294.     m_Objects = new Object[m_Objects.length];
  295.     m_Size = 0;
  296.   }
  297.   /**
  298.    * Appends all elements of the supplied vector to this vector.
  299.    *
  300.    * @param toAppend the FastVector containing elements to append.
  301.    */
  302.   public final void appendElements(FastVector toAppend) {
  303.     setCapacity(size() + toAppend.size());
  304.     System.arraycopy(toAppend.m_Objects, 0, m_Objects, size(), toAppend.size());
  305.     m_Size = m_Objects.length;
  306.   }
  307.   /** 
  308.    * Returns all the elements of this vector as an array
  309.    *
  310.    * @param an array containing all the elements of this vector
  311.    */
  312.   public final Object [] toArray() {
  313.     Object [] newObjects = new Object[size()];
  314.     System.arraycopy(m_Objects, 0, newObjects, 0, size());
  315.     return newObjects;
  316.   }
  317.   /**
  318.    * Sets the vector's capacity to the given value.
  319.    *
  320.    * @param capacity the new capacity
  321.    */
  322.   public final void setCapacity(int capacity) {
  323.     Object[] newObjects = new Object[capacity];
  324.    
  325.     System.arraycopy(m_Objects, 0, newObjects, 0, Math.min(capacity, m_Size));
  326.     m_Objects = newObjects;
  327.     if (m_Objects.length < m_Size)
  328.       m_Size = m_Objects.length;
  329.   }
  330.   /**
  331.    * Sets the element at the given index.
  332.    *
  333.    * @param element the element to be put into the vector
  334.    * @param index the index at which the element is to be placed
  335.    */
  336.   public final void setElementAt(Object element, int index) {
  337.     m_Objects[index] = element;
  338.   }
  339.   /**
  340.    * Returns the vector's current size.
  341.    *
  342.    * @return the vector's current size
  343.    */
  344.   public final int size() {
  345.     return m_Size;
  346.   }
  347.   /**
  348.    * Swaps two elements in the vector.
  349.    *
  350.    * @param first index of the first element
  351.    * @param second index of the second element
  352.    */
  353.   public final void swap(int first, int second) {
  354.     Object help = m_Objects[first];
  355.     m_Objects[first] = m_Objects[second];
  356.     m_Objects[second] = help;
  357.   }
  358.   /**
  359.    * Sets the vector's capacity to its size.
  360.    */
  361.   public final void trimToSize() {
  362.     Object[] newObjects = new Object[m_Size];
  363.     
  364.     System.arraycopy(m_Objects, 0, newObjects, 0, m_Size);
  365.     m_Objects = newObjects;
  366.   }
  367. }