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

IntSet.java

00001 package ca.mcgill.sable.util;
00002 
00003 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
00004  * SableUtil, a clean room implementation of the Collection API.     *
00005  * Copyright (C) 1997, 1998 Etienne Gagnon (gagnon@sable.mcgill.ca). *
00006  * All rights reserved.                                              *
00007  *                                                                   *
00008  * This work was done as a project of the Sable Research Group,      *
00009  * School of Computer Science, McGill University, Canada             *
00010  * (http://www.sable.mcgill.ca/).  It is understood that any         *
00011  * modification not identified as such is not covered by the         *
00012  * 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 library; if not, write to the             *
00026  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,      *
00027  * Boston, MA  02111-1307, USA.                                      *
00028  *                                                                   *
00029  * To submit a bug report, send a comment, or get the latest news on *
00030  * this project and other Sable Research Group projects, please      *
00031  * visit the web site: http://www.sable.mcgill.ca/                   *
00032  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00033 
00034 /*
00035  Reference Version
00036  -----------------
00037  This is the latest official version on which this file is based.
00038  The reference version is: $SableUtilVersion: 1.11 $
00039 
00040  Change History
00041  --------------
00042  A) Notes:
00043 
00044  Please use the following template.  Most recent changes should
00045  appear at the top of the list.
00046 
00047  - Modified on [date (March 1, 1900)] by [name]. [(*) if appropriate]
00048    [description of modification].
00049 
00050  Any Modification flagged with "(*)" was done as a project of the
00051  Sable Research Group, School of Computer Science,
00052  McGill University, Canada (http://www.sable.mcgill.ca/).
00053 
00054  You should add your copyright, using the following template, at
00055  the top of this file, along with other copyrights.
00056 
00057  *                                                                   *
00058  * Modifications by [name] are                                       *
00059  * Copyright (C) [year(s)] [your name (or company)].  All rights     *
00060  * reserved.                                                         *
00061  *                                                                   *
00062 
00063  B) Changes:
00064 
00065  - Modified on October 14, 1998 by Etienne Gagnon (gagnon@sable.mcgill.ca). (*)
00066    Added the elementCount method.
00067 
00068  - Modified on July 5, 1998 by Etienne Gagnon (gagnon@sable.mcgill.ca). (*)
00069    Initial version.
00070 */
00071 
00072 /**
00073 A space efficient (internal int array) implementation of the java.util.BitSet functionality.
00074 <P>
00075 This class is useful for sparse bit sets. In addition to the methods of BitSet, this class
00076 provides a useful elements() method.
00077 */
00078 
00079 public class IntSet
00080 {
00081     private int[] elements;
00082     private int size;
00083 
00084     public IntSet()
00085     {
00086         elements = new int[0];
00087         size = 0;
00088     }
00089     private IntSet(IntSet set)
00090     {
00091         elements = set.elements();
00092         size = set.size;
00093     }
00094     public void and(IntSet set)
00095     {
00096         if(set == this)
00097         {
00098             return;
00099         }
00100 
00101         int new_size = 0;
00102         int l = 0; int r = 0;
00103         while((l < size) && (r < set.size))
00104         {
00105             if(elements[l] < set.elements[r])
00106             {
00107                 l++;
00108             }
00109             else if(elements[l] == set.elements[r])
00110             {
00111                 elements[new_size++] = elements[l++];
00112                 r++;
00113             }
00114             else
00115             {
00116                 r++;
00117             }
00118         }
00119 
00120         size = new_size;
00121     }
00122     public void clear(int  bit)
00123     {
00124         if(get(bit))
00125         {
00126             for(int i = 0; i < size; i++)
00127             {
00128                 if(bit < elements[i])
00129                 {
00130                     elements[i - 1] = elements[i];
00131                 }
00132             }
00133 
00134             size--;
00135         }
00136     }
00137     public Object clone()
00138     {
00139         return new IntSet(this);
00140     }
00141     public int elementCount()
00142     {
00143       return size;
00144     }
00145     public int[] elements()
00146     {
00147         int[] result = new int[size];
00148         System.arraycopy(elements, 0, result, 0, size);
00149         return result;
00150     }
00151     public boolean equals(Object  obj)
00152     {
00153         if(obj == null)
00154         {
00155             return false;
00156         }
00157 
00158         if(!(obj.getClass().equals(getClass())))
00159         {
00160             return false;
00161         }
00162 
00163         IntSet set = (IntSet) obj;
00164 
00165         if(size != set.size)
00166         {
00167             return false;
00168         }
00169 
00170         for(int i = 0; i < size; i++)
00171         {
00172             if(elements[i] != set.elements[i])
00173             {
00174                 return false;
00175             }
00176         }
00177 
00178         return true;
00179     }
00180     public boolean get(int  bit)
00181     {
00182         int low = 0;
00183         int high = size - 1;
00184 
00185         while(low <= high)
00186         {
00187             int middle = (low + high) / 2;
00188 
00189             if(bit < elements[middle])
00190             {
00191                 high = middle - 1;
00192             }
00193             else if(bit == elements[middle])
00194             {
00195                 return true;
00196             }
00197             else
00198             {
00199                 low = middle + 1;
00200             }
00201         }
00202 
00203         return false;
00204     }
00205     private void grow()
00206     {
00207         int[] old = elements;
00208         elements = new int[old.length * 2 + 1];
00209         System.arraycopy(old, 0, elements, 0, old.length);
00210     }
00211     public int hashCode()
00212     {
00213         int result = 0;
00214 
00215         for(int i = 0; i < size; i++)
00216         {
00217             result += elements[i];
00218         }
00219 
00220         return result;
00221     }
00222     public void or(IntSet  set)
00223     {
00224         if(set == this)
00225         {
00226             return;
00227         }
00228 
00229         int[] old = elements;
00230         elements = new int[size + set.size];
00231 
00232         int new_size = 0;
00233         int l = 0; int r = 0;
00234         while((l < size) || (r < set.size))
00235         {
00236             if((r == set.size) ||
00237                 ((l != size) && (old[l] < set.elements[r])))
00238             {
00239                 elements[new_size++] = old[l++];
00240             }
00241             else if((l == size) ||
00242                 (old[l] > set.elements[r]))
00243             {
00244                 elements[new_size++] = set.elements[r++];
00245             }
00246             else
00247             {
00248                 elements[new_size++] = old[l++];
00249                 r++;
00250             }
00251         }
00252 
00253         size = new_size;
00254     }
00255     public void set(int bit)
00256     {
00257         if(!get(bit))
00258         {
00259             if(++size > elements.length)
00260             {
00261                 grow();
00262             }
00263 
00264             for(int i = size - 1; ; i--)
00265             {
00266                 if(( i == 0) || (bit > elements[i - 1]))
00267                 {
00268                     elements[i] = bit;
00269                     break;
00270                 }
00271 
00272                 elements[i] = elements[i - 1];
00273             }
00274         }
00275     }
00276     /**
00277     Returns the size as if it was a BitSet
00278     */
00279 
00280     public int size()
00281     {
00282         if(size == 0)
00283         {
00284             return 0;
00285         }
00286 
00287         return elements[size - 1] + 1;
00288     }
00289     public String toString()
00290     {
00291         StringBuffer s = new StringBuffer();
00292 
00293         s.append("{");
00294 
00295         boolean comma = false;
00296 
00297         for(int i = 0; i < size; i++)
00298         {
00299             if(comma)
00300             {
00301                 s.append(", ");
00302             }
00303             else
00304             {
00305                 comma = true;
00306             }
00307 
00308             s.append(elements[i]);
00309         }
00310         s.append("}");
00311 
00312         return s.toString();
00313     }
00314     public void xor(IntSet  set)
00315     {
00316         if(set == this)
00317         {
00318             elements = new int[0];
00319             size = 0;
00320             return;
00321         }
00322 
00323         int[] old = elements;
00324         elements = new int[size + set.size];
00325 
00326         int new_size = 0;
00327         int l = 0; int r = 0;
00328         while((l < size) || (r < set.size))
00329         {
00330             if((r == set.size) ||
00331                 ((l != size) && (old[l] < set.elements[r])))
00332             {
00333                 elements[new_size++] = old[l++];
00334             }
00335             else if((l == size) ||
00336                 (old[l] > set.elements[r]))
00337             {
00338                 elements[new_size++] = set.elements[r++];
00339             }
00340             else
00341             {
00342                 l++;
00343                 r++;
00344             }
00345         }
00346 
00347         size = new_size;
00348     }
00349 }

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