zipora 3.1.3

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
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
//! Local Pattern Matching Engine for PA-Zip Algorithm
//!
//! This module implements a hash table-based local pattern matching system for the PA-Zip
//! compression algorithm. Unlike global dictionary matching, this focuses on finding matches
//! within a sliding window of recent data for optimal compression of local patterns.
//!
//! # Algorithm Overview
//!
//! The local matcher uses a sophisticated hash table approach:
//! 1. **Hash Chains**: Efficient collision handling with configurable probe limits
//! 2. **Sliding Window**: Maintains a circular buffer of recent data
//! 3. **Fast Lookups**: Hash table for O(1) average case pattern lookups
//! 4. **SIMD Acceleration**: Hardware-accelerated string comparisons
//! 5. **Cost-Aware Selection**: Optimizes for PA-Zip compression types
//!
//! # Performance Characteristics
//!
//! - **Pattern Lookup**: O(1) average, O(k) worst case (k = probe limit)
//! - **String Comparison**: SIMD-accelerated with AVX2/SSE4.2 support
//! - **Memory Usage**: Configurable sliding window size (typically 64KB-1MB)
//! - **Cache Efficiency**: >90% hit rate for typical data with repetitive patterns
//!
//! # Integration with PA-Zip
//!
//! The local matcher generates matches for distance-based compression types:
//! - **RLE**: Run-length encoding (distance=1, length 2-33)
//! - **NearShort**: Short nearby matches (distance 2-9, length 2-5)
//! - **Far1Short**: Medium distance matches (distance 2-257, length 2-33)
//! - **Far2Short**: Far distance matches (distance 258-65793, length 2-33)
//! - **Far2Long**: Long matches (distance 0-65535, length 34+)
//! - **Far3Long**: Very long matches (distance 0-16M-1, variable length)
//!
//! # Usage Example
//!
//! ```rust
//! use zipora::compression::dict_zip::local_matcher::{LocalMatcher, LocalMatcherConfig};
//! use zipora::memory::{SecureMemoryPool, SecurePoolConfig};
//!
//! // Create local matcher with 64KB sliding window
//! let config = LocalMatcherConfig {
//!     window_size: 64 * 1024,
//!     max_probe_distance: 8,
//!     min_match_length: 3,
//!     max_match_length: 258,
//!     ..Default::default()
//! };
//!
//! let pool = SecureMemoryPool::new(SecurePoolConfig::new(4096, 1024, 8))?;
//! let mut matcher = LocalMatcher::new(config, pool)?;
//!
//! // Add data to sliding window and find matches
//! let input = b"the quick brown fox jumps over the lazy dog";
//! for (pos, &byte) in input.iter().enumerate() {
//!     matcher.add_byte(byte, pos);
//! }
//!
//! // Find best local match at position
//! let matches = matcher.find_matches(input, 35, 100)?; // "the" at end
//! for local_match in matches {
//!     println!("Match: len={}, distance={}, type={:?}",
//!         local_match.length, local_match.distance, local_match.compression_type);
//! }
//! # Ok::<(), zipora::error::ZiporaError>(())
//! ```

use crate::compression::dict_zip::compression_types::{CompressionType, MAX_FAR3_LONG_DISTANCE};
use crate::error::{Result, ZiporaError};
use crate::hash_map::{ZiporaHashMap, fabo_hash_combine_u32, SimdStringOps};
use crate::memory::SecureMemoryPool;

#[cfg(test)]
use crate::memory::get_global_pool_for_size;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use std::cmp::{Ordering, min};
use std::collections::VecDeque;
use std::sync::Arc;

/// Default sliding window size (64KB)
pub const DEFAULT_WINDOW_SIZE: usize = 64 * 1024;

/// Default maximum probe distance for hash chains
pub const DEFAULT_MAX_PROBE_DISTANCE: usize = 8;

/// Default minimum match length
pub const DEFAULT_MIN_MATCH_LENGTH: usize = 3;

/// Default maximum match length  
pub const DEFAULT_MAX_MATCH_LENGTH: usize = 258;

/// Default hash table initial capacity
pub const DEFAULT_HASH_TABLE_CAPACITY: usize = 4096;

/// Maximum entries per hash chain
pub const MAX_CHAIN_LENGTH: usize = 16;

/// Hash pattern length (bytes used for hashing)
pub const HASH_PATTERN_LENGTH: usize = 4;

/// Local match result containing position, distance, and compression type information
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct LocalMatch {
    /// Length of the matched pattern
    pub length: usize,
    /// Distance to the previous occurrence (1 = previous byte)
    pub distance: usize,
    /// Position in input where match starts
    pub input_position: usize,
    /// Position in history where match was found
    pub history_position: usize,
    /// Recommended compression type for this match
    pub compression_type: CompressionType,
    /// Match quality score (0.0 to 1.0, higher is better)
    pub quality: f64,
    /// Estimated compression benefit (bytes saved)
    pub compression_benefit: isize,
}

impl LocalMatch {
    /// Create a new local match
    pub fn new(
        length: usize,
        distance: usize,
        input_position: usize,
        history_position: usize,
    ) -> Self {
        let compression_type = Self::determine_compression_type(distance, length);
        let quality = Self::calculate_quality(length, distance, compression_type);
        let compression_benefit = Self::calculate_compression_benefit(length, compression_type);

        Self {
            length,
            distance,
            input_position,
            history_position,
            compression_type,
            quality,
            compression_benefit,
        }
    }

    /// Determine the optimal compression type for given distance and length
    fn determine_compression_type(distance: usize, length: usize) -> CompressionType {
        // Determine compression type based on PA-Zip algorithm rules
        if distance == 1 && length >= 2 && length <= 33 {
            CompressionType::RLE
        } else if distance >= 2 && distance <= 9 && length >= 2 && length <= 5 {
            CompressionType::NearShort
        } else if distance >= 2 && distance <= 257 && length >= 2 && length <= 33 {
            CompressionType::Far1Short
        } else if distance >= 258 && distance <= 65793 && length >= 2 && length <= 33 {
            CompressionType::Far2Short
        } else if distance <= 65535 && length >= 34 {
            CompressionType::Far2Long
        } else if distance <= MAX_FAR3_LONG_DISTANCE && length >= 34 {
            CompressionType::Far3Long
        } else {
            // Fallback - should not happen with proper input validation
            CompressionType::Far1Short
        }
    }

    /// Calculate match quality based on length, distance, and compression type
    fn calculate_quality(length: usize, distance: usize, compression_type: CompressionType) -> f64 {
        // Base quality from length (longer matches are better)
        let length_quality = 1.0 - (-(length as f64) / 50.0).exp();

        // Distance penalty (closer matches are generally better)
        let distance_penalty = match compression_type {
            CompressionType::RLE => 0.0, // No penalty for RLE
            CompressionType::NearShort => 0.05,
            CompressionType::Far1Short => 0.1,
            CompressionType::Far2Short => 0.15,
            CompressionType::Far2Long => 0.1, // Long matches offset distance penalty
            CompressionType::Far3Long => 0.12,
            _ => 0.2, // Should not happen
        };

        // Compression type bonus (some types compress better)
        let type_bonus = match compression_type {
            CompressionType::RLE => 0.2, // RLE is very efficient
            CompressionType::NearShort => 0.1,
            CompressionType::Far1Short => 0.05,
            CompressionType::Far2Short => 0.0,
            CompressionType::Far2Long => 0.15, // Long matches are efficient
            CompressionType::Far3Long => 0.1,
            _ => 0.0,
        };

        (length_quality - distance_penalty + type_bonus).clamp(0.0, 1.0)
    }

    /// Calculate estimated compression benefit in bytes
    fn calculate_compression_benefit(length: usize, compression_type: CompressionType) -> isize {
        // Estimate encoding cost for this compression type
        let encoding_cost = match compression_type {
            CompressionType::RLE => 2, // Type + length
            CompressionType::NearShort => 2, // Type + distance + length
            CompressionType::Far1Short => 3, // Type + distance (1-2 bytes) + length
            CompressionType::Far2Short => 4, // Type + distance (2 bytes) + length
            CompressionType::Far2Long => 4, // Type + distance (2 bytes) + length
            CompressionType::Far3Long => 5, // Type + distance (3 bytes) + length
            _ => 4, // Conservative estimate
        };

        length as isize - encoding_cost
    }

    /// Check if this match is better than another
    pub fn is_better_than(&self, other: &LocalMatch) -> bool {
        // Primary comparison: compression benefit
        match self.compression_benefit.cmp(&other.compression_benefit) {
            Ordering::Greater => true,
            Ordering::Equal => {
                // Secondary: match length
                match self.length.cmp(&other.length) {
                    Ordering::Greater => true,
                    Ordering::Equal => {
                        // Tertiary: quality score
                        self.quality > other.quality
                    }
                    Ordering::Less => false,
                }
            }
            Ordering::Less => false,
        }
    }
}

/// Hash chain entry for collision handling
#[derive(Debug, Clone, Copy)]
struct ChainEntry {
    /// Position in the sliding window
    position: usize,
    /// Hash of the pattern at this position
    pattern_hash: u32,
    /// Length of valid data from this position
    available_length: usize,
}

impl ChainEntry {
    fn new(position: usize, pattern_hash: u32, available_length: usize) -> Self {
        Self {
            position,
            pattern_hash,
            available_length,
        }
    }
}

/// Configuration for local pattern matcher
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct LocalMatcherConfig {
    /// Size of sliding window buffer
    pub window_size: usize,
    /// Maximum distance to probe in hash chains
    pub max_probe_distance: usize,
    /// Minimum match length to consider
    pub min_match_length: usize,
    /// Maximum match length to search for
    pub max_match_length: usize,
    /// Initial hash table capacity
    pub hash_table_capacity: usize,
    /// Enable SIMD optimizations for string comparisons
    pub enable_simd: bool,
    /// Maximum number of matches to return per search
    pub max_matches_per_search: usize,
    /// Enable run-length encoding detection
    pub enable_rle_detection: bool,
    /// Minimum RLE length to consider
    pub min_rle_length: usize,
}

impl Default for LocalMatcherConfig {
    fn default() -> Self {
        Self {
            window_size: DEFAULT_WINDOW_SIZE,
            max_probe_distance: DEFAULT_MAX_PROBE_DISTANCE,
            min_match_length: DEFAULT_MIN_MATCH_LENGTH,
            max_match_length: DEFAULT_MAX_MATCH_LENGTH,
            hash_table_capacity: DEFAULT_HASH_TABLE_CAPACITY,
            enable_simd: cfg!(feature = "simd"),
            max_matches_per_search: 8,
            enable_rle_detection: true,
            min_rle_length: 3,
        }
    }
}

impl LocalMatcherConfig {
    /// Validate configuration parameters
    pub fn validate(&self) -> Result<()> {
        if self.window_size == 0 {
            return Err(ZiporaError::invalid_data("Window size must be > 0"));
        }

        if self.window_size > 16 * 1024 * 1024 {
            return Err(ZiporaError::invalid_data("Window size too large (max 16MB)"));
        }

        if self.max_probe_distance == 0 {
            return Err(ZiporaError::invalid_data("Max probe distance must be > 0"));
        }

        if self.max_probe_distance > MAX_CHAIN_LENGTH {
            return Err(ZiporaError::invalid_data(
                format!("Max probe distance must be <= {}", MAX_CHAIN_LENGTH).as_str()
            ));
        }

        if self.min_match_length == 0 {
            return Err(ZiporaError::invalid_data("Min match length must be > 0"));
        }

        if self.max_match_length < self.min_match_length {
            return Err(ZiporaError::invalid_data(
                "Max match length must be >= min match length"
            ));
        }

        if self.max_match_length > 65536 {
            return Err(ZiporaError::invalid_data("Max match length too large (max 64KB)"));
        }

        Ok(())
    }

    /// Create configuration optimized for fast compression
    pub fn fast_compression() -> Self {
        Self {
            window_size: 32 * 1024, // 32KB
            max_probe_distance: 4,
            min_match_length: 4,
            max_match_length: 64,
            max_matches_per_search: 4,
            ..Default::default()
        }
    }

    /// Create configuration optimized for maximum compression
    pub fn max_compression() -> Self {
        Self {
            window_size: 256 * 1024, // 256KB
            max_probe_distance: 16,
            min_match_length: 3,
            max_match_length: 512,
            max_matches_per_search: 16,
            ..Default::default()
        }
    }

    /// Create configuration for real-time compression
    pub fn realtime() -> Self {
        Self {
            window_size: 16 * 1024, // 16KB
            max_probe_distance: 2,
            min_match_length: 4,
            max_match_length: 32,
            max_matches_per_search: 2,
            enable_simd: true,
            ..Default::default()
        }
    }
}

/// Performance statistics for local matcher
#[derive(Debug, Clone, Default)]
pub struct LocalMatcherStats {
    /// Total number of bytes added to sliding window
    pub bytes_added: u64,
    /// Total number of match searches performed
    pub searches_performed: u64,
    /// Total number of matches found
    pub matches_found: u64,
    /// Total number of hash collisions encountered
    pub hash_collisions: u64,
    /// Total number of string comparisons performed
    pub string_comparisons: u64,
    /// Total time spent in SIMD string operations (microseconds)
    pub simd_time_us: u64,
    /// Average match length for successful matches
    pub avg_match_length: f64,
    /// Hash table load factor (0.0 to 1.0)
    pub hash_table_load_factor: f64,
    /// Number of chain entries evicted due to window sliding
    pub entries_evicted: u64,
}

impl LocalMatcherStats {
    /// Calculate match success ratio
    pub fn match_success_ratio(&self) -> f64 {
        if self.searches_performed == 0 {
            0.0
        } else {
            self.matches_found as f64 / self.searches_performed as f64
        }
    }

    /// Calculate average search efficiency (matches per collision)
    pub fn search_efficiency(&self) -> f64 {
        if self.hash_collisions == 0 {
            if self.matches_found > 0 { f64::INFINITY } else { 0.0 }
        } else {
            self.matches_found as f64 / self.hash_collisions as f64
        }
    }
}

/// High-performance local pattern matcher with hash table approach
#[derive(Clone)]
pub struct LocalMatcher {
    /// Configuration parameters
    config: LocalMatcherConfig,
    /// Sliding window buffer
    window: VecDeque<u8>,
    /// Current position in the input stream
    current_position: usize,
    /// Hash table mapping pattern hashes to chain entries
    hash_table: ZiporaHashMap<u32, Vec<ChainEntry>>,
    /// SIMD string operations
    simd_ops: Arc<SimdStringOps>,
    /// Memory pool for allocations
    memory_pool: Arc<SecureMemoryPool>,
    /// Performance statistics
    stats: LocalMatcherStats,
}

impl LocalMatcher {
    /// Create a new local matcher with the given configuration
    pub fn new(config: LocalMatcherConfig, memory_pool: Arc<SecureMemoryPool>) -> Result<Self> {
        config.validate()?;

        let hash_table = ZiporaHashMap::new()?;

        let simd_ops = Arc::new(SimdStringOps::new());

        let window_size = config.window_size;
        Ok(Self {
            config,
            window: VecDeque::with_capacity(window_size),
            current_position: 0,
            hash_table,
            simd_ops,
            memory_pool,
            stats: LocalMatcherStats::default(),
        })
    }

    /// Add a byte to the sliding window and update hash table
    pub fn add_byte(&mut self, byte: u8, position: usize) -> Result<()> {
        self.current_position = position;
        
        // Add byte to sliding window
        if self.window.len() >= self.config.window_size {
            // Remove oldest byte and clean up hash table
            let _removed_byte = self.window.pop_front()
                .ok_or_else(|| ZiporaError::invalid_data("Window unexpectedly empty during byte removal"))?;
            self.cleanup_hash_table_entry(position - self.config.window_size)?;
        }
        
        self.window.push_back(byte);
        self.stats.bytes_added += 1;

        // Add new hash table entries if we have enough data
        self.add_hash_table_entries(position)?;

        Ok(())
    }

    /// Add multiple bytes to the sliding window efficiently
    pub fn add_bytes(&mut self, bytes: &[u8], start_position: usize) -> Result<()> {
        for (i, &byte) in bytes.iter().enumerate() {
            self.add_byte(byte, start_position + i)?;
        }
        Ok(())
    }

    /// Find local matches at the given input position
    pub fn find_matches(
        &mut self,
        input: &[u8],
        input_pos: usize,
        max_search_length: usize,
    ) -> Result<Vec<LocalMatch>> {
        self.stats.searches_performed += 1;

        if input_pos >= input.len() {
            return Ok(Vec::new());
        }

        let search_end = min(
            input_pos + max_search_length,
            input.len()
        );
        let pattern_len = min(
            search_end - input_pos,
            self.config.max_match_length
        );

        if pattern_len < self.config.min_match_length {
            return Ok(Vec::new());
        }

        let mut matches = Vec::new();

        // First check for RLE (run-length encoding)
        if self.config.enable_rle_detection {
            if let Some(rle_match) = self.find_rle_match(input, input_pos, pattern_len)? {
                matches.push(rle_match);
            }
        }

        // Then search for general pattern matches
        let pattern_matches = self.find_pattern_matches(input, input_pos, pattern_len)?;
        matches.extend(pattern_matches);

        // Sort by quality and return best matches
        matches.sort_by(|a, b| {
            b.quality.partial_cmp(&a.quality).unwrap_or(Ordering::Equal)
                .then_with(|| b.compression_benefit.cmp(&a.compression_benefit))
        });

        matches.truncate(self.config.max_matches_per_search);

        self.stats.matches_found += matches.len() as u64;

        // Update rolling average match length
        if !matches.is_empty() {
            let total_length: usize = matches.iter().map(|m| m.length).sum();
            let avg_length = total_length as f64 / matches.len() as f64;
            self.stats.avg_match_length = 
                (self.stats.avg_match_length * (self.stats.searches_performed - 1) as f64 + avg_length) 
                / self.stats.searches_performed as f64;
        }

        Ok(matches)
    }

    /// Find RLE (run-length encoding) matches
    fn find_rle_match(
        &self,
        input: &[u8],
        input_pos: usize,
        max_length: usize,
    ) -> Result<Option<LocalMatch>> {
        if input_pos == 0 || input_pos >= input.len() {
            return Ok(None);
        }

        let current_byte = input[input_pos];
        let prev_byte = input[input_pos - 1];

        if current_byte != prev_byte {
            return Ok(None);
        }

        // Count consecutive identical bytes
        let mut rle_length = 1; // Count current byte
        for i in (input_pos + 1)..min(input_pos + max_length, input.len()) {
            if input[i] == current_byte {
                rle_length += 1;
            } else {
                break;
            }
        }

        if rle_length >= self.config.min_rle_length {
            Ok(Some(LocalMatch::new(
                rle_length,
                1, // Distance 1 for RLE
                input_pos,
                input_pos - 1,
            )))
        } else {
            Ok(None)
        }
    }

    /// Find pattern matches using hash table lookup
    fn find_pattern_matches(
        &mut self,
        input: &[u8],
        input_pos: usize,
        max_length: usize,
    ) -> Result<Vec<LocalMatch>> {
        if input_pos + HASH_PATTERN_LENGTH > input.len() {
            return Ok(Vec::new());
        }

        let pattern_bytes = &input[input_pos..input_pos + HASH_PATTERN_LENGTH];
        let pattern_hash = self.hash_pattern(pattern_bytes);

        let mut matches = Vec::new();


        // First get a copy of the chain to avoid borrowing conflicts
        let chain_copy = match self.hash_table.get(&pattern_hash) {
            Some(chain) => {
                self.stats.hash_collisions += 1;
                chain.clone() // Clone the chain to avoid borrowing issues
            }
            None => return Ok(matches),
        };

        for (probe_idx, entry) in chain_copy.iter().enumerate() {
            if probe_idx >= self.config.max_probe_distance {
                break;
            }

            // Calculate distance
            let window_position = self.get_window_position(entry.position);
            if window_position.is_none() {
                continue; // Entry has been evicted
            }

            // Calculate distance - entry.position should be less than input_pos for a valid match
            if entry.position >= input_pos {
                continue; // Can't have a match where the pattern comes after the current position
            }
            let distance = input_pos - entry.position;
            if distance == 0 || distance > MAX_FAR3_LONG_DISTANCE {
                continue;
            }

            // Verify hash matches
            if entry.pattern_hash != pattern_hash {
                continue;
            }

            // Perform string comparison to find actual match length
            let match_length = self.compare_strings_and_find_length(
                input,
                input_pos,
                entry.position,
                max_length,
            )?;

            if match_length >= self.config.min_match_length {
                let local_match = LocalMatch::new(
                    match_length,
                    distance,
                    input_pos,
                    entry.position,
                );

                matches.push(local_match);
            }

            self.stats.string_comparisons += 1;
        }

        Ok(matches)
    }

    /// Hash a pattern using FaboHashCombine
    fn hash_pattern(&self, pattern: &[u8]) -> u32 {
        if pattern.len() >= 4 {
            let word = u32::from_le_bytes([pattern[0], pattern[1], pattern[2], pattern[3]]);
            fabo_hash_combine_u32(word, 0x9e3779b9) // Golden ratio constant
        } else {
            // Handle short patterns
            let mut word = 0u32;
            for (i, &byte) in pattern.iter().enumerate() {
                word |= (byte as u32) << (i * 8);
            }
            fabo_hash_combine_u32(word, 0x9e3779b9)
        }
    }

    /// Compare strings and find match length using SIMD when possible
    fn compare_strings_and_find_length(
        &mut self,
        input: &[u8],
        input_pos: usize,
        history_pos: usize,
        max_length: usize,
    ) -> Result<usize> {
        let input_slice = &input[input_pos..min(input_pos + max_length, input.len())];
        
        // Get history slice from sliding window
        let window_idx = match self.get_window_position(history_pos) {
            Some(idx) => idx,
            None => return Ok(0),
        };
        
        if window_idx + max_length > self.window.len() {
            return Ok(0);
        }

        // Convert VecDeque slice to contiguous slice for comparison
        let history_slice = {
            let (first, second) = self.window.as_slices();
            if window_idx + max_length <= first.len() {
                &first[window_idx..window_idx + max_length]
            } else if window_idx >= first.len() {
                let second_idx = window_idx - first.len();
                &second[second_idx..min(second_idx + max_length, second.len())]
            } else {
                // Split across boundary - use slower approach
                return self.compare_strings_across_boundary(input_slice, window_idx, max_length);
            }
        };

        // Use SIMD comparison if enabled and beneficial
        #[cfg(feature = "simd")]
        if self.config.enable_simd && max_length >= 16 {
            let start_time = std::time::Instant::now();
            let result = self.simd_compare_and_find_length(input_slice, history_slice, max_length);
            self.stats.simd_time_us += start_time.elapsed().as_micros() as u64;
            return Ok(result);
        }

        // Fallback to scalar comparison
        Ok(self.scalar_compare_and_find_length(input_slice, history_slice))
    }

    /// Compare strings across VecDeque boundary (optimized for boundary crossing)
    fn compare_strings_across_boundary(
        &self,
        input_slice: &[u8],
        window_idx: usize,
        max_length: usize,
    ) -> Result<usize> {
        let search_len = min(input_slice.len(), max_length);
        if search_len == 0 {
            return Ok(0);
        }

        let (first, second) = self.window.as_slices();
        let mut match_length = 0;
        let mut input_pos = 0;
        let mut window_pos = window_idx;

        // First chunk: from window_idx to end of first slice
        if window_pos < first.len() {
            let first_chunk_len = min(first.len() - window_pos, search_len);
            let first_chunk = &first[window_pos..window_pos + first_chunk_len];
            let input_chunk = &input_slice[input_pos..input_pos + first_chunk_len];
            
            // Compare chunk efficiently using memcmp-style comparison
            for i in 0..first_chunk_len {
                if input_chunk[i] == first_chunk[i] {
                    match_length += 1;
                } else {
                    return Ok(match_length);
                }
            }
            
            input_pos += first_chunk_len;
            window_pos = 0; // Move to start of second slice
        } else {
            // Adjust window position to second slice
            window_pos -= first.len();
        }

        // Second chunk: continue in second slice if needed
        if input_pos < search_len && !second.is_empty() {
            let remaining_search = search_len - input_pos;
            let second_chunk_len = min(second.len() - window_pos, remaining_search);
            if second_chunk_len > 0 {
                let second_chunk = &second[window_pos..window_pos + second_chunk_len];
                let input_chunk = &input_slice[input_pos..input_pos + second_chunk_len];
                
                // Compare second chunk efficiently
                for i in 0..second_chunk_len {
                    if input_chunk[i] == second_chunk[i] {
                        match_length += 1;
                    } else {
                        return Ok(match_length);
                    }
                }
            }
        }

        Ok(match_length)
    }

    /// SIMD-accelerated string comparison and find length
    #[cfg(feature = "simd")]
    fn simd_compare_and_find_length(&self, input: &[u8], history: &[u8], max_length: usize) -> usize {
        use std::arch::x86_64::*;
        
        let max_len = min(min(input.len(), history.len()), max_length);
        let mut pos = 0;

        // Process 16-byte chunks with SSE2
        // SAFETY: Pointers are bounds-checked before _mm_loadu_si128, SSE2 guaranteed by x86_64 target
        unsafe {
            let simd_chunks = max_len / 16;
            for _ in 0..simd_chunks {
                if pos + 16 > input.len() || pos + 16 > history.len() {
                    break;
                }

                let input_chunk = _mm_loadu_si128(input.as_ptr().add(pos) as *const __m128i);
                let history_chunk = _mm_loadu_si128(history.as_ptr().add(pos) as *const __m128i);
                
                let comparison = _mm_cmpeq_epi8(input_chunk, history_chunk);
                let mask = _mm_movemask_epi8(comparison) as u16;

                if mask != 0xFFFF {
                    // Found mismatch within this chunk
                    let mismatch_pos = mask.trailing_ones() as usize;
                    return pos + mismatch_pos;
                }

                pos += 16;
            }
        }

        // Handle remaining bytes with scalar comparison
        while pos < max_len {
            if input[pos] != history[pos] {
                break;
            }
            pos += 1;
        }

        pos
    }

    /// Scalar string comparison (fallback)
    fn scalar_compare_and_find_length(&self, input: &[u8], history: &[u8]) -> usize {
        let max_len = min(input.len(), history.len());
        
        for i in 0..max_len {
            if input[i] != history[i] {
                return i;
            }
        }

        max_len
    }

    /// Add hash table entries for patterns starting at the current position
    fn add_hash_table_entries(&mut self, position: usize) -> Result<()> {
        if self.window.len() < HASH_PATTERN_LENGTH {
            return Ok(());
        }

        // Only add hash entries if we have enough positions to form a complete pattern
        // We need at least HASH_PATTERN_LENGTH positions (0, 1, 2, 3) to form a pattern
        if position + 1 < HASH_PATTERN_LENGTH {
            return Ok(());
        }

        // Extract pattern from the end of the window
        let pattern_start = self.window.len() - HASH_PATTERN_LENGTH;
        let pattern_bytes: Vec<u8> = self.window.range(pattern_start..).copied().collect();
        let pattern_hash = self.hash_pattern(&pattern_bytes);


        // The pattern starts at position - HASH_PATTERN_LENGTH + 1
        // Rewrite to avoid usize underflow: position - HASH_PATTERN_LENGTH + 1 = position + 1 - HASH_PATTERN_LENGTH
        let pattern_start_position = position + 1 - HASH_PATTERN_LENGTH;
        let entry = ChainEntry::new(
            pattern_start_position,
            pattern_hash,
            self.window.len() - pattern_start,
        );

        // Add to hash table chain
        if self.hash_table.get(&pattern_hash).is_none() {
            self.hash_table.insert(pattern_hash, Vec::new());
        }
        let chain = self.hash_table.get_mut(&pattern_hash)
            .ok_or_else(|| ZiporaError::invalid_data("Hash table entry unexpectedly missing"))?;

        // Keep chain length limited
        if chain.len() >= MAX_CHAIN_LENGTH {
            chain.remove(0); // Remove oldest entry
            self.stats.entries_evicted += 1;
        }

        chain.push(entry);

        // Update hash table load factor
        self.stats.hash_table_load_factor = 
            self.hash_table.len() as f64 / self.config.hash_table_capacity as f64;

        Ok(())
    }

    /// Clean up hash table entries that have been evicted from the sliding window
    fn cleanup_hash_table_entry(&mut self, evicted_position: usize) -> Result<()> {
        // This is a simplified cleanup - in practice, we might want more sophisticated
        // garbage collection to avoid iterating through all chains
        let mut _empty_keys: Vec<u32> = Vec::new();

        let keys_to_remove: Vec<u32> = self.hash_table.iter()
            .filter_map(|(hash, chain)| {
                if chain.iter().all(|entry| entry.position <= evicted_position) {
                    Some(*hash)
                } else {
                    None
                }
            })
            .collect();

        // Remove chains with all evicted entries
        for key in keys_to_remove {
            self.hash_table.remove(&key);
        }

        Ok(())
    }

    /// Get position in sliding window for absolute position
    fn get_window_position(&self, absolute_position: usize) -> Option<usize> {
        if absolute_position > self.current_position {
            return None;
        }

        let distance = self.current_position - absolute_position;
        if distance >= self.window.len() {
            return None;
        }

        Some(self.window.len() - distance - 1)
    }

    /// Get current performance statistics
    pub fn stats(&self) -> &LocalMatcherStats {
        &self.stats
    }

    /// Reset performance statistics
    pub fn reset_stats(&mut self) {
        self.stats = LocalMatcherStats::default();
    }

    /// Get current configuration
    pub fn config(&self) -> &LocalMatcherConfig {
        &self.config
    }

    /// Get current sliding window size
    pub fn window_size(&self) -> usize {
        self.window.len()
    }

    /// Check if the sliding window is full
    pub fn is_window_full(&self) -> bool {
        self.window.len() >= self.config.window_size
    }

    /// Clear the sliding window and hash table (for reset)
    pub fn clear(&mut self) {
        self.window.clear();
        self.hash_table.clear();
        self.current_position = 0;
        self.reset_stats();
    }

    /// Validate internal consistency (for debugging)
    pub fn validate(&self) -> Result<()> {
        // Check window size
        if self.window.len() > self.config.window_size {
            return Err(ZiporaError::invalid_data("Window size exceeds configuration limit"));
        }

        // Check hash table chain lengths
        for (_, chain) in self.hash_table.iter() {
            if chain.len() > MAX_CHAIN_LENGTH {
                return Err(ZiporaError::invalid_data("Hash chain exceeds maximum length"));
            }
        }

        Ok(())
    }

    /// Find the best local match at the given position
    pub fn find_match(
        &mut self,
        remaining: &[u8],
        max_probe_distance: usize,
        max_length: usize,
    ) -> Result<Option<LocalMatch>> {
        if remaining.is_empty() {
            return Ok(None);
        }

        // Use find_matches and return the best one
        let matches = self.find_matches(remaining, 0, max_length)?;
        
        // Return the longest match that doesn't exceed probe distance
        Ok(matches
            .into_iter()
            .filter(|m| m.distance <= max_probe_distance)
            .max_by_key(|m| m.length))
    }
}

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

    fn create_test_matcher() -> LocalMatcher {
        let config = LocalMatcherConfig {
            window_size: 1024,
            max_probe_distance: 4,
            min_match_length: 3,
            max_match_length: 64,
            ..Default::default()
        };
        let pool = get_global_pool_for_size(1024).clone();
        LocalMatcher::new(config, pool).unwrap()
    }

    #[test]
    fn test_local_matcher_creation() {
        let matcher = create_test_matcher();
        assert_eq!(matcher.config().window_size, 1024);
        assert_eq!(matcher.config().max_probe_distance, 4);
        assert_eq!(matcher.window_size(), 0);
        assert!(!matcher.is_window_full());
    }

    #[test]
    fn test_add_bytes() {
        let mut matcher = create_test_matcher();
        let data = b"hello world hello";
        
        for (i, &byte) in data.iter().enumerate() {
            matcher.add_byte(byte, i).unwrap();
        }

        assert_eq!(matcher.window_size(), data.len());
        assert_eq!(matcher.stats().bytes_added, data.len() as u64);
    }

    #[test]
    fn test_sliding_window_overflow() {
        let mut matcher = LocalMatcher::new(
            LocalMatcherConfig {
                window_size: 8,
                ..Default::default()
            },
            get_global_pool_for_size(1024).clone()
        ).unwrap();

        let data = b"0123456789abcdef"; // 16 bytes, window is 8
        
        for (i, &byte) in data.iter().enumerate() {
            matcher.add_byte(byte, i).unwrap();
        }

        assert_eq!(matcher.window_size(), 8);
        assert!(matcher.is_window_full());
        
        // Window should contain the last 8 bytes: "9abcdef"
        let window_data: Vec<u8> = matcher.window.iter().copied().collect();
        assert_eq!(window_data, b"89abcdef");
    }

    #[test]
    fn test_rle_detection() {
        let mut matcher = create_test_matcher();
        let data = b"abcaaaa"; // Should detect RLE for 'aaaa'
        
        for (i, &byte) in data.iter().enumerate() {
            matcher.add_byte(byte, i).unwrap();
        }

        let matches = matcher.find_matches(data, 4, 10).unwrap(); // Search at 'a' after 'c'
        
        assert!(!matches.is_empty());
        let rle_match = &matches[0];
        assert_eq!(rle_match.compression_type, CompressionType::RLE);
        assert_eq!(rle_match.distance, 1);
        assert!(rle_match.length >= 3); // At least 3 consecutive 'a's
    }

    #[test]
    fn test_pattern_matching() {
        let mut matcher = create_test_matcher();
        
        // Use a simpler test case with clear repetition
        let data = b"abcabcabc"; // Clear pattern repetition
        
        // Add all data to sliding window
        for (i, &byte) in data.iter().enumerate() {
            matcher.add_byte(byte, i).unwrap();
        }

        // Search for "abc" at position 3 (should match "abc" at position 0)
        let matches = matcher.find_matches(data, 3, 6).unwrap(); 
        
        assert!(!matches.is_empty(), "Should find pattern match in 'abcabcabc'");
        
        let best_match = &matches[0];
        assert!(best_match.length >= 3); // At least "abc"
        assert!(best_match.distance > 0 && best_match.distance <= 6); // Reasonable distance  
        assert_ne!(best_match.compression_type, CompressionType::RLE);
    }

    #[test]
    fn test_compression_type_determination() {
        // Test RLE
        let rle_match = LocalMatch::new(5, 1, 10, 9);
        assert_eq!(rle_match.compression_type, CompressionType::RLE);

        // Test NearShort
        let near_match = LocalMatch::new(4, 5, 10, 5);
        assert_eq!(near_match.compression_type, CompressionType::NearShort);

        // Test Far1Short
        let far1_match = LocalMatch::new(10, 100, 10, 0);
        assert_eq!(far1_match.compression_type, CompressionType::Far1Short);

        // Test Far2Long
        let far2_long_match = LocalMatch::new(50, 1000, 100, 50);
        assert_eq!(far2_long_match.compression_type, CompressionType::Far2Long);
    }

    #[test]
    fn test_match_quality_calculation() {
        let short_match = LocalMatch::new(3, 5, 0, 0);
        let long_match = LocalMatch::new(20, 5, 0, 0);
        
        assert!(long_match.quality > short_match.quality);
        assert!(long_match.is_better_than(&short_match));
    }

    #[test]
    fn test_configuration_validation() {
        // Valid configuration
        let valid_config = LocalMatcherConfig::default();
        assert!(valid_config.validate().is_ok());

        // Invalid: zero window size
        let invalid_config = LocalMatcherConfig {
            window_size: 0,
            ..Default::default()
        };
        assert!(invalid_config.validate().is_err());

        // Invalid: max length < min length
        let invalid_config = LocalMatcherConfig {
            min_match_length: 10,
            max_match_length: 5,
            ..Default::default()
        };
        assert!(invalid_config.validate().is_err());
    }

    #[test]
    fn test_preset_configurations() {
        let fast = LocalMatcherConfig::fast_compression();
        assert_eq!(fast.window_size, 32 * 1024);
        assert_eq!(fast.max_probe_distance, 4);

        let max_comp = LocalMatcherConfig::max_compression();
        assert_eq!(max_comp.window_size, 256 * 1024);
        assert_eq!(max_comp.max_probe_distance, 16);

        let realtime = LocalMatcherConfig::realtime();
        assert_eq!(realtime.window_size, 16 * 1024);
        assert_eq!(realtime.max_probe_distance, 2);
    }

    #[test]
    fn test_hash_pattern() {
        let matcher = create_test_matcher();
        
        let pattern1 = b"test";
        let pattern2 = b"test";
        let pattern3 = b"TEST";

        let hash1 = matcher.hash_pattern(pattern1);
        let hash2 = matcher.hash_pattern(pattern2);
        let hash3 = matcher.hash_pattern(pattern3);

        assert_eq!(hash1, hash2); // Same pattern should have same hash
        assert_ne!(hash1, hash3); // Different pattern should have different hash
    }

    #[test]
    fn test_statistics_tracking() {
        let mut matcher = create_test_matcher();
        let data = b"test data test";
        
        // Add data
        for (i, &byte) in data.iter().enumerate() {
            matcher.add_byte(byte, i).unwrap();
        }

        // Perform searches
        let _ = matcher.find_matches(data, 10, 5).unwrap();
        let _ = matcher.find_matches(data, 5, 8).unwrap();

        let stats = matcher.stats();
        assert_eq!(stats.bytes_added, data.len() as u64);
        assert_eq!(stats.searches_performed, 2);
        assert!(stats.match_success_ratio() >= 0.0 && stats.match_success_ratio() <= 1.0);
    }

    #[test]
    fn test_clear_and_reset() {
        let mut matcher = create_test_matcher();
        let data = b"some test data";
        
        for (i, &byte) in data.iter().enumerate() {
            matcher.add_byte(byte, i).unwrap();
        }

        assert!(matcher.window_size() > 0);
        assert!(matcher.stats().bytes_added > 0);

        matcher.clear();

        assert_eq!(matcher.window_size(), 0);
        assert_eq!(matcher.stats().bytes_added, 0);
        assert!(!matcher.is_window_full());
    }

    #[test]
    fn test_validation() {
        let matcher = create_test_matcher();
        assert!(matcher.validate().is_ok());
    }
}