All pastes #791412 Raw Edit

Something

public text v1 · immutable
·published 2007-11-20 21:38 UTC
rendered paste body


import java.util.*;
import de.tu_darmstadt.es.sopra.exercises.*;



public class Solution06 extends UnionFind {

    Vector<HashSet<java.lang.Object>> h = new Vector<HashSet<Object>>();
    public void makePartition(java.lang.Object obj) {

        if (obj != null) {
            HashSet<Object> h2 = new HashSet<Object>();
            h2.add(obj);
            h.add(h2);
        }
    }
    
    public java.util.Collection<Object> find(java.lang.Object obj) {
        if (obj != null) {
            Enumeration<HashSet<Object>> i = h.elements();
            while (i.hasMoreElements()) {
                HashSet<Object> hs = i.nextElement();
                Iterator<Object> it = hs.iterator();
                while (it.hasNext()) {
                    if (it.next() == obj) {
                        return hs;
                    }
                }
            }
        }

        return null;
    }

    public int getNrObjects() {
        int i = 0;
        Enumeration<HashSet<Object>> e = h.elements();
        while (e.hasMoreElements()) {
            try {
                System.out.println("getNrObjects : " + h.get(i));
            } catch (Exception exception) {
            }
            i += e.nextElement().size();
        }
        return i;
    }

    public int getNrPartitions() {
        return h.size();
    }

    public void merge(java.lang.Object obj1, java.lang.Object obj2) {
        if (obj1 != null && obj2 != null) {
            HashSet<Object> g = new HashSet<Object>();
            if (obj1 instanceof HashSet && obj2 instanceof HashSet == false) {
                g = (HashSet<Object>) obj2;
                g.add(obj2);
                h.add(g);                
                
            }

            if (obj1 instanceof HashSet == false && obj2 instanceof HashSet) {
                g = (HashSet<Object>) obj2;
                g.add(obj1);
                h.add(g);  
                
            }

            if (obj1 instanceof HashSet == false && obj2 instanceof HashSet == false) {
                g.add(obj1);
                g.add(obj2);
                h.add(g);                
                
                
            }

            if (obj1 instanceof HashSet && obj2 instanceof HashSet) {
                g = (HashSet<Object>) obj2;
                HashSet<Object> h1 = (HashSet<Object>) obj1;
                g.addAll(h1);
                h.add(g);
                
            }
            h.removeElementAt(h.indexOf(obj1)+1);  
        }
    }

    /**
    #
     * Returns information of the team.<p>
    #
     *
    #
     * @return   text with team information
    #
     * @since   08.11.2006
    #
     */

    public String getTeam() {

        return "Team4";
    }

/**
    #
     * Returns authors of this solution.<p>
    #
     *
    #
     * @return  String array with author names
    #
     * @since   08.11.2006
    #
     */

    public String[] getAuthors() {

        String[] Authors = {"Ben Ghozi", "Mahjoub", "Dhif", "Al Gauabra"};
        return Authors;
    }

/**
    #
     * Returns version of this implementation.<p>
    #
     *
    #
     * @return  version as text
    #
     * @since   08.11.2006
    #
     */

    public String getVersion() {

        return "3.11.07";
    }



    public static void main(String[] args) {
        Object EINS = "EINS";
        Object ZWEI = "ZWEI";
        Object DREI = "DREI";
        Object VIER = "VIER";
        Object FUENF = "FUENF";
        Solution06 uf = new Solution06();
        uf.makePartition(EINS);
        uf.makePartition(ZWEI);
        uf.makePartition(DREI);
        uf.makePartition(VIER);
        uf.makePartition(FUENF);
        uf.merge(DREI, ZWEI);
        uf.merge(FUENF, VIER);
        Collection p1 = uf.find(EINS);
        Collection p2 = uf.find(FUENF);
        uf.merge(FUENF, EINS);
        System.out.println("partition : " + uf.getNrPartitions() + " objects : " + uf.getNrObjects());     
    }
}