X-Git-Url: https://projects.mako.cc/source/rubyvote/blobdiff_plain/56dd8bed5abf1ab2379cae2b891ce3b4b2c5e9b0..cadb8cabfdcf3cb009319e36102beaf31362da89:/lib/rubyvote/condorcet.rb diff --git a/lib/rubyvote/condorcet.rb b/lib/rubyvote/condorcet.rb index f4ed7b7..3269261 100644 --- a/lib/rubyvote/condorcet.rb +++ b/lib/rubyvote/condorcet.rb @@ -33,6 +33,19 @@ class CondorcetVote < ElectionVote + def initialize(votes=nil) + unless defined?(@candidates) + @candidates = Array.new + votes.each do |vote_row| + vote_row = vote_row.flatten if vote_row.class == Array + vote_row.each do |vote| + @candidates << vote unless @candidates.include?(vote) + end + end + end + super(votes) + end + def tally_vote(vote=nil) vote.each_with_index do |winner, index| @@ -43,14 +56,13 @@ class CondorcetVote < ElectionVote if vote.length - 1 == index losers = [] else - losers = vote.last( vote.flatten.length - index ) + losers = vote.flatten.last( vote.flatten.length - index - 1) end losers.each do |place| place = [place] unless place.class == Array place.each do |loser| - next if winner == loser @votes[winner] = Hash.new unless @votes.has_key?(winner) @votes[loser] = Hash.new unless @votes.has_key?(loser) @@ -63,24 +75,13 @@ class CondorcetVote < ElectionVote # make sure we have a comparable object @votes[loser][winner] = 0 unless @votes[loser].has_key?( winner ) - - @candidates << loser unless @candidates.include?( loser ) end end - - @candidates << winner unless @candidates.include?( winner ) || - winner.class == Array end end def result - top_result = resultFactory( self ) - until @candidates.empty? - aResult = resultFactory( self ) - top_result.full_results << aResult - filter_out(aResult) - end - top_result + resultFactory( self ) end protected @@ -101,6 +102,17 @@ class CloneproofSSDVote < CondorcetVote def resultFactory(init) CloneproofSSDResult.new(init) end + + def result + top_result = resultFactory( self ) + until @candidates.empty? + aResult = resultFactory( self ) + top_result.full_results << aResult + filter_out(aResult) + end + top_result + end + end @@ -125,6 +137,7 @@ class CondorcetResult < ElectionResult votes = @election.votes unless votes defeats = Array.new + candidates = [candidates] unless candidates.class == Array candidates.each do |candidate| candidates.each do |challenger| next if candidate == challenger @@ -196,6 +209,7 @@ class CloneproofSSDResult < CondorcetResult # see the array with the standard defeats transitive_defeats = self.defeats(candidates, votes) + candidates = [candidates] unless candidates.class == Array candidates.each do |cand1| candidates.each do |cand2| candidates.each do |cand3|