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
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
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
use crate::conversions::FromGraph;
use crate::graph::{Graph, GraphError, GraphWithMutableEdges};

/// A space-efficient adjacency matrix implementation using bit-level storage.
///
/// `BitMatrix` represents a directed graph using a bit-packed adjacency matrix.
/// Each bit in the matrix represents the presence (1) or absence (0) of an edge
/// between two nodes. This implementation is particularly memory-efficient for
/// dense graphs, as it uses only one bit per potential edge.
///
/// # Type Parameters
/// * `C` - Number of columns (bytes) per row. Each byte can store 8 edges.
/// * `R` - Number of rows in the matrix. Must equal 8 * C for valid matrices.
///
/// # Matrix Layout
/// The matrix is stored as a 2D array of bytes, where:
/// * Each row represents a source node
/// * Each byte in a row represents 8 potential target nodes
/// * The total number of nodes is 8 * C
/// * The total number of edges is (8 * C)²
///
/// # Example
/// ```
/// use heapless_graphs::matrix::bit_matrix::BitMatrix;
/// use heapless_graphs::graph::Graph;
///
/// // Create a graph with 8 nodes (1 byte per row, 8 rows)
/// let bits = [
///     [0b00000001u8], // Node 0 -> Node 0
///     [0b00000010u8], // Node 1 -> Node 1
///     [0b00000100u8], // Node 2 -> Node 2
///     [0b00001000u8], // Node 3 -> Node 3
///     [0b00010000u8], // Node 4 -> Node 4
///     [0b00100000u8], // Node 5 -> Node 5
///     [0b01000000u8], // Node 6 -> Node 6
///     [0b10000000u8], // Node 7 -> Node 7
/// ];
/// let matrix = BitMatrix::<1, 8>::new(bits).unwrap();
///
/// // Use the public Graph trait API
/// let nodes: Vec<_> = matrix.iter_nodes().unwrap().collect();
/// assert_eq!(nodes, vec![0, 1, 2, 3, 4, 5, 6, 7]);
/// let edges: Vec<_> = matrix.iter_edges().unwrap().collect();
/// assert_eq!(edges, vec![(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7)]);
/// ```
pub struct BitMatrix<const C: usize, const R: usize> {
    bits: [[u8; C]; R], // R rows, each with C columns
}

impl<const C: usize, const R: usize> FromGraph<usize, GraphError<usize>> for BitMatrix<C, R> {
    fn from_graph<G>(source_graph: &G) -> Result<Self, GraphError<usize>>
    where
        G: Graph<usize>,
        GraphError<usize>: From<G::Error>,
    {
        // Create empty bit matrix
        let mut bits = [[0u8; C]; R];

        // Validate all nodes are within range first
        for node in source_graph.iter_nodes()? {
            if node >= 8 * C {
                return Err(GraphError::EdgeHasInvalidNode(node));
            }
        }

        // Add all edges to the matrix
        for (source, destination) in source_graph.iter_edges()? {
            // Validate bounds (should be caught above, but double-check)
            if source >= R {
                return Err(GraphError::EdgeHasInvalidNode(source));
            }
            if destination >= 8 * C {
                return Err(GraphError::EdgeHasInvalidNode(destination));
            }

            // Set the bit: source -> destination
            let byte_col = destination / 8;
            let bit_col = destination % 8;
            bits[source][byte_col] |= 1 << bit_col;
        }

        Ok(Self::new_unchecked(bits))
    }
}

impl<const C: usize, const R: usize> BitMatrix<C, R> {
    /// Creates a new `BitMatrix` with validation of matrix dimensions.
    ///
    /// # Arguments
    /// * `bits` - The raw bit matrix data. Each row must have C columns of bytes.
    ///
    /// # Returns
    /// * `Ok(Self)` if R = 8*C, ensuring the matrix can represent a valid graph
    /// * `Err(GraphError::InvalidMatrixSize)` if dimensions are invalid
    ///
    /// # Example
    /// ```
    /// use heapless_graphs::matrix::bit_matrix::BitMatrix;
    ///
    /// // Valid: 8 rows, 1 column (8 = 8*1)
    /// let bits = [[0u8]; 8];
    /// assert!(BitMatrix::<1, 8>::new(bits).is_ok());
    ///
    /// // Invalid: 4 rows, 1 column (4 ≠ 8*1)
    /// let bits = [[0u8]; 4];
    /// assert!(BitMatrix::<1, 4>::new(bits).is_err());
    /// ```
    pub fn new(bits: [[u8; C]; R]) -> Result<Self, GraphError<usize>> {
        if R != 8 * C {
            return Err(GraphError::InvalidMatrixSize);
        }
        Ok(Self { bits })
    }

    /// Creates a new `BitMatrix` without dimension validation.
    ///
    /// # Safety
    /// This constructor bypasses the R = 8*C validation. The caller must ensure
    /// that the matrix dimensions are valid, otherwise the graph operations
    /// may not work as expected.
    ///
    /// # Arguments
    /// * `bits` - The raw bit matrix data. Each row must have C columns of bytes.
    pub fn new_unchecked(bits: [[u8; C]; R]) -> Self {
        Self { bits }
    }

    /// Internal: Checks if an edge exists between two nodes.
    /// Returns true if the edge exists and indices are in bounds, false otherwise.
    pub(super) fn get(&self, row: usize, col: usize) -> bool {
        if row >= R || col >= 8 * C {
            return false;
        }
        let byte_col = col / 8;
        let bit_col = col % 8;
        (self.bits[row][byte_col] & (1 << bit_col)) != 0
    }
}

impl<const C: usize, const R: usize> Graph<usize> for BitMatrix<C, R> {
    type Error = GraphError<usize>;

    fn iter_nodes(&self) -> Result<impl Iterator<Item = usize>, Self::Error> {
        Ok(0..8 * C)
    }

    fn iter_edges(&self) -> Result<impl Iterator<Item = (usize, usize)>, Self::Error> {
        let iter = (0..8 * C)
            .flat_map(move |row| (0..8 * C).map(move |col| (row, col)))
            .filter(move |(row, col)| self.get(*row, *col));
        Ok(iter)
    }

    fn outgoing_edges(&self, node: usize) -> Result<impl Iterator<Item = usize>, Self::Error> {
        Ok((0..8 * C).filter(move |&col| self.get(node, col)))
    }

    fn incoming_edges(&self, node: usize) -> Result<impl Iterator<Item = usize>, Self::Error> {
        Ok((0..8 * C).filter(move |&row| self.get(row, node)))
    }
}

impl<const C: usize, const R: usize> GraphWithMutableEdges<usize> for BitMatrix<C, R> {
    fn add_edge(&mut self, source: usize, destination: usize) -> Result<(), Self::Error> {
        // Validate bounds (nodes must exist in the matrix)
        if source >= R {
            return Err(GraphError::EdgeHasInvalidNode(source));
        }
        if destination >= 8 * C {
            return Err(GraphError::EdgeHasInvalidNode(destination));
        }

        // Set the bit: source -> destination
        let byte_col = destination / 8;
        let bit_col = destination % 8;
        self.bits[source][byte_col] |= 1 << bit_col;

        Ok(())
    }

    fn remove_edge(&mut self, source: usize, destination: usize) -> Result<(), Self::Error> {
        // Check bounds and if edge exists
        if source >= R || destination >= 8 * C || !self.get(source, destination) {
            return Err(GraphError::EdgeNotFound(source, destination));
        }

        // Clear the bit: source -> destination
        let byte_col = destination / 8;
        let bit_col = destination % 8;
        self.bits[source][byte_col] &= !(1 << bit_col);

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::adjacency_list::map_adjacency_list::MapAdjacencyList;
    use crate::containers::maps::staticdict::Dictionary;
    use crate::containers::maps::MapTrait;
    use crate::graph::GraphWithMutableEdges;
    use crate::tests::{collect, collect_sorted};

    #[test]
    fn test_bit_matrix_basic() {
        let bits = [
            [0b00001011u8], // 0->0, 0->1, 0->3
            [0b00000000u8], // 1-> no outgoing edges
            [0b00000010u8], // 2->1
            [0b00000100u8], // 3->2
            [0b00000000u8], // 4-> no outgoing edges
            [0b00000001u8], // 5->0
            [0b00000000u8], // 6-> no outgoing edges
            [0b00000000u8], // 7-> no outgoing edges
        ];

        let matrix = BitMatrix::new_unchecked(bits);

        // Test nodes iteration
        let mut nodes = [0usize; 16];
        let nodes_slice = collect(matrix.iter_nodes().unwrap(), &mut nodes);
        assert_eq!(nodes_slice, &[0, 1, 2, 3, 4, 5, 6, 7]);

        // Test edges iteration
        let mut edges = [(0usize, 0usize); 16];
        let edges_slice = collect(matrix.iter_edges().unwrap(), &mut edges);
        let expected_edges = [(0, 0), (0, 1), (0, 3), (2, 1), (3, 2), (5, 0)];
        assert_eq!(edges_slice, &expected_edges);

        // Test outgoing edges for node 0
        let mut outgoing_0 = [0usize; 8];
        let outgoing_slice = collect(matrix.outgoing_edges(0).unwrap(), &mut outgoing_0);
        assert_eq!(outgoing_slice, &[0, 1, 3]);

        // Test outgoing edges for node 1 (should be empty)
        assert_eq!(matrix.outgoing_edges(1).unwrap().count(), 0);

        // Test outgoing edges for node 2
        let mut outgoing_2 = [0usize; 8];
        let outgoing_slice = collect(matrix.outgoing_edges(2).unwrap(), &mut outgoing_2);
        assert_eq!(outgoing_slice, &[1]);

        // Test outgoing edges for node 5
        let mut outgoing_5 = [0usize; 8];
        let outgoing_slice = collect(matrix.outgoing_edges(5).unwrap(), &mut outgoing_5);
        assert_eq!(outgoing_slice, &[0]);
    }

    #[test]
    fn test_bit_matrix_get() {
        let bits = [
            [0b00000101u8], // 0->0, 0->2
            [0b00000010u8], // 1->1
            [0b00000000u8], // 2-> no edges
            [0b00000000u8], // 3-> no edges
            [0b00000000u8], // 4-> no edges
            [0b00000000u8], // 5-> no edges
            [0b00000000u8], // 6-> no edges
            [0b00000000u8], // 7-> no edges
        ];

        let matrix = BitMatrix::new_unchecked(bits);

        // Test get method
        assert!(matrix.get(0, 0)); // 0->0 exists
        assert!(!matrix.get(0, 1)); // 0->1 doesn't exist
        assert!(matrix.get(0, 2)); // 0->2 exists
        assert!(!matrix.get(0, 3)); // 0->3 doesn't exist

        assert!(!matrix.get(1, 0)); // 1->0 doesn't exist
        assert!(matrix.get(1, 1)); // 1->1 exists
        assert!(!matrix.get(1, 2)); // 1->2 doesn't exist

        assert!(!matrix.get(2, 0)); // 2->0 doesn't exist
        assert!(!matrix.get(2, 1)); // 2->1 doesn't exist
    }

    #[test]
    fn test_bit_matrix_bounds_checking() {
        let bits = [
            [0b00000001u8], // 0->0 exists
            [0b00000000u8],
            [0b00000000u8],
            [0b00000000u8],
            [0b00000000u8],
            [0b00000000u8],
            [0b00000000u8],
            [0b00000000u8],
        ];

        let matrix = BitMatrix::new_unchecked(bits);

        // Test valid bounds
        assert!(matrix.get(0, 0)); // Should be true
        assert!(!matrix.get(0, 1)); // Should be false
        assert!(!matrix.get(7, 7)); // Last valid position, should be false

        // Test row out of bounds
        assert!(!matrix.get(8, 0)); // Row 8 is out of bounds (max is 7)
        assert!(!matrix.get(100, 0)); // Way out of bounds
        assert!(!matrix.get(usize::MAX, 0)); // Maximum value

        // Test column out of bounds (8*N = 8*1 = 8, so max valid col is 7)
        assert!(!matrix.get(0, 8)); // Column 8 is out of bounds
        assert!(!matrix.get(0, 100)); // Way out of bounds
        assert!(!matrix.get(0, usize::MAX)); // Maximum value

        // Test both row and column out of bounds
        assert!(!matrix.get(8, 8));
        assert!(!matrix.get(usize::MAX, usize::MAX));
    }

    #[test]
    fn test_bit_matrix_outgoing_edges_bounds() {
        let bits = [
            [0b00000011u8], // 0->0, 0->1
            [0b00000000u8],
            [0b00000000u8],
            [0b00000000u8],
            [0b00000000u8],
            [0b00000000u8],
            [0b00000000u8],
            [0b00000000u8],
        ];

        let matrix = BitMatrix::new_unchecked(bits);

        // Test outgoing edges for valid nodes
        let mut outgoing_0 = [0usize; 8];
        let outgoing_slice = collect(matrix.outgoing_edges(0).unwrap(), &mut outgoing_0);
        assert_eq!(outgoing_slice, &[0, 1]);

        // Test outgoing edges for out-of-bounds nodes should return empty
        assert_eq!(matrix.outgoing_edges(8).unwrap().count(), 0); // Node 8 is out of bounds
        assert_eq!(matrix.outgoing_edges(100).unwrap().count(), 0); // Node 100 is way out of bounds
        assert_eq!(matrix.outgoing_edges(usize::MAX).unwrap().count(), 0); // usize::MAX should not panic
    }

    #[test]
    fn test_bit_matrix_all_edges_set() {
        // Test a matrix where all possible edges are set
        let bits = [
            [0b11111111u8], // All 8 edges from node 0
            [0b11111111u8], // All 8 edges from node 1
            [0b11111111u8], // All 8 edges from node 2
            [0b11111111u8], // All 8 edges from node 3
            [0b11111111u8], // All 8 edges from node 4
            [0b11111111u8], // All 8 edges from node 5
            [0b11111111u8], // All 8 edges from node 6
            [0b11111111u8], // All 8 edges from node 7
        ];

        let matrix = BitMatrix::new_unchecked(bits);

        // Should have 8 nodes
        assert_eq!(matrix.iter_nodes().unwrap().count(), 8);

        // Should have 8*8 = 64 edges
        assert_eq!(matrix.iter_edges().unwrap().count(), 64);

        // Each node should have 8 outgoing edges
        for node in 0..8 {
            assert_eq!(matrix.outgoing_edges(node).unwrap().count(), 8);
        }

        // Test specific edge checks
        for row in 0..8 {
            for col in 0..8 {
                assert!(matrix.get(row, col), "Edge {}->{} should exist", row, col);
            }
        }
    }

    #[test]
    fn test_bit_matrix_empty() {
        let bits = [[0u8], [0u8], [0u8], [0u8], [0u8], [0u8], [0u8], [0u8]];

        let matrix = BitMatrix::new_unchecked(bits);

        // Test that empty matrix has nodes but no edges
        let mut nodes = [0usize; 16];
        let nodes_slice = collect(matrix.iter_nodes().unwrap(), &mut nodes);
        assert_eq!(nodes_slice, &[0, 1, 2, 3, 4, 5, 6, 7]);

        // Test that there are no edges
        assert_eq!(matrix.iter_edges().unwrap().count(), 0);

        // Test that all outgoing edge lists are empty
        for node in 0..8 {
            assert_eq!(matrix.outgoing_edges(node).unwrap().count(), 0);
        }
    }

    #[test]
    fn test_bit_matrix_multi_byte() {
        // Test with N=2, so we have 8*2=16 columns requiring 2 bytes per row
        let bits = [
            [0b00000001u8, 0b10000000u8], // 0->0, 0->15 (spans both bytes)
            [0b11110000u8, 0b00001111u8], // 1->4,5,6,7,8,9,10,11 (spans both bytes)
            [0b00000000u8, 0b00000000u8], // 2-> no edges
            [0b01010101u8, 0b10101010u8], // 3->0,2,4,6,9,11,13,15 (alternating pattern)
            [0b00000000u8, 0b00000000u8], // 4-> no edges
            [0b00000000u8, 0b00000000u8], // 5-> no edges
            [0b00000000u8, 0b00000000u8], // 6-> no edges
            [0b00000000u8, 0b00000000u8], // 7-> no edges
        ];

        let matrix = BitMatrix::new_unchecked(bits);

        // Should have 16 nodes (8*2)
        assert_eq!(matrix.iter_nodes().unwrap().count(), 16);

        // Test specific edge existence across byte boundaries
        assert!(matrix.get(0, 0)); // First bit of first byte
        assert!(matrix.get(0, 15)); // Last bit of second byte
        assert!(!matrix.get(0, 1)); // Should be false
        assert!(!matrix.get(0, 14)); // Should be false

        // Test row 1 with pattern spanning both bytes
        assert!(!matrix.get(1, 0)); // First 4 bits should be 0
        assert!(!matrix.get(1, 1));
        assert!(!matrix.get(1, 2));
        assert!(!matrix.get(1, 3));
        assert!(matrix.get(1, 4)); // Next 4 bits should be 1
        assert!(matrix.get(1, 5));
        assert!(matrix.get(1, 6));
        assert!(matrix.get(1, 7));
        assert!(matrix.get(1, 8)); // First 4 bits of second byte should be 1
        assert!(matrix.get(1, 9));
        assert!(matrix.get(1, 10));
        assert!(matrix.get(1, 11));
        assert!(!matrix.get(1, 12)); // Last 4 bits should be 0
        assert!(!matrix.get(1, 13));
        assert!(!matrix.get(1, 14));
        assert!(!matrix.get(1, 15));

        // Test alternating pattern in row 3: 0b01010101, 0b10101010
        // Let's test each bit individually to verify the pattern
        assert!(matrix.get(3, 0)); // 0b01010101 bit 0 = 1
        assert!(!matrix.get(3, 1)); // 0b01010101 bit 1 = 0
        assert!(matrix.get(3, 2)); // 0b01010101 bit 2 = 1
        assert!(!matrix.get(3, 3)); // 0b01010101 bit 3 = 0
        assert!(matrix.get(3, 4)); // 0b01010101 bit 4 = 1
        assert!(!matrix.get(3, 5)); // 0b01010101 bit 5 = 0
        assert!(matrix.get(3, 6)); // 0b01010101 bit 6 = 1
        assert!(!matrix.get(3, 7)); // 0b01010101 bit 7 = 0

        assert!(!matrix.get(3, 8)); // 0b10101010 bit 0 = 0
        assert!(matrix.get(3, 9)); // 0b10101010 bit 1 = 1
        assert!(!matrix.get(3, 10)); // 0b10101010 bit 2 = 0
        assert!(matrix.get(3, 11)); // 0b10101010 bit 3 = 1
        assert!(!matrix.get(3, 12)); // 0b10101010 bit 4 = 0
        assert!(matrix.get(3, 13)); // 0b10101010 bit 5 = 1
        assert!(!matrix.get(3, 14)); // 0b10101010 bit 6 = 0
        assert!(matrix.get(3, 15)); // 0b10101010 bit 7 = 1

        // Test outgoing edges counts
        assert_eq!(matrix.outgoing_edges(0).unwrap().count(), 2); // 0->0, 0->15
        assert_eq!(matrix.outgoing_edges(1).unwrap().count(), 8); // 8 edges spanning bytes
        assert_eq!(matrix.outgoing_edges(2).unwrap().count(), 0); // No edges
        assert_eq!(matrix.outgoing_edges(3).unwrap().count(), 8); // Alternating pattern = 8 edges

        // Test bounds checking with 16 columns
        assert!(!matrix.get(0, 16)); // Column 16 should be out of bounds
        assert!(!matrix.get(0, 100)); // Way out of bounds
        assert_eq!(matrix.outgoing_edges(8).unwrap().count(), 0); // Row 8 out of bounds
    }

    #[test]
    fn test_incoming_edges() {
        let bits = [
            [0b00000010u8], // 0->1
            [0b00000100u8], // 1->2
            [0b00000001u8], // 2->0
            [0b00000010u8], // 3->1
            [0b00000000u8], // 4-> no edges
            [0b00000000u8], // 5-> no edges
            [0b00000000u8], // 6-> no edges
            [0b00000000u8], // 7-> no edges
        ];
        let matrix = BitMatrix::new_unchecked(bits);

        // Test node 0's incoming edges (should be from node 2)
        let mut edges = [0usize; 4];
        let edges_slice = collect(matrix.incoming_edges(0).unwrap(), &mut edges);
        assert_eq!(edges_slice, &[2]);

        // Test node 1's incoming edges (should be from nodes 0 and 3)
        let mut edges = [0usize; 4];
        let edges_slice = collect_sorted(matrix.incoming_edges(1).unwrap(), &mut edges);
        assert_eq!(edges_slice, &[0, 3]);

        // Test node 2's incoming edges (should be from node 1)
        let mut edges = [0usize; 4];
        let edges_slice = collect(matrix.incoming_edges(2).unwrap(), &mut edges);
        assert_eq!(edges_slice, &[1]);

        // Test node 3's incoming edges (should be empty)
        let mut edges = [0usize; 4];
        let edges_slice = collect(matrix.incoming_edges(3).unwrap(), &mut edges);
        assert_eq!(edges_slice, &[]);
    }

    #[test]
    fn test_incoming_edges_empty() {
        let bits = [
            [0b00000000u8], // no edges
            [0b00000000u8], // no edges
            [0b00000000u8], // no edges
            [0b00000000u8], // no edges
            [0b00000000u8], // no edges
            [0b00000000u8], // no edges
            [0b00000000u8], // no edges
            [0b00000000u8], // no edges
        ];
        let matrix = BitMatrix::new_unchecked(bits);

        // Test all nodes have no incoming edges in an empty matrix
        for node in 0..4 {
            let mut edges = [0usize; 4];
            let edges_slice = collect(matrix.incoming_edges(node).unwrap(), &mut edges);
            assert_eq!(edges_slice, &[]);
        }
    }

    #[test]
    fn test_incoming_edges_self_loops() {
        let bits = [
            [0b00000001u8], // 0->0
            [0b00000000u8], // no edges
            [0b00000100u8], // 2->2
            [0b00000000u8], // no edges
            [0b00000000u8], // no edges
            [0b00000000u8], // no edges
            [0b00000000u8], // no edges
            [0b00000000u8], // no edges
        ];
        let matrix = BitMatrix::new_unchecked(bits);

        // Test node 0's incoming edges (should include self-loop)
        let mut edges = [0usize; 4];
        let edges_slice = collect(matrix.incoming_edges(0).unwrap(), &mut edges);
        assert_eq!(edges_slice, &[0]);

        // Test node 2's incoming edges (should include self-loop)
        let mut edges = [0usize; 4];
        let edges_slice = collect(matrix.incoming_edges(2).unwrap(), &mut edges);
        assert_eq!(edges_slice, &[2]);

        // Test nodes 1 and 3 have no incoming edges
        for node in [1, 3] {
            let mut edges = [0usize; 4];
            let edges_slice = collect(matrix.incoming_edges(node).unwrap(), &mut edges);
            assert_eq!(edges_slice, &[]);
        }
    }

    #[test]
    fn test_incoming_edges_multiple_incoming() {
        let bits = [
            [0b00000010u8], // 0->1
            [0b00000000u8], // no edges
            [0b00000010u8], // 2->1
            [0b00000010u8], // 3->1
            [0b00000000u8], // no edges
            [0b00000000u8], // no edges
            [0b00000000u8], // no edges
            [0b00000000u8], // no edges
        ];
        let matrix = BitMatrix::new_unchecked(bits);

        // Test node 1's incoming edges
        let mut edges = [0usize; 4];
        let edges_slice = collect_sorted(matrix.incoming_edges(1).unwrap(), &mut edges);
        assert_eq!(edges_slice, &[0, 2, 3]);
    }

    #[test]
    fn test_bit_matrix_new_constructor() {
        // Valid case: 8 rows (R=8) and 1 column (C=1) satisfies R = 8*C
        let valid_bits = [
            [0b00000001u8],
            [0b00000010u8],
            [0b00000100u8],
            [0b00001000u8],
            [0b00010000u8],
            [0b00100000u8],
            [0b01000000u8],
            [0b10000000u8],
        ];
        assert!(BitMatrix::<1, 8>::new(valid_bits).is_ok());

        // Invalid case: 4 rows (R=4) and 1 column (C=1) does not satisfy R = 8*C
        let invalid_bits = [
            [0b00000001u8],
            [0b00000010u8],
            [0b00000100u8],
            [0b00001000u8],
        ];
        assert!(matches!(
            BitMatrix::<1, 4>::new(invalid_bits),
            Err(GraphError::InvalidMatrixSize)
        ));

        // Another valid case: 16 rows (R=16) and 2 columns (C=2) satisfies R = 8*C
        let valid_bits_2 = [
            [0b00000001u8, 0b00000000u8],
            [0b00000010u8, 0b00000000u8],
            [0b00000100u8, 0b00000000u8],
            [0b00001000u8, 0b00000000u8],
            [0b00010000u8, 0b00000000u8],
            [0b00100000u8, 0b00000000u8],
            [0b01000000u8, 0b00000000u8],
            [0b10000000u8, 0b00000000u8],
            [0b00000000u8, 0b00000001u8],
            [0b00000000u8, 0b00000010u8],
            [0b00000000u8, 0b00000100u8],
            [0b00000000u8, 0b00001000u8],
            [0b00000000u8, 0b00010000u8],
            [0b00000000u8, 0b00100000u8],
            [0b00000000u8, 0b01000000u8],
            [0b00000000u8, 0b10000000u8],
        ];
        assert!(BitMatrix::<2, 16>::new(valid_bits_2).is_ok());
    }

    #[test]
    fn test_bit_matrix_add_edge_success() {
        let bits = [
            [0b00000000u8], // Initially no edges
            [0b00000000u8],
            [0b00000000u8],
            [0b00000000u8],
            [0b00000000u8],
            [0b00000000u8],
            [0b00000000u8],
            [0b00000000u8],
        ];
        let mut matrix = BitMatrix::new_unchecked(bits);

        // Add edges
        assert!(matrix.add_edge(0, 1).is_ok());
        assert!(matrix.add_edge(1, 2).is_ok());
        assert!(matrix.add_edge(0, 7).is_ok());

        // Verify edges were added
        assert!(matrix.get(0, 1));
        assert!(matrix.get(1, 2));
        assert!(matrix.get(0, 7));
        assert!(!matrix.get(0, 2)); // Should not exist

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

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

    #[test]
    fn test_bit_matrix_add_edge_invalid_nodes() {
        let bits = [[0b00000000u8]; 8];
        let mut matrix = BitMatrix::new_unchecked(bits);

        // Try to add edge with invalid source (row >= R)
        let result = matrix.add_edge(8, 1);
        assert!(matches!(result, Err(GraphError::EdgeHasInvalidNode(8))));

        // Try to add edge with invalid destination (col >= 8*C)
        let result = matrix.add_edge(0, 8);
        assert!(matches!(result, Err(GraphError::EdgeHasInvalidNode(8))));

        // Try with way out of bounds
        let result = matrix.add_edge(100, 200);
        assert!(matches!(result, Err(GraphError::EdgeHasInvalidNode(100))));
    }

    #[test]
    fn test_bit_matrix_remove_edge_success() {
        let bits = [
            [0b00001010u8], // 0->1, 0->3
            [0b00000100u8], // 1->2
            [0b00000001u8], // 2->0
            [0b00000000u8],
            [0b00000000u8],
            [0b00000000u8],
            [0b00000000u8],
            [0b00000000u8],
        ];
        let mut matrix = BitMatrix::new_unchecked(bits);

        // Verify initial state
        assert_eq!(matrix.iter_edges().unwrap().count(), 4);
        assert!(matrix.get(0, 1));
        assert!(matrix.get(0, 3));

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

        // Verify edge was removed
        assert!(!matrix.get(0, 1));
        assert!(matrix.get(0, 3)); // Other edge should remain
        assert_eq!(matrix.iter_edges().unwrap().count(), 3);

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

    #[test]
    fn test_bit_matrix_remove_edge_not_found() {
        let bits = [
            [0b00000010u8], // 0->1
            [0b00000000u8],
            [0b00000000u8],
            [0b00000000u8],
            [0b00000000u8],
            [0b00000000u8],
            [0b00000000u8],
            [0b00000000u8],
        ];
        let mut matrix = BitMatrix::new_unchecked(bits);

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

        // Try to remove edge with invalid bounds
        let result = matrix.remove_edge(8, 1);
        assert!(matches!(result, Err(GraphError::EdgeNotFound(8, 1))));

        // Verify original edge is still there
        assert_eq!(matrix.iter_edges().unwrap().count(), 1);
        assert!(matrix.get(0, 1));
    }

    #[test]
    fn test_bit_matrix_add_remove_edge_comprehensive() {
        let bits = [[0b00000000u8]; 8];
        let mut matrix = BitMatrix::new_unchecked(bits);

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

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

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

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

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

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

    #[test]
    fn test_bit_matrix_self_loops() {
        let bits = [[0b00000000u8]; 8];
        let mut matrix = BitMatrix::new_unchecked(bits);

        // Add self-loops and regular edges
        assert!(matrix.add_edge(0, 0).is_ok()); // Self-loop
        assert!(matrix.add_edge(0, 1).is_ok());
        assert!(matrix.add_edge(2, 2).is_ok()); // Self-loop

        assert_eq!(matrix.iter_edges().unwrap().count(), 3);
        assert!(matrix.get(0, 0));
        assert!(matrix.get(0, 1));
        assert!(matrix.get(2, 2));

        // Remove self-loop
        assert!(matrix.remove_edge(0, 0).is_ok());
        assert_eq!(matrix.iter_edges().unwrap().count(), 2);
        assert!(!matrix.get(0, 0));
        assert!(matrix.get(0, 1)); // Regular edge should remain
    }

    #[test]
    fn test_bit_matrix_edge_overwrite() {
        let bits = [[0b00000000u8]; 8];
        let mut matrix = BitMatrix::new_unchecked(bits);

        // Add edge
        assert!(matrix.add_edge(0, 1).is_ok());
        assert!(matrix.get(0, 1));
        assert_eq!(matrix.iter_edges().unwrap().count(), 1);

        // Add same edge again (should be idempotent)
        assert!(matrix.add_edge(0, 1).is_ok());
        assert!(matrix.get(0, 1));
        assert_eq!(matrix.iter_edges().unwrap().count(), 1); // Still just one edge
    }

    #[test]
    fn test_bit_matrix_from_graph() {
        // Create a source graph (map adjacency list with nodes 0, 1, 2)
        let mut dict = Dictionary::<usize, [usize; 2], 8>::new();
        dict.insert(0, [1, 2]).unwrap(); // 0 -> 1, 2
        dict.insert(1, [2, 0]).unwrap(); // 1 -> 2, 0
        dict.insert(2, [0, 1]).unwrap(); // 2 -> 0, 1
        let source = MapAdjacencyList::new_unchecked(dict);

        // Convert to BitMatrix (1 column for 8 nodes: 8 = 8*1)
        let matrix: BitMatrix<1, 8> = BitMatrix::from_graph(&source).unwrap();

        // Verify nodes are represented (BitMatrix always has 0..8*C nodes)
        let node_count = matrix.iter_nodes().unwrap().count();
        assert_eq!(node_count, 8); // BitMatrix always has 8 nodes for 1 column

        // Verify edges were copied correctly
        let mut edges = [(0usize, 0usize); 16];
        let edges_slice = collect_sorted(matrix.iter_edges().unwrap(), &mut edges);
        assert_eq!(
            edges_slice,
            &[(0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1)]
        );
    }

    #[test]
    fn test_bit_matrix_from_graph_empty() {
        // Create an empty source graph
        let dict = Dictionary::<usize, [usize; 2], 8>::default();
        let source = MapAdjacencyList::new_unchecked(dict);

        // Convert to BitMatrix
        let matrix: BitMatrix<1, 8> = BitMatrix::from_graph(&source).unwrap();

        // Verify no edges (but still has 8 nodes)
        assert_eq!(matrix.iter_edges().unwrap().count(), 0);
        assert_eq!(matrix.iter_nodes().unwrap().count(), 8);
    }

    #[test]
    fn test_bit_matrix_from_graph_node_out_of_range() {
        // Create a source graph with node index too large for 1-column matrix (8 nodes max)
        let mut dict = Dictionary::<usize, [usize; 1], 8>::new();
        dict.insert(0, [1]).unwrap();
        dict.insert(1, [8]).unwrap(); // Node 8 is out of range for 8-node matrix (0..7)
        let source = MapAdjacencyList::new_unchecked(dict);

        // Try to convert to BitMatrix with only 8 nodes (1 column)
        let result: Result<BitMatrix<1, 8>, _> = BitMatrix::from_graph(&source);

        // Should fail because node 8 is out of range
        assert!(matches!(result, Err(GraphError::EdgeHasInvalidNode(8))));
    }

    #[test]
    fn test_bit_matrix_from_graph_single_node() {
        // Create a source graph with a single node and self-loop
        let mut dict = Dictionary::<usize, [usize; 1], 8>::new();
        dict.insert(5, [5]).unwrap(); // Node 5 -> 5 (self-loop)
        let source = MapAdjacencyList::new_unchecked(dict);

        // Convert to BitMatrix
        let matrix: BitMatrix<1, 8> = BitMatrix::from_graph(&source).unwrap();

        // Verify self-loop edge
        let mut edges = [(0usize, 0usize); 16];
        let edges_slice = collect(matrix.iter_edges().unwrap(), &mut edges);
        assert_eq!(edges_slice, &[(5, 5)]);

        // Verify matrix still has all 8 nodes
        assert_eq!(matrix.iter_nodes().unwrap().count(), 8);
    }

    #[test]
    fn test_bit_matrix_from_graph_larger_matrix() {
        // Create a source graph that uses higher node indices
        let mut dict = Dictionary::<usize, [usize; 2], 8>::new();
        dict.insert(8, [9, 10]).unwrap(); // Node 8 -> 9, 10
        dict.insert(9, [10, 11]).unwrap(); // Node 9 -> 10, 11
        dict.insert(15, [8, 9]).unwrap(); // Node 15 -> 8, 9
        let source = MapAdjacencyList::new_unchecked(dict);

        // Convert to BitMatrix (2 columns for 16 nodes: 16 = 8*2)
        let matrix: BitMatrix<2, 16> = BitMatrix::from_graph(&source).unwrap();

        // Verify nodes are represented (BitMatrix always has 0..16 nodes)
        let node_count = matrix.iter_nodes().unwrap().count();
        assert_eq!(node_count, 16); // BitMatrix always has 16 nodes for 2 columns

        // Verify edges were copied correctly
        let mut edges = [(0usize, 0usize); 16];
        let edges_slice = collect_sorted(matrix.iter_edges().unwrap(), &mut edges);
        assert_eq!(
            edges_slice,
            &[(8, 9), (8, 10), (9, 10), (9, 11), (15, 8), (15, 9)]
        );
    }
}