<?php
/**
 * @author Pascal MARTIN
 * 
 * Copyright (c) 2007 Pascal MARTIN
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

require_once('compare-utils.php');

$nbrIterations = 1;

$articles = getArticles();


// Astuce : Supprimez (ou rajoutez) le premier slash en début de ligne suivante pour passer d'une portion de code à l'autre
//*
$liste1 = $articles[0];
$liste2 = $articles[1];
for ($i=0 ; $i<$nbrIterations ; $i++)
{
    //$nbr1 = compare_arrays_our($liste1, $liste2);
    //$nbr2 = compare_arrays($liste1, $liste2);
    $nbr3 = compare_arrays_hash_keys($liste1, $liste2);
    $nbr4 = compare_arrays_intersect($liste1, $liste2);
    $nbr5 = compare_arrays_hash_isset($liste1, $liste2);
    $nbr6 = compare_arrays_hash_flip_isset($liste1, $liste2);
}
//var_dump($nbr1); var_dump($nbr2); var_dump($nbr3);
//var_dump($nbr4); var_dump($nbr5); var_dump($nbr6);
/*/
foreach ($articles as $liste2)
{
    foreach ($articles as $liste1)
    {
        for ($i=0 ; $i<$nbrIterations ; $i++)
        {
            compare_arrays_our($liste1, $liste2);
        }
        
        for ($i=0 ; $i<$nbrIterations ; $i++)
        {
            compare_arrays($liste1, $liste2);
        }
        
        for ($i=0 ; $i<$nbrIterations ; $i++)
        {
            compare_arrays_hash_keys($liste1, $liste2);
        }
        
        for ($i=0 ; $i<$nbrIterations ; $i++)
        {
            compare_arrays_intersect($liste1, $liste2);
        }
        
        for ($i=0 ; $i<$nbrIterations ; $i++)
        {
            compare_arrays_hash_isset($liste1, $liste2);
        }
        
        for ($i=0 ; $i<$nbrIterations ; $i++)
        {
            compare_arrays_hash_flip_isset($liste1, $liste2);
        }
    } // Fin second parcours
} // Fin premier parcours
//*/







function compare_arrays_our($liste1, $liste2)
{
    $nbr = 0;
    foreach ($liste1 as $mot1)
    {
        foreach ($liste2 as $mot2)
        {
            if ($mot1 == $mot2)
            {
                $nbr++;
            }
        }
    }
    return $nbr;
} // compare_arrays_our

function compare_arrays($liste1, $liste2)
{
    $nbr = 0;
    foreach ($liste1 as $mot1)
    {
        if (in_array($mot1, $liste2))
        {
            $nbr++;
        }
    }
    return $nbr;
} // compare_arrays

function compare_arrays_hash_keys($liste1, $liste2)
{
    $hash2 = toHash($liste2);
    $nbr = 0;
    foreach ($liste1 as $mot1)
    {
        if (array_key_exists($mot1, $hash2))
        {
            $nbr++;
        }
    }
    return $nbr;
} // compare_arrays_hash_keys

function compare_arrays_intersect($liste1, $liste2)
{
    $nbr = 0;
    $nbr = count(array_intersect($liste1, $liste2));
    return $nbr;
} // compare_arrays_intersect

function compare_arrays_hash_isset($liste1, $liste2)
{
    $hash2 = toHash($liste2);
    $nbr = 0;
    foreach ($liste1 as $mot1)
    {
        if (isset($hash2[$mot1]))
        {
            $nbr++;
        }
    }
    return $nbr;
} // compare_arrays_hash_isset

function compare_arrays_hash_flip_isset($liste1, $liste2)
{
    $hash2 = array_flip($liste2);
    $nbr = 0;
    foreach ($liste1 as $mot1)
    {
        if (isset($hash2[$mot1]))
        {
            $nbr++;
        }
    }
    return $nbr;
} // compare_arrays_hash_flip_isset

?>