1use super::RuleMap;
6use core::hash::Hash;
7use rstm_core::{Head, Rule, Tail};
8use rstm_state::RawState;
9use std::collections::hash_map;
10
11impl<Q, S> RuleMap<Q, S>
12where
13 Q: RawState + Eq + Hash,
14 S: Eq + Hash,
15{
16}
17
18impl<Q, S> core::ops::Index<Head<Q, S>> for RuleMap<Q, S>
19where
20 Q: RawState + Eq + Hash,
21 S: Eq + Hash,
22{
23 type Output = Tail<Q, S>;
24
25 fn index(&self, head: Head<Q, S>) -> &Self::Output {
26 self.get(&head).expect("Rule not found")
27 }
28}
29impl<Q, S> Extend<(Head<Q, S>, Tail<Q, S>)> for RuleMap<Q, S>
30where
31 Q: RawState + Eq + Hash,
32 S: Eq + Hash,
33{
34 fn extend<I>(&mut self, iter: I)
35 where
36 I: IntoIterator<Item = (Head<Q, S>, Tail<Q, S>)>,
37 {
38 for (head, tail) in iter {
39 self.insert(head, tail);
40 }
41 }
42}
43
44impl<Q, S> Extend<Rule<Q, S>> for RuleMap<Q, S>
45where
46 Q: RawState + Eq + Hash,
47 S: Eq + Hash,
48{
49 fn extend<I>(&mut self, iter: I)
50 where
51 I: IntoIterator<Item = Rule<Q, S>>,
52 {
53 for rule in iter {
54 self.insert(rule.head, rule.tail);
55 }
56 }
57}
58
59impl<Q, S> FromIterator<(Head<Q, S>, Tail<Q, S>)> for RuleMap<Q, S>
60where
61 Q: RawState + Eq + Hash,
62 S: Eq + Hash,
63{
64 fn from_iter<I>(iter: I) -> Self
65 where
66 I: IntoIterator<Item = (Head<Q, S>, Tail<Q, S>)>,
67 {
68 Self {
69 rules: std::collections::HashMap::from_iter(iter),
70 }
71 }
72}
73
74impl<Q, S> FromIterator<Rule<Q, S>> for RuleMap<Q, S>
75where
76 Q: RawState + Default + Eq + Hash,
77 S: Eq + Hash,
78{
79 fn from_iter<I>(iter: I) -> Self
80 where
81 I: IntoIterator<Item = Rule<Q, S>>,
82 {
83 Self {
84 rules: std::collections::HashMap::from_iter(
85 iter.into_iter().map(|rule| (rule.head, rule.tail)),
86 ),
87 }
88 }
89}
90
91impl<Q, S> IntoIterator for RuleMap<Q, S>
92where
93 Q: RawState + Eq + Hash,
94 S: Eq + Hash,
95{
96 type Item = (Head<Q, S>, Tail<Q, S>);
97 type IntoIter = hash_map::IntoIter<Head<Q, S>, Tail<Q, S>>;
98
99 fn into_iter(self) -> Self::IntoIter {
100 self.rules.into_iter()
101 }
102}
103
104impl<'a, Q, S> IntoIterator for &'a RuleMap<Q, S>
105where
106 Q: RawState + Eq + Hash,
107 S: Eq + Hash,
108{
109 type Item = (&'a Head<Q, S>, &'a Tail<Q, S>);
110 type IntoIter = hash_map::Iter<'a, Head<Q, S>, Tail<Q, S>>;
111
112 fn into_iter(self) -> Self::IntoIter {
113 self.rules.iter()
114 }
115}
116
117impl<'a, Q, S> IntoIterator for &'a mut RuleMap<Q, S>
118where
119 Q: RawState + Eq + Hash,
120 S: Eq + Hash,
121{
122 type Item = (&'a Head<Q, S>, &'a mut Tail<Q, S>);
123 type IntoIter = hash_map::IterMut<'a, Head<Q, S>, Tail<Q, S>>;
124
125 fn into_iter(self) -> Self::IntoIter {
126 self.rules.iter_mut()
127 }
128}