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

ArraySet.java

00001 package ca.mcgill.sable.util;
00002 
00003 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
00004  * SableUtil, a clean room implementation of the Collection API.     *
00005  * Copyright (C) 1997, 1998 Raja Vallee-Rai (kor@sable.mcgill.ca).   *
00006  * All rights reserved.                                              *
00007  *                                                                   *
00008  * Modifications by Etienne Gagnon (gagnon@sable.mcgill.ca) are      *
00009  * Copyright (C) 1998 Etienne Gagnon (gagnon@sable.mcgill.ca). All   *
00010  * rights reserved.                                                  *
00011  *                                                                   *
00012  * This work was done as a project of the Sable Research Group,      *
00013  * School of Computer Science, McGill University, Canada             *
00014  * (http://www.sable.mcgill.ca/).  It is understood that any         *
00015  * modification not identified as such is not covered by the         *
00016  * preceding statement.                                              *
00017  *                                                                   *
00018  * This work is free software; you can redistribute it and/or        *
00019  * modify it under the terms of the GNU Library General Public       *
00020  * License as published by the Free Software Foundation; either      *
00021  * version 2 of the License, or (at your option) any later version.  *
00022  *                                                                   *
00023  * This work is distributed in the hope that it will be useful,      *
00024  * but WITHOUT ANY WARRANTY; without even the implied warranty of    *
00025  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU *
00026  * Library General Public License for more details.                  *
00027  *                                                                   *
00028  * You should have received a copy of the GNU Library General Public *
00029  * License along with this library; if not, write to the             *
00030  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,      *
00031  * Boston, MA  02111-1307, USA.                                      *
00032  *                                                                   *
00033  * To submit a bug report, send a comment, or get the latest news on *
00034  * this project and other Sable Research Group projects, please      *
00035  * visit the web site: http://www.sable.mcgill.ca/                   *
00036  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00037 
00038 /*
00039  Reference Version
00040  -----------------
00041  This is the latest official version on which this file is based.
00042  The reference version is: $SableUtilVersion: 1.11 $
00043 
00044  Change History
00045  --------------
00046  A) Notes:
00047 
00048  Please use the following template.  Most recent changes should
00049  appear at the top of the list.
00050 
00051  - Modified on [date (March 1, 1900)] by [name]. [(*) if appropriate]
00052    [description of modification].
00053 
00054  Any Modification flagged with "(*)" was done as a project of the
00055  Sable Research Group, School of Computer Science,
00056  McGill University, Canada (http://www.sable.mcgill.ca/).
00057 
00058  You should add your copyright, using the following template, at
00059  the top of this file, along with other copyrights.
00060 
00061  *                                                                   *
00062  * Modifications by [name] are                                       *
00063  * Copyright (C) [year(s)] [your name (or company)].  All rights     *
00064  * reserved.                                                         *
00065  *                                                                   *
00066 
00067  B) Changes:
00068 
00069  - Modified on July 23, 1998 by Etienne Gagnon (gagnon@sable.mcgill.ca). (*)
00070    Added toArray(Object[]).
00071 
00072  - Modified on June 15, 1998 by Raja Vallee-Rai (kor@sable.mcgill.ca). (*)
00073    First release of this file.
00074 */
00075 
00076 /**
00077  * Provides an implementation of the Set object using java.util.Array
00078  */
00079 
00080 public class ArraySet extends AbstractSet
00081 {
00082     private static final int DEFAULT_SIZE = 8;
00083 
00084     private int numElements;
00085     private int maxElements;
00086     private Object[] elements;
00087 
00088     private class ArrayIterator implements Iterator
00089     {
00090         int nextIndex;
00091 
00092         ArrayIterator()
00093         {
00094             nextIndex = 0;
00095         }
00096 
00097         public boolean hasNext()
00098         {
00099             return nextIndex < numElements;
00100         }
00101 
00102         public Object next() throws NoSuchElementException
00103         {
00104             if(!(nextIndex < numElements))
00105                 throw new NoSuchElementException();
00106 
00107             return elements[nextIndex++];
00108         }
00109 
00110         public void remove() throws NoSuchElementException
00111         {
00112             if(nextIndex == 0)
00113                 throw new NoSuchElementException();
00114             else
00115             {
00116                 removeElementAt(nextIndex - 1);
00117                 nextIndex = nextIndex - 1;
00118             }
00119         }
00120     }
00121 
00122     public ArraySet()
00123     {
00124         maxElements = DEFAULT_SIZE;
00125         elements = new Object[DEFAULT_SIZE];
00126         numElements = 0;
00127     }
00128     /**
00129      * Create a set which contains the given elements.
00130      */
00131 
00132     public ArraySet(Object[] elements)
00133     {
00134         this();
00135 
00136         for(int i = 0; i < elements.length; i++)
00137             add(elements[i]);
00138     }
00139     public boolean add(Object e)
00140     {
00141         if(contains(e))
00142             return false;
00143         else
00144         {
00145             // Expand array if necessary
00146                 if(numElements == maxElements)
00147                     doubleCapacity();
00148 
00149             // Add element
00150                 elements[numElements++] = e;
00151                 return true;
00152         }
00153     }
00154     public void clear()
00155     {
00156         numElements = 0;
00157     }
00158     public boolean contains(Object obj)
00159     {
00160         for(int i = 0; i < numElements; i++)
00161             if(elements[i].equals(obj))
00162                 return true;
00163 
00164         return false;
00165     }
00166     private void doubleCapacity()
00167     {
00168         int newSize = maxElements * 2;
00169 
00170         Object[] newElements = new Object[newSize];
00171 
00172         System.arraycopy(elements, 0, newElements, 0, numElements);
00173         elements = newElements;
00174         maxElements = newSize;
00175     }
00176     public Iterator iterator()
00177     {
00178         return new ArrayIterator();
00179     }
00180     private void removeElementAt(int index)
00181     {
00182         // Handle simple case
00183             if(index  == numElements - 1)
00184             {
00185                 numElements--;
00186                 return;
00187             }
00188 
00189         // Else, shift over elements
00190             System.arraycopy(elements, index + 1, elements, index, numElements - (index + 1));
00191             numElements--;
00192     }
00193     public int size()
00194     {
00195         return numElements;
00196     }
00197     public Object[] toArray()
00198     {
00199         Object[] array = new Object[numElements];
00200 
00201         System.arraycopy(elements, 0, array, 0, numElements);
00202         return array;
00203     }
00204     public void toArray(Object[] array)
00205     {
00206         System.arraycopy(elements, 0, array, 0, numElements);
00207     }
00208 }

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