1 | <?php |
||
18 | class Pairwise implements \ArrayAccess, \Iterator |
||
19 | { |
||
20 | use CondorcetVersion; |
||
21 | |||
22 | // Implement ArrayAccess |
||
23 | public function offsetSet($offset, $value) : void {} |
||
24 | |||
25 | public function offsetExists($offset) : bool { |
||
28 | |||
29 | public function offsetUnset($offset) : void {} |
||
30 | |||
31 | 35 | public function offsetGet($offset) : ?array { |
|
34 | |||
35 | |||
36 | // Implement Iterator |
||
37 | private $valid = true; |
||
38 | |||
39 | 84 | public function rewind() : void { |
|
43 | |||
44 | 84 | public function current() : array { |
|
47 | |||
48 | 84 | public function key() : ?int { |
|
51 | |||
52 | 84 | public function next() : void { |
|
57 | |||
58 | 84 | public function valid() : bool { |
|
61 | |||
62 | |||
63 | // Pairwise |
||
64 | |||
65 | protected $_Election; |
||
66 | protected $_Pairwise = []; |
||
67 | |||
68 | 87 | public function __construct (Election &$link) |
|
73 | |||
74 | 1 | public function __clone () |
|
78 | |||
79 | 87 | public function setElection (Election $election) : void |
|
83 | |||
84 | 4 | public function getExplicitPairwise () : array |
|
104 | |||
105 | 87 | protected function doPairwise () : void |
|
106 | { |
||
107 | // Chrono |
||
108 | 87 | new Timer_Chrono ( $this->_Election->getTimerManager(), 'Do Pairwise' ); |
|
109 | |||
110 | 87 | $this->formatNewpairwise(); |
|
111 | |||
112 | // Win && Null |
||
113 | 87 | foreach ( $this->_Election->getVotesManager() as $vote_id => $oneVote ) : |
|
114 | 87 | $vote_ranking = $oneVote->getContextualRanking($this->_Election); |
|
115 | |||
116 | 87 | $voteWeight = ($this->_Election->isVoteWeightIsAllowed()) ? $oneVote->getWeight() : 1; |
|
117 | |||
118 | $vote_candidate_list = (function (array $r) : array { $list = []; |
||
119 | 87 | foreach ($r as $rank) : |
|
120 | 87 | foreach ($rank as $oneCandidate) : |
|
121 | 87 | $list[] = $oneCandidate; |
|
122 | endforeach; |
||
123 | endforeach; |
||
124 | |||
125 | 87 | return $list;})($vote_ranking); |
|
126 | |||
127 | 87 | $done_Candidates = []; |
|
128 | |||
129 | 87 | foreach ($vote_ranking as $candidates_in_rank) : |
|
130 | |||
131 | 87 | $candidates_in_rank_keys = []; |
|
132 | |||
133 | 87 | foreach ($candidates_in_rank as $candidate) : |
|
134 | 87 | $candidates_in_rank_keys[] = $this->_Election->getCandidateKey($candidate); |
|
135 | endforeach; |
||
136 | |||
137 | 87 | foreach ($candidates_in_rank as $candidate) : |
|
138 | |||
139 | 87 | $candidate_key = $this->_Election->getCandidateKey($candidate); |
|
140 | |||
141 | // Process |
||
142 | 87 | foreach ( $vote_candidate_list as $g_Candidate ) : |
|
143 | |||
144 | 87 | $g_candidate_key = $this->_Election->getCandidateKey($g_Candidate); |
|
145 | |||
146 | 87 | if ($candidate_key === $g_candidate_key) : |
|
147 | 87 | continue; |
|
148 | endif; |
||
149 | |||
150 | // Win & Lose |
||
151 | 87 | if ( !in_array($g_candidate_key, $done_Candidates, true) && |
|
152 | 87 | !in_array($g_candidate_key, $candidates_in_rank_keys, true) ) : |
|
153 | |||
154 | 87 | $this->_Pairwise[$candidate_key]['win'][$g_candidate_key] += $voteWeight; |
|
155 | 87 | $this->_Pairwise[$g_candidate_key]['lose'][$candidate_key] += $voteWeight; |
|
156 | |||
157 | 87 | $done_Candidates[] = $candidate_key; |
|
158 | |||
159 | // Null |
||
160 | 87 | elseif (in_array($g_candidate_key, $candidates_in_rank_keys, true)) : |
|
161 | 87 | $this->_Pairwise[$candidate_key]['null'][$g_candidate_key] += $voteWeight; |
|
162 | endif; |
||
163 | |||
164 | endforeach; |
||
165 | |||
166 | endforeach; |
||
167 | |||
168 | endforeach; |
||
169 | |||
170 | endforeach; |
||
171 | 87 | } |
|
172 | |||
173 | 87 | protected function formatNewpairwise () : void |
|
191 | } |
||
192 |