1 | <?php |
||
18 | use CondorcetVersion; |
||
19 | |||
20 | // Implement ArrayAccess |
||
21 | public function offsetSet($offset, $value) : void {} |
||
22 | |||
23 | public function offsetExists($offset) : bool { |
||
24 | return isset($this->_Pairwise[$offset]); |
||
25 | } |
||
26 | |||
27 | public function offsetUnset($offset) : void {} |
||
28 | |||
29 | 38 | public function offsetGet($offset) : ?array { |
|
30 | 38 | return $this->_Pairwise[$offset] ?? null; |
|
31 | } |
||
32 | |||
33 | |||
34 | // Implement Iterator |
||
35 | private $valid = true; |
||
36 | |||
37 | 92 | public function rewind() : void { |
|
38 | 92 | reset($this->_Pairwise); |
|
39 | 92 | $this->valid = true; |
|
40 | 92 | } |
|
41 | |||
42 | 92 | public function current() : array { |
|
43 | 92 | return $this->_Pairwise[$this->key()]; |
|
44 | } |
||
45 | |||
46 | 92 | public function key() : ?int { |
|
47 | 92 | return key($this->_Pairwise); |
|
48 | } |
||
49 | |||
50 | 92 | public function next() : void { |
|
51 | 92 | if (next($this->_Pairwise) === false) : |
|
52 | 70 | $this->valid = false; |
|
53 | endif; |
||
54 | 92 | } |
|
55 | |||
56 | 92 | public function valid() : bool { |
|
57 | 92 | return $this->valid; |
|
58 | } |
||
59 | |||
60 | |||
61 | // Pairwise |
||
62 | |||
63 | protected $_Election; |
||
64 | protected $_Pairwise_Model; |
||
65 | protected $_Pairwise; |
||
66 | |||
67 | 95 | public function __construct (Election $link) |
|
68 | { |
||
69 | 95 | $this->setElection($link); |
|
70 | 95 | $this->formatNewpairwise(); |
|
71 | 95 | $this->doPairwise(); |
|
72 | 95 | } |
|
73 | |||
74 | 1 | public function __clone () |
|
78 | |||
79 | 95 | public function setElection (Election $election) : void |
|
83 | |||
84 | 6 | public function addNewVote (int $key) : void |
|
85 | { |
||
86 | 6 | new Timer_Chrono ( $this->_Election->getTimerManager(), 'Add Vote To Pairwise' ); |
|
87 | |||
88 | 6 | $this->computeOneVote($this->_Pairwise,$this->_Election->getVotesManager()[$key]); |
|
89 | 6 | } |
|
90 | |||
91 | 4 | public function removeVotes (int $key) : void |
|
92 | { |
||
93 | 4 | new Timer_Chrono ( $this->_Election->getTimerManager(), 'Remove Vote To Pairwise' ); |
|
94 | |||
107 | |||
108 | 2 | public function getExplicitPairwise () : array |
|
128 | |||
129 | 95 | protected function formatNewpairwise () : void |
|
149 | |||
150 | 95 | protected function doPairwise () : void |
|
161 | |||
162 | 95 | protected function computeOneVote (array &$pairwise, Vote $oneVote) : void |
|
163 | { |
||
164 | 95 | $vote_ranking = $oneVote->getContextualRanking($this->_Election); |
|
165 | |||
166 | 95 | $voteWeight = $this->_Election->isVoteWeightAllowed() ? $oneVote->getWeight() : 1; |
|
167 | |||
168 | 95 | $vote_candidate_list = []; |
|
169 | |||
170 | 95 | foreach ($vote_ranking as $rank) : |
|
171 | 95 | foreach ($rank as $oneCandidate) : |
|
172 | 95 | $vote_candidate_list[] = $oneCandidate; |
|
173 | endforeach; |
||
174 | endforeach; |
||
175 | |||
176 | 95 | $done_Candidates = []; |
|
177 | |||
178 | 95 | foreach ($vote_ranking as $candidates_in_rank) : |
|
179 | |||
180 | 95 | $candidates_in_rank_keys = []; |
|
181 | |||
182 | 95 | foreach ($candidates_in_rank as $candidate) : |
|
183 | 95 | $candidates_in_rank_keys[] = $this->_Election->getCandidateKey($candidate); |
|
184 | endforeach; |
||
185 | |||
186 | 95 | foreach ($candidates_in_rank as $candidate) : |
|
187 | |||
188 | 95 | $candidate_key = $this->_Election->getCandidateKey($candidate); |
|
189 | |||
190 | // Process |
||
191 | 95 | foreach ( $vote_candidate_list as $g_Candidate ) : |
|
192 | |||
193 | 95 | $g_candidate_key = $this->_Election->getCandidateKey($g_Candidate); |
|
194 | |||
195 | 95 | if ($candidate_key === $g_candidate_key) : |
|
196 | 95 | continue; |
|
197 | endif; |
||
198 | |||
221 |