Skip to content

Commit

Permalink
add missing methods from Ranked Pairs
Browse files Browse the repository at this point in the history
  • Loading branch information
ikluft committed Jul 22, 2024
1 parent 828eec0 commit ffad0b5
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/perl/kr2/lib/PrefVote/KR2.pm
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,37 @@ has pair => (
},
);

# create candidate pair node if it didn't exist
sub make_pair_node
{
my ( $self, $cand_i, $cand_j ) = @_;
if ( not exists $self->{pair}{$cand_i} ) {
$self->{pair}{$cand_i} = {};
}
if ( not exists $self->{pair}{$cand_i}{$cand_j} ) {
$self->{pair}{$cand_i}{$cand_j} = PrefVote::RankedPairs::PairData->new();
}
return;
}

# record a candidate-pair preference
# This adds to a total of votes favoring candidate cand1 over cand2. Note: cand2 over cand1 is a separate table entry.
sub add_preference
{
my ( $self, $cand_i, $cand_j, $quantity ) = @_;
$self->make_pair_node( $cand_i, $cand_j );
return $self->{pair}{$cand_i}{$cand_j}->add_preference($quantity);
}

# get preference in matrix entry
sub get_preference
{
my ( $self, $cand_i, $cand_j ) = @_;
return 0 if not exists $self->{pair}{$cand_i}; # use zero if the node doesn't exist
return 0 if not exists $self->{pair}{$cand_i}{$cand_j}; # use zero if the node doesn't exist
return $self->{pair}{$cand_i}{$cand_j}->preference() // 0; # return preference, or zero if the node didn't have it
}

# TODO to be continued...

# compute candidate-pair preference totals
Expand Down

0 comments on commit ffad0b5

Please sign in to comment.