third_party/rust/lalrpop/src/kernel_set.rs
author Dorel Luca <dluca@mozilla.com>
Sat, 12 Jan 2019 03:43:46 +0200
changeset 453620 def9811f0311
parent 453619 3c4b8e03e722
child 460816 c5e856d5edba
permissions -rw-r--r--
Backed out 2 changesets (bug 1516337) for build bustage. CLOSED TREE Backed out changeset 3c4b8e03e722 (bug 1516337) Backed out changeset 4fc377013db5 (bug 1516337)

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

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()
    }
}