|
|
|
@ -2,12 +2,13 @@ package judgment
|
|
|
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
|
|
|
|
|
|
// PollTally describes the amount of judgments received by each proposal on each grade.
|
|
|
|
|
type PollTally struct {
|
|
|
|
|
AmountOfJudges uint64 // Helps balancing tallies using default judgments.
|
|
|
|
|
Proposals []*ProposalTally // Tallies of each proposal. Its order is preserved in the result.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Mutates the PollTally
|
|
|
|
|
// BalanceWithStaticDefault mutates the PollTally
|
|
|
|
|
func (pollTally *PollTally) BalanceWithStaticDefault(defaultGrade uint8) (err error) {
|
|
|
|
|
for _, proposalTally := range pollTally.Proposals {
|
|
|
|
|
proposalErr := proposalTally.FillWithStaticDefault(pollTally.AmountOfJudges, defaultGrade)
|
|
|
|
@ -18,7 +19,7 @@ func (pollTally *PollTally) BalanceWithStaticDefault(defaultGrade uint8) (err er
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Mutates the PollTally
|
|
|
|
|
// BalanceWithMedianDefault mutates the PollTally
|
|
|
|
|
func (pollTally *PollTally) BalanceWithMedianDefault() (err error) {
|
|
|
|
|
for _, proposalTally := range pollTally.Proposals {
|
|
|
|
|
proposalErr := proposalTally.FillWithMedianDefault(pollTally.AmountOfJudges)
|
|
|
|
@ -31,16 +32,19 @@ func (pollTally *PollTally) BalanceWithMedianDefault() (err error) {
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
// ProposalTally holds the amount of judgments received per Grade for a single Proposal
|
|
|
|
|
type ProposalTally struct {
|
|
|
|
|
Tally []uint64 // Amount of judgments received for each grade, from "worst" grade to "best" grade.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Analyze a ProposalTally and return its ProposalAnalysis
|
|
|
|
|
func (proposalTally *ProposalTally) Analyze() (_ *ProposalAnalysis) {
|
|
|
|
|
analysis := &ProposalAnalysis{}
|
|
|
|
|
analysis.Run(proposalTally, true)
|
|
|
|
|
return analysis
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Copy a ProposalTally (deeply)
|
|
|
|
|
func (proposalTally *ProposalTally) Copy() (_ *ProposalTally) {
|
|
|
|
|
// There might exist an elegant one-liner to copy a slice of uint64
|
|
|
|
|
intTally := make([]uint64, 0, 8)
|
|
|
|
@ -52,6 +56,7 @@ func (proposalTally *ProposalTally) Copy() (_ *ProposalTally) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CountJudgments tallies the received judgments by a Proposal
|
|
|
|
|
func (proposalTally *ProposalTally) CountJudgments() (_ uint64) {
|
|
|
|
|
amountOfJudgments := uint64(0)
|
|
|
|
|
for _, gradeTally := range proposalTally.Tally {
|
|
|
|
@ -60,11 +65,13 @@ func (proposalTally *ProposalTally) CountJudgments() (_ uint64) {
|
|
|
|
|
return amountOfJudgments
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CountAvailableGrades returns the amount of available grades in the poll (usually 7 or so).
|
|
|
|
|
func (proposalTally *ProposalTally) CountAvailableGrades() (_ uint8) {
|
|
|
|
|
return uint8(len(proposalTally.Tally))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Mutates the proposalTally.
|
|
|
|
|
// RegradeJudgments mutates the proposalTally by moving judgments from one grade to another.
|
|
|
|
|
// Useful when computing the score ; perhaps this method should not be exported, though.
|
|
|
|
|
func (proposalTally *ProposalTally) RegradeJudgments(fromGrade uint8, intoGrade uint8) (err error) {
|
|
|
|
|
|
|
|
|
|
if fromGrade == intoGrade {
|
|
|
|
@ -85,7 +92,7 @@ func (proposalTally *ProposalTally) RegradeJudgments(fromGrade uint8, intoGrade
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Mutates the proposalTally
|
|
|
|
|
// FillWithStaticDefault mutates the proposalTally
|
|
|
|
|
func (proposalTally *ProposalTally) FillWithStaticDefault(upToAmount uint64, defaultGrade uint8) (err error) {
|
|
|
|
|
// More silent integer casting awkwardness… ; we need to fix this
|
|
|
|
|
missingAmount := int(upToAmount) - int(proposalTally.CountJudgments())
|
|
|
|
@ -104,7 +111,7 @@ func (proposalTally *ProposalTally) FillWithStaticDefault(upToAmount uint64, def
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Mutates the proposalTally
|
|
|
|
|
// FillWithMedianDefault mutates the proposalTally
|
|
|
|
|
func (proposalTally *ProposalTally) FillWithMedianDefault(upToAmount uint64) (err error) {
|
|
|
|
|
analysis := proposalTally.Analyze()
|
|
|
|
|
fillErr := proposalTally.FillWithStaticDefault(upToAmount, analysis.MedianGrade)
|
|
|
|
|