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
use crate::{id, Id, ValidId};
use locspan::Meta;
use std::collections::HashMap;
use std::hash::Hash;
use std::marker::PhantomData;

pub struct Environment<'n, T, B, M, N, G> {
	id: PhantomData<T>,
	vocabulary: &'n mut N,
	generator: G,
	map: HashMap<B, Meta<ValidId<T, B>, M>>,
}

impl<'n, T, B, M, N, G> Environment<'n, T, B, M, N, G> {
	pub fn new(vocabulary: &'n mut N, generator: G) -> Self {
		Self {
			id: PhantomData,
			vocabulary,
			generator,
			map: HashMap::new(),
		}
	}
}

impl<'n, T: Clone, B: Clone + Hash + Eq, M: Clone, N, G: id::Generator<T, B, N, M>>
	Environment<'n, T, B, M, N, G>
{
	pub fn assign(&mut self, blank_id: B) -> Meta<ValidId<T, B>, M> {
		use std::collections::hash_map::Entry;
		match self.map.entry(blank_id) {
			Entry::Occupied(entry) => entry.get().clone(),
			Entry::Vacant(entry) => {
				let id = self.generator.next(self.vocabulary);
				entry.insert(id.clone());
				id
			}
		}
	}

	pub fn assign_node_id(&mut self, r: Option<&Meta<Id<T, B>, M>>) -> Meta<Id<T, B>, M> {
		match r {
			Some(Meta(Id::Valid(ValidId::Blank(id)), _)) => self.assign(id.clone()).cast(),
			Some(r) => r.clone(),
			None => self.next().cast(),
		}
	}

	#[allow(clippy::should_implement_trait)]
	pub fn next(&mut self) -> Meta<ValidId<T, B>, M> {
		self.generator.next(self.vocabulary)
	}
}