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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
use std::{
    collections::{HashMap, HashSet, VecDeque},
    fs::File,
    hash::Hash,
    path::Path,
};

#[cfg(feature = "dot")]
pub mod dot;

#[cfg(feature = "plantuml")]
pub mod plantuml;

#[cfg(feature = "mermaid")]
pub mod mermaid;


/// Write to file operation.
pub trait TryWriteFile {
    /// Try to write `self` to the file in `path`.
    /// This operation uses the `Display` representation for its output.
    /// If successful, returns the written [File], otherwise, an [std::io::Error] is returned.
    fn try_write_file<P: AsRef<Path>>(self, path: P) -> std::io::Result<File>;
}

/// Type alias for the transition "minigraph".
type Deltas<State, Transition> = HashMap<State, HashMap<Transition, HashSet<State>>>;

/// Delta kind enumeration.
/// Distinguishes between `Delta` (forward transitions) and
/// `IDelta` (backward transitions) which are the inverse of `Delta`.
enum Delta {
    Delta,
    IDelta,
}

/// Type alias for [`DeterministicFiniteAutomata`].
pub type Dfa<State, Transition> = DeterministicFiniteAutomata<State, Transition>;

/// A representation for a deterministic finite automata.
#[derive(Debug, Clone)]
pub struct DeterministicFiniteAutomata<State, Transition>
where
    State: Eq + Hash + Clone,
    Transition: Eq + Hash + Clone,
{
    /// Deterministic finite automata states.
    pub states: HashSet<State>,
    /// Deterministic finite automata transition symbols.
    sigma: HashSet<Transition>,
    /// Deterministic finite automata initial state-indexes.
    pub initial_states: HashMap<State, HashSet<Transition>>,
    /// Deterministic finite automata final state-indexes.
    pub final_states: HashMap<State, HashSet<Transition>>,
    /// Deterministic finite automata transition functions.
    /// Map of state indexes to map of transitions to state indexes.
    delta: HashMap<State, HashMap<Transition, State>>,
    /// The inverse paths of delta.
    /// This structure helps algorithms requiring interation in the "inverse" order.
    idelta: HashMap<State, HashMap<Transition, State>>,
}

impl<State, Transition> DeterministicFiniteAutomata<State, Transition>
where
    State: Eq + Hash + Clone,
    Transition: Eq + Hash + Clone,
{
    /// Construct a new deterministic finite automata.
    #[must_use]
    pub fn new() -> Self {
        Self {
            states: HashSet::new(),
            sigma: HashSet::new(),
            initial_states: HashMap::new(),
            final_states: HashMap::new(),
            delta: HashMap::new(),
            idelta: HashMap::new(),
        }
    }

    /// Add a state to the automata.
    pub fn add_state(&mut self, state: State) {
        self.states.insert(state);
    }

    /// Add an initial state to the automata.
    pub fn add_initial(&mut self, state: State, symbol: Transition) {
        self.states.insert(state.clone());
        if let Some(symbols) = self.initial_states.get_mut(&state) {
            symbols.insert(symbol);
        } else {
            let mut symbols = HashSet::new();
            symbols.insert(symbol);
            self.initial_states.insert(state, symbols);
        }
    }

    /// Add a final state to the automata.
    pub fn add_final(&mut self, state: State, symbol: Transition) {
        self.states.insert(state.clone());
        if let Some(symbols) = self.final_states.get_mut(&state) {
            symbols.insert(symbol);
        } else {
            let mut symbols = HashSet::new();
            symbols.insert(symbol);
            self.final_states.insert(state, symbols);
        }
    }

    /// Add a new symbol to the automata alphabet.
    pub fn add_sigma(&mut self, sigma: Transition) {
        self.sigma.insert(sigma);
    }

    fn add_delta(&mut self, source: State, symbol: Transition, destination: State, delta: &Delta) {
        let delta = match delta {
            Delta::Delta => &mut self.delta,
            Delta::IDelta => &mut self.idelta,
        };
        if let Some(transitions) = delta.get_mut(&source) {
            if transitions.get_mut(&symbol).is_some() {
                // TODO duplicate: return error or replace?
            } else {
                transitions.insert(symbol, destination);
            }
        } else {
            let mut transitions = HashMap::new();
            transitions.insert(symbol, destination);
            delta.insert(source, transitions);
        }
    }

    // Add a transition from `source` to `destination`, consuming `symbol`.
    pub fn add_transition(&mut self, source: State, symbol: Transition, destination: State) {
        // TODO check for state existence or add regardless
        self.add_sigma(symbol.clone());
        self.add_delta(
            source.clone(),
            symbol.clone(),
            destination.clone(),
            &Delta::Delta,
        );
        self.add_delta(destination, symbol, source, &Delta::IDelta);
    }

    /// Compute the automata productive states.
    #[must_use]
    pub fn productive_states(&self) -> HashSet<State> {
        let mut stack: VecDeque<_> = self.final_states.keys().collect();
        // productive == visited
        let mut productive = HashSet::new();
        while let Some(state) = stack.pop_back() {
            if productive.insert(state.clone()) {
                if let Some(states) = self
                    .idelta
                    .get(state)
                    .map(std::collections::HashMap::values)
                {
                    stack.extend(states)
                }
            }
        }
        productive
    }

    /// Compute the automata non-productive states.
    ///
    /// This is done by calling [`DeterministicFiniteAutomata::productive_states`] and performing a diference against the full state set.
    #[must_use]
    pub fn non_productive_states(&self) -> HashSet<State> {
        // TODO check if the clone should be here or if we can return `HashSet<&State>`
        self.states
            .difference(&self.productive_states())
            .cloned()
            .collect()
    }

    /// Compute the automata useful states.
    #[must_use]
    pub fn useful_states(&self) -> HashSet<State> {
        // TODO this could benefit from some "caching" of results on productive
        let productive = self.productive_states();
        let mut stack: VecDeque<_> = self.initial_states.keys().collect();
        // productive == visited
        let mut reachable = HashSet::new();
        while let Some(state) = stack.pop_back() {
            if reachable.insert(state.clone()) {
                if let Some(states) = self.delta.get(state).map(std::collections::HashMap::values) {
                    stack.extend(states)
                }
            }
        }
        productive.intersection(&reachable).cloned().collect()
    }

    /// Compute the automata non-useful states.
    ///
    /// This is done by calling [`DeterministicFiniteAutomata::useful_states`] and performing a diference against the full state set.
    #[must_use]
    pub fn non_useful_states(&self) -> HashSet<State> {
        // TODO check if the clone should be here or if we can return `HashSet<&State>`
        self.states
            .difference(&self.useful_states())
            .cloned()
            .collect()
    }
}

/// Implementation of the [`Default`] trait for a [`DeterministicFiniteAutomata`].
impl<State, Transition> Default for DeterministicFiniteAutomata<State, Transition>
where
    State: Eq + Hash + Clone,
    Transition: Eq + Hash + Clone,
{
    /// This function is equivalent to [`DeterministicFiniteAutomata::new`].
    fn default() -> Self {
        Self::new()
    }
}

/// Tests for [DeterministicFiniteAutomata].
#[cfg(test)]
mod dfa_tests {
    use super::test_traits::*;
    use super::*;

    fn setup_automata() -> Dfa<i32, usize> {
        let mut dfa = Dfa::new();
        let state_list = [1, 2, 3, 4, 5, 6, 7];
        for &state in state_list.iter() {
            dfa.add_state(state);
        }
        dfa.add_initial(1, 0);
        dfa.add_final(7, 0);
        let transition_list = [
            (1, 2),
            (1, 3),
            (2, 6),
            (3, 4),
            (3, 5),
            (3, 6),
            (4, 5),
            (5, 7),
            (6, 7),
        ];
        for (idx, &(src, dst)) in transition_list.iter().enumerate() {
            dfa.add_transition(src, idx + 1, dst);
        }
        dfa
    }

    fn setup_automata_loop() -> Dfa<i32, ()> {
        let mut dfa = Dfa::new();
        dfa.add_initial(1, ());
        dfa.add_final(2, ());
        dfa.add_transition(1, (), 2);
        dfa.add_transition(2, (), 1);
        dfa
    }

    #[test]
    fn test_add_state() {
        let dfa = setup_automata();
        let expected_states = [1, 2, 3, 4, 5, 6, 7].into_hash_set();
        let result_states = dfa.states;
        assert_eq!(expected_states, result_states);
    }

    #[test]
    fn test_add_initial_state() {
        let dfa = setup_automata();
        let expected_states = [1].into_hash_set();
        let result_states: HashSet<i32> = dfa.initial_states.keys().map(|i| *i).collect();
        assert_eq!(expected_states, result_states);
    }

    #[test]
    fn test_add_final_state() {
        let dfa = setup_automata();
        let expected_states = [7].into_hash_set();
        let result_states: HashSet<i32> = dfa.final_states.keys().map(|i| *i).collect();
        assert_eq!(expected_states, result_states);
    }

    #[test]
    fn test_add_transition() {
        let dfa = setup_automata();
        let expected_deltas = [
            (1, 2),
            (1, 3),
            (2, 6),
            (3, 4),
            (3, 5),
            (3, 6),
            (4, 5),
            (5, 7),
            (6, 7),
        ]
        .iter()
        .map(|t| t.to_owned())
        .collect::<HashSet<_>>();
        let expected_ideltas = expected_deltas
            .iter()
            .map(|(fst, snd)| (*snd, *fst))
            .map(|t| t.to_owned())
            .collect::<HashSet<_>>();
        let mut result_deltas = HashSet::new();
        for (src, transitions) in dfa.delta {
            for &dst in transitions.values() {
                result_deltas.insert((src, dst));
            }
        }
        let mut result_ideltas = HashSet::new();
        for (src, transitions) in dfa.idelta {
            for &dst in transitions.values() {
                result_ideltas.insert((src, dst));
            }
        }
        assert_eq!(expected_deltas, result_deltas);
        assert_eq!(expected_ideltas, result_ideltas);
    }

    #[test]
    fn test_productive() {
        let dfa = setup_automata();
        let result = dfa.productive_states();
        let expected = [1, 2, 3, 4, 5, 6, 7]
            .iter()
            .map(|i| i.to_owned())
            .collect::<HashSet<i32>>();
        assert_eq!(expected, result);
    }

    #[test]
    fn test_productive_loop() {
        let dfa = setup_automata_loop();
        let result = dfa.productive_states();
        let expected = [1, 2]
            .iter()
            .map(|i| i.to_owned())
            .collect::<HashSet<i32>>();
        assert_eq!(expected, result);
    }

    #[test]
    fn test_useful() {
        let dfa = setup_automata();
        let result = dfa.useful_states();
        let expected = [1, 2, 3, 4, 5, 6, 7]
            .iter()
            .map(|i| i.to_owned())
            .collect::<HashSet<i32>>();
        assert_eq!(expected, result);
    }
}

/// Type alias for [`NonDeterministicFiniteAutomata`].
pub type Nfa<State, Transition> = NonDeterministicFiniteAutomata<State, Transition>;

/// A representation for non-deterministic finite automata.
#[derive(Debug, Clone)]
pub struct NonDeterministicFiniteAutomata<State, Transition>
where
    State: Eq + Hash + Clone,
    Transition: Eq + Hash + Clone,
{
    /// Deterministic finite automata states.
    pub states: HashSet<State>,
    /// Deterministic finite automata transition symbols.
    sigma: HashSet<Transition>,
    /// Deterministic finite automata initial state-indexes.
    pub initial_states: HashMap<State, HashSet<Transition>>,
    /// Deterministic finite automata final state-indexes.
    pub final_states: HashMap<State, HashSet<Transition>>,
    /// Deterministic finite automata transition functions.
    /// Map of state indexes to map of transitions to state indexes.
    delta: Deltas<State, Transition>,
    /// The inverse paths of delta.
    /// This structure helps algorithms requiring interation in the "inverse" order.
    idelta: Deltas<State, Transition>,
}

impl<State, Transition> NonDeterministicFiniteAutomata<State, Transition>
where
    State: Eq + Hash + Clone,
    Transition: Eq + Hash + Clone,
{
    /// Construct a new non-deterministic finite automata.
    #[must_use]
    pub fn new() -> Self {
        Self {
            states: HashSet::new(),
            sigma: HashSet::new(),
            initial_states: HashMap::new(),
            final_states: HashMap::new(),
            delta: HashMap::new(),
            idelta: HashMap::new(),
        }
    }

    /// Add a state to the automata.
    pub fn add_state(&mut self, state: State) {
        self.states.insert(state);
    }

    /// Add an initial state to the automata.
    pub fn add_initial(&mut self, state: State, symbol: Transition) {
        self.states.insert(state.clone());
        if let Some(symbols) = self.initial_states.get_mut(&state) {
            symbols.insert(symbol);
        } else {
            let mut symbols = HashSet::new();
            symbols.insert(symbol);
            self.initial_states.insert(state, symbols);
        }
    }

    /// Add a final state to the automata.
    pub fn add_final(&mut self, state: State, symbol: Transition) {
        self.states.insert(state.clone());
        if let Some(symbols) = self.final_states.get_mut(&state) {
            symbols.insert(symbol);
        } else {
            let mut symbols = HashSet::new();
            symbols.insert(symbol);
            self.final_states.insert(state, symbols);
        }
    }

    /// Add a new symbol to the automata alphabet.
    pub fn add_sigma(&mut self, sigma: Transition) {
        self.sigma.insert(sigma);
    }

    fn add_delta(&mut self, source: State, symbol: Transition, destination: State, delta: &Delta) {
        let delta = match delta {
            Delta::Delta => &mut self.delta,
            Delta::IDelta => &mut self.idelta,
        };
        if let Some(transitions) = delta.get_mut(&source) {
            if let Some(destinations) = transitions.get_mut(&symbol) {
                destinations.insert(destination);
            } else {
                let mut destinations = HashSet::new();
                destinations.insert(destination);
                transitions.insert(symbol, destinations);
            }
        } else {
            let mut transitions = HashMap::new();
            let mut destinations = HashSet::new();
            destinations.insert(destination);
            transitions.insert(symbol, destinations);
            delta.insert(source, transitions);
        }
    }

    // Add a transition from `source` to `destination`, consuming `symbol`.
    pub fn add_transition(&mut self, source: State, symbol: Transition, destination: State) {
        // TODO check for state existence or add regardless
        self.add_sigma(symbol.clone());
        self.add_delta(
            source.clone(),
            symbol.clone(),
            destination.clone(),
            &Delta::Delta,
        );
        self.add_delta(destination, symbol, source, &Delta::IDelta);
    }

    // Add a transition from `source` to `destinations`, consuming `symbol`.
    pub fn add_non_deterministic_transitions(
        &mut self,
        source: &State,
        symbol: &Transition,
        destinations: impl Iterator<Item = State>,
    ) {
        // TODO check for state existence or add regardless
        self.add_sigma(symbol.clone());
        for destination in destinations {
            self.add_delta(
                source.clone(),
                symbol.clone(),
                destination.clone(),
                &Delta::Delta,
            );
            self.add_delta(
                destination.clone(),
                symbol.clone(),
                source.clone(),
                &Delta::IDelta,
            );
        }
    }

    /// Compute the automata productive states.
    #[must_use]
    pub fn productive_states(&self) -> HashSet<State> {
        let mut stack: VecDeque<_> = self.final_states.keys().collect();
        // productive == visited
        let mut productive = HashSet::new();
        while let Some(state) = stack.pop_back() {
            if productive.insert(state.clone()) {
                if let Some(states) = self.idelta.get(state).map(|transitions| {
                    transitions
                        .values()
                        .flat_map(std::collections::HashSet::iter)
                }) {
                    stack.extend(states)
                }
            }
        }
        productive
    }

    /// Compute the automata non-productive states.
    ///
    /// This is done by calling [`NonDeterministicFiniteAutomata::productive_states`] and performing a diference against the full state set.
    #[must_use]
    pub fn non_productive_states(&self) -> HashSet<State> {
        // TODO check if the clone should be here or if we can return `HashSet<&State>`
        self.states
            .difference(&self.productive_states())
            .cloned()
            .collect()
    }

    /// Compute the automata useful states.
    #[must_use]
    pub fn useful_states(&self) -> HashSet<State> {
        // TODO this could benefit from some "caching" of results on productive
        let productive = self.productive_states();
        let mut stack: VecDeque<_> = self.initial_states.keys().collect();
        // productive == visited
        let mut reachable = HashSet::new();
        while let Some(state) = stack.pop_back() {
            if reachable.insert(state.clone()) {
                if let Some(states) = self.delta.get(state).map(|transitions| {
                    transitions
                        .values()
                        .flat_map(std::collections::HashSet::iter)
                }) {
                    stack.extend(states)
                }
            }
        }
        productive.intersection(&reachable).cloned().collect()
    }

    /// Compute the automata non-useful states.
    ///
    /// This is done by calling [`NonDeterministicFiniteAutomata::useful_states`] and performing a diference against the full state set.
    #[must_use]
    pub fn non_useful_states(&self) -> HashSet<State> {
        // TODO check if the clone should be here or if we can return `HashSet<&State>`
        self.states
            .difference(&self.useful_states())
            .cloned()
            .collect()
    }
}

/// Implementation of the [`Default`] trait for a [`NonDeterministicFiniteAutomata`].
impl<State, Transition> Default for NonDeterministicFiniteAutomata<State, Transition>
where
    State: Eq + Hash + Clone,
    Transition: Eq + Hash + Clone,
{
    /// This function is equivalent to [`NonDeterministicFiniteAutomata::new`].
    fn default() -> Self {
        Self::new()
    }
}

/// Tests for [`NonDeterministicFiniteAutomata`].
#[cfg(test)]
mod nfa_tests {
    use super::test_traits::*;
    use super::*;

    fn setup_automata() -> Nfa<i32, ()> {
        let mut nfa = Nfa::new();
        let state_list = [1, 2, 3, 4, 5, 6, 7];
        for &state in state_list.iter() {
            nfa.add_state(state);
        }
        nfa.add_initial(1, ());
        nfa.add_final(7, ());
        let transition_list = [
            (1, 2),
            (1, 3),
            (2, 6),
            (3, 4),
            (3, 5),
            (3, 6),
            (4, 5),
            (5, 7),
            (6, 7),
        ];
        for &(src, dst) in transition_list.iter() {
            nfa.add_transition(src, (), dst);
        }
        nfa
    }

    fn setup_automata_loop() -> Nfa<i32, ()> {
        let mut nfa = Nfa::new();
        nfa.add_initial(1, ());
        nfa.add_final(2, ());
        nfa.add_transition(1, (), 2);
        nfa.add_transition(2, (), 1);
        nfa
    }

    #[test]
    fn test_add_state() {
        let nfa = setup_automata();
        let expected_states = [1, 2, 3, 4, 5, 6, 7].into_hash_set();
        let result_states = nfa.states;
        assert_eq!(expected_states, result_states);
    }

    #[test]
    fn test_add_initial_state() {
        let nfa = setup_automata();
        let expected_states = [1].into_hash_set();
        let result_states: HashSet<i32> = nfa.initial_states.keys().map(|i| *i).collect();
        assert_eq!(expected_states, result_states);
    }

    #[test]
    fn test_add_final_state() {
        let nfa = setup_automata();
        let expected_states = [7].into_hash_set();
        let result_states: HashSet<i32> = nfa.final_states.keys().map(|i| *i).collect();
        assert_eq!(expected_states, result_states);
    }

    #[test]
    fn test_add_transition() {
        let nfa = setup_automata();
        let expected_deltas = [
            (1, 2),
            (1, 3),
            (2, 6),
            (3, 4),
            (3, 5),
            (3, 6),
            (4, 5),
            (5, 7),
            (6, 7),
        ]
        .iter()
        .map(|t| t.to_owned())
        .collect::<HashSet<_>>();
        let expected_ideltas = expected_deltas
            .iter()
            .map(|(fst, snd)| (*snd, *fst))
            .map(|t| t.to_owned())
            .collect::<HashSet<_>>();
        let mut result_deltas = HashSet::new();
        for (src, transitions) in nfa.delta {
            for destinations in transitions.values() {
                for &dst in destinations {
                    result_deltas.insert((src, dst));
                }
            }
        }
        let mut result_ideltas = HashSet::new();
        for (src, transitions) in nfa.idelta {
            for destinations in transitions.values() {
                for &dst in destinations {
                    result_ideltas.insert((src, dst));
                }
            }
        }
        assert_eq!(expected_deltas, result_deltas);
        assert_eq!(expected_ideltas, result_ideltas);
    }

    #[test]
    fn test_add_non_deterministic_transition() {
        let mut nfa = Nfa::new();
        [1, 2, 3, 4, 5, 6, 7]
            .iter()
            .map(|i| *i)
            .for_each(|i| nfa.add_state(i));
        nfa.add_initial(1, ());
        nfa.add_final(7, ());
        let non_deterministic_transitions = vec![
            (1, vec![2, 3]),
            (2, vec![6]),
            (3, vec![4, 5, 6]),
            (4, vec![5]),
            (5, vec![7]),
            (6, vec![7]),
        ];
        for (source, destinations) in non_deterministic_transitions {
            nfa.add_non_deterministic_transitions(&source, &(), destinations.into_iter());
        }
        let expected_deltas = [
            (1, 2),
            (1, 3),
            (2, 6),
            (3, 4),
            (3, 5),
            (3, 6),
            (4, 5),
            (5, 7),
            (6, 7),
        ]
        .iter()
        .map(|t| t.to_owned())
        .collect::<HashSet<_>>();
        let expected_ideltas = expected_deltas
            .iter()
            .map(|(fst, snd)| (*snd, *fst))
            .map(|t| t.to_owned())
            .collect::<HashSet<_>>();
        let mut result_deltas = HashSet::new();
        for (src, transitions) in nfa.delta {
            for destinations in transitions.values() {
                for &dst in destinations {
                    result_deltas.insert((src, dst));
                }
            }
        }
        let mut result_ideltas = HashSet::new();
        for (src, transitions) in nfa.idelta {
            for destinations in transitions.values() {
                for &dst in destinations {
                    result_ideltas.insert((src, dst));
                }
            }
        }
        assert_eq!(expected_deltas, result_deltas);
        assert_eq!(expected_ideltas, result_ideltas);
    }

    #[test]
    fn test_productive() {
        let nfa = setup_automata();
        let result = nfa.productive_states();
        let expected = [1, 2, 3, 4, 5, 6, 7]
            .iter()
            .map(|i| i.to_owned())
            .collect::<HashSet<i32>>();
        assert_eq!(expected, result);
    }

    #[test]
    fn test_productive_loop() {
        let nfa = setup_automata_loop();
        let result = nfa.productive_states();
        let expected = [1, 2]
            .iter()
            .map(|i| i.to_owned())
            .collect::<HashSet<i32>>();
        assert_eq!(expected, result);
    }

    #[test]
    fn test_useful() {
        let nfa = setup_automata();
        let result = nfa.useful_states();
        let expected = [1, 2, 3, 4, 5, 6, 7]
            .iter()
            .map(|i| i.to_owned())
            .collect::<HashSet<i32>>();
        assert_eq!(expected, result);
    }
}

/// Module containing useful traits for tests.
#[cfg(test)] // should only be available for tests
mod test_traits {
    use std::{collections::HashSet, hash::Hash, rc::Rc};

    /// A conversion to HashSet<T> trait.
    pub(crate) trait IntoHashSet<T>
    where
        T: Eq + Hash,
    {
        fn into_hash_set(self) -> HashSet<T>;
    }

    /// Implementation of [IntoHashSet<T>] for a slice of `i32` integers.
    impl<const N: usize> IntoHashSet<i32> for [i32; N] {
        fn into_hash_set(self) -> HashSet<i32> {
            self.iter().map(|t| t.to_owned()).collect()
        }
    }

    /// Implementation of [IntoHashSet<T>] for an `HashSet<Rc<i32>>`.
    impl IntoHashSet<i32> for HashSet<Rc<i32>> {
        fn into_hash_set(self) -> HashSet<i32> {
            self.iter().map(|i| *i.to_owned()).collect()
        }
    }
}