Search code examples
rustcounthashmap

Count the number of occurrences of each variant of an enum


I am trying to write a function to calculate poker hands. For this I need to check how many duplicates of each card value exist. I know this can be done with hashmaps, but I was wondering if it can also be done with some sort of statically allocated memory e.g. arrays given that the number of variants in the enum is known at compile time.

#[derive(Debug, Clone, PartialEq, Eq)]
enum Value {
    Ace,
    King,
    Queen,
    Jack,
    Ten,
    Nine,
    Eight,
    Seven,
    Six,
    Five,
    Four,
    Three,
    Two,
}

P.S. I'm pretty sure rust has a function for taking a vector or slice and returning a HashMap containing the counts of each distinct element in it e.g.

let a = vec![1, 2, 1, 3, 2, 4]
let b = a.counts();
let mut c = HashMap<i32, usize> = HashMap::new();
c.insert(1, 2);
c.insert(2, 2);
c.insert(3, 1);
c.insert(4, 1);
assert_eq(b, c);

However, I haven't been able to find it. Does anyone know what this function is?


Solution

  • By casting enum variants to integers, this can efficiently be solved by using an array with the counts instead of a hash-map: (playground)

    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    enum Value {
        Ace,
        King,
        Queen,
        Jack,
        Ten,
        Nine,
        Eight,
        Seven,
        Six,
        Five,
        Four,
        Three,
        Two,
    }
    
    fn counter<I>(iter: I) -> [u64;13]
    where
        I: Iterator<Item = Value>
    {
        let mut count = [0u64;13];
        for v in iter {
            let i = v as usize;
            count[i] += 1;
        }
        count
    }
    
    fn main() {
        let a = [Value::Ace, Value::Two, Value::Two];
        let b = counter(a.iter().copied());
        dbg!(b);
    }