heapless_graphs 0.2.3

Implementation of composable graphs for no_alloc environments
Documentation
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
use crate::containers::maps::MapTrait;
use crate::conversions::FromGraph;
use crate::graph::{
    integrity_check, Graph, GraphError, GraphWithMutableEdges, GraphWithMutableNodes, NodeIndex,
};
use crate::nodes::{MutableNodes, NodesIterable};

pub struct MapAdjacencyList<NI, E, M>
where
    NI: NodeIndex,
    E: NodesIterable<Node = NI>,
    M: MapTrait<NI, E>,
{
    nodes: M,
    _phantom: core::marker::PhantomData<E>,
}

impl<NI, E, M> MapAdjacencyList<NI, E, M>
where
    NI: NodeIndex,
    E: NodesIterable<Node = NI>,
    M: MapTrait<NI, E>,
{
    /// Create new map adjacency list with validation
    ///
    /// This function validates that all edge destinations exist in the node set.
    /// Returns an error if any edge references a non-existent node.
    pub fn new(nodes: M) -> Result<Self, GraphError<NI>>
    where
        NI: Copy,
        Self: Graph<NI, Error = GraphError<NI>>,
    {
        let result = Self::new_unchecked(nodes);
        integrity_check::<NI, _>(&result)?;
        Ok(result)
    }

    pub fn new_unchecked(nodes: M) -> Self {
        Self {
            nodes,
            _phantom: core::marker::PhantomData,
        }
    }
}

impl<NI, E, M> FromGraph<NI, GraphError<NI>> for MapAdjacencyList<NI, E, M>
where
    NI: NodeIndex,
    E: NodesIterable<Node = NI> + MutableNodes<NI> + Default,
    M: MapTrait<NI, E> + Default,
{
    fn from_graph<G>(source_graph: &G) -> Result<Self, GraphError<NI>>
    where
        G: Graph<NI>,
        GraphError<NI>: From<G::Error>,
    {
        let mut nodes = M::new();
        for node in source_graph.iter_nodes()? {
            let mut outbound = E::default();
            for edge in source_graph.outgoing_edges(node)? {
                outbound.add(edge);
            }
            nodes
                .insert(node, outbound)
                .map_err(|_| GraphError::OutOfCapacity)?;
        }
        Ok(Self {
            nodes,
            _phantom: Default::default(),
        })
    }
}

impl<NI, E, M> Graph<NI> for MapAdjacencyList<NI, E, M>
where
    M: MapTrait<NI, E>,
    NI: NodeIndex + Eq + core::hash::Hash + Copy,
    E: NodesIterable<Node = NI>,
{
    type Error = GraphError<NI>;

    fn iter_nodes(&self) -> Result<impl Iterator<Item = NI>, Self::Error> {
        Ok(self.nodes.keys().copied())
    }

    fn iter_edges(&self) -> Result<impl Iterator<Item = (NI, NI)>, Self::Error> {
        Ok(self
            .nodes
            .iter()
            .flat_map(|(n, e_container)| e_container.iter_nodes().map(move |m| (*n, *m))))
    }

    /// Optimized O(1) contains_node for map adjacency list
    fn contains_node(&self, node: NI) -> Result<bool, Self::Error> {
        Ok(self.nodes.contains_key(&node))
    }

    /// Optimized O(out-degree) outgoing_edges for map adjacency list
    fn outgoing_edges(&self, node: NI) -> Result<impl Iterator<Item = NI>, Self::Error> {
        // The key insight: use Option to unify the iterator types, then flatten
        let edges_option = self.nodes.get(&node).map(|edges| edges.iter_nodes());
        Ok(edges_option.into_iter().flatten().copied())
    }
}

impl<NI, E, M> GraphWithMutableNodes<NI> for MapAdjacencyList<NI, E, M>
where
    NI: NodeIndex + Eq + core::hash::Hash + Copy,
    E: NodesIterable<Node = NI> + Default,
    M: MapTrait<NI, E>,
{
    fn add_node(&mut self, node: NI) -> Result<(), Self::Error> {
        if self.nodes.contains_key(&node) {
            return Err(GraphError::DuplicateNode(node));
        }
        let outbound = E::default();
        self.nodes
            .insert(node, outbound)
            .map_err(|_| GraphError::OutOfCapacity)?;
        Ok(())
    }

    fn remove_node(&mut self, node: NI) -> Result<(), Self::Error> {
        // Check if node exists
        if !self.nodes.contains_key(&node) {
            return Err(GraphError::NodeNotFound(node));
        }

        // Check if node has incoming edges
        if self.incoming_edges(node)?.next().is_some() {
            return Err(GraphError::NodeHasIncomingEdges(node));
        }

        // Check if node has outgoing edges
        if self.outgoing_edges(node)?.next().is_some() {
            return Err(GraphError::NodeHasOutgoingEdges(node));
        }

        // Remove the node (this automatically removes all its outgoing edges)
        self.nodes.remove(&node);
        Ok(())
    }
}

impl<NI, E, M> GraphWithMutableEdges<NI> for MapAdjacencyList<NI, E, M>
where
    NI: NodeIndex + Eq + core::hash::Hash + Copy + PartialEq,
    E: NodesIterable<Node = NI> + MutableNodes<NI>,
    M: MapTrait<NI, E>,
{
    fn add_edge(&mut self, source: NI, destination: NI) -> Result<(), Self::Error> {
        // Validate both nodes exist with minimal lookups:
        // 1. Use get() for destination (read-only)
        // 2. Use get_mut() for source (gets mutable ref + validates existence)

        // Check destination exists first to preserve error precedence when both are invalid
        if self.nodes.get(&destination).is_none() {
            return Err(GraphError::EdgeHasInvalidNode(destination));
        }

        // Get mutable reference to source node's adjacency list (validates source exists)
        let source_edges = self
            .nodes
            .get_mut(&source)
            .ok_or(GraphError::EdgeHasInvalidNode(source))?;

        // Add the destination to the source's adjacency list
        source_edges
            .add(destination)
            .ok_or(GraphError::OutOfCapacity)?;

        Ok(())
    }

    fn remove_edge(&mut self, source: NI, destination: NI) -> Result<(), Self::Error> {
        // Get mutable reference to source node's adjacency list
        let source_edges = self
            .nodes
            .get_mut(&source)
            .ok_or(GraphError::EdgeNotFound(source, destination))?;

        // Remove the destination from the source's adjacency list
        source_edges
            .remove(destination)
            .ok_or(GraphError::EdgeNotFound(source, destination))?;

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::containers::maps::staticdict::Dictionary;
    use crate::edgelist::edge_list::EdgeList;
    use crate::graph::GraphWithMutableEdges;
    use crate::nodes::NodeStructOption;
    use crate::tests::{collect, collect_sorted};

    #[test]
    fn test_map_adjacency_list_new() {
        let mut dict = Dictionary::<usize, [usize; 2], 5>::new();
        dict.insert(0, [1, 2]).unwrap();
        dict.insert(1, [2, 0]).unwrap();
        dict.insert(2, [0, 0]).unwrap();

        let graph = MapAdjacencyList::new(dict).unwrap();

        let mut nodes = [0usize; 4];
        let nodes_slice = collect_sorted(graph.iter_nodes().unwrap(), &mut nodes);
        assert_eq!(nodes_slice, &[0, 1, 2]);
    }

    #[test]
    fn test_map_adjacency_list_new_unchecked() {
        let mut dict = Dictionary::<usize, [usize; 2], 5>::new();
        dict.insert(0, [1, 2]).unwrap();
        dict.insert(1, [2, 0]).unwrap();
        dict.insert(2, [0, 0]).unwrap();

        let graph = MapAdjacencyList::new_unchecked(dict);

        let mut nodes = [0usize; 4];
        let nodes_slice = collect_sorted(graph.iter_nodes().unwrap(), &mut nodes);
        assert_eq!(nodes_slice, &[0, 1, 2]);
    }

    #[test]
    fn test_map_adjacency_list_empty() {
        let dict = Dictionary::<usize, [usize; 0], 5>::new();
        let graph = MapAdjacencyList::new(dict).unwrap();

        assert_eq!(graph.iter_nodes().unwrap().count(), 0);
    }

    #[test]
    fn test_map_adjacency_list_single_node() {
        let mut dict = Dictionary::<usize, [usize; 0], 5>::new();
        dict.insert(42, []).unwrap();

        let graph = MapAdjacencyList::new(dict).unwrap();

        let mut nodes = [0usize; 2];
        let nodes_slice = collect(graph.iter_nodes().unwrap(), &mut nodes);
        assert_eq!(nodes_slice, &[42]);
    }

    #[test]
    fn test_graph_iter_nodes() {
        let mut dict = Dictionary::<usize, [usize; 2], 5>::new();
        dict.insert(0, [1, 2]).unwrap();
        dict.insert(1, [2, 0]).unwrap();
        dict.insert(2, [0, 0]).unwrap();

        let graph = MapAdjacencyList::new_unchecked(dict);

        let mut nodes = [0usize; 4];
        let nodes_slice = collect(graph.iter_nodes().unwrap(), &mut nodes);
        assert_eq!(nodes_slice, &[2, 0, 1]);
    }

    #[test]
    fn test_graph_iter_edges() {
        let mut dict = Dictionary::<usize, [usize; 2], 5>::new();
        dict.insert(0, [1, 2]).unwrap();
        dict.insert(1, [2, 0]).unwrap();
        dict.insert(2, [0, 0]).unwrap();

        let graph = MapAdjacencyList::new_unchecked(dict);

        let mut edges = [(0usize, 0usize); 8];
        let edges_slice = collect(graph.iter_edges().unwrap(), &mut edges);
        assert_eq!(edges_slice.len(), 6);
        assert_eq!(
            edges_slice,
            &[(2, 0), (2, 0), (0, 1), (0, 2), (1, 2), (1, 0)]
        );
    }

    #[test]
    fn test_graph_contains_node() {
        let mut dict = Dictionary::<usize, [usize; 2], 5>::new();
        dict.insert(0, [1, 2]).unwrap();
        dict.insert(1, [2, 0]).unwrap();
        dict.insert(2, [0, 0]).unwrap();

        let graph = MapAdjacencyList::new_unchecked(dict);

        assert!(graph.contains_node(0).unwrap());
        assert!(graph.contains_node(1).unwrap());
        assert!(graph.contains_node(2).unwrap());
        assert!(!graph.contains_node(3).unwrap());
        assert!(!graph.contains_node(42).unwrap());
    }

    #[test]
    fn test_graph_outgoing_edges() {
        let mut dict = Dictionary::<usize, [usize; 2], 5>::new();
        dict.insert(0, [1, 2]).unwrap();
        dict.insert(1, [2, 0]).unwrap();
        dict.insert(2, [0, 0]).unwrap();

        let graph = MapAdjacencyList::new_unchecked(dict);

        // Test node 0 outgoing edges
        let mut edges = [0usize; 4];
        let edges_slice = collect(graph.outgoing_edges(0).unwrap(), &mut edges);
        assert_eq!(edges_slice, &[1, 2]);

        // Test node 1 outgoing edges
        let mut edges = [0usize; 4];
        let edges_slice = collect(graph.outgoing_edges(1).unwrap(), &mut edges);
        assert_eq!(edges_slice, &[2, 0]);

        // Test node 2 outgoing edges
        let mut edges = [0usize; 4];
        let edges_slice = collect(graph.outgoing_edges(2).unwrap(), &mut edges);
        assert_eq!(edges_slice, &[0, 0]); // Both edges go to node 0

        // Test non-existent node
        assert_eq!(graph.outgoing_edges(99).unwrap().count(), 0);
    }

    #[test]
    fn test_graph_empty_graph() {
        let dict = Dictionary::<usize, [usize; 0], 5>::new();
        let graph = MapAdjacencyList::new_unchecked(dict);

        // Test empty node iteration
        assert_eq!(graph.iter_nodes().unwrap().count(), 0);

        // Test empty edge iteration
        assert_eq!(graph.iter_edges().unwrap().count(), 0);

        // Test contains_node on empty graph
        assert!(!graph.contains_node(0).unwrap());
        assert!(!graph.contains_node(42).unwrap());
    }

    #[test]
    fn test_graph_single_node_no_edges() {
        let mut dict = Dictionary::<usize, [usize; 0], 5>::new();
        dict.insert(42, []).unwrap();

        let graph = MapAdjacencyList::new_unchecked(dict);

        // Test single node
        let mut nodes = [0usize; 2];
        let nodes_slice = collect(graph.iter_nodes().unwrap(), &mut nodes);
        assert_eq!(nodes_slice, &[42]);

        // Test contains_node
        assert!(graph.contains_node(42).unwrap());
        assert!(!graph.contains_node(0).unwrap());

        // Test no edges
        assert_eq!(graph.iter_edges().unwrap().count(), 0);

        // Test no outgoing edges
        assert_eq!(graph.outgoing_edges(42).unwrap().count(), 0);
    }

    #[test]
    fn test_graph_self_loops() {
        let mut dict = Dictionary::<usize, [usize; 2], 5>::new();
        dict.insert(0, [0, 1]).unwrap();
        dict.insert(1, [1, 1]).unwrap();

        let graph = MapAdjacencyList::new_unchecked(dict);

        // Test self-loop edges
        let mut edges = [(0usize, 0usize); 8];
        let edges_slice = collect_sorted(graph.iter_edges().unwrap(), &mut edges);
        assert_eq!(edges_slice, &[(0, 0), (0, 1), (1, 1), (1, 1)]);

        // Test outgoing edges with self-loops
        let mut edges = [0usize; 4];
        let edges_slice = collect(graph.outgoing_edges(0).unwrap(), &mut edges);
        assert_eq!(edges_slice, &[0, 1]);
    }

    #[test]
    fn test_graph_multiple_nodes_same_target() {
        let mut dict = Dictionary::<usize, [usize; 2], 5>::new();
        dict.insert(0, [1, 1]).unwrap();
        dict.insert(2, [1, 0]).unwrap();
        dict.insert(1, [0, 0]).unwrap();

        let graph = MapAdjacencyList::new_unchecked(dict);

        // Test multiple edges pointing to same target
        let mut edges = [(0usize, 0usize); 8];
        let edges_slice = collect_sorted(graph.iter_edges().unwrap(), &mut edges);
        assert_eq!(
            edges_slice,
            &[(0, 1), (0, 1), (1, 0), (1, 0), (2, 0), (2, 1)]
        );
    }

    #[test]
    fn test_map_adjacency_list_not_all_edges() {
        let mut dict = Dictionary::<_, NodeStructOption<3, _>, 5>::new();
        dict.insert(0, NodeStructOption([Some(1), Some(2), None]))
            .unwrap();
        dict.insert(1, NodeStructOption([Some(2), Some(0), None]))
            .unwrap();
        dict.insert(2, NodeStructOption([Some(0), None, None]))
            .unwrap();

        let graph = MapAdjacencyList::new_unchecked(dict);
        let mut edges = [(0usize, 0usize); 8];
        let edges_slice = collect(graph.iter_edges().unwrap(), &mut edges);
        assert_eq!(edges_slice, &[(2, 0), (0, 1), (0, 2), (1, 2), (1, 0)]);
    }

    #[test]
    fn test_map_adjacency_list_from_graph() {
        let src_graph = EdgeList::<8, _, _>::new([(0, 1), (0, 2), (1, 3), (2, 3)]);
        let adjlist =
            MapAdjacencyList::<_, _, Dictionary<_, NodeStructOption<5, _>, 5>>::from_graph(
                &src_graph,
            )
            .unwrap();

        let mut nodes = [0usize; 8];
        let node_slice = collect_sorted(adjlist.iter_nodes().unwrap(), &mut nodes);
        assert_eq!(node_slice, &[0usize, 1, 2, 3]);

        let mut edges = [(0usize, 0usize); 8];
        let edge_slice = collect_sorted(adjlist.iter_edges().unwrap(), &mut edges);
        assert_eq!(edge_slice, &[(0, 1), (0, 2), (1, 3), (2, 3)]);

        assert_eq!(
            collect_sorted(adjlist.outgoing_edges(0).unwrap(), &mut nodes),
            &[1, 2]
        );
        assert_eq!(
            collect_sorted(adjlist.outgoing_edges(1).unwrap(), &mut nodes),
            &[3]
        );
        assert_eq!(
            collect_sorted(adjlist.outgoing_edges(2).unwrap(), &mut nodes),
            &[3]
        );
        assert_eq!(
            collect_sorted(adjlist.outgoing_edges(3).unwrap(), &mut nodes),
            &[]
        );
    }

    #[test]
    fn test_add_node_to_empty_graph() {
        let dict = Dictionary::<usize, NodeStructOption<2, usize>, 5>::new();
        let mut graph = MapAdjacencyList::new(dict).unwrap();

        // Add a node to empty graph
        graph.add_node(42).unwrap();

        // Verify the node was added
        assert!(graph.contains_node(42).unwrap());
        assert_eq!(graph.iter_nodes().unwrap().count(), 1);
        assert_eq!(graph.outgoing_edges(42).unwrap().count(), 0);
    }

    #[test]
    fn test_add_node_to_existing_graph() {
        let mut dict = Dictionary::<usize, NodeStructOption<2, usize>, 5>::new();
        dict.insert(0, NodeStructOption([Some(1), None])).unwrap();
        dict.insert(1, NodeStructOption([None, None])).unwrap();

        let mut graph = MapAdjacencyList::new(dict).unwrap();

        // Add a new node
        graph.add_node(2).unwrap();

        // Verify all nodes exist
        assert!(graph.contains_node(0).unwrap());
        assert!(graph.contains_node(1).unwrap());
        assert!(graph.contains_node(2).unwrap());
        assert_eq!(graph.iter_nodes().unwrap().count(), 3);

        // Verify new node has no outgoing edges
        assert_eq!(graph.outgoing_edges(2).unwrap().count(), 0);

        // Verify existing edges are preserved
        let mut edges = [0usize; 2];
        let edges_slice = collect(graph.outgoing_edges(0).unwrap(), &mut edges);
        assert_eq!(edges_slice, &[1]);
    }

    #[test]
    fn test_add_duplicate_node() {
        let mut dict = Dictionary::<usize, NodeStructOption<1, usize>, 5>::new();
        dict.insert(0, NodeStructOption([None])).unwrap();

        let mut graph = MapAdjacencyList::new(dict).unwrap();

        // Try to add duplicate node
        let result = graph.add_node(0);

        // Should return error
        assert!(matches!(result, Err(GraphError::DuplicateNode(0))));

        // Original graph should be unchanged
        assert_eq!(graph.iter_nodes().unwrap().count(), 1);
    }

    #[test]
    fn test_add_node_capacity_exceeded() {
        // Create a dictionary with capacity for only 2 nodes
        let mut dict = Dictionary::<usize, NodeStructOption<1, usize>, 2>::new();
        dict.insert(0, NodeStructOption([None])).unwrap();
        dict.insert(1, NodeStructOption([None])).unwrap();

        let mut graph = MapAdjacencyList::new(dict).unwrap();

        // Try to add a third node (should exceed capacity)
        let result = graph.add_node(2);

        // Should return capacity error
        assert!(matches!(result, Err(GraphError::OutOfCapacity)));

        // Original graph should be unchanged
        assert_eq!(graph.iter_nodes().unwrap().count(), 2);
    }

    #[test]
    fn test_add_multiple_nodes() {
        let dict = Dictionary::<usize, NodeStructOption<3, usize>, 10>::new();
        let mut graph = MapAdjacencyList::new(dict).unwrap();

        // Add multiple nodes
        graph.add_node(10).unwrap();
        graph.add_node(20).unwrap();
        graph.add_node(30).unwrap();

        // Verify all nodes were added
        assert!(graph.contains_node(10).unwrap());
        assert!(graph.contains_node(20).unwrap());
        assert!(graph.contains_node(30).unwrap());
        assert_eq!(graph.iter_nodes().unwrap().count(), 3);

        // Verify no edges exist between nodes
        assert_eq!(graph.iter_edges().unwrap().count(), 0);

        // Verify each node has no outgoing edges
        assert_eq!(graph.outgoing_edges(10).unwrap().count(), 0);
        assert_eq!(graph.outgoing_edges(20).unwrap().count(), 0);
        assert_eq!(graph.outgoing_edges(30).unwrap().count(), 0);
    }

    #[test]
    fn test_add_edge_success() {
        let mut dict = Dictionary::<usize, NodeStructOption<3, usize>, 5>::new();
        dict.insert(0, NodeStructOption([None, None, None]))
            .unwrap(); // Empty adjacency list
        dict.insert(1, NodeStructOption([None, None, None]))
            .unwrap();
        dict.insert(2, NodeStructOption([None, None, None]))
            .unwrap();

        let mut graph = MapAdjacencyList::new(dict).unwrap();

        // Add edges between existing nodes
        assert!(graph.add_edge(0, 1).is_ok());
        assert!(graph.add_edge(1, 2).is_ok());
        assert!(graph.add_edge(0, 2).is_ok());

        // Verify edges were added by checking edge iteration
        let edge_count = graph.iter_edges().unwrap().count();
        assert_eq!(edge_count, 3);

        // Verify specific edges exist
        let mut edges = [(0usize, 0usize); 8];
        let edges_slice = collect_sorted(graph.iter_edges().unwrap(), &mut edges);
        assert_eq!(edges_slice, &[(0, 1), (0, 2), (1, 2)]);
    }

    #[test]
    fn test_add_edge_invalid_nodes() {
        let mut dict = Dictionary::<usize, NodeStructOption<2, usize>, 5>::new();
        dict.insert(0, NodeStructOption([None, None])).unwrap();
        dict.insert(1, NodeStructOption([None, None])).unwrap(); // Only nodes 0, 1

        let mut graph = MapAdjacencyList::new(dict).unwrap();

        // Try to add edge with non-existent source node
        let result = graph.add_edge(2, 1);
        assert!(matches!(result, Err(GraphError::EdgeHasInvalidNode(2))));

        // Try to add edge with non-existent destination node
        let result = graph.add_edge(0, 3);
        assert!(matches!(result, Err(GraphError::EdgeHasInvalidNode(3))));

        // Try to add edge with both nodes non-existent
        let result = graph.add_edge(5, 6);
        assert!(matches!(result, Err(GraphError::EdgeHasInvalidNode(6)))); // Destination checked first in optimized version
    }

    #[test]
    fn test_add_edge_capacity_exceeded() {
        let mut dict = Dictionary::<usize, NodeStructOption<2, usize>, 5>::new();
        dict.insert(0, NodeStructOption([None, None])).unwrap(); // Capacity for only 2 edges
        dict.insert(1, NodeStructOption([None, None])).unwrap();
        dict.insert(2, NodeStructOption([None, None])).unwrap();

        let mut graph = MapAdjacencyList::new(dict).unwrap();

        // Add edges up to capacity for node 0
        assert!(graph.add_edge(0, 1).is_ok());
        assert!(graph.add_edge(0, 2).is_ok());

        // Try to add one more edge from node 0 (should exceed capacity)
        let result = graph.add_edge(0, 1); // Try to add duplicate or different edge
        assert!(matches!(result, Err(GraphError::OutOfCapacity)));
    }

    #[test]
    fn test_remove_edge_success() {
        let mut dict = Dictionary::<usize, NodeStructOption<3, usize>, 5>::new();
        dict.insert(0, NodeStructOption([Some(1), Some(2), None]))
            .unwrap();
        dict.insert(1, NodeStructOption([Some(2), None, None]))
            .unwrap();
        dict.insert(2, NodeStructOption([None, None, None]))
            .unwrap();

        let mut graph = MapAdjacencyList::new(dict).unwrap();

        // Verify initial edge count
        assert_eq!(graph.iter_edges().unwrap().count(), 3);

        // Remove an edge
        assert!(graph.remove_edge(0, 1).is_ok());

        // Verify edge was removed
        assert_eq!(graph.iter_edges().unwrap().count(), 2);
        let mut edges = [(0usize, 0usize); 8];
        let edges_slice = collect_sorted(graph.iter_edges().unwrap(), &mut edges);
        assert_eq!(edges_slice, &[(0, 2), (1, 2)]);
    }

    #[test]
    fn test_remove_edge_not_found() {
        let mut dict = Dictionary::<usize, NodeStructOption<2, usize>, 5>::new();
        dict.insert(0, NodeStructOption([Some(1), None])).unwrap();
        dict.insert(1, NodeStructOption([None, None])).unwrap();
        dict.insert(2, NodeStructOption([None, None])).unwrap();

        let mut graph = MapAdjacencyList::new(dict).unwrap();

        // Try to remove edge that doesn't exist
        let result = graph.remove_edge(0, 2);
        assert!(matches!(result, Err(GraphError::EdgeNotFound(0, 2))));

        // Verify original edges are still there
        assert_eq!(graph.iter_edges().unwrap().count(), 1);
    }

    #[test]
    fn test_remove_edge_with_nonexistent_nodes() {
        let mut dict = Dictionary::<usize, NodeStructOption<1, usize>, 5>::new();
        dict.insert(0, NodeStructOption([Some(1)])).unwrap();
        dict.insert(1, NodeStructOption([None])).unwrap(); // Only nodes 0, 1

        let mut graph = MapAdjacencyList::new(dict).unwrap();

        // Try to remove edge involving non-existent source node
        let result = graph.remove_edge(2, 3);
        assert!(matches!(result, Err(GraphError::EdgeNotFound(2, 3))));

        // Verify original edge is still there
        assert_eq!(graph.iter_edges().unwrap().count(), 1);
    }

    #[test]
    fn test_map_adjacency_list_from_graph_trait() {
        let src_graph = EdgeList::<8, _, _>::new([(0, 1), (0, 2), (1, 3), (2, 3)]);
        let adjlist =
            MapAdjacencyList::<_, _, Dictionary<_, NodeStructOption<5, _>, 5>>::from_graph(
                &src_graph,
            )
            .unwrap();

        let mut nodes = [0usize; 8];
        let node_slice = collect_sorted(adjlist.iter_nodes().unwrap(), &mut nodes);
        assert_eq!(node_slice, &[0usize, 1, 2, 3]);

        let mut edges = [(0usize, 0usize); 8];
        let edge_slice = collect_sorted(adjlist.iter_edges().unwrap(), &mut edges);
        assert_eq!(edge_slice, &[(0, 1), (0, 2), (1, 3), (2, 3)]);

        assert_eq!(
            collect_sorted(adjlist.outgoing_edges(0).unwrap(), &mut nodes),
            &[1, 2]
        );
        assert_eq!(
            collect_sorted(adjlist.outgoing_edges(1).unwrap(), &mut nodes),
            &[3]
        );
        assert_eq!(
            collect_sorted(adjlist.outgoing_edges(2).unwrap(), &mut nodes),
            &[3]
        );
        assert_eq!(
            collect_sorted(adjlist.outgoing_edges(3).unwrap(), &mut nodes),
            &[]
        );
    }

    #[test]
    fn test_add_remove_edge_comprehensive() {
        let mut dict = Dictionary::<usize, NodeStructOption<5, usize>, 5>::new();
        dict.insert(0, NodeStructOption([None, None, None, None, None]))
            .unwrap();
        dict.insert(1, NodeStructOption([None, None, None, None, None]))
            .unwrap();
        dict.insert(2, NodeStructOption([None, None, None, None, None]))
            .unwrap();
        dict.insert(3, NodeStructOption([None, None, None, None, None]))
            .unwrap();

        let mut graph = MapAdjacencyList::new(dict).unwrap();

        // Start with empty graph
        assert_eq!(graph.iter_edges().unwrap().count(), 0);

        // Add several edges
        assert!(graph.add_edge(0, 1).is_ok());
        assert!(graph.add_edge(1, 2).is_ok());
        assert!(graph.add_edge(2, 3).is_ok());
        assert!(graph.add_edge(0, 3).is_ok());
        assert_eq!(graph.iter_edges().unwrap().count(), 4);

        // Remove some edges
        assert!(graph.remove_edge(1, 2).is_ok());
        assert_eq!(graph.iter_edges().unwrap().count(), 3);

        // Try to remove the same edge again (should fail)
        let result = graph.remove_edge(1, 2);
        assert!(matches!(result, Err(GraphError::EdgeNotFound(1, 2))));

        // Add the edge back
        assert!(graph.add_edge(1, 2).is_ok());
        assert_eq!(graph.iter_edges().unwrap().count(), 4);

        // Verify final edge set
        let mut edges = [(0usize, 0usize); 8];
        let edges_slice = collect_sorted(graph.iter_edges().unwrap(), &mut edges);
        assert_eq!(edges_slice, &[(0, 1), (0, 3), (1, 2), (2, 3)]);
    }
}