sqlitegraph 2.2.2

Embedded graph database with full ACID transactions, HNSW vector search, dual backend support, and comprehensive graph algorithms library
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
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
//! Program slicing for bug isolation and impact analysis.
//!
//! This module provides backward and forward program slicing algorithms
//! for control flow graphs (CFGs). Program slicing answers fundamental questions
//! about program behavior and change impact.
//!
//! # What is Program Slicing?
//!
//! Program slicing extracts a subset of a program (the "slice") that affects
//! or is affected by a specified point (the "criterion"). It's a form of
//! program decomposition that focuses on relevant behavior.
//!
//! ## Backward Slicing ("what can affect this node?")
//!
//! Computes all statements that can influence the value at a given point.
//!
//! **Use cases:**
//! - **Bug isolation**: Find what causes a bug at line N
//! - **Root cause analysis**: Trace backwards from error to source
//! - **Refactoring safety**: Check if changing X affects Y
//! - **Program comprehension**: Understand data flow to a point
//!
//! **Source:** Weiser, M. "Program Slicing" IEEE TSE 1984
//!
//! ## Forward Slicing ("what does this node affect?")
//!
//! Computes all statements that can be influenced by a given point.
//!
//! **Use cases:**
//! - **Impact analysis**: What breaks if I change line N?
//! - **Regression testing**: What tests to run after this change?
//! - **Change propagation**: Where does this modification flow to?
//! - **Dead code elimination**: What code is unreachable from here?
//!
//! **Source:** Bergeretti & Carre, ACM TOPLAS 1985
//!
//! # Algorithm
//!
//! Program slicing is computed as the union of **control dependence** and **data dependence**:
//!
//! - **Control dependence**: "What conditions must execute?" (via CDG)
//! - **Data dependence**: "Where does data flow from/to?" (via reachability)
//!
//! ```
//! Backward slice(target) = control_predecessors(target) + data_predecessors(target)
//! Forward slice(source) = control_successors(source) + data_successors(source)
//! ```
//!
//! ## Complexity
//!
//! - **Time**: O(|V| + |E|) - two BFS traversals (control + data)
//! - **Space**: O(|V|) - for visited sets and slice result
//!
//! Where:
//! - V = number of vertices
//! - E = number of edges
//!
//! # Example
//!
//! ```rust,ignore
//! use sqlitegraph::{
//!     SqliteGraph,
//!     algo::{control_dependence_from_exit, backward_slice, forward_slice},
//! };
//!
//! let graph = SqliteGraph::open_in_memory()?;
//! // ... build CFG with entry 0 and exit 5 ...
//!
//! // Compute control dependence first (required for slicing)
//! let cdg = control_dependence_from_exit(&graph)?;
//!
//! // Backward: What affects node 4?
//! let backward = backward_slice(&graph, &cdg, 4)?;
//! println!("Slice affecting node 4: {:?}", backward.sorted_nodes());
//!
//! // Forward: What does node 0 affect?
//! let forward = forward_slice(&graph, &cdg, 0)?;
//! println!("Slice affected by node 0: {:?}", forward.sorted_nodes());
//! ```
//!
//! # Slice Result Structure
//!
//! Results separate **control_nodes** and **data_nodes** for debugging:
//!
//! - `control_nodes`: Nodes in the slice due to control flow (conditions, branches)
//! - `data_nodes`: Nodes in the slice due to data flow (definitions, uses)
//! - `slice_nodes`: Union of both (complete slice)
//!
//! # References
//!
//! - Weiser, M. "Program Slicing" IEEE Transactions on Software Engineering, 1984
//! - Ferrante et al. "The Program Dependence Graph" ACM TOPLAS, 1987
//! - Bergeretti & Carre "Information-flow and data-flow analysis" ACM TOPLAS, 1985

use std::collections::VecDeque;

use ahash::AHashSet;

use crate::errors::SqliteGraphError;
use crate::graph::SqliteGraph;
use crate::progress::ProgressCallback;

use super::control_dependence::ControlDependenceResult;
use super::reachability::{reachable_from, reverse_reachable_from};

/// Result of a program slicing operation.
///
/// Contains the complete slice with separation between control-dependent
/// and data-dependent nodes for debugging and analysis.
///
/// # Fields
///
/// - `criterion`: The slicing criterion node (the target/source node)
/// - `slice_nodes`: All nodes in the slice (union of control + data)
/// - `control_nodes`: Nodes in the slice due to control dependence
/// - `data_nodes`: Nodes in the slice due to data dependence
/// - `size`: Number of nodes in the slice
///
/// # Example
///
/// ```rust,ignore
/// let result = backward_slice(&graph, &cdg, target)?;
///
/// println!("Slice size: {}", result.size);
/// println!("Control nodes: {:?}", result.control_nodes);
/// println!("Data nodes: {:?}", result.data_nodes);
///
/// // Check if a node is in the slice
/// if result.contains(node_id) {
///     println!("Node {} is in the slice", node_id);
/// }
///
/// // Get deterministic sorted output
/// for node in result.sorted_nodes() {
///     println!("Slice node: {}", node);
/// }
/// ```
#[derive(Debug, Clone)]
pub struct SliceResult {
    /// The slicing criterion (node the slice is based on)
    pub criterion: i64,

    /// All nodes in the slice (union of control + data dependence)
    pub slice_nodes: AHashSet<i64>,

    /// Control-dependent nodes in the slice (via CDG)
    pub control_nodes: AHashSet<i64>,

    /// Data-dependent nodes in the slice (via reachability)
    pub data_nodes: AHashSet<i64>,

    /// Number of nodes in the slice
    pub size: usize,
}

impl SliceResult {
    /// Checks if a node is in the slice.
    ///
    /// Returns `true` if the node is in the complete slice (either control
    /// or data dependence).
    ///
    /// # Arguments
    /// * `node` - Node ID to check
    ///
    /// # Returns
    /// `true` if node is in slice_nodes, `false` otherwise
    ///
    /// # Example
    /// ```rust,ignore
    /// let result = backward_slice(&graph, &cdg, target)?;
    /// if result.contains(some_node) {
    ///     println!("Node {} is relevant to the criterion", some_node);
    /// }
    /// ```
    pub fn contains(&self, node: i64) -> bool {
        self.slice_nodes.contains(&node)
    }

    /// Gets sorted slice nodes for deterministic output.
    ///
    /// Returns nodes in ascending order by ID. Useful for testing,
    /// debugging, and consistent display.
    ///
    /// # Returns
    /// Vector of node IDs sorted in ascending order
    ///
    /// # Example
    /// ```rust,ignore
    /// let result = backward_slice(&graph, &cdg, target)?;
    /// for node in result.sorted_nodes() {
    ///     println!("Slice node: {}", node);
    /// }
    /// ```
    pub fn sorted_nodes(&self) -> Vec<i64> {
        let mut nodes: Vec<i64> = self.slice_nodes.iter().copied().collect();
        nodes.sort();
        nodes
    }

    /// Gets sorted control nodes for deterministic output.
    ///
    /// Returns control-dependent nodes in ascending order by ID.
    pub fn sorted_control_nodes(&self) -> Vec<i64> {
        let mut nodes: Vec<i64> = self.control_nodes.iter().copied().collect();
        nodes.sort();
        nodes
    }

    /// Gets sorted data nodes for deterministic output.
    ///
    /// Returns data-dependent nodes in ascending order by ID.
    pub fn sorted_data_nodes(&self) -> Vec<i64> {
        let mut nodes: Vec<i64> = self.data_nodes.iter().copied().collect();
        nodes.sort();
        nodes
    }
}

/// Computes backward program slice: "what can affect this node?"
///
/// Returns all nodes that can influence the value at the target node.
/// Combines control dependence (what conditions must execute) and data
/// dependence (what definitions flow to this point).
///
/// The slice is computed as:
/// - **Control**: Follow reverse CDG edges backward (what does target depend on?)
/// - **Data**: Follow reverse reachability (what can reach target?)
/// - **Union**: Control nodes + Data nodes = complete backward slice
///
/// # Arguments
/// * `graph` - The control flow graph to analyze
/// * `cdg_result` - Pre-computed control dependence result (from `control_dependence_graph`)
/// * `target` - The target node ID (slicing criterion)
///
/// # Returns
/// `SliceResult` containing all nodes affecting the target, separated by control/data dependence.
///
/// # Complexity
/// - **Time**: O(|V| + |E|) - BFS for control + BFS for data
/// - **Space**: O(|V|) - for visited sets and slice result
///
/// # Algorithm Steps
///
/// 1. **Initialize slice**: Add target to slice_nodes, control_nodes, data_nodes
/// 2. **Control dependence BFS**:
///    - Start from target
///    - Follow reverse_cdg edges (what does each node depend on?)
///    - Add dependencies to control_nodes and slice_nodes
///    - Continue until queue exhausted (visited set prevents cycles)
/// 3. **Data dependence**: Call `reverse_reachable_from(graph, target)` for data dependencies
/// 4. **Merge**: Add data dependencies to data_nodes and slice_nodes
/// 5. **Return**: SliceResult with size = slice_nodes.len()
///
/// # Self-Inclusion
///
/// The target node is always included in the slice (self-inclusion requirement).
/// This ensures the slice is never empty and the criterion itself is considered.
///
/// # Example
///
/// ```rust,ignore
/// use sqlitegraph::{SqliteGraph, algo::{control_dependence_from_exit, backward_slice}};
///
/// let graph = SqliteGraph::open_in_memory()?;
/// // ... build CFG ...
///
/// let cdg = control_dependence_from_exit(&graph)?;
/// let slice = backward_slice(&graph, &cdg, target_node)?;
///
/// println!("Backward slice from {}: {} nodes", target_node, slice.size);
/// println!("Control nodes: {:?}", slice.sorted_control_nodes());
/// println!("Data nodes: {:?}", slice.sorted_data_nodes());
/// ```
///
/// # References
///
/// - Weiser, M. "Program Slicing" IEEE TSE 1984
/// - Ferrante et al. "The Program Dependence Graph" ACM TOPLAS 1987
pub fn backward_slice(
    graph: &SqliteGraph,
    cdg_result: &ControlDependenceResult,
    target: i64,
) -> Result<SliceResult, SqliteGraphError> {
    let mut slice_nodes = AHashSet::new();
    let mut control_nodes = AHashSet::new();
    let mut data_nodes = AHashSet::new();

    // Include target itself (self-inclusion requirement)
    slice_nodes.insert(target);

    // Step 1: Follow reverse CDG for control dependencies
    // BFS to find all nodes that the target depends on for control
    let mut queue = VecDeque::new();
    let mut visited = AHashSet::new();

    queue.push_back(target);
    visited.insert(target);

    while let Some(node) = queue.pop_front() {
        // What does this node depend on for control?
        if let Some(deps) = cdg_result.reverse_cdg.get(&node) {
            for &dep in deps {
                if visited.insert(dep) {
                    control_nodes.insert(dep);
                    slice_nodes.insert(dep);
                    queue.push_back(dep);
                }
            }
        }
    }

    // Step 2: Follow data flow backward
    // Data dependence: what can reach the target via data flow edges?
    let data_reachable = reverse_reachable_from(graph, target)?;
    for &node in &data_reachable {
        data_nodes.insert(node);
        slice_nodes.insert(node);
    }

    // Step 3: Compute size before moving
    let size = slice_nodes.len();

    // Step 4: Return result
    Ok(SliceResult {
        criterion: target,
        slice_nodes,
        control_nodes,
        data_nodes,
        size,
    })
}

/// Computes backward slice with progress tracking.
///
/// Same algorithm as [`backward_slice`] but reports progress during execution.
/// Useful for long-running operations on large graphs.
///
/// # Arguments
/// * `graph` - The control flow graph to analyze
/// * `cdg_result` - Pre-computed control dependence result
/// * `target` - The target node ID (slicing criterion)
/// * `progress` - Progress callback for reporting execution status
///
/// # Returns
/// `SliceResult` containing all nodes affecting the target.
///
/// # Progress Reporting
///
/// The callback receives:
/// - `current`: Current number of nodes visited
/// - `total`: None (unknown total for BFS)
/// - `message`: "Backward slice: visited {current} nodes, {control} control, {data} data"
///
/// Progress is reported periodically (every ~10 nodes visited) to avoid
/// excessive callback overhead while still providing feedback.
///
/// # Example
///
/// ```rust,ignore
/// use sqlitegraph::{
///     algo::backward_slice_with_progress,
///     progress::ConsoleProgress
/// };
///
/// let progress = ConsoleProgress::new();
/// let slice = backward_slice_with_progress(&graph, &cdg, target, &progress)?;
/// // Output: Backward slice: visited 10 nodes, 3 control, 7 data...
/// ```
pub fn backward_slice_with_progress<F>(
    graph: &SqliteGraph,
    cdg_result: &ControlDependenceResult,
    target: i64,
    progress: &F,
) -> Result<SliceResult, SqliteGraphError>
where
    F: ProgressCallback,
{
    let mut slice_nodes = AHashSet::new();
    let mut control_nodes = AHashSet::new();
    let mut data_nodes = AHashSet::new();

    // Include target itself
    slice_nodes.insert(target);

    // Step 1: Follow reverse CDG for control dependencies
    let mut queue = VecDeque::new();
    let mut visited = AHashSet::new();
    let mut nodes_processed = 0;

    queue.push_back(target);
    visited.insert(target);

    while let Some(node) = queue.pop_front() {
        nodes_processed += 1;

        // Report progress every 10 nodes
        if nodes_processed % 10 == 0 {
            progress.on_progress(
                nodes_processed,
                None,
                &format!(
                    "Backward slice: visited {} nodes, {} control, {} data",
                    nodes_processed,
                    control_nodes.len(),
                    data_nodes.len()
                ),
            );
        }

        if let Some(deps) = cdg_result.reverse_cdg.get(&node) {
            for &dep in deps {
                if visited.insert(dep) {
                    control_nodes.insert(dep);
                    slice_nodes.insert(dep);
                    queue.push_back(dep);
                }
            }
        }
    }

    // Step 2: Follow data flow backward
    let data_reachable = reverse_reachable_from(graph, target)?;
    for &node in &data_reachable {
        data_nodes.insert(node);
        slice_nodes.insert(node);
    }

    // Step 3: Compute size before moving
    let size = slice_nodes.len();

    // Report completion
    progress.on_complete();

    Ok(SliceResult {
        criterion: target,
        slice_nodes,
        control_nodes,
        data_nodes,
        size,
    })
}

/// Computes forward program slice: "what does this node affect?"
///
/// Returns all nodes influenced by the source node.
/// Combines control dependence (what branches does this control?) and
/// forward reachability (where does data flow from here?).
///
/// The slice is computed as:
/// - **Control**: Follow CDG edges forward (what does source control?)
/// - **Data**: Follow forward reachability (what can source reach?)
/// - **Union**: Control nodes + Data nodes = complete forward slice
///
/// # Arguments
/// * `graph` - The control flow graph to analyze
/// * `cdg_result` - Pre-computed control dependence result (from `control_dependence_graph`)
/// * `source` - The source node ID (slicing criterion)
///
/// # Returns
/// `SliceResult` containing all nodes affected by the source, separated by control/data dependence.
///
/// # Complexity
/// - **Time**: O(|V| + |E|) - BFS for control + BFS for data
/// - **Space**: O(|V|) - for visited sets and slice result
///
/// # Algorithm Steps
///
/// 1. **Initialize slice**: Add source to slice_nodes, control_nodes, data_nodes
/// 2. **Control dependence BFS**:
///    - Start from source
///    - Follow cdg edges (what does each node control?)
///    - Add controlled nodes to control_nodes and slice_nodes
///    - Continue until queue exhausted (visited set prevents cycles)
/// 3. **Data dependence**: Call `reachable_from(graph, source)` for data flow
/// 4. **Merge**: Add data flow nodes to data_nodes and slice_nodes
/// 5. **Return**: SliceResult with size = slice_nodes.len()
///
/// # Self-Inclusion
///
/// The source node is always included in the slice (self-inclusion requirement).
///
/// # Example
///
/// ```rust,ignore
/// use sqlitegraph::{SqliteGraph, algo::{control_dependence_from_exit, forward_slice}};
///
/// let graph = SqliteGraph::open_in_memory()?;
/// // ... build CFG ...
///
/// let cdg = control_dependence_from_exit(&graph)?;
/// let slice = forward_slice(&graph, &cdg, source_node)?;
///
/// println!("Forward slice from {}: {} nodes", source_node, slice.size);
/// println!("Control nodes: {:?}", slice.sorted_control_nodes());
/// println!("Data nodes: {:?}", slice.sorted_data_nodes());
/// ```
///
/// # References
///
/// - Bergeretti & Carre "Information-flow and data-flow analysis" ACM TOPLAS 1985
/// - Silva "A Vocabulary of Program Slicing" 2007
pub fn forward_slice(
    graph: &SqliteGraph,
    cdg_result: &ControlDependenceResult,
    source: i64,
) -> Result<SliceResult, SqliteGraphError> {
    let mut slice_nodes = AHashSet::new();
    let mut control_nodes = AHashSet::new();
    let mut data_nodes = AHashSet::new();

    // Include source itself (self-inclusion requirement)
    slice_nodes.insert(source);

    // Step 1: Follow CDG forward for controlled nodes
    // BFS to find all nodes that the source controls
    let mut queue = VecDeque::new();
    let mut visited = AHashSet::new();

    queue.push_back(source);
    visited.insert(source);

    while let Some(node) = queue.pop_front() {
        // What does this node control?
        if let Some(controlled) = cdg_result.cdg.get(&node) {
            for &controlled_node in controlled {
                if visited.insert(controlled_node) {
                    control_nodes.insert(controlled_node);
                    slice_nodes.insert(controlled_node);
                    queue.push_back(controlled_node);
                }
            }
        }
    }

    // Step 2: Follow data flow forward
    // Data dependence: what can the source reach via data flow edges?
    let data_affected = reachable_from(graph, source)?;
    for &node in &data_affected {
        data_nodes.insert(node);
        slice_nodes.insert(node);
    }

    // Step 3: Compute size before moving
    let size = slice_nodes.len();

    // Step 4: Return result
    Ok(SliceResult {
        criterion: source,
        slice_nodes,
        control_nodes,
        data_nodes,
        size,
    })
}

/// Computes forward slice with progress tracking.
///
/// Same algorithm as [`forward_slice`] but reports progress during execution.
/// Useful for long-running operations on large graphs.
///
/// # Arguments
/// * `graph` - The control flow graph to analyze
/// * `cdg_result` - Pre-computed control dependence result
/// * `source` - The source node ID (slicing criterion)
/// * `progress` - Progress callback for reporting execution status
///
/// # Returns
/// `SliceResult` containing all nodes affected by the source.
///
/// # Progress Reporting
///
/// The callback receives:
/// - `current`: Current number of nodes visited
/// - `total`: None (unknown total for BFS)
/// - `message`: "Forward slice: visited {current} nodes, {control} control, {data} data"
///
/// Progress is reported periodically (every ~10 nodes visited) to avoid
/// excessive callback overhead while still providing feedback.
///
/// # Example
///
/// ```rust,ignore
/// use sqlitegraph::{
///     algo::forward_slice_with_progress,
///     progress::ConsoleProgress
/// };
///
/// let progress = ConsoleProgress::new();
/// let slice = forward_slice_with_progress(&graph, &cdg, source, &progress)?;
/// // Output: Forward slice: visited 10 nodes, 3 control, 7 data...
/// ```
pub fn forward_slice_with_progress<F>(
    graph: &SqliteGraph,
    cdg_result: &ControlDependenceResult,
    source: i64,
    progress: &F,
) -> Result<SliceResult, SqliteGraphError>
where
    F: ProgressCallback,
{
    let mut slice_nodes = AHashSet::new();
    let mut control_nodes = AHashSet::new();
    let mut data_nodes = AHashSet::new();

    // Include source itself
    slice_nodes.insert(source);

    // Step 1: Follow CDG forward for controlled nodes
    let mut queue = VecDeque::new();
    let mut visited = AHashSet::new();
    let mut nodes_processed = 0;

    queue.push_back(source);
    visited.insert(source);

    while let Some(node) = queue.pop_front() {
        nodes_processed += 1;

        // Report progress every 10 nodes
        if nodes_processed % 10 == 0 {
            progress.on_progress(
                nodes_processed,
                None,
                &format!(
                    "Forward slice: visited {} nodes, {} control, {} data",
                    nodes_processed,
                    control_nodes.len(),
                    data_nodes.len()
                ),
            );
        }

        if let Some(controlled) = cdg_result.cdg.get(&node) {
            for &controlled_node in controlled {
                if visited.insert(controlled_node) {
                    control_nodes.insert(controlled_node);
                    slice_nodes.insert(controlled_node);
                    queue.push_back(controlled_node);
                }
            }
        }
    }

    // Step 2: Follow data flow forward
    let data_affected = reachable_from(graph, source)?;
    for &node in &data_affected {
        data_nodes.insert(node);
        slice_nodes.insert(node);
    }

    // Step 3: Compute size before moving
    let size = slice_nodes.len();

    // Report completion
    progress.on_complete();

    Ok(SliceResult {
        criterion: source,
        slice_nodes,
        control_nodes,
        data_nodes,
        size,
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{GraphEdge, GraphEntity};

    /// Helper: Create linear chain CFG: 0 -> 1 -> 2 -> 3 (no control dependence)
    fn create_linear_chain() -> SqliteGraph {
        let graph = SqliteGraph::open_in_memory().expect("Failed to create graph");

        // Create 4 nodes
        for i in 0..4 {
            let entity = GraphEntity {
                id: 0,
                kind: "node".to_string(),
                name: format!("node_{}", i),
                file_path: Some(format!("node_{}.rs", i)),
                data: serde_json::json!({"index": i}),
            };
            graph
                .insert_entity(&entity)
                .expect("Failed to insert entity");
        }

        let entity_ids: Vec<i64> = graph.list_entity_ids().expect("Failed to get IDs");

        // Create chain: 0 -> 1 -> 2 -> 3
        for i in 0..entity_ids.len().saturating_sub(1) {
            let edge = GraphEdge {
                id: 0,
                from_id: entity_ids[i],
                to_id: entity_ids[i + 1],
                edge_type: "next".to_string(),
                data: serde_json::json!({}),
            };
            graph.insert_edge(&edge).expect("Failed to insert edge");
        }

        graph
    }

    /// Helper: Create if-then-else CFG: 0 -> 1, 0 -> 2, 1 -> 3, 2 -> 3
    /// Expected: Node 3 is control-dependent on node 0 (merge point depends on branch)
    fn create_if_then_else_cfg() -> SqliteGraph {
        let graph = SqliteGraph::open_in_memory().expect("Failed to create graph");

        // Create 4 nodes
        for i in 0..4 {
            let entity = GraphEntity {
                id: 0,
                kind: "node".to_string(),
                name: format!("node_{}", i),
                file_path: Some(format!("node_{}.rs", i)),
                data: serde_json::json!({"index": i}),
            };
            graph
                .insert_entity(&entity)
                .expect("Failed to insert entity");
        }

        let entity_ids: Vec<i64> = graph.list_entity_ids().expect("Failed to get IDs");

        // Create if-then-else: 0 -> 1, 0 -> 2, 1 -> 3, 2 -> 3
        let edges = vec![(0, 1), (0, 2), (1, 3), (2, 3)];
        for (from_idx, to_idx) in edges {
            let edge = GraphEdge {
                id: 0,
                from_id: entity_ids[from_idx],
                to_id: entity_ids[to_idx],
                edge_type: "next".to_string(),
                data: serde_json::json!({}),
            };
            graph.insert_edge(&edge).expect("Failed to insert edge");
        }

        graph
    }

    /// Helper: Create nested CFG for multi-level control dependence
    /// Structure: 0 -> 1, 0 -> 5, 1 -> 2, 1 -> 3, 2 -> 4, 3 -> 4, 4 -> 5
    /// Expected: Nested control dependencies (4 depends on 1 and 0)
    fn create_nested_cfg() -> SqliteGraph {
        let graph = SqliteGraph::open_in_memory().expect("Failed to create graph");

        // Create 6 nodes
        for i in 0..6 {
            let entity = GraphEntity {
                id: 0,
                kind: "node".to_string(),
                name: format!("node_{}", i),
                file_path: Some(format!("node_{}.rs", i)),
                data: serde_json::json!({"index": i}),
            };
            graph
                .insert_entity(&entity)
                .expect("Failed to insert entity");
        }

        let entity_ids: Vec<i64> = graph.list_entity_ids().expect("Failed to get IDs");

        // Create nested: 0 -> 1, 0 -> 5, 1 -> 2, 1 -> 3, 2 -> 4, 3 -> 4, 4 -> 5
        let edges = vec![(0, 1), (0, 5), (1, 2), (1, 3), (2, 4), (3, 4), (4, 5)];
        for (from_idx, to_idx) in edges {
            let edge = GraphEdge {
                id: 0,
                from_id: entity_ids[from_idx],
                to_id: entity_ids[to_idx],
                edge_type: "next".to_string(),
                data: serde_json::json!({}),
            };
            graph.insert_edge(&edge).expect("Failed to insert edge");
        }

        graph
    }

    // Tests for backward_slice

    #[test]
    fn test_backward_slice_linear() {
        // Scenario: Linear chain 0 -> 1 -> 2 -> 3
        // Expected: All nodes via data flow (no control dependence)
        let graph = create_linear_chain();
        let entity_ids: Vec<i64> = graph.list_entity_ids().expect("Failed to get IDs");

        let cdg = super::super::control_dependence::control_dependence_from_exit(&graph)
            .expect("Failed to compute CDG");

        let result =
            backward_slice(&graph, &cdg, entity_ids[3]).expect("Failed to compute backward slice");

        // All 4 nodes should be in slice (via data flow)
        assert_eq!(result.size, 4, "Expected 4 nodes in slice");
        assert_eq!(
            result.criterion, entity_ids[3],
            "Criterion should be target"
        );

        // Nodes should be in slice (via data flow and/or control dependence)
        assert!(
            result.data_nodes.len() + result.control_nodes.len() >= 3,
            "Should have nodes in slice"
        );

        // Target should be in slice
        assert!(result.contains(entity_ids[3]), "Target should be in slice");
    }

    #[test]
    fn test_backward_slice_if_then_else() {
        // Scenario: If-then-else: 0 -> 1, 0 -> 2, 1 -> 3, 2 -> 3
        // Expected: Node 0 in control_nodes (controls node 3)
        let graph = create_if_then_else_cfg();
        let entity_ids: Vec<i64> = graph.list_entity_ids().expect("Failed to get IDs");

        let cdg = super::super::control_dependence::control_dependence_from_exit(&graph)
            .expect("Failed to compute CDG");

        let result =
            backward_slice(&graph, &cdg, entity_ids[3]).expect("Failed to compute backward slice");

        // All nodes should be in slice
        assert_eq!(result.size, 4, "Expected 4 nodes in slice");

        // Node 0 should be in control_nodes (it controls node 3)
        assert!(
            result.control_nodes.contains(&entity_ids[0]),
            "Node 0 should be in control_nodes (controls merge point)"
        );
    }

    #[test]
    fn test_backward_slice_self_inclusion() {
        // Scenario: Self-inclusion requirement
        // Expected: Target node is always in results
        let graph = create_linear_chain();
        let entity_ids: Vec<i64> = graph.list_entity_ids().expect("Failed to get IDs");

        let cdg = super::super::control_dependence::control_dependence_from_exit(&graph)
            .expect("Failed to compute CDG");

        // Test from node 1
        let result =
            backward_slice(&graph, &cdg, entity_ids[1]).expect("Failed to compute backward slice");

        assert_eq!(
            result.criterion, entity_ids[1],
            "Criterion should be target"
        );
        assert!(result.contains(entity_ids[1]), "Target should be in slice");
    }

    #[test]
    fn test_backward_slice_control_data_separation() {
        // Scenario: Verify both control and data nodes populated
        // Expected: Both sets non-empty for conditional CFG
        let graph = create_if_then_else_cfg();
        let entity_ids: Vec<i64> = graph.list_entity_ids().expect("Failed to get IDs");

        let cdg = super::super::control_dependence::control_dependence_from_exit(&graph)
            .expect("Failed to compute CDG");

        let result =
            backward_slice(&graph, &cdg, entity_ids[3]).expect("Failed to compute backward slice");

        // Should have both control and data nodes
        assert!(
            !result.control_nodes.is_empty(),
            "Control nodes should be non-empty"
        );
        assert!(
            !result.data_nodes.is_empty(),
            "Data nodes should be non-empty"
        );

        // Union should be slice_nodes
        let expected_union: AHashSet<i64> = result
            .control_nodes
            .union(&result.data_nodes)
            .copied()
            .collect();
        assert_eq!(
            result.slice_nodes, expected_union,
            "Slice nodes should be union of control + data"
        );
    }

    #[test]
    fn test_backward_slice_empty_graph() {
        // Scenario: Empty graph
        // Expected: Handles gracefully with minimal slice
        let graph = SqliteGraph::open_in_memory().expect("Failed to create graph");

        let cdg = super::super::control_dependence::control_dependence_from_exit(&graph)
            .expect("Failed to compute CDG");

        let result = backward_slice(&graph, &cdg, 999).expect("Failed to compute backward slice");

        // Should have just the target (self-inclusion)
        assert_eq!(result.size, 1, "Empty graph should have minimal slice");
        assert_eq!(result.criterion, 999, "Criterion should be target");
    }

    // Tests for forward_slice

    #[test]
    fn test_forward_slice_linear() {
        // Scenario: Linear chain 0 -> 1 -> 2 -> 3
        // Expected: All nodes via data flow (no control dependence)
        let graph = create_linear_chain();
        let entity_ids: Vec<i64> = graph.list_entity_ids().expect("Failed to get IDs");

        let cdg = super::super::control_dependence::control_dependence_from_exit(&graph)
            .expect("Failed to compute CDG");

        let result =
            forward_slice(&graph, &cdg, entity_ids[0]).expect("Failed to compute forward slice");

        // All 4 nodes should be in slice (via data flow)
        assert_eq!(result.size, 4, "Expected 4 nodes in slice");
        assert_eq!(
            result.criterion, entity_ids[0],
            "Criterion should be source"
        );

        // Nodes should be in slice (via data flow and/or control dependence)
        assert!(
            result.data_nodes.len() + result.control_nodes.len() >= 3,
            "Should have nodes in slice"
        );
    }

    #[test]
    fn test_forward_slice_if_then_else() {
        // Scenario: If-then-else: 0 -> 1, 0 -> 2, 1 -> 3, 2 -> 3
        // Expected: Node 0 controls all branches (nodes 1, 2, 3 in control_nodes)
        let graph = create_if_then_else_cfg();
        let entity_ids: Vec<i64> = graph.list_entity_ids().expect("Failed to get IDs");

        let cdg = super::super::control_dependence::control_dependence_from_exit(&graph)
            .expect("Failed to compute CDG");

        let result =
            forward_slice(&graph, &cdg, entity_ids[0]).expect("Failed to compute forward slice");

        // All nodes should be in slice
        assert_eq!(result.size, 4, "Expected 4 nodes in slice");

        // Node 3 should be in control_nodes (controlled by node 0)
        assert!(
            result.control_nodes.contains(&entity_ids[3]),
            "Node 3 should be in control_nodes (controlled by branch)"
        );
    }

    #[test]
    fn test_forward_slice_self_inclusion() {
        // Scenario: Self-inclusion requirement
        // Expected: Source node is always in results
        let graph = create_linear_chain();
        let entity_ids: Vec<i64> = graph.list_entity_ids().expect("Failed to get IDs");

        let cdg = super::super::control_dependence::control_dependence_from_exit(&graph)
            .expect("Failed to compute CDG");

        let result =
            forward_slice(&graph, &cdg, entity_ids[1]).expect("Failed to compute forward slice");

        assert_eq!(
            result.criterion, entity_ids[1],
            "Criterion should be source"
        );
        assert!(result.contains(entity_ids[1]), "Source should be in slice");
    }

    #[test]
    fn test_forward_slice_control_data_separation() {
        // Scenario: Verify both control and data nodes populated
        // Expected: Both sets non-empty for conditional CFG
        let graph = create_if_then_else_cfg();
        let entity_ids: Vec<i64> = graph.list_entity_ids().expect("Failed to get IDs");

        let cdg = super::super::control_dependence::control_dependence_from_exit(&graph)
            .expect("Failed to compute CDG");

        let result =
            forward_slice(&graph, &cdg, entity_ids[0]).expect("Failed to compute forward slice");

        // Should have both control and data nodes
        assert!(
            !result.control_nodes.is_empty(),
            "Control nodes should be non-empty"
        );
        assert!(
            !result.data_nodes.is_empty(),
            "Data nodes should be non-empty"
        );

        // Union should be slice_nodes
        let expected_union: AHashSet<i64> = result
            .control_nodes
            .union(&result.data_nodes)
            .copied()
            .collect();
        assert_eq!(
            result.slice_nodes, expected_union,
            "Slice nodes should be union of control + data"
        );
    }

    // Tests for SliceResult methods

    #[test]
    fn test_slice_result_contains() {
        // Scenario: contains() method works correctly
        // Expected: Returns true for nodes in slice, false otherwise
        let graph = create_linear_chain();
        let entity_ids: Vec<i64> = graph.list_entity_ids().expect("Failed to get IDs");

        let cdg = super::super::control_dependence::control_dependence_from_exit(&graph)
            .expect("Failed to compute CDG");

        let result =
            forward_slice(&graph, &cdg, entity_ids[0]).expect("Failed to compute forward slice");

        // All nodes should be in slice
        for &node_id in &entity_ids {
            assert!(
                result.contains(node_id),
                "Node {} should be in slice",
                node_id
            );
        }

        // Non-existent node should not be in slice
        assert!(
            !result.contains(9999),
            "Non-existent node should not be in slice"
        );
    }

    #[test]
    fn test_slice_result_sorted_nodes() {
        // Scenario: sorted_nodes() returns deterministic output
        // Expected: Nodes in ascending order
        let graph = create_if_then_else_cfg();
        let entity_ids: Vec<i64> = graph.list_entity_ids().expect("Failed to get IDs");

        let cdg = super::super::control_dependence::control_dependence_from_exit(&graph)
            .expect("Failed to compute CDG");

        let result =
            forward_slice(&graph, &cdg, entity_ids[0]).expect("Failed to compute forward slice");

        let sorted = result.sorted_nodes();

        // Should be sorted (ascending order)
        for i in 1..sorted.len() {
            assert!(
                sorted[i - 1] <= sorted[i],
                "sorted_nodes should be in ascending order"
            );
        }

        // All slice nodes should be present
        assert_eq!(
            sorted.len(),
            result.size,
            "All slice nodes should be in sorted output"
        );
    }

    #[test]
    fn test_slice_result_sorted_control_nodes() {
        // Scenario: sorted_control_nodes() returns deterministic output
        // Expected: Control nodes in ascending order
        let graph = create_if_then_else_cfg();
        let entity_ids: Vec<i64> = graph.list_entity_ids().expect("Failed to get IDs");

        let cdg = super::super::control_dependence::control_dependence_from_exit(&graph)
            .expect("Failed to compute CDG");

        let result =
            forward_slice(&graph, &cdg, entity_ids[0]).expect("Failed to compute forward slice");

        let sorted = result.sorted_control_nodes();

        // Should be sorted
        for i in 1..sorted.len() {
            assert!(
                sorted[i - 1] <= sorted[i],
                "sorted_control_nodes should be in ascending order"
            );
        }

        // All control nodes should be present
        assert_eq!(sorted.len(), result.control_nodes.len());
    }

    #[test]
    fn test_slice_result_sorted_data_nodes() {
        // Scenario: sorted_data_nodes() returns deterministic output
        // Expected: Data nodes in ascending order
        let graph = create_if_then_else_cfg();
        let entity_ids: Vec<i64> = graph.list_entity_ids().expect("Failed to get IDs");

        let cdg = super::super::control_dependence::control_dependence_from_exit(&graph)
            .expect("Failed to compute CDG");

        let result =
            forward_slice(&graph, &cdg, entity_ids[0]).expect("Failed to compute forward slice");

        let sorted = result.sorted_data_nodes();

        // Should be sorted
        for i in 1..sorted.len() {
            assert!(
                sorted[i - 1] <= sorted[i],
                "sorted_data_nodes should be in ascending order"
            );
        }

        // All data nodes should be present
        assert_eq!(sorted.len(), result.data_nodes.len());
    }

    #[test]
    fn test_backward_slice_nested_cfg() {
        // Scenario: Nested CFG with multi-level control dependence
        // Expected: Correctly captures nested dependencies
        let graph = create_nested_cfg();
        let entity_ids: Vec<i64> = graph.list_entity_ids().expect("Failed to get IDs");

        let cdg = super::super::control_dependence::control_dependence_from_exit(&graph)
            .expect("Failed to compute CDG");

        let result =
            backward_slice(&graph, &cdg, entity_ids[4]).expect("Failed to compute backward slice");

        // Node 4 (inner merge) should have control dependencies
        assert!(
            !result.control_nodes.is_empty(),
            "Nested CFG should have control dependencies"
        );

        // Most nodes should be in slice via data flow
        assert!(
            result.size >= 4,
            "Should have at least 4 nodes in slice, got {}",
            result.size
        );
    }

    #[test]
    fn test_forward_slice_nested_cfg() {
        // Scenario: Nested CFG with multi-level control flow
        // Expected: Node 0 controls downstream nodes
        let graph = create_nested_cfg();
        let entity_ids: Vec<i64> = graph.list_entity_ids().expect("Failed to get IDs");

        let cdg = super::super::control_dependence::control_dependence_from_exit(&graph)
            .expect("Failed to compute CDG");

        let result =
            forward_slice(&graph, &cdg, entity_ids[0]).expect("Failed to compute forward slice");

        // All nodes should be in slice
        assert_eq!(result.size, 6, "All 6 nodes should be in slice");

        // Should have control nodes (node 0 controls branches)
        assert!(
            !result.control_nodes.is_empty(),
            "Should have control nodes from branching"
        );
    }

    // Tests for progress variants

    #[test]
    fn test_backward_slice_with_progress() {
        // Scenario: Progress variant matches non-progress variant
        // Expected: Same results
        use crate::progress::NoProgress;

        let graph = create_linear_chain();
        let entity_ids: Vec<i64> = graph.list_entity_ids().expect("Failed to get IDs");

        let cdg = super::super::control_dependence::control_dependence_from_exit(&graph)
            .expect("Failed to compute CDG");

        let progress = NoProgress;
        let result_with =
            backward_slice_with_progress(&graph, &cdg, entity_ids[3], &progress).expect("Failed");
        let result_without = backward_slice(&graph, &cdg, entity_ids[3]).expect("Failed");

        assert_eq!(
            result_with.size, result_without.size,
            "Progress and non-progress results should match"
        );

        for &node in &result_with.slice_nodes {
            assert!(
                result_without.contains(node),
                "Progress result contains node not in non-progress result"
            );
        }
    }

    #[test]
    fn test_forward_slice_with_progress() {
        // Scenario: Progress variant matches non-progress variant
        // Expected: Same results
        use crate::progress::NoProgress;

        let graph = create_linear_chain();
        let entity_ids: Vec<i64> = graph.list_entity_ids().expect("Failed to get IDs");

        let cdg = super::super::control_dependence::control_dependence_from_exit(&graph)
            .expect("Failed to compute CDG");

        let progress = NoProgress;
        let result_with =
            forward_slice_with_progress(&graph, &cdg, entity_ids[0], &progress).expect("Failed");
        let result_without = forward_slice(&graph, &cdg, entity_ids[0]).expect("Failed");

        assert_eq!(
            result_with.size, result_without.size,
            "Progress and non-progress results should match"
        );

        for &node in &result_with.slice_nodes {
            assert!(
                result_without.contains(node),
                "Progress result contains node not in non-progress result"
            );
        }
    }

    #[test]
    fn test_backward_forward_symmetry() {
        // Scenario: Test relationship between backward and forward slices
        // Expected: In a linear chain, backward from N should contain same as forward from first
        let graph = create_linear_chain();
        let entity_ids: Vec<i64> = graph.list_entity_ids().expect("Failed to get IDs");

        let cdg = super::super::control_dependence::control_dependence_from_exit(&graph)
            .expect("Failed to compute CDG");

        // In linear chain, backward from last node should contain all nodes
        let backward = backward_slice(&graph, &cdg, entity_ids[3]).expect("Failed");

        // Forward from first node should also contain all nodes
        let forward = forward_slice(&graph, &cdg, entity_ids[0]).expect("Failed");

        // Both should contain all nodes
        assert_eq!(backward.size, 4, "Backward slice should contain all nodes");
        assert_eq!(forward.size, 4, "Forward slice should contain all nodes");

        // Slice nodes should be equal (in linear chain with no control dependence)
        assert_eq!(
            backward.slice_nodes, forward.slice_nodes,
            "In linear chain, backward and forward slices should match"
        );
    }
}