ballot_avl.gno
0.76 Kb ยท 43 lines
1package daocond
2
3import (
4 "gno.land/p/nt/avl"
5)
6
7type BallotAVL struct {
8 tree *avl.Tree
9}
10
11func NewBallot() Ballot {
12 return &BallotAVL{tree: avl.NewTree()}
13}
14
15// Implements Ballot interface
16func (b *BallotAVL) Vote(voter string, vote Vote) {
17 b.tree.Set(voter, vote)
18}
19
20// Implements Ballot interface
21func (b *BallotAVL) Get(voter string) Vote {
22 vote, ok := b.tree.Get(voter)
23 if !ok {
24 return VoteAbstain
25 }
26 return vote.(Vote)
27}
28
29// Implements Ballot interface
30func (b *BallotAVL) Total() int {
31 return b.tree.Size()
32}
33
34// Implements Ballot interface
35func (b *BallotAVL) Iterate(fn func(voter string, vote Vote) bool) {
36 b.tree.Iterate("", "", func(key string, value interface{}) bool {
37 v, ok := value.(Vote)
38 if !ok {
39 return false
40 }
41 return fn(key, v)
42 })
43}