<commit_before>package Union;

import java.util.Collection;
import java.util.Vector;

/**
 * Class is a set data structure.
 * The data structure has a <i>representative</i>.
 * The most useful methods there are :
 *  - find to determinate if the data structure contains an element or not
 *  - union to join to classes
 * Class is in fact a tree, implemented in tables.
 * @author <NAME>
 * @version 0.1 14 nov. 2014
 */
public class UnionFind<T>
{
    /**
     * The tags table give element value.
     */
    private Vector<T> tags;
    /** The fathers table give for the same index number in tag the father
     * of a element in the tree.
     * If a element is his own father, then it's the root of tree.
     */
    private Vector<Integer> fathers;

    /**
     * Constructor
     * When creating a class, you need at least one element
     * @param elements The classes in the set
     */
    public UnionFind( Collection<T> elements )
    {
        tags = new Vector<T>();
        fathers = new Vector<Integer>();
        int i = 0;
        for(T element : elements) {
            tags.add(i , element);
            fathers.add(i ,i);
            ++i;
        }
    }

    /**
     * Constructor
     * When creating a class, you need at least one element
     * @param elements The classes in a tabular
     */
    public UnionFind( T[] elements )
    {
        tags = new Vector<T>();
        fathers = new Vector<Integer>();
        int i = 0;
        for(T element : elements)
        {
            tags.add( i , element );
            fathers.add( i ,i );
            ++i;
        }
    }

    /**
     * Is an element in the class ?
     * @param element element to test
     * @return true if the element is here else false
     */
    public boolean contains( T element )
    {
        return -1 != tags.indexOf( element );
    }


    /**
     * Who is the <i>representative</i> of a class ?
     * @param element Element to get his representative
     * @return The <i>representative</i> of the class which contains element
     */
    public T find( T element )
    {
        try
        {
            int idx = tags.indexOf( element );
            // si pas à la racine de l'arbre
            if ( !( fathers.get( idx ).equals( idx ) ) )
            {
                // compresser le chemin
                // remplacer le père de l'élément par son grand-père
                fathers.set( tags.indexOf( element ), tags.indexOf( find( tags.elementAt( fathers.elementAt( idx ) ) ) ) );
            }
            // retourner le père de l'élément, qui est forcément la racine
             return tags.elementAt( fathers.elementAt( idx ) );
        }
        catch( ArrayIndexOutOfBoundsException ex )
        {
            System.err.println( ex );
            return element;
        }
    }

    /**
     * Join the classes in which arg1 and arg2 belong to
     * @param arg1 an element of the first class to join
     * @param arg2 an element of the second class to join
     */
    public void union( T arg1, T arg2 )
    {
        T representative1 = find( arg1 );
        T representative2 = find( arg2 );
        try
        {
            fathers.setElementAt( tags.indexOf( representative2 ),
                                fathers.elementAt( tags.indexOf( representative1 ) ) );
        }
        catch( ArrayIndexOutOfBoundsException ex)
        {
            System.err.println( ex );<commit_msg>tested and functionnal union-find<commit_after>package Union;

import java.util.Collection;
import java.util.Vector;

/**
 * Class is a set data structure.
 * The data structure has a <i>representative</i>.
 * The most useful methods there are :
 *  - find to determinate if the data structure contains an element or not
 *  - union to join to classes
 * Class is in fact a tree, implemented in tables.
 * @author <NAME>
 * @version 0.1 14 nov. 2014
 */
public class UnionFind<T>
{
    /**
     * The tags table give element value.
     */
    private Vector<T> tags;
    /** The fathers table give for the same index number in tag the father
     * of a element in the tree.
     * If a element is his own father, then it's the root of tree.
     */
    private Vector<Integer> fathers;

    /**
     * Constructor
     * When creating a class, you need at least one element
     * @param elements The classes in the set
     */
    public UnionFind( Collection<T> elements )
    {
        tags = new Vector<T>();
        fathers = new Vector<Integer>();
        int i = 0;
        for(T element : elements) {
            tags.add(i , element);
            fathers.add(i ,i);
            ++i;
        }
    }

    /**
     * Constructor
     * When creating a class, you need at least one element
     * @param elements The classes in a tabular
     */
    public UnionFind( T[] elements )
    {
        tags = new Vector<T>();
        fathers = new Vector<Integer>();
        int i = 0;
        for(T element : elements)
        {
            tags.add( i , element );
            fathers.add( i ,i );
            ++i;
        }
    }

    /**
     * Is an element in the class ?
     * @param element element to test
     * @return true if the element is here else false
     */
    public boolean contains( T element )
    {
        return -1 != tags.indexOf( element );
    }


    /**
     * Who is the <i>representative</i> of a class ?
     * @param element Element to get his representative
     * @return The <i>representative</i> of the class which contains element
     */
    public T find( T element )
    {
        try
        {
            int idx = tags.indexOf( element );
            // si pas à la racine de l'arbre
            if ( !( fathers.get( idx ).equals( idx ) ) )
            {
                // compresser le chemin
                // remplacer le père de l'élément par son grand-père
                fathers.set( tags.indexOf( element ), tags.indexOf( find( tags.elementAt( fathers.elementAt( idx ) ) ) ) );
            }
            // retourner le père de l'élément, qui est forcément la racine
             return tags.elementAt( fathers.elementAt( idx ) );
        }
        catch( ArrayIndexOutOfBoundsException ex )
        {
            System.err.println( ex );
            return element;
        }
    }

    /**
     * Join the classes in which arg1 and arg2 belong to
     * @param arg1 an element of the first class to join
     * @param arg2 an element of the second class to join
     */
    public void union( T arg1, T arg2 )
    {
        T representative1 = find( arg1 );
        T representative2 = find( arg2 );
        try
        {
            fathers.setElementAt( tags.indexOf( representative1 ),
                                fathers.elementAt( tags.indexOf( representative2 ) ) );
        }
        catch( ArrayIndexOutOfBoundsException ex)
        {
            System.err.println( ex );
{'fraction_non_alphanumeric': 0.09496800465386852, 'fraction_numerical': 0.006689936009307737, 'mean_word_length': 1.7235643564356435, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '629162', 'n_tokens_mistral': 1938, 'n_tokens_neox': 1886, 'n_words': 713}