All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.clementlevallois.utils.FindAllPairs Maven / Gradle / Ivy

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.clementlevallois.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

/**
 *
 * C.Levallois
 * @param 
 */
public class FindAllPairs> {

    private T t;

    /**
     *
     * @param setObjects
     * @return
     */
    public Set> getAllDirectedPairs(Set setObjects) {
        Set setObjectsProcessed = new HashSet();
        Set> setPairs = new HashSet();
        Iterator setObjectsIteratorA = setObjects.iterator();
        Iterator setObjectsIteratorB;
        T currTA;
        T currTB;
        while (setObjectsIteratorA.hasNext()) {
            currTA = setObjectsIteratorA.next();
            setObjectsIteratorB = setObjects.iterator();
            while (setObjectsIteratorB.hasNext()) {
                currTB = setObjectsIteratorB.next();
                if (!setObjectsProcessed.contains(currTB) && !currTA.equals(currTB)) {
                    setPairs.add(new DirectedPair(currTA, currTB));
                }
            }
            setObjectsProcessed.add(currTA);
        }
        return setPairs;

    }

    /**
     *
     * @param setSources
     * @param setTargets
     * @return
     */
    public Set> getAllDirectedPairsFromTwoSets(Set setSources, Set setTargets) {
        Set> setPairs = new TreeSet();
        Iterator setSourcesIterator = setSources.iterator();
        Iterator setTargetsIterator;
        T source;
        T target;
        while (setSourcesIterator.hasNext()) {
            source = setSourcesIterator.next();
            setTargetsIterator = setTargets.iterator();
            while (setTargetsIterator.hasNext()) {
                target = setTargetsIterator.next();
                if (!source.equals(target)) {
                    setPairs.add(new DirectedPair(source, target));
                }
            }
        }
        return setPairs;

    }

    /**
     *
     * @param setObjects
     * @return
     */
    public Set> getAllUndirectedPairs(Set setObjects) {
        Set> setPairs;
        setPairs = new HashSet();
        if (setObjects == null || setObjects.size() < 2) {
            return setPairs;
        }

        Set setObjectsProcessed = new HashSet();

        Iterator setObjectsIteratorA = setObjects.iterator();
        Iterator setObjectsIteratorB;
        T currTA;
        T currTB;

        while (setObjectsIteratorA.hasNext()) {
            currTA = setObjectsIteratorA.next();
            setObjectsIteratorB = setObjects.iterator();
            while (setObjectsIteratorB.hasNext()) {
                currTB = setObjectsIteratorB.next();
                if (!setObjectsProcessed.contains(currTB) && !currTA.equals(currTB)) {
                    setPairs.add(new UnDirectedPair(currTA, currTB));
                }
            }
            setObjectsProcessed.add(currTA);
        }
        return setPairs;
    }

    /**
     *
     * @param listObjects
     * @return
     */
    public Set> getAllUndirectedPairsFromList(List listObjects) {
        Set> setPairs;
        setPairs = new HashSet();
        if (listObjects.size() < 2) {
            return setPairs;
        }

        Set setObjectsProcessed = new HashSet();

        Iterator setObjectsIteratorA = listObjects.iterator();
        Iterator setObjectsIteratorB;
        T currTA;
        T currTB;

        while (setObjectsIteratorA.hasNext()) {
            currTA = setObjectsIteratorA.next();
            setObjectsIteratorB = listObjects.iterator();
            while (setObjectsIteratorB.hasNext()) {
                currTB = setObjectsIteratorB.next();
                if (!setObjectsProcessed.contains(currTB) && !currTA.equals(currTB)) {
                    setPairs.add(new UnDirectedPair(currTA, currTB));
                }
            }
            setObjectsProcessed.add(currTA);
        }
        return setPairs;
    }

    /**
     *
     * @param setObjects
     * @return
     */
    public List> getAllUndirectedPairsAsList(Set setObjects) {
//        Clock findingAllPairsClock = new Clock("finding all pairs in a set of "+setObjects.size()+" objects");
        List listObjects = new ArrayList();
        listObjects.addAll(setObjects);

        List> listPairs = new ArrayList();
        Iterator listIterator1 = listObjects.listIterator();
        Iterator listIterator2;
        int count = 1;
        T object1;
        while (listIterator1.hasNext()) {
            object1 = listIterator1.next();
            listIterator2 = listObjects.listIterator(count++);
            while (listIterator2.hasNext()) {
                Map pair = new HashMap();
                pair.put("left", object1);
                pair.put("right",listIterator2.next());
                listPairs.add(pair);
            }
        }
//        System.out.println("number of pairs: "+listPairs.size());
//        findingAllPairsClock.closeAndPrintClock();
        return listPairs;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy