sturavoting

package module
v0.0.0-...-4792ff4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 23, 2017 License: MIT Imports: 21 Imported by: 0

README

sturavoting

Documentation

Index

Constants

View Source
const InvalidID = ^uint(0)

Variables

This section is empty.

Functions

func GenKeyPair

func GenKeyPair() ([][]byte, error)

func InsertCategory

func InsertCategory(context *VotingContext, name string) error

func InsertVoters

func InsertVoters(context *VotingContext, revisionID uint, voters []*Voter) error

func InsertVotersRevision

func InsertVotersRevision(context *VotingContext, categoryID uint) error

func IntMax

func IntMax(a, b int) int

IntMax returns the maximum of a and b.

func IntMin

func IntMin(a, b int) int

IntMin returns the minimum of a and b.

func Now

func Now() time.Time

Now returns the current time in UTC.

func ReadKeyPairs

func ReadKeyPairs(path string) ([][]byte, error)

func SortMedianVotes

func SortMedianVotes(votes []*MedianVote)

SortMedianVotes sorts the votes according to the voted value. Votes with hightest values come first.

func TimeFromScanType

func TimeFromScanType(val interface{}) (time.Time, error)

DefaultTimeFromScanType is the default function to return database entries to a time.Time.

func WriteKeyPairs

func WriteKeyPairs(path string, keyPairs ...[]byte) error

Types

type Category

type Category struct {
	Name    string
	Created time.Time
	ID      uint
}

func ListCategories

func ListCategories(context *VotingContext) ([]*Category, error)

type IntMatrix

type IntMatrix [][]int

IntMatrix is a quadratic matrix of integer values.

func NewIntMatrix

func NewIntMatrix(n int) IntMatrix

NewIntMatrix returns a new n x n IntMatrix.

func (IntMatrix) Equals

func (m IntMatrix) Equals(other IntMatrix) bool

Equals compares to n x n IntMatrix instances.

type MedianResult

type MedianResult struct {
	// Value is the value that has a majority.
	Value int
	// VotesRequired is the number of votes required for a majority.
	VotesRequired int
}

MedianResult is a result type for median votings.

func EvaluateMedian

func EvaluateMedian(votes []*MedianVote, percentRequired float64) *MedianResult

EvaluateMedian evalues all votes given in votes and returns the greatest value that has a majority. percentRequired is a float and should be greater than 0 and lesser than 1. It describes how many percents of all votes are required for a majority. It returns 0 for value if no value was agreed upon.

type MedianVote

type MedianVote struct {
	// Weight is the weight of the voter.
	Weight int

	// Value is the value the voter chose.
	Value int
}

MedianVote is used as a vote in a median procedure. It contains information about the weight of the voter and the value chosen.

func NewMedianVote

func NewMedianVote(weight, value int) *MedianVote

NewMedianVote returns a new MedianVote.

type MedianVoting

type MedianVoting struct {
	Name            string
	MaxValue        int
	PercentRequired float64
}

func (*MedianVoting) String

func (voting *MedianVoting) String() string

type SchulzeRes

type SchulzeRes struct {
	// VotesRequired is the number of votes required for a majority.
	VotesRequired int
	// D is the matrix d as described in Wikipedia.
	D IntMatrix
	// P is the matrix p as described in Wikipedia.
	P IntMatrix
	// Ranked contains the result of the ranking algorithm.
	// It contains a list of list of integers.
	// The first list contains all options that are winners,
	// the second list contains all options that are on the second place etc.
	Ranked [][]int
	// Percents is a list of length n - 1 containing the percentage of votes
	// that voted the options 0...n-1 before no (no being the last option).
	// So if there are three options and 50% voted option 1 before no and
	// 75% voted option 2 before no this slice will be [0.5, 0.75].
	Percents []float64
}

SchulzeRes is the result returned by the schulze method.

func EvaluateSchulze

func EvaluateSchulze(votes []*SchulzeVote, n int, percentRequired float64) (*SchulzeRes, error)

EvaluateSchulze evaluates the Schulze method. votes contains all votes to be evaluated, n is the number of options in the voting (so all votes must have a Ranking slice of length n) and percentRequired is a float and should be greater than 0 and lesser than 1. It describes how many percents of all votes are required for a majority.

type SchulzeVote

type SchulzeVote struct {
	// Weight is the weight of the voter.
	Weight int
	// Ranking is the ordering for all options.
	// It must be a list of n elements if n is the number of possible options
	// where Ordering[i] is the value in the ranking.
	// Smaller values mean that the option is voted higher (comes first) in the
	// ranking.
	// For example: If there are three options and the first and third one should
	// be equally preferred to option two the ranking would be
	// [0, 1, 0].
	Ranking []int
}

SchulzeVote is a vote used in the Schulze procedure.

func NewSchulzeVote

func NewSchulzeVote(weight int, ranking []int) *SchulzeVote

NewSchulzeVote returns a new SchulzeVote. See struct documentation for details.

type SchulzeVoting

type SchulzeVoting struct {
	Name            string
	Options         []string
	PercentRequired float64
}

func (*SchulzeVoting) String

func (voting *SchulzeVoting) String() string

type SyntaxError

type SyntaxError struct {
	// contains filtered or unexported fields
}

func NewSyntaxError

func NewSyntaxError(lineNumber int, message string) *SyntaxError

func (*SyntaxError) Error

func (err *SyntaxError) Error() string

type Voter

type Voter struct {
	Name       string
	Weight     int
	ID         uint
	RevisionID uint
}

func ListVoters

func ListVoters(context *VotingContext, revisionID uint) ([]*Voter, error)

func NewVoter

func NewVoter(name string, weight int) *Voter

func ParseVoters

func ParseVoters(r io.Reader) ([]*Voter, error)

type VotersRevision

type VotersRevision struct {
	CategoryID uint
	Created    time.Time
	ID         uint
}

func ListVotersRevision

func ListVotersRevision(context *VotingContext, categoryID uint) ([]*VotersRevision, error)

type VotingCollection

type VotingCollection struct {
	Name   string
	Date   time.Time
	Groups []*VotingGroup
}

func ParseVotingCollection

func ParseVotingCollection(r io.Reader) (*VotingCollection, error)

func (*VotingCollection) String

func (collection *VotingCollection) String() string

type VotingContext

type VotingContext struct {
	DB                *sql.DB
	ConfigDir         string
	Store             sessions.Store
	Logger            *logrus.Logger
	UserHandler       goauth.UserHandler
	SessionController *goauth.SessionController
	Keys              [][]byte
	Templates         map[string]*template.Template
	SessionLifespan   time.Duration
	Port              int
}

func ParseConfig

func ParseConfig(configDir string) (*VotingContext, error)

func (*VotingContext) ReadOrCreateKeys

func (context *VotingContext) ReadOrCreateKeys()

type VotingGroup

type VotingGroup struct {
	Name           string
	MedianVotings  []*MedianVoting
	SchulzeVotings []*SchulzeVoting
}

func (*VotingGroup) String

func (group *VotingGroup) String() string

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL