1
//! `BoolIndexedArray`
2

            
3
use super::*;
4

            
5
/// Array of two values, one for `false` and one for `true`
6
///
7
/// This is a wrapper around `[T; 2]`, which views the array as being indexed by `bool`.
8
///
9
/// Can be constructed from a `[T; 2]` using `From`.
10
/// Implements `Default` if `T` is `Default`.
11
///
12
/// Ordering compares first the `true` value, then the `false` one.
13
//
14
// We use array rather than tuple, because in Rust an array is always better than a tuple
15
// if the values are known to be of the same type.
16
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Hash)] //
17
#[derive(derive_more::Into, derive_more::From)]
18
// TODO move this somewhere else maybe? flesh out this API?
19
pub(crate) struct BoolIndexedArray<T>([T; 2]);
20

            
21
impl<T> std::ops::Index<bool> for BoolIndexedArray<T> {
22
    type Output = T;
23
1856
    fn index(&self, b: bool) -> &T {
24
1856
        &self.0[usize::from(b)]
25
1856
    }
26
}
27
impl<T> std::ops::IndexMut<bool> for BoolIndexedArray<T> {
28
5252
    fn index_mut(&mut self, b: bool) -> &mut T {
29
5252
        &mut self.0[usize::from(b)]
30
5252
    }
31
}
32

            
33
impl<T> BoolIndexedArray<T> {
34
    /// Treating this as a vote tally, are there (strictly) more yeses than noes?
35
928
    pub(crate) fn more_yes_than_no(&self) -> bool
36
928
    where
37
928
        T: Ord,
38
    {
39
928
        self[true] > self[false]
40
928
    }
41
}