third_party/rust/lalrpop/src/kernel_set.rs
author Bastien Orivel <eijebong@bananium.fr>
Sat, 12 Jan 2019 03:19:31 +0200
changeset 453619 3c4b8e03e7221c6edc8651a13cd84cc344ce9b76
parent 412088 a97cccaa866a0fbd5721842ad8d8e862cd26ea65
child 453620 def9811f03111c1fe26bb578255cc30e56a87414
permissions -rw-r--r--
Bug 1516337 - Part 2: Revendor rust dependencies r=froydnj

use collections::{map, Map};
use std::collections::VecDeque;
use std::fmt::Debug;
use std::hash::Hash;

pub struct KernelSet<K: Kernel> {
    counter: usize,
    kernels: VecDeque<K>,
    map: Map<K, K::Index>,
}

pub trait Kernel: Clone + Debug + Hash + Eq + PartialOrd + Ord {
    type Index: Copy + Debug;

    fn index(c: usize) -> Self::Index;
}

impl<K: Kernel> KernelSet<K> {
    pub fn new() -> KernelSet<K> {
        KernelSet {
            kernels: VecDeque::new(),
            map: map(),
            counter: 0,
        }
    }

    pub fn add_state(&mut self, kernel: K) -> K::Index {
        let kernels = &mut self.kernels;
        let counter = &mut self.counter;
        *self.map.entry(kernel.clone()).or_insert_with(|| {
            let index = *counter;
            *counter += 1;
            kernels.push_back(kernel);
            K::index(index)
        })
    }

    pub fn next(&mut self) -> Option<K> {
        self.kernels.pop_front()
    }
}