package com.knecon.fforesight.service.layoutparser.processor.docstrum.model;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

public class UnionFind<T> extends org.jgrapht.alg.util.UnionFind<T> {

    public UnionFind(Set<T> elements) {

        super(elements);
    }


    public Collection<Set<T>> getGroups() {

        Map<T, Set<T>> setRep = new LinkedHashMap<>();
        for (T t : getParentMap().keySet()) {
            T representative = find(t);
            if (!setRep.containsKey(representative)) {
                setRep.put(representative, new LinkedHashSet<>());
            }
            setRep.get(representative).add(t);
        }

        return setRep.values();
    }

}
