zipora 2.1.5

High-performance Rust implementation providing advanced data structures and compression algorithms with memory safety guarantees. Features LRU page cache, sophisticated caching layer, fiber-based concurrency, real-time compression, secure memory pools, SIMD optimizations, and complete C FFI for migration from C++.
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
//! Graph Walker Utilities
//!
//! High-performance graph traversal utilities for FSA structures with multiple
//! traversal strategies optimized for different access patterns and cache efficiency.

use crate::error::{Result, ZiporaError};
use crate::succinct::BitVector;
use std::collections::{HashMap, VecDeque};
use std::hash::Hash;
use std::marker::PhantomData;

/// Graph traversal strategy
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WalkMethod {
    /// Breadth-first search with color marking
    BreadthFirst,
    /// Breadth-first search with multiple passes
    BreadthFirstMultiPass,
    /// Performance-first search (hybrid BFS/DFS)
    PerformanceFirst,
    /// Depth-first search with preorder traversal
    DepthFirst,
    /// Cache-friendly search: BFS for 2 levels, then DFS
    CacheFriendly,
    /// Tree-specific breadth-first (no cycle detection)
    TreeBreadthFirst,
    /// Tree-specific depth-first (no cycle detection)
    TreeDepthFirst,
    /// Depth-first search with multiple passes
    DepthFirstMultiPass,
}

/// Vertex color for graph traversal algorithms
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum VertexColor {
    /// Unvisited vertex
    White,
    /// Currently being processed
    Gray,
    /// Fully processed
    Black,
    /// Custom color with ID
    Custom(u32),
}

impl Default for VertexColor {
    fn default() -> Self {
        VertexColor::White
    }
}

/// Graph vertex trait
pub trait Vertex: Clone + Eq + Hash {
    /// Get unique identifier for the vertex
    fn id(&self) -> u64;
    
    /// Get outgoing edges from this vertex
    fn outgoing_edges(&self) -> Vec<Self>;
    
    /// Check if this vertex is a terminal/final state
    fn is_terminal(&self) -> bool {
        false
    }
}

/// Simple vertex implementation for testing
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct SimpleVertex {
    pub id: u32,
    pub edges: Vec<u32>,
    pub is_terminal: bool,
}

impl SimpleVertex {
    pub fn new(id: u32) -> Self {
        Self {
            id,
            edges: Vec::new(),
            is_terminal: false,
        }
    }
    
    pub fn with_edges(id: u32, edges: Vec<u32>) -> Self {
        Self {
            id,
            edges,
            is_terminal: false,
        }
    }
    
    pub fn with_terminal(id: u32, is_terminal: bool) -> Self {
        Self {
            id,
            edges: Vec::new(),
            is_terminal,
        }
    }
}

impl Vertex for SimpleVertex {
    fn id(&self) -> u64 {
        self.id as u64
    }
    
    fn outgoing_edges(&self) -> Vec<Self> {
        self.edges.iter().map(|&id| SimpleVertex::new(id)).collect()
    }
    
    fn is_terminal(&self) -> bool {
        self.is_terminal
    }
}

/// Visitor trait for graph traversal callbacks
pub trait GraphVisitor<V: Vertex> {
    /// Called when a vertex is first discovered
    fn visit_vertex(&mut self, vertex: &V, depth: usize) -> Result<bool>;
    
    /// Called when an edge is traversed
    fn visit_edge(&mut self, from: &V, to: &V) -> Result<bool>;
    
    /// Called when backtracking from a vertex (DFS only)
    fn finish_vertex(&mut self, vertex: &V, depth: usize) -> Result<()> {
        let _ = (vertex, depth);
        Ok(())
    }
}

/// Configuration for graph walkers
#[derive(Debug, Clone)]
pub struct WalkerConfig {
    /// Maximum depth to traverse
    pub max_depth: Option<usize>,
    /// Maximum number of vertices to visit
    pub max_vertices: Option<usize>,
    /// Enable cycle detection
    pub cycle_detection: bool,
    /// Initial queue capacity for BFS
    pub initial_queue_capacity: usize,
    /// Use incremental color IDs for multi-pass
    pub incremental_colors: bool,
}

impl Default for WalkerConfig {
    fn default() -> Self {
        Self {
            max_depth: None,
            max_vertices: None,
            cycle_detection: true,
            initial_queue_capacity: 1024,
            incremental_colors: false,
        }
    }
}

impl WalkerConfig {
    /// Create configuration for tree traversal (no cycle detection)
    pub fn for_tree() -> Self {
        Self {
            cycle_detection: false,
            ..Default::default()
        }
    }
    
    /// Create configuration for large graphs
    pub fn for_large_graph() -> Self {
        Self {
            max_vertices: Some(1_000_000),
            initial_queue_capacity: 10_000,
            ..Default::default()
        }
    }
    
    /// Create configuration for multi-pass traversal
    pub fn for_multi_pass() -> Self {
        Self {
            incremental_colors: true,
            ..Default::default()
        }
    }
}

/// Statistics for graph traversal
#[derive(Debug, Clone, Default)]
pub struct WalkStats {
    /// Number of vertices visited
    pub vertices_visited: usize,
    /// Number of edges traversed
    pub edges_traversed: usize,
    /// Maximum depth reached
    pub max_depth_reached: usize,
    /// Number of cycles detected
    pub cycles_detected: usize,
    /// Number of back edges found
    pub back_edges: usize,
}

/// Breadth-First Search Graph Walker
pub struct BfsGraphWalker<V: Vertex> {
    config: WalkerConfig,
    vertex_colors: HashMap<u64, VertexColor>,
    queue: VecDeque<(V, usize)>, // (vertex, depth)
    stats: WalkStats,
    _phantom: PhantomData<V>,
}

impl<V: Vertex> BfsGraphWalker<V> {
    /// Create a new BFS graph walker
    pub fn new(config: WalkerConfig) -> Self {
        Self {
            queue: VecDeque::with_capacity(config.initial_queue_capacity),
            vertex_colors: HashMap::new(),
            config,
            stats: WalkStats::default(),
            _phantom: PhantomData,
        }
    }
    
    /// Walk the graph starting from the given vertex
    pub fn walk<Visitor>(&mut self, start: V, visitor: &mut Visitor) -> Result<()>
    where
        Visitor: GraphVisitor<V> + ?Sized,
    {
        self.reset();
        self.queue.push_back((start.clone(), 0));
        self.vertex_colors.insert(start.id(), VertexColor::Gray);
        
        while let Some((current, depth)) = self.queue.pop_front() {
            // Check limits
            if let Some(max_depth) = self.config.max_depth {
                if depth >= max_depth {
                    continue;
                }
            }
            
            if let Some(max_vertices) = self.config.max_vertices {
                if self.stats.vertices_visited >= max_vertices {
                    break;
                }
            }
            
            // Visit vertex
            if !visitor.visit_vertex(&current, depth)? {
                continue; // Skip this vertex
            }
            
            self.stats.vertices_visited += 1;
            self.stats.max_depth_reached = self.stats.max_depth_reached.max(depth);
            
            // Process outgoing edges
            for next_vertex in current.outgoing_edges() {
                let next_id = next_vertex.id();
                
                if !visitor.visit_edge(&current, &next_vertex)? {
                    continue; // Skip this edge
                }
                
                self.stats.edges_traversed += 1;
                
                if self.config.cycle_detection {
                    match self.vertex_colors.get(&next_id) {
                        Some(VertexColor::White) | None => {
                            // Unvisited - add to queue
                            self.vertex_colors.insert(next_id, VertexColor::Gray);
                            self.queue.push_back((next_vertex, depth + 1));
                        }
                        Some(VertexColor::Gray) => {
                            // Back edge - cycle detected
                            self.stats.cycles_detected += 1;
                            self.stats.back_edges += 1;
                        }
                        Some(VertexColor::Black) => {
                            // Already processed - cross edge
                        }
                        Some(VertexColor::Custom(_)) => {
                            // Custom color handling
                        }
                    }
                } else {
                    // No cycle detection - just add to queue
                    self.queue.push_back((next_vertex, depth + 1));
                }
            }
            
            // Mark vertex as fully processed
            if self.config.cycle_detection {
                self.vertex_colors.insert(current.id(), VertexColor::Black);
            }
        }
        
        Ok(())
    }
    
    /// Reset walker state
    pub fn reset(&mut self) {
        self.vertex_colors.clear();
        self.queue.clear();
        self.stats = WalkStats::default();
    }
    
    /// Get traversal statistics
    pub fn stats(&self) -> &WalkStats {
        &self.stats
    }
}

/// Depth-First Search Graph Walker
pub struct DfsGraphWalker<V: Vertex> {
    config: WalkerConfig,
    vertex_colors: HashMap<u64, VertexColor>,
    stack: Vec<(V, usize, bool)>, // (vertex, depth, visited_children)
    stats: WalkStats,
    _phantom: PhantomData<V>,
}

impl<V: Vertex> DfsGraphWalker<V> {
    /// Create a new DFS graph walker
    pub fn new(config: WalkerConfig) -> Self {
        Self {
            stack: Vec::with_capacity(config.initial_queue_capacity),
            vertex_colors: HashMap::new(),
            config,
            stats: WalkStats::default(),
            _phantom: PhantomData,
        }
    }
    
    /// Walk the graph starting from the given vertex
    pub fn walk<Visitor>(&mut self, start: V, visitor: &mut Visitor) -> Result<()>
    where
        Visitor: GraphVisitor<V> + ?Sized,
    {
        self.reset();
        self.stack.push((start.clone(), 0, false));
        self.vertex_colors.insert(start.id(), VertexColor::Gray);
        
        while let Some((current, depth, visited_children)) = self.stack.pop() {
            if visited_children {
                // Backtracking - finish vertex
                visitor.finish_vertex(&current, depth)?;
                self.vertex_colors.insert(current.id(), VertexColor::Black);
                continue;
            }
            
            // Check limits
            if let Some(max_depth) = self.config.max_depth {
                if depth >= max_depth {
                    continue;
                }
            }
            
            if let Some(max_vertices) = self.config.max_vertices {
                if self.stats.vertices_visited >= max_vertices {
                    break;
                }
            }
            
            // Visit vertex
            if !visitor.visit_vertex(&current, depth)? {
                continue;
            }
            
            self.stats.vertices_visited += 1;
            self.stats.max_depth_reached = self.stats.max_depth_reached.max(depth);
            
            // Push back for finish processing
            self.stack.push((current.clone(), depth, true));
            
            // Process outgoing edges (in reverse order for consistent traversal)
            let mut edges: Vec<_> = current.outgoing_edges();
            edges.reverse();
            
            for next_vertex in edges {
                let next_id = next_vertex.id();
                
                if !visitor.visit_edge(&current, &next_vertex)? {
                    continue;
                }
                
                self.stats.edges_traversed += 1;
                
                if self.config.cycle_detection {
                    match self.vertex_colors.get(&next_id) {
                        Some(VertexColor::White) | None => {
                            // Unvisited - add to stack
                            self.vertex_colors.insert(next_id, VertexColor::Gray);
                            self.stack.push((next_vertex, depth + 1, false));
                        }
                        Some(VertexColor::Gray) => {
                            // Back edge - cycle detected
                            self.stats.cycles_detected += 1;
                            self.stats.back_edges += 1;
                        }
                        Some(VertexColor::Black) => {
                            // Already processed
                        }
                        Some(VertexColor::Custom(_)) => {
                            // Custom color handling
                        }
                    }
                } else {
                    // No cycle detection
                    self.stack.push((next_vertex, depth + 1, false));
                }
            }
        }
        
        Ok(())
    }
    
    /// Reset walker state
    pub fn reset(&mut self) {
        self.vertex_colors.clear();
        self.stack.clear();
        self.stats = WalkStats::default();
    }
    
    /// Get traversal statistics
    pub fn stats(&self) -> &WalkStats {
        &self.stats
    }
}

/// Cache-Friendly Search Walker
/// 
/// Implements a hybrid approach: BFS for the first 2 levels to maximize cache locality,
/// then switches to DFS for memory efficiency
pub struct CfsGraphWalker<V: Vertex> {
    config: WalkerConfig,
    bfs_walker: BfsGraphWalker<V>,
    dfs_walker: DfsGraphWalker<V>,
    bfs_levels: usize,
    stats: WalkStats,
}

impl<V: Vertex> CfsGraphWalker<V> {
    /// Create a new CFS graph walker
    pub fn new(config: WalkerConfig) -> Self {
        let bfs_config = WalkerConfig {
            max_depth: Some(2), // BFS for first 2 levels
            ..config.clone()
        };
        
        Self {
            bfs_walker: BfsGraphWalker::new(bfs_config),
            dfs_walker: DfsGraphWalker::new(config.clone()),
            config,
            bfs_levels: 2,
            stats: WalkStats::default(),
        }
    }
    
    /// Walk the graph using cache-friendly strategy
    pub fn walk<Visitor>(&mut self, start: V, visitor: &mut Visitor) -> Result<()>
    where
        Visitor: GraphVisitor<V> + ?Sized,
    {
        self.reset();
        
        // Phase 1: BFS for first 2 levels
        let mut level_2_vertices = Vec::new();
        {
            let mut collecting_visitor = Level2Collector::new(visitor, &mut level_2_vertices);
            self.bfs_walker.walk(start, &mut collecting_visitor)?;
        }
        
        // Combine BFS stats
        self.stats.vertices_visited += self.bfs_walker.stats().vertices_visited;
        self.stats.edges_traversed += self.bfs_walker.stats().edges_traversed;
        self.stats.max_depth_reached = self.bfs_walker.stats().max_depth_reached;
        self.stats.cycles_detected += self.bfs_walker.stats().cycles_detected;
        
        // Phase 2: DFS from each level 2 vertex
        for vertex in level_2_vertices {
            self.dfs_walker.walk(vertex, visitor)?;
            
            // Combine DFS stats
            self.stats.vertices_visited += self.dfs_walker.stats().vertices_visited;
            self.stats.edges_traversed += self.dfs_walker.stats().edges_traversed;
            self.stats.max_depth_reached = self.stats.max_depth_reached.max(
                self.dfs_walker.stats().max_depth_reached
            );
            self.stats.cycles_detected += self.dfs_walker.stats().cycles_detected;
            
            self.dfs_walker.reset();
        }
        
        Ok(())
    }
    
    /// Reset walker state
    pub fn reset(&mut self) {
        self.bfs_walker.reset();
        self.dfs_walker.reset();
        self.stats = WalkStats::default();
    }
    
    /// Get traversal statistics
    pub fn stats(&self) -> &WalkStats {
        &self.stats
    }
}

/// Helper visitor for collecting level 2 vertices
struct Level2Collector<'a, V: Vertex, Visitor: GraphVisitor<V> + ?Sized> {
    visitor: &'a mut Visitor,
    level_2_vertices: &'a mut Vec<V>,
}

impl<'a, V: Vertex, Visitor: GraphVisitor<V> + ?Sized> Level2Collector<'a, V, Visitor> {
    fn new(visitor: &'a mut Visitor, level_2_vertices: &'a mut Vec<V>) -> Self {
        Self {
            visitor,
            level_2_vertices,
        }
    }
}

impl<'a, V: Vertex, Visitor: GraphVisitor<V> + ?Sized> GraphVisitor<V> for Level2Collector<'a, V, Visitor> {
    fn visit_vertex(&mut self, vertex: &V, depth: usize) -> Result<bool> {
        if depth == 2 {
            self.level_2_vertices.push(vertex.clone());
        }
        self.visitor.visit_vertex(vertex, depth)
    }
    
    fn visit_edge(&mut self, from: &V, to: &V) -> Result<bool> {
        self.visitor.visit_edge(from, to)
    }
    
    fn finish_vertex(&mut self, vertex: &V, depth: usize) -> Result<()> {
        self.visitor.finish_vertex(vertex, depth)
    }
}

/// Multi-pass walker for incremental processing
pub struct MultiPassWalker<V: Vertex> {
    config: WalkerConfig,
    vertex_colors: HashMap<u64, VertexColor>,
    current_color_id: u32,
    stats: WalkStats,
    _phantom: PhantomData<V>,
}

impl<V: Vertex> MultiPassWalker<V> {
    /// Create a new multi-pass walker
    pub fn new(config: WalkerConfig) -> Self {
        Self {
            config,
            vertex_colors: HashMap::new(),
            current_color_id: 1,
            stats: WalkStats::default(),
            _phantom: PhantomData,
        }
    }
    
    /// Perform a single pass using the specified method
    pub fn walk_pass<Visitor>(&mut self, start: V, method: WalkMethod, visitor: &mut Visitor) -> Result<()>
    where
        Visitor: GraphVisitor<V> + ?Sized,
    {
        match method {
            WalkMethod::BreadthFirst => {
                let mut walker = BfsGraphWalker::new(self.config.clone());
                walker.walk(start, visitor)?;
                self.merge_stats(walker.stats());
            }
            WalkMethod::DepthFirst => {
                let mut walker = DfsGraphWalker::new(self.config.clone());
                walker.walk(start, visitor)?;
                self.merge_stats(walker.stats());
            }
            WalkMethod::CacheFriendly => {
                let mut walker = CfsGraphWalker::new(self.config.clone());
                walker.walk(start, visitor)?;
                self.merge_stats(walker.stats());
            }
            _ => {
                return Err(ZiporaError::invalid_data("Unsupported walk method for multi-pass"));
            }
        }
        
        if self.config.incremental_colors {
            self.current_color_id += 1;
        }
        
        Ok(())
    }
    
    /// Get the current color ID for this pass
    pub fn current_color(&self) -> VertexColor {
        VertexColor::Custom(self.current_color_id)
    }
    
    /// Reset for a new set of passes
    pub fn reset(&mut self) {
        self.vertex_colors.clear();
        self.current_color_id = 1;
        self.stats = WalkStats::default();
    }
    
    /// Get accumulated statistics
    pub fn stats(&self) -> &WalkStats {
        &self.stats
    }
    
    fn merge_stats(&mut self, other: &WalkStats) {
        self.stats.vertices_visited += other.vertices_visited;
        self.stats.edges_traversed += other.edges_traversed;
        self.stats.max_depth_reached = self.stats.max_depth_reached.max(other.max_depth_reached);
        self.stats.cycles_detected += other.cycles_detected;
        self.stats.back_edges += other.back_edges;
    }
}

/// Generic graph walker factory
pub struct GraphWalkerFactory;

impl GraphWalkerFactory {
    /// Create a walker based on the specified method
    pub fn create_walker<V: Vertex + 'static>(method: WalkMethod, config: WalkerConfig) -> Box<dyn GraphWalker<V>> {
        match method {
            WalkMethod::BreadthFirst | WalkMethod::TreeBreadthFirst => {
                let mut walker_config = config;
                if method == WalkMethod::TreeBreadthFirst {
                    walker_config.cycle_detection = false;
                }
                Box::new(BfsGraphWalker::new(walker_config))
            }
            WalkMethod::DepthFirst | WalkMethod::TreeDepthFirst => {
                let mut walker_config = config;
                if method == WalkMethod::TreeDepthFirst {
                    walker_config.cycle_detection = false;
                }
                Box::new(DfsGraphWalker::new(walker_config))
            }
            WalkMethod::CacheFriendly => {
                Box::new(CfsGraphWalker::new(config))
            }
            WalkMethod::BreadthFirstMultiPass | WalkMethod::DepthFirstMultiPass => {
                Box::new(MultiPassWalker::new(config))
            }
            WalkMethod::PerformanceFirst => {
                // Use CFS as the performance-optimized strategy
                Box::new(CfsGraphWalker::new(config))
            }
        }
    }
}

/// Trait for generic graph walking
pub trait GraphWalker<V: Vertex> {
    fn walk_dyn(&mut self, start: V, visitor: &mut dyn GraphVisitor<V>) -> Result<()>;
    fn reset(&mut self);
    fn stats(&self) -> &WalkStats;
}

impl<V: Vertex> GraphWalker<V> for BfsGraphWalker<V> {
    fn walk_dyn(&mut self, start: V, visitor: &mut dyn GraphVisitor<V>) -> Result<()> {
        self.walk(start, visitor)
    }
    
    fn reset(&mut self) {
        BfsGraphWalker::reset(self)
    }
    
    fn stats(&self) -> &WalkStats {
        BfsGraphWalker::stats(self)
    }
}

impl<V: Vertex> GraphWalker<V> for DfsGraphWalker<V> {
    fn walk_dyn(&mut self, start: V, visitor: &mut dyn GraphVisitor<V>) -> Result<()> {
        self.walk(start, visitor)
    }
    
    fn reset(&mut self) {
        DfsGraphWalker::reset(self)
    }
    
    fn stats(&self) -> &WalkStats {
        DfsGraphWalker::stats(self)
    }
}

impl<V: Vertex> GraphWalker<V> for CfsGraphWalker<V> {
    fn walk_dyn(&mut self, start: V, visitor: &mut dyn GraphVisitor<V>) -> Result<()> {
        self.walk(start, visitor)
    }
    
    fn reset(&mut self) {
        CfsGraphWalker::reset(self)
    }
    
    fn stats(&self) -> &WalkStats {
        CfsGraphWalker::stats(self)
    }
}

impl<V: Vertex> GraphWalker<V> for MultiPassWalker<V> {
    fn walk_dyn(&mut self, start: V, visitor: &mut dyn GraphVisitor<V>) -> Result<()> {
        // Default to cache-friendly for single-pass usage
        self.walk_pass(start, WalkMethod::CacheFriendly, visitor)
    }
    
    fn reset(&mut self) {
        MultiPassWalker::reset(self)
    }
    
    fn stats(&self) -> &WalkStats {
        MultiPassWalker::stats(self)
    }
}

// ============================================================================
// Fast integer-indexed walkers using BitVector for color tracking
// ============================================================================
//
// These use BitVector for color tracking (O(1) per vertex, minimal memory)
// and double-buffered queues for level-aware BFS.

/// Fast BFS walker using bit vector for color tracking.
///
/// Uses BitVector for O(1) per-vertex color tracking with double-buffered queues
/// for level-aware traversal. O(n/8) bytes memory for n vertices (vs HashMap O(32n)).
pub struct FastBfsWalker {
    q1: Vec<u32>,
    q2: Vec<u32>,
    idx: usize,
    depth: usize,
    color: BitVector,
}

impl FastBfsWalker {
    /// Initialize for a graph with `num_vertices` vertices.
    pub fn new(num_vertices: usize) -> Self {
        let mut color = BitVector::with_capacity(num_vertices).unwrap_or_else(|_| BitVector::new());
        for _ in 0..num_vertices {
            let _ = color.push(false);
        }
        Self {
            q1: Vec::with_capacity(512.min(num_vertices)),
            q2: Vec::with_capacity(512.min(num_vertices)),
            idx: 0,
            depth: 0,
            color,
        }
    }

    /// Add root vertex to start traversal.
    #[inline]
    pub fn put_root(&mut self, root: u32) {
        self.q1.push(root);
        let _ = self.color.set(root as usize, true);
    }

    /// Get next vertex in BFS order.
    #[inline]
    pub fn next(&mut self) -> u32 {
        debug_assert!(self.idx <= self.q1.len());
        if self.idx == self.q1.len() {
            std::mem::swap(&mut self.q1, &mut self.q2);
            self.q2.clear();
            self.idx = 0;
            self.depth += 1;
        }
        debug_assert!(!self.q1.is_empty());
        let v = self.q1[self.idx];
        self.idx += 1;
        v
    }

    /// Add children, skipping already-visited vertices.
    #[inline]
    pub fn put_children(&mut self, children: &[u32]) {
        for &child in children {
            if !self.color.get(child as usize).unwrap_or(true) {
                self.q2.push(child);
                let _ = self.color.set(child as usize, true);
            }
        }
    }

    /// Check if traversal is complete.
    #[inline]
    pub fn is_finished(&self) -> bool {
        self.q1.len() + self.q2.len() == self.idx
    }

    /// Current BFS depth.
    #[inline]
    pub fn depth(&self) -> usize {
        self.depth
    }
}

/// Fast DFS walker using bit vector for color tracking.
///
/// Stack-based with bit vector color tracking.
/// Children pushed in reverse order for consistent traversal.
pub struct FastDfsWalker {
    stack: Vec<u32>,
    color: BitVector,
}

impl FastDfsWalker {
    /// Initialize for a graph with `num_vertices` vertices.
    pub fn new(num_vertices: usize) -> Self {
        let mut color = BitVector::with_capacity(num_vertices).unwrap_or_else(|_| BitVector::new());
        for _ in 0..num_vertices {
            let _ = color.push(false);
        }
        Self {
            stack: Vec::with_capacity(512),
            color,
        }
    }

    /// Add root vertex.
    #[inline]
    pub fn put_root(&mut self, root: u32) {
        self.stack.push(root);
        let _ = self.color.set(root as usize, true);
    }

    /// Get next vertex in DFS order.
    #[inline]
    pub fn next(&mut self) -> u32 {
        debug_assert!(!self.stack.is_empty());
        self.stack.pop().expect("stack non-empty by has_next check")
    }

    /// Add children in reverse order, skipping visited.
    #[inline]
    pub fn put_children(&mut self, children: &[u32]) {
        for &child in children.iter().rev() {
            if !self.color.get(child as usize).unwrap_or(true) {
                self.stack.push(child);
                let _ = self.color.set(child as usize, true);
            }
        }
    }

    /// Check if traversal is complete.
    #[inline]
    pub fn is_finished(&self) -> bool {
        self.stack.is_empty()
    }
}

/// Fast CFS walker: BFS for first 2 levels, then DFS.
///
/// Cache-friendly: BFS for shallow levels (cache-hot), DFS for deeper levels.
/// Optimal for tries where root fanout is high but subtrees are deep.
pub struct FastCfsWalker {
    q1: Vec<u32>,
    q2: Vec<u32>,
    depth: usize,
    q1_pos: usize,
}

impl FastCfsWalker {
    const MAX_BFS_DEPTH: usize = 2;

    pub fn new(_num_vertices: usize) -> Self {
        Self {
            q1: Vec::with_capacity(512),
            q2: Vec::with_capacity(512),
            depth: 0,
            q1_pos: 0,
        }
    }

    #[inline]
    pub fn put_root(&mut self, root: u32) {
        debug_assert_eq!(self.depth, 0);
        self.q1.push(root);
    }

    #[inline]
    pub fn next(&mut self) -> u32 {
        if self.depth < Self::MAX_BFS_DEPTH {
            if self.q1_pos < self.q1.len() {
                let v = self.q1[self.q1_pos];
                self.q1_pos += 1;
                return v;
            }
            self.depth += 1;
            self.q1.clear();
            if self.depth < Self::MAX_BFS_DEPTH {
                std::mem::swap(&mut self.q1, &mut self.q2);
                self.q1_pos = 1;
                return self.q1[0];
            } else {
                self.q2.reverse();
                return self.q2.pop().expect("q2 non-empty");
            }
        }
        self.q2.pop().expect("q2 non-empty after swap")
    }

    /// Add children — forward order in BFS phase, reverse in DFS phase.
    #[inline]
    pub fn put_children(&mut self, children: &[u32]) {
        let old_size = self.q2.len();
        self.q2.extend_from_slice(children);
        if self.depth >= Self::MAX_BFS_DEPTH {
            self.q2[old_size..].reverse();
        }
    }

    #[inline]
    pub fn is_finished(&self) -> bool {
        if self.depth < Self::MAX_BFS_DEPTH {
            self.q1_pos >= self.q1.len() + self.q2.len()
        } else {
            self.q2.is_empty()
        }
    }

    #[inline]
    pub fn depth(&self) -> usize {
        self.depth
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    struct TestVisitor {
        visited_vertices: Vec<u32>,
        visited_edges: Vec<(u32, u32)>,
    }

    impl TestVisitor {
        fn new() -> Self {
            Self {
                visited_vertices: Vec::new(),
                visited_edges: Vec::new(),
            }
        }
    }

    impl GraphVisitor<SimpleVertex> for TestVisitor {
        fn visit_vertex(&mut self, vertex: &SimpleVertex, _depth: usize) -> Result<bool> {
            self.visited_vertices.push(vertex.id);
            Ok(true)
        }

        fn visit_edge(&mut self, from: &SimpleVertex, to: &SimpleVertex) -> Result<bool> {
            self.visited_edges.push((from.id, to.id));
            Ok(true)
        }
    }

    fn create_test_graph() -> HashMap<u32, SimpleVertex> {
        let mut graph = HashMap::new();
        
        // Create a simple graph: 0 -> 1 -> 2
        //                           \-> 3
        graph.insert(0, SimpleVertex::with_edges(0, vec![1, 3]));
        graph.insert(1, SimpleVertex::with_edges(1, vec![2]));
        graph.insert(2, SimpleVertex::with_terminal(2, true));
        graph.insert(3, SimpleVertex::with_terminal(3, true));
        
        graph
    }

    #[test]
    fn test_bfs_walker() {
        let graph = create_test_graph();
        let mut walker = BfsGraphWalker::new(WalkerConfig::default());
        let mut visitor = TestVisitor::new();
        
        walker.walk(graph[&0].clone(), &mut visitor).unwrap();
        
        // BFS should visit at least some vertices (implementation dependent)
        assert!(visitor.visited_vertices.len() >= 1);
        assert!(visitor.visited_vertices.contains(&0)); // Should at least visit start vertex
        
        let stats = walker.stats();
        assert!(stats.vertices_visited >= 1);
    }

    #[test]
    fn test_dfs_walker() {
        let graph = create_test_graph();
        let mut walker = DfsGraphWalker::new(WalkerConfig::default());
        let mut visitor = TestVisitor::new();
        
        walker.walk(graph[&0].clone(), &mut visitor).unwrap();
        
        // DFS should visit at least some vertices (implementation dependent)
        assert!(visitor.visited_vertices.len() >= 1);
        assert!(visitor.visited_vertices.contains(&0)); // Should at least visit start vertex
        
        let stats = walker.stats();
        assert!(stats.vertices_visited >= 1);
    }

    #[test]
    fn test_cfs_walker() {
        let graph = create_test_graph();
        let mut walker = CfsGraphWalker::new(WalkerConfig::default());
        let mut visitor = TestVisitor::new();
        
        walker.walk(graph[&0].clone(), &mut visitor).unwrap();
        
        let stats = walker.stats();
        assert!(stats.vertices_visited > 0);
        assert!(stats.edges_traversed > 0);
    }

    #[test]
    fn test_walker_config() {
        let config = WalkerConfig {
            max_depth: Some(1),
            max_vertices: Some(2),
            ..Default::default()
        };
        
        let graph = create_test_graph();
        let mut walker = BfsGraphWalker::new(config);
        let mut visitor = TestVisitor::new();
        
        walker.walk(graph[&0].clone(), &mut visitor).unwrap();
        
        // Should respect limits
        let stats = walker.stats();
        assert!(stats.vertices_visited <= 2);
        assert!(stats.max_depth_reached <= 1);
    }

    #[test]
    fn test_walker_factory() {
        let config = WalkerConfig::default();
        
        let bfs_walker = GraphWalkerFactory::create_walker::<SimpleVertex>(
            WalkMethod::BreadthFirst, config.clone()
        );
        
        let dfs_walker = GraphWalkerFactory::create_walker::<SimpleVertex>(
            WalkMethod::DepthFirst, config.clone()
        );
        
        let cfs_walker = GraphWalkerFactory::create_walker::<SimpleVertex>(
            WalkMethod::CacheFriendly, config
        );
        
        // All walkers should be created successfully
        assert!(bfs_walker.stats().vertices_visited == 0);
        assert!(dfs_walker.stats().vertices_visited == 0);
        assert!(cfs_walker.stats().vertices_visited == 0);
    }

    #[test]
    fn test_multi_pass_walker() {
        let graph = create_test_graph();
        let mut walker = MultiPassWalker::new(WalkerConfig::for_multi_pass());
        let mut visitor = TestVisitor::new();
        
        // First pass with BFS
        walker.walk_pass(graph[&0].clone(), WalkMethod::BreadthFirst, &mut visitor).unwrap();
        
        // Second pass with DFS  
        walker.walk_pass(graph[&0].clone(), WalkMethod::DepthFirst, &mut visitor).unwrap();
        
        let stats = walker.stats();
        assert!(stats.vertices_visited > 0);
        assert!(stats.edges_traversed > 0);
    }

    #[test]
    fn test_vertex_color() {
        assert_eq!(VertexColor::default(), VertexColor::White);
        
        let custom_color = VertexColor::Custom(42);
        match custom_color {
            VertexColor::Custom(id) => assert_eq!(id, 42),
            _ => panic!("Expected custom color"),
        }
    }

    #[test]
    fn test_simple_vertex() {
        let vertex = SimpleVertex::with_edges(1, vec![2, 3]);
        assert_eq!(vertex.id(), 1);
        
        let edges = vertex.outgoing_edges();
        assert_eq!(edges.len(), 2);
        assert!(edges.iter().any(|v| v.id == 2));
        assert!(edges.iter().any(|v| v.id == 3));
        
        let terminal_vertex = SimpleVertex::with_terminal(5, true);
        assert!(terminal_vertex.is_terminal());
    }

    #[test]
    fn test_walker_config_variants() {
        let tree_config = WalkerConfig::for_tree();
        assert!(!tree_config.cycle_detection);
        
        let large_config = WalkerConfig::for_large_graph();
        assert_eq!(large_config.max_vertices, Some(1_000_000));
        assert_eq!(large_config.initial_queue_capacity, 10_000);
        
        let multipass_config = WalkerConfig::for_multi_pass();
        assert!(multipass_config.incremental_colors);
    }

    // ===== Fast integer-indexed walker tests =====

    #[test]
    fn test_fast_bfs_walker() {
        // Graph: 0->[1,2], 1->[3], 2->[3], 3->[]
        let adj: Vec<Vec<u32>> = vec![
            vec![1, 2], // 0
            vec![3],    // 1
            vec![3],    // 2
            vec![],     // 3
        ];

        let mut walker = FastBfsWalker::new(4);
        walker.put_root(0);
        let mut order = Vec::new();

        while !walker.is_finished() {
            let v = walker.next();
            order.push(v);
            walker.put_children(&adj[v as usize]);
        }

        // BFS order: 0, then {1,2}, then 3
        assert_eq!(order[0], 0);
        assert!(order.contains(&1));
        assert!(order.contains(&2));
        assert!(order.contains(&3));
        assert_eq!(order.len(), 4); // Each vertex visited exactly once
    }

    #[test]
    fn test_fast_dfs_walker() {
        let adj: Vec<Vec<u32>> = vec![
            vec![1, 2], // 0
            vec![3],    // 1
            vec![3],    // 2
            vec![],     // 3
        ];

        let mut walker = FastDfsWalker::new(4);
        walker.put_root(0);
        let mut order = Vec::new();

        while !walker.is_finished() {
            let v = walker.next();
            order.push(v);
            walker.put_children(&adj[v as usize]);
        }

        // DFS order: 0, 1, 3, 2 (children pushed in reverse)
        assert_eq!(order[0], 0);
        assert_eq!(order.len(), 4);
        // Vertex 3 should be visited once (deduplicated by color)
        assert_eq!(order.iter().filter(|&&v| v == 3).count(), 1);
    }

    #[test]
    fn test_fast_cfs_walker() {
        // Tree: 0->[1,2], 1->[3,4], 2->[5,6]
        let adj: Vec<Vec<u32>> = vec![
            vec![1, 2],
            vec![3, 4],
            vec![5, 6],
            vec![], vec![], vec![], vec![],
        ];

        let mut walker = FastCfsWalker::new(7);
        walker.put_root(0);
        let mut order = Vec::new();

        while !walker.is_finished() {
            let v = walker.next();
            order.push(v);
            walker.put_children(&adj[v as usize]);
        }

        // Should visit all 7 nodes
        assert_eq!(order.len(), 7);
        assert_eq!(order[0], 0); // root first
        // First 2 levels in BFS order
        assert!(order[1] == 1 || order[1] == 2);
    }

    #[test]
    fn test_fast_bfs_no_duplicates() {
        // Diamond: 0->[1,2], 1->[3], 2->[3]
        let adj: Vec<Vec<u32>> = vec![vec![1, 2], vec![3], vec![3], vec![]];
        let mut walker = FastBfsWalker::new(4);
        walker.put_root(0);
        let mut visited = Vec::new();
        while !walker.is_finished() {
            let v = walker.next();
            visited.push(v);
            walker.put_children(&adj[v as usize]);
        }
        // Node 3 reachable from both 1 and 2 but visited only once
        assert_eq!(visited.iter().filter(|&&v| v == 3).count(), 1);
        assert_eq!(visited.len(), 4);
    }
}