001    /*
002     * Created on Jun 9, 2005
003     *
004     */
005    package aima.util;
006    
007    import java.util.HashSet;
008    import java.util.Iterator;
009    import java.util.Set;
010    
011    /**
012     * @author Ravi Mohan
013     * 
014     */
015    
016    public class SetOps<T> {
017            public Set<T> union(Set<T> one, Set<T> two) {
018                    Set<T> union = new HashSet<T>(one);
019                    union.addAll(two);
020                    return union;
021            }
022    
023            public Set<T> intersection(Set<T> one, Set<T> two) {
024                    Set<T> intersection = new HashSet<T>(one);
025                    intersection.retainAll(two);
026                    return intersection;
027            }
028    
029            public Set<T> difference(Set<T> one, Set<T> two) {
030                    Set<T> three = new HashSet<T>();
031                    Iterator<T> iteratorOne = one.iterator();
032                    while (iteratorOne.hasNext()) {
033                            T sym = iteratorOne.next();
034                            if (!(in(two, sym))) {
035                                    three.add(sym);
036                            }
037                    }
038                    return three;
039            }
040    
041            public boolean in(Set<T> s, T o) {
042    
043                    Iterator<T> i = s.iterator();
044                    while (i.hasNext()) {
045                            Object obj = i.next();
046                            if (obj.equals(o)) {
047                                    return true;
048                            }
049                    }
050                    return false;
051            }
052    
053    }