Main Page   Packages   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

GenericSet.java

00001 package edu.ksu.cis.bandera.util;
00002 
00003 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
00004  * Bandera, a Java(TM) analysis and transformation toolkit           *
00005  * Copyright (C) 1998, 1999   Shawn Laubach (laubach@acm.org)        *
00006  * All rights reserved.                                              *
00007  *                                                                   *
00008  * This work was done as a project in the SAnToS Laboratory,         *
00009  * Department of Computing and Information Sciences, Kansas State    *
00010  * University, USA (http://www.cis.ksu.edu/santos).                  *
00011  * It is understood that any modification not identified as such is  *
00012  * not covered by the preceding statement.                           *
00013  *                                                                   *
00014  * This work is free software; you can redistribute it and/or        *
00015  * modify it under the terms of the GNU Library General Public       *
00016  * License as published by the Free Software Foundation; either      *
00017  * version 2 of the License, or (at your option) any later version.  *
00018  *                                                                   *
00019  * This work is distributed in the hope that it will be useful,      *
00020  * but WITHOUT ANY WARRANTY; without even the implied warranty of    *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU *
00022  * Library General Public License for more details.                  *
00023  *                                                                   *
00024  * You should have received a copy of the GNU Library General Public *
00025  * License along with this toolkit; if not, write to the             *
00026  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,      *
00027  * Boston, MA  02111-1307, USA.                                      *
00028  *                                                                   *
00029  * Java is a trademark of Sun Microsystems, Inc.                     *
00030  *                                                                   *
00031  * To submit a bug report, send a comment, or get the latest news on *
00032  * this project and other SAnToS projects, please visit the web-site *
00033  *                http://www.cis.ksu.edu/santos                      *
00034  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00035 import java.util.Vector;
00036 
00037 /**
00038  * Generic set class.  It is not a fully functional set class yet.  It only has
00039  * a union method.  It does not construct a new set when union is used.  All
00040  * will be changed as soon as time permits.  It is not need right now and it
00041  * should not drastically change the interface, but it might.  It is used in
00042  * wrapper classes so it shouldn't affect things very much.
00043  *</P>
00044  * All elements should be mappable.
00045  *
00046  * @author <A HREF="mailto:laubach@cis.ksu.edu">Shawn Laubach</A>
00047  *
00048  * @version 0.2
00049  */
00050 public class GenericSet
00051 {
00052   Vector set;
00053 
00054   /**
00055    * Creates a new empty set.
00056    */
00057   public GenericSet()
00058     {
00059       set = new Vector();
00060     }
00061   /** 
00062    * Creates a new set with a single element.
00063    *
00064    * @param e the single element.
00065    */
00066   public GenericSet(Object e)
00067     {
00068       set = new Vector();
00069       set.addElement(e);
00070     }
00071   public GenericSet(Vector v)
00072     {
00073       set = (Vector)v.clone();
00074     }
00075   public void addElemToSet(Object ele)
00076     {
00077       if (!set.contains(ele)) set.addElement(ele);
00078     }
00079   /**
00080    * Checks if an element is in the set.
00081    *
00082    * @param m the element
00083    *
00084    * @return True if m is an element of the set, otherwise false.
00085    */
00086   public boolean contains(Object e)
00087     {
00088       return set.contains(e);
00089     }
00090   /**
00091    * Finds the difference between two sets.
00092    *
00093    * @param s another set.
00094    */
00095   public GenericSet difference(GenericSet s)
00096     {
00097       int i;
00098       GenericSet n = new GenericSet();
00099 
00100       for (i = 0; i < set.size(); i++)
00101     if (!s.set.contains(set.elementAt(i)))
00102       n.set.addElement(set.elementAt(i));
00103 
00104       return n;
00105     }
00106   public boolean equals(GenericSet another)
00107     {
00108       int i =0;
00109       boolean includes = true;
00110       
00111       while (i<set.size() && includes)
00112     {
00113       includes = another.set.contains(set.elementAt(i));
00114       i++;
00115     }
00116 
00117       if (! includes) return false;
00118 
00119       i= 0;
00120 
00121       while (i<another.set.size() && includes)
00122     {
00123       includes = set.contains(another.set.elementAt(i));
00124       i++;
00125     }
00126       return includes;
00127     }
00128   /**
00129    * Intersects another set together.
00130    *
00131    * @param s another set.
00132    */
00133   public GenericSet intersect(GenericSet s)
00134     {
00135       int i;
00136       GenericSet n = new GenericSet();
00137 
00138       for (i = 0; i < set.size(); i++)
00139     if (s.set.contains(set.elementAt(i)))
00140       n.set.addElement(set.elementAt(i));
00141 
00142       return n;
00143     }
00144   public static void main(String args[])
00145     {
00146       String aa[] = {"a", "b", "c"};
00147       GenericSet a = new GenericSet("a");
00148       GenericSet b = new GenericSet("b");
00149       GenericSet c = a.union(b);
00150       GenericSet d = a.union(c);
00151       GenericSet e = new GenericSet("c");
00152       
00153       d.addElemToSet("dddd") ;
00154 
00155       System.out.println(a + "\n" + b + "\n" + c);
00156       System.out.println(d);
00157       System.out.println(d.union(e).intersect(b.union(e)));
00158       System.out.println(d.union(e).intersect(e.union(a)));
00159       System.out.println(d.union(e).difference(a));
00160       System.out.println(d.union(e).difference(e));
00161       System.out.println(d.union(e).difference(a.union(e)));
00162     }
00163   public void remove(Object ele)
00164     {
00165       if (set.contains(ele)) set.removeElement(ele);
00166     }
00167   /**
00168    * Gets the ith element from the set.
00169    *
00170    * @param i the element position
00171    *
00172    * @return The ith element.
00173    */
00174   public Object setRef(int i)
00175     {
00176       return set.elementAt(i);
00177     }
00178   /**
00179    * Returns the number of elements in the set.
00180    *
00181    * @return The number of elements in the set.
00182    */
00183   public int size()
00184     {
00185       return set.size();
00186     }
00187   /**
00188    * Converts the set to a string
00189    *
00190    * @return String representation of the set.
00191    */
00192   public String toString()
00193     {
00194       return set.toString();
00195     }
00196   /**
00197    * Unions another set together.
00198    *
00199    * @param s another set.
00200    */
00201   public GenericSet union(GenericSet s)
00202     {
00203       int i;
00204       GenericSet n = new GenericSet(set);
00205       
00206       for (i = 0; i < s.set.size(); i++)
00207     if (!n.contains(s.set.elementAt(i)))
00208       n.set.addElement(s.set.elementAt(i));
00209       
00210       return n;
00211     }
00212 }

Generated at Thu Feb 7 06:45:56 2002 for Bandera by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001