<?php

class schulze
{
	public int $nvotant;
	public int $ncandidats = 5;
	public array $nvotes;

	public array $v = array(
		array(5, 1, 3, 2, 4),
		array(1, 5, 2, 4, 3),
		array(5, 3, 2, 4, 1),
		array(5, 1, 3, 2, 4),
		array(5, 3, 2, 4, 1),
	);

	function stringify($votes)
	{
		$r = array();
		foreach($votes as $vote)
		{
			$r[] = implode("/", $vote);
		}
		return $r;
	}

	function votify(&$v)
	{
		$votes = array_flip($v);
		foreach($votes as $vote)
		{
			$vote = explode("/", $vote);
		}
	}

	function callBack($votes)
	{
		$mvotes = $this->matrix($votes);
		$rvotes = $this->result($mvotes);
		print_r($p);
	}

	function matrix($v)
	{
		$w = array();
		$i = 0;
		$j = 0;
		$v = $this->stringify($v);
		sort($v);
		print_r($v);
		for($i = 0;$i < count($v) - 1;$i++)
		{
			$s = $v[$i];
			echo $i . " => " . $s . "==>" . $v[$i+1];  "\n";
			if ($i == 0)
			{
				echo "i = 0\n";
				$w[$s] = 1;
			}
			if ($s == $v[$i+1])
			{
				echo "egal\n";
				$w[$s] += 1;
				print_r($w);

			}else
			{
				echo "non egal\n";
				$w[$v[$i+1]] = 1;
				print_r($w);
			}
		}
		$w = $this->votify($w);
		print_r($w);
		return $w;
	}

	function paires($w)
	{
		foreach($w as $votes)
		{

		}
	}

	function result($d)
	{
		$ncandidats = $this->ncandidats;
		for ($i=1;$i<=$ncandidats;$i++)
		{
			for ($j=1;$j<=$ncandidats;$j++)
			{
				if ($d[$i][$j] > $d[$j][$i])
				{
					$p[$i][$j] = $d[$i][$j];
				}else
				{
					$p[$i][$j] = 0;
				}
			}
		}
		for ($i=1;$i<=$ncandidats;$i++)
		{
			for ($j=1;$j<=$ncandidats;$j++)
			{
				for ($k=1;$k<=$ncandidats;$k++)
				{
					if ($j != $k)
					{
						$p[$j][$k] = max($p[$j][$k], min($p[$j][$i],$p[$i][$k]));
					}
				}
			}
		}
		return $p;
	}
}
?>
