ballot_avl.gno
0.94 Kb ยท 44 lines
1package daocond
2
3import (
4 "gno.land/p/nt/avl"
5)
6
7type BallotAVL struct {
8 tree *avl.Tree
9}
10
11// Creates a new ballot implementation using AVL tree.
12func NewBallot() Ballot {
13 return &BallotAVL{tree: avl.NewTree()}
14}
15
16// Records a vote from a specific voter, overwriting any previous vote.
17func (b *BallotAVL) Vote(voter string, vote Vote) {
18 b.tree.Set(voter, vote)
19}
20
21// Gets the vote from a specific voter, returns VoteAbstain if no vote recorded.
22func (b *BallotAVL) Get(voter string) Vote {
23 vote, ok := b.tree.Get(voter)
24 if !ok {
25 return VoteAbstain
26 }
27 return vote.(Vote)
28}
29
30// Returns the total number of votes cast.
31func (b *BallotAVL) Total() int {
32 return b.tree.Size()
33}
34
35// Iterates over all votes, calling fn for each voter-vote pair.
36func (b *BallotAVL) Iterate(fn func(voter string, vote Vote) bool) {
37 b.tree.Iterate("", "", func(key string, value interface{}) bool {
38 v, ok := value.(Vote)
39 if !ok {
40 return false
41 }
42 return fn(key, v)
43 })
44}