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
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
use std::error;
use std::fmt;
use std::collections::{HashMap, HashSet};

pub fn run_election<'a, Iter1, SubIter: std::iter::IntoIterator + 'a, Vobj: 'a + std::cmp::Eq + std::hash::Hash>(voters: &'a Iter1, majority_mode: MajorityMode) -> Result<ElectionResult<Vobj>>
    where &'a Iter1: IntoIterator<Item = &'a SubIter>, &'a SubIter: IntoIterator<Item = &'a Vobj>
{
    //contains the list of eliminated candidates
    let mut eliminated: HashSet<&Vobj> = HashSet::new();
    loop {
        // setup the vote data structure
        let mut round_votes: HashMap<&Vobj, u32> = HashMap::new();
        // number of voters
        let mut num_voters: u32 = 0;
        let mut saw_voters: bool = false;
        // foreach voter
        for voter in voters {
            //set saw voters to give better error messaging
            saw_voters = true;
            //in CompleteMajority, we're always going to increase the votecounter, in remaining majority
            //we're only going to increase the counter if this voter has a valid vote
            let mut valid_vote = majority_mode == MajorityMode::CompleteMajority;
            //foreach of those voters votes
            for vote in voter {
                if !eliminated.contains(vote){
                    let mut vc = round_votes.entry(vote).or_insert(0);
                    *vc = *vc + 1;
                    valid_vote = true;
                    break;
                }
            }
            if valid_vote {
                let (num_voters_checked, overflow) = num_voters.overflowing_add(1);
                if overflow {
                    return Err(ElectionError::Overflow);
                }
                num_voters = num_voters_checked;
            }            
        }
        // ensure that we had voters
        if !saw_voters {
            return Err(ElectionError::EmptyVoteCollection);
        }
        // essure that we had votes
        if round_votes.is_empty() {
            return Err(ElectionError::NoMajorityWinner)
        }
        // find the fifty percentile and see if we have a winner by majority
        // note: we need to make sure we don't overflow num_voters when getting
        // the fifty percentile
        let (fifty_numerator, overflow) = num_voters.overflowing_add(1);
        if overflow {
            return Err(ElectionError::Overflow);
        }
        let fifty_percent = (fifty_numerator) / 2;
        let mut winners = Vec::new();
        for (v, votecount) in round_votes.iter() {
            if *votecount >= fifty_percent {
                winners.push(*v);
            }
        }
        match winners.len() {
            0 => {}
            1 => {return Ok(ElectionResult::Winner(winners[0]));}
            _ => { return Ok(ElectionResult::Tie(winners)); }
        }
        // if we don't have a majority we need to eliminate the 
        // minimum vote getters
        let mut add_elim = Vec::new();
        let mut elim_count = u32::max_value();
        for (v, votecount) in round_votes.iter() {
            if *votecount < elim_count {
                add_elim.clear();
                add_elim.push(v);
                elim_count = *votecount;
            } else if *votecount == elim_count {
                add_elim.push(v);
            }
        }
        // add who to eliminate to the eliminated hashmap
        for elim in add_elim {
            eliminated.insert(elim);
        }
    }
    //return Err(ElectionError::EmptyVoteCollection);
}

#[derive(Debug, PartialEq)]
pub enum ElectionResult<'a, T: 'a> {
    Winner(&'a T),
    Tie(Vec<&'a T>),
}

#[derive(Debug, PartialEq)]
pub enum MajorityMode {
    CompleteMajority,
    RemainingMajority
}

type Result<T> = std::result::Result<T, ElectionError>;
#[derive(Debug, Clone, PartialEq)]
pub enum ElectionError {
    EmptyVoteCollection,
    NoMajorityWinner,
    Overflow,
}
impl fmt::Display for ElectionError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Error with election input")
    }
}
impl error::Error for ElectionError {
    fn description(&self) -> &str {
        match &self {
            ElectionError::EmptyVoteCollection => {
                "Vote Collection is empty"
            },
            ElectionError::NoMajorityWinner => {
                "There were voters, but no votes"
            }
            ElectionError::Overflow => {
                "An integer overflow occured"
            }
            // _ => {
            //     "Other Error"
            // }
        }        
    }

    fn cause(&self) -> Option<&error::Error> {
        // Generic error, underlying cause isn't tracked.
        None
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::VecDeque;

    #[test]
    fn basic_run() {
        let mut voter_a = VecDeque::new();
        voter_a.push_front("sue");
        voter_a.push_front("bill");
        voter_a.push_front("bob");

        let mut voter_b = VecDeque::new();
        voter_b.push_front("bill");
        voter_b.push_front("bob");
        voter_b.push_front("sue");

        let mut voter_c = VecDeque::new();
        voter_c.push_front("bob");
        voter_c.push_front("sue");
        voter_c.push_front("bill");

        let mut voter_d = VecDeque::new();
        voter_d.push_front("sue");
        voter_d.push_front("bill");
        voter_d.push_front("bob");

        let mut voter_e = VecDeque::new();
        voter_e.push_front("bill");
        voter_e.push_front("bob");
        voter_e.push_front("sue");

        let vec = vec![voter_a, voter_b, voter_c, voter_d, voter_e];
        let winner = run_election(&vec, MajorityMode::CompleteMajority);
        //println!("{:?}", winner);
        if let Ok(winner) = winner {
            assert_eq!(ElectionResult::Winner(&"sue"), winner);
        } else {
            assert!(false);
        }
        //test again with remaining majority
        let winner = run_election(&vec, MajorityMode::RemainingMajority);
        //println!("{:?}", winner);
        if let Ok(winner) = winner {
            assert_eq!(ElectionResult::Winner(&"sue"), winner);
        } else {
            assert!(false);
        }
        //println!("winner is: {:?}", winner);
    }

    #[test]
    fn tie() {
        let mut voter_a = VecDeque::new();
        voter_a.push_front("sue");

        let mut voter_b = VecDeque::new();
        voter_b.push_front("bill");

        let vec = vec![voter_a, voter_b];
        let winner = run_election(&vec, MajorityMode::CompleteMajority);
        if let Ok(ElectionResult::Tie(tie_res)) = winner {
            assert!(tie_res.contains(&&"bill"));
            assert!(tie_res.contains(&&"sue"));
        }
        else {
            assert!(false);
        }
        let winner = run_election(&vec, MajorityMode::RemainingMajority);
        if let Ok(ElectionResult::Tie(tie_res)) = winner {
            assert!(tie_res.contains(&&"bill"));
            assert!(tie_res.contains(&&"sue"));
        }
        else {
            assert!(false);
        }
    }

    #[test]
    fn tie2() {
        let mut voter_a = VecDeque::new();
        voter_a.push_front("sue");
        voter_a.push_front("bill");

        let mut voter_b = VecDeque::new();
        voter_b.push_front("bill");
        voter_b.push_front("sue");

        let vec = vec![voter_a, voter_b];
        let winner = run_election(&vec, MajorityMode::CompleteMajority);
        if let Ok(ElectionResult::Tie(tie_res)) = winner {
            assert!(tie_res.contains(&&"bill"));
            assert!(tie_res.contains(&&"sue"));
        }
        else {
            assert!(false);
        }
        let winner = run_election(&vec, MajorityMode::RemainingMajority);
        if let Ok(ElectionResult::Tie(tie_res)) = winner {
            assert!(tie_res.contains(&&"bill"));
            assert!(tie_res.contains(&&"sue"));
        }
        else {
            assert!(false);
        }
    }

    #[test]
    fn voters_no_votes() {
        let voter_a: Vec<&str> = Vec::new();
        let voter_b: Vec<&str> = Vec::new();
        let voters = vec![voter_a, voter_b];
        let winner = run_election(&voters, MajorityMode::CompleteMajority);
        if let Err(error_res) = winner {
            assert_eq!(error_res, ElectionError::NoMajorityWinner);
        } else {
            assert!(false);
        }
        let winner = run_election(&voters, MajorityMode::RemainingMajority);
        if let Err(error_res) = winner {
            assert_eq!(error_res, ElectionError::NoMajorityWinner);
        } else {
            assert!(false);
        }
    }

    #[test]
    fn minority_majority() {
        let mut voter_a = VecDeque::new();
        voter_a.push_front("sam");
        voter_a.push_front("alice");

        let mut voter_b = VecDeque::new();
        voter_b.push_front("sam");
        voter_b.push_front("bob");

        let mut voter_c = VecDeque::new();
        voter_c.push_front("bob");
        voter_c.push_front("chris");

        let mut voter_d = VecDeque::new();
        voter_d.push_front("chris");
        voter_d.push_front("sam");

        let mut voter_e = VecDeque::new();
        voter_e.push_front("chris");
        voter_e.push_front("alice");

        let vec = vec![voter_a, voter_b, voter_c, voter_d, voter_e];
        //run with complete majority mode
        let winner = run_election(&vec, MajorityMode::CompleteMajority);
        if let Err(error_res) = winner {
            assert_eq!(error_res, ElectionError::NoMajorityWinner);
        } else {
            assert!(false);
        }
        //run with remaining majority mode
        let winner = run_election(&vec, MajorityMode::RemainingMajority);
        if let Ok(winner) = winner {
            assert_eq!(ElectionResult::Winner(&"alice"), winner);
        } else {
            println!("{:?}", winner);
            assert!(false);
        }
    }
}