-
Notifications
You must be signed in to change notification settings - Fork 2
/
voting.go
45 lines (38 loc) · 1.55 KB
/
voting.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Copyright (c) 2022, Janoš Guljaš <janos@resenje.org>
// All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package schulze
// Voting holds number of votes for every pair of choices. It is a convenient
// construct to use when the preferences slice does not have to be exposed, and
// should be kept safe from accidental mutation. Methods on the Voting type are
// not safe for concurrent calls.
type Voting[C comparable] struct {
choices []C
preferences []int
}
// NewVoting initializes a new voting state for the provided choices.
func NewVoting[C comparable](choices []C) *Voting[C] {
return &Voting[C]{
choices: choices,
preferences: NewPreferences(len(choices)),
}
}
// Vote adds a voting preferences by a single voting ballot.
func (v *Voting[C]) Vote(b Ballot[C]) error {
return Vote(v.preferences, v.choices, b)
}
// Unvote removes a voting preferences from a single voting ballot.
func (v *Voting[C]) Unvote(b Ballot[C]) error {
return Unvote(v.preferences, v.choices, b)
}
// SetChoices updates the voting accommodate the changes to the choices. It is
// required to pass a complete updated choices.
func (v *Voting[C]) SetChoices(updated []C) {
v.preferences = SetChoices(v.preferences, v.choices, updated)
}
// Compute calculates a sorted list of choices with the total number of wins for
// each of them. If there are multiple winners, tie boolean parameter is true.
func (v *Voting[C]) Compute() (results []Result[C], tie bool) {
return Compute(v.preferences, v.choices)
}