class Vote::Condorcet::Schulze::Input
Public Class Methods
new(vote_list, candidate_count = nil)
click to toggle source
# File lib/vote/condorcet/schulze/input.rb, line 5 def initialize(vote_list, candidate_count = nil) @vote_list = vote_list @candidate_count = candidate_count @candidates_abc = [] if @candidate_count.nil? insert_vote_file(@vote_list) if vote_list.is_a?(File) else @vote_matrix = ::Matrix.scalar(@candidate_count, 0).extend(Vote::Matrix) insert_vote_array(@vote_list) if vote_list.is_a?(Array) insert_vote_strings(@vote_list) if vote_list.is_a?(String) end end
Public Instance Methods
all_numbers?(array)
click to toggle source
# File lib/vote/condorcet/schulze/input.rb, line 56 def all_numbers?(array) array.map { |e| e[0] }.all? { |el| /\A\d+\z/.match(el) } end
candidates()
click to toggle source
# File lib/vote/condorcet/schulze/input.rb, line 76 def candidates @candidate_count end
extract_vote_string(tmp)
click to toggle source
# File lib/vote/condorcet/schulze/input.rb, line 42 def extract_vote_string(tmp) # array of preferences [['1, 2'], ['3']. ['4, 5']] tmp2 = flatten_votes(order_and_remap(tmp)) tmp2.map! { |e| [e[0].to_i, e[1]] } if all_numbers?(tmp2) tmp2.sort.map { |e| e[1] } # order, strip & add end
flatten_votes(votes)
click to toggle source
# File lib/vote/condorcet/schulze/input.rb, line 48 def flatten_votes(votes) tmp2 = [] votes.map do |e| # find equal-weighted candidates (e[0].size > 1) ? e[0].split(/,/).each { |f| tmp2 << [f, e[1]] } : tmp2 << e end tmp2 end
insert_vote_array(va)
click to toggle source
# File lib/vote/condorcet/schulze/input.rb, line 18 def insert_vote_array(va) va.each do |vote| @vote_matrix.each_with_index do |_e, x, y| next if x == y @vote_matrix[x, y] += 1 if vote[x] > vote[y] end end @vote_count = va.size end
insert_vote_file(vf)
click to toggle source
# File lib/vote/condorcet/schulze/input.rb, line 64 def insert_vote_file(vf) vf.rewind @candidate_count = vf.first.strip.to_i # reads first line for count @vote_matrix = ::Matrix.scalar(@candidate_count, 0).extend(Vote::Matrix) insert_vote_strings vf.read # reads rest of file (w/o line 1) vf.close end
insert_vote_strings(vs)
click to toggle source
# File lib/vote/condorcet/schulze/input.rb, line 28 def insert_vote_strings(vs) vote_array = [] vs.split(/\n|\n\r|\r/).each do |voter| voter = voter.split(/=/) vcount = (voter.size == 1) ? 1 : voter[0].to_i vcount.times do tmp = voter.last.split(/;/) vote_array << extract_vote_string(tmp) end end insert_vote_array vote_array end
matrix()
click to toggle source
# File lib/vote/condorcet/schulze/input.rb, line 72 def matrix @vote_matrix end
order_and_remap(tmp)
click to toggle source
# File lib/vote/condorcet/schulze/input.rb, line 60 def order_and_remap(tmp) tmp.map { |e| [e, @candidate_count - tmp.index(e)] } end
voters()
click to toggle source
# File lib/vote/condorcet/schulze/input.rb, line 80 def voters @vote_count end