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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
    Appellation: workload <module>
    Contrib: FL03 <jo3mccain@icloud.com>
*/
// #![cfg(feature = "std")]
use super::Rule;
use crate::{Head, State, Tail};
use std::collections::hash_map::{self, HashMap};

#[derive(Clone, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct RuleSet<Q, S>
where
    Q: Eq + core::hash::Hash,
    S: Eq + core::hash::Hash,
{
    pub(crate) initial_state: State<Q>,
    pub(crate) rules: HashMap<Head<Q, S>, Tail<Q, S>>,
}

impl<Q, S> RuleSet<Q, S>
where
    Q: Eq + core::hash::Hash,
    S: Eq + core::hash::Hash,
{
    pub fn new() -> Self
    where
        Q: Default,
    {
        Self {
            initial_state: State::default(),
            rules: HashMap::new(),
        }
    }

    pub fn from_state(State(initial_state): State<Q>) -> Self {
        Self {
            initial_state: State(initial_state),
            rules: HashMap::new(),
        }
    }

    pub fn from_iter<I>(iter: I) -> Self
    where
        Q: Default,
        I: IntoIterator<Item = (Head<Q, S>, Tail<Q, S>)>,
    {
        Self {
            initial_state: State::default(),
            rules: HashMap::from_iter(iter),
        }
    }

    pub fn from_rules<I>(iter: I) -> Self
    where
        Q: Default,
        I: IntoIterator<Item = super::Rule<Q, S>>,
    {
        let mut rules = HashMap::new();
        for rule in iter {
            rules.insert(rule.head, rule.tail);
        }
        Self {
            initial_state: State::default(),
            rules,
        }
    }

    pub fn with_initial_state(self, State(state): State<Q>) -> Self {
        Self {
            initial_state: State(state),
            ..self
        }
    }

    pub fn with_instructions(
        self,
        instructions: impl IntoIterator<Item = (Head<Q, S>, Tail<Q, S>)>,
    ) -> Self {
        let mut rules = HashMap::new();
        for (head, tail) in instructions {
            rules.insert(head, tail);
        }
        Self { rules, ..self }
    }
    /// Returns an instance of [State] which owns a reference to the interval value.
    pub fn initial_state(&self) -> State<&'_ Q> {
        self.initial_state.to_ref()
    }
    /// Returns an immutable reference to the set of rules.
    pub const fn rules(&self) -> &HashMap<Head<Q, S>, Tail<Q, S>> {
        &self.rules
    }
    /// Returns a mutable reference to the set of rules.
    pub fn rules_mut(&mut self) -> &mut HashMap<Head<Q, S>, Tail<Q, S>> {
        &mut self.rules
    }
    /// Clears the set of rules.
    pub fn clear(&mut self) {
        self.rules.clear();
    }
    /// Returns an the entry for the given head within the set of rules.
    pub fn entry(&mut self, head: Head<Q, S>) -> hash_map::Entry<Head<Q, S>, Tail<Q, S>> {
        self.rules.entry(head)
    }
    /// Returns an immutable reference to the tail of the rule for the given head; returns none
    /// if the head is not found.
    pub fn get(&self, head: &Head<Q, S>) -> Option<&Tail<Q, S>> {
        self.rules.get(head)
    }
    /// Returns a mutable reference to the tail of the rule for the given head; returns none if
    /// the head is not found.
    pub fn get_mut(&mut self, head: &Head<Q, S>) -> Option<&mut Tail<Q, S>> {
        self.rules.get_mut(head)
    }
    /// Returns the tail of the rule for the given head; returns none if the head is not found
    /// within the set of rules.
    pub fn get_ref(&self, head: &Head<Q, S>) -> Option<Tail<&Q, &S>> {
        self.get(head).map(|tail| tail.to_ref())
    }
    /// Inserts a new rule into the set of rules.
    pub fn insert(&mut self, head: Head<Q, S>, tail: Tail<Q, S>) {
        self.rules.insert(head, tail);
    }
    /// Inserts a new rule into the set of rules.
    pub fn insert_rule(&mut self, rule: Rule<Q, S>) {
        self.insert(rule.head, rule.tail);
    }
    /// Returns true if the set of rules is empty.
    pub fn is_empty(&self) -> bool {
        self.rules.is_empty()
    }
    /// Returns the number of rules in the set.
    pub fn len(&self) -> usize {
        self.rules.len()
    }
    /// Returns a mutable reference to the tail of the rule for the given head; inserts the
    /// tail if the head is not found.
    pub fn or_insert(&mut self, head: Head<Q, S>, tail: Tail<Q, S>) -> &mut Tail<Q, S> {
        self.rules.entry(head).or_insert(tail)
    }
    /// Returns a mutable reference to the tail of the rule for the given head; inserts the
    /// tail if the head is not found.
    pub fn or_insert_with<F>(&mut self, head: Head<Q, S>, f: F) -> &mut Tail<Q, S>
    where
        F: FnOnce() -> Tail<Q, S>,
    {
        self.rules.entry(head).or_insert_with(f)
    }
    /// Returns a mutable reference to the tail of the rule for the given head; inserts the
    /// default tail if the head is not found.
    pub fn or_insert_default(&mut self, head: Head<Q, S>) -> &mut Tail<Q, S>
    where
        Q: Default,
        S: Default,
    {
        self.or_insert(head, Tail::default())
    }
    /// Removes a rule from the set of rules.
    pub fn remove(&mut self, head: &Head<Q, S>) -> Option<Tail<Q, S>> {
        self.rules.remove(head)
    }
}

impl<Q, S> core::iter::Extend<(Head<Q, S>, Tail<Q, S>)> for RuleSet<Q, S>
where
    Q: Eq + core::hash::Hash,
    S: Eq + core::hash::Hash,
{
    fn extend<I>(&mut self, iter: I)
    where
        I: IntoIterator<Item = (Head<Q, S>, Tail<Q, S>)>,
    {
        for (head, tail) in iter {
            self.insert(head, tail);
        }
    }
}

impl<Q, S> core::iter::Extend<Rule<Q, S>> for RuleSet<Q, S>
where
    Q: Eq + core::hash::Hash,
    S: Eq + core::hash::Hash,
{
    fn extend<I>(&mut self, iter: I)
    where
        I: IntoIterator<Item = Rule<Q, S>>,
    {
        for rule in iter {
            self.insert(rule.head, rule.tail);
        }
    }
}

impl<Q, S> core::ops::Index<Head<Q, S>> for RuleSet<Q, S>
where
    Q: Eq + core::hash::Hash,
    S: Eq + core::hash::Hash,
{
    type Output = Tail<Q, S>;

    fn index(&self, head: Head<Q, S>) -> &Self::Output {
        &self.rules[&head]
    }
}

impl<Q, S> core::iter::FromIterator<(Head<Q, S>, Tail<Q, S>)> for RuleSet<Q, S>
where
    Q: Default + Eq + core::hash::Hash,
    S: Eq + core::hash::Hash,
{
    fn from_iter<I>(iter: I) -> Self
    where
        I: IntoIterator<Item = (Head<Q, S>, Tail<Q, S>)>,
    {
        Self::from_iter(iter)
    }
}

impl<Q, S> core::iter::FromIterator<Rule<Q, S>> for RuleSet<Q, S>
where
    Q: Default + Eq + core::hash::Hash,
    S: Eq + core::hash::Hash,
{
    fn from_iter<I>(iter: I) -> Self
    where
        I: IntoIterator<Item = Rule<Q, S>>,
    {
        Self::from_rules(iter)
    }
}

impl<Q, S> core::iter::IntoIterator for RuleSet<Q, S>
where
    Q: Eq + core::hash::Hash,
    S: Eq + core::hash::Hash,
{
    type Item = (Head<Q, S>, Tail<Q, S>);
    type IntoIter = std::collections::hash_map::IntoIter<Head<Q, S>, Tail<Q, S>>;

    fn into_iter(self) -> Self::IntoIter {
        self.rules.into_iter()
    }
}

impl<'a, Q, S> core::iter::IntoIterator for &'a RuleSet<Q, S>
where
    Q: Eq + core::hash::Hash,
    S: Eq + core::hash::Hash,
{
    type Item = (&'a Head<Q, S>, &'a Tail<Q, S>);
    type IntoIter = std::collections::hash_map::Iter<'a, Head<Q, S>, Tail<Q, S>>;

    fn into_iter(self) -> Self::IntoIter {
        self.rules.iter()
    }
}

impl<'a, Q, S> core::iter::IntoIterator for &'a mut RuleSet<Q, S>
where
    Q: Eq + core::hash::Hash,
    S: Eq + core::hash::Hash,
{
    type Item = (&'a Head<Q, S>, &'a mut Tail<Q, S>);
    type IntoIter = std::collections::hash_map::IterMut<'a, Head<Q, S>, Tail<Q, S>>;

    fn into_iter(self) -> Self::IntoIter {
        self.rules.iter_mut()
    }
}