zipora 3.1.2

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
//! Advanced Set Operations for Multi-way Algorithms
//!
//! This module provides sophisticated set operations optimized for multi-way merge
//! scenarios, with specialized implementations for different numbers of input streams:
//!
//! - **Bit mask optimization**: For ≤32 ways using efficient bit manipulation
//! - **General algorithms**: For larger numbers of ways
//! - **Memory-efficient operations**: Streaming intersection and union
//! - **Hardware acceleration**: SIMD optimizations where applicable

use crate::error::{Result, ZiporaError};
use crate::algorithms::tournament_tree::EnhancedLoserTree;
use std::cmp::Ordering;
use std::collections::HashMap;

/// Configuration for set operations
#[derive(Debug, Clone)]
pub struct SetOperationsConfig {
    /// Use bit mask optimization for small numbers of ways
    pub use_bit_mask_optimization: bool,
    /// Threshold for switching to bit mask optimization
    pub bit_mask_threshold: usize,
    /// Enable frequency counting during operations
    pub count_frequencies: bool,
    /// Use SIMD acceleration when available
    pub use_simd: bool,
}

impl Default for SetOperationsConfig {
    fn default() -> Self {
        Self {
            use_bit_mask_optimization: true,
            bit_mask_threshold: 32,
            count_frequencies: false,
            use_simd: cfg!(feature = "simd"),
        }
    }
}

/// Statistics from set operations
#[derive(Debug, Clone)]
pub struct SetOperationStats {
    /// Number of input ways processed
    pub ways_processed: usize,
    /// Total input elements examined
    pub elements_examined: usize,
    /// Number of output elements produced
    pub output_elements: usize,
    /// Whether bit mask optimization was used
    pub used_bit_mask: bool,
    /// Processing time in microseconds
    pub processing_time_us: u64,
}

/// Advanced set operations for multi-way algorithms
pub struct SetOperations {
    config: SetOperationsConfig,
    stats: SetOperationStats,
}

impl SetOperations {
    /// Create a new set operations instance
    pub fn new() -> Self {
        Self::with_config(SetOperationsConfig::default())
    }

    /// Create with custom configuration
    pub fn with_config(config: SetOperationsConfig) -> Self {
        Self {
            config,
            stats: SetOperationStats {
                ways_processed: 0,
                elements_examined: 0,
                output_elements: 0,
                used_bit_mask: false,
                processing_time_us: 0,
            },
        }
    }

    /// Compute intersection of multiple sorted sequences
    ///
    /// Returns elements that appear in ALL input sequences.
    /// For ≤32 ways, uses bit mask optimization for O(1) membership testing.
    pub fn intersection<T, I>(&mut self, iterators: Vec<I>) -> Result<Vec<T>>
    where
        T: Ord + Clone + std::fmt::Debug,
        I: Iterator<Item = T> + 'static,
    {
        if iterators.is_empty() {
            return Ok(Vec::new());
        }

        let num_ways = iterators.len();
        self.stats.ways_processed = num_ways;

        let start = std::time::Instant::now();

        let result = if self.config.use_bit_mask_optimization && num_ways <= self.config.bit_mask_threshold {
            self.stats.used_bit_mask = true;
            self.intersection_bit_mask(iterators)?
        } else {
            self.stats.used_bit_mask = false;
            self.intersection_general(iterators)?
        };

        // Use nanosecond precision and convert to microseconds, ensuring minimum of 1
        // This handles sub-microsecond operations that would otherwise report as 0
        let elapsed_ns = start.elapsed().as_nanos() as u64;
        self.stats.processing_time_us = std::cmp::max(1, (elapsed_ns + 500) / 1000);
        self.stats.output_elements = result.len();

        Ok(result)
    }

    /// Intersection using bit mask optimization for ≤32 ways
    fn intersection_bit_mask<T, I>(&mut self, mut iterators: Vec<I>) -> Result<Vec<T>>
    where
        T: Ord + Clone + std::fmt::Debug,
        I: Iterator<Item = T> + 'static,
    {
        let num_ways = iterators.len();
        let full_mask = if num_ways >= 32 {
            0xFFFFFFFF
        } else {
            (1u32 << num_ways) - 1
        };

        let mut result = Vec::new();
        let mut current_values: Vec<Option<T>> = iterators.iter_mut().map(|it| it.next()).collect();
        
        loop {
            // Find the minimum value among all current values
            let mut min_value: Option<&T> = None;
            let mut min_indices = Vec::new();

            for (idx, value) in current_values.iter().enumerate() {
                if let Some(val) = value {
                    match min_value {
                        None => {
                            min_value = Some(val);
                            min_indices = vec![idx];
                        }
                        Some(min_val) => match val.cmp(min_val) {
                            Ordering::Less => {
                                min_value = Some(val);
                                min_indices = vec![idx];
                            }
                            Ordering::Equal => {
                                min_indices.push(idx);
                            }
                            Ordering::Greater => {}
                        }
                    }
                }
            }

            // If no minimum found, we're done
            let min_val = match min_value {
                Some(val) => val.clone(),
                None => break,
            };

            // Create bit mask for ways that have this minimum value
            let mut current_mask = 0u32;
            for &idx in &min_indices {
                current_mask |= 1u32 << idx;
            }

            // If all ways have this value, add to intersection
            if current_mask == full_mask {
                result.push(min_val.clone());
            }

            // Advance iterators that had the minimum value
            for &idx in &min_indices {
                current_values[idx] = iterators[idx].next();
            }

            self.stats.elements_examined += min_indices.len();
        }

        Ok(result)
    }

    /// General intersection algorithm for larger numbers of ways
    fn intersection_general<T, I>(&mut self, iterators: Vec<I>) -> Result<Vec<T>>
    where
        T: Ord + Clone + std::fmt::Debug,
        I: Iterator<Item = T> + 'static,
    {
        let num_ways = iterators.len();
        let mut result = Vec::new();
        
        // Convert iterators to way iterators for the tournament tree
        let mut tree = EnhancedLoserTree::new(crate::algorithms::LoserTreeConfig::default());
        
        for iterator in iterators {
            tree.add_way(iterator)?;
        }
        
        tree.initialize()?;

        // Process elements using the tournament tree
        let mut current_key: Option<T> = None;
        let mut count = 0;

        while !tree.is_empty() {
            if let Some(value) = tree.pop()? {
                match &current_key {
                    None => {
                        current_key = Some(value.clone());
                        count = 1;
                    }
                    Some(key) => {
                        match value.cmp(key) {
                            Ordering::Equal => {
                                count += 1;
                            }
                            Ordering::Greater => {
                                // Check if previous key appeared in all ways
                                if count == num_ways {
                                    result.push(key.clone());
                                }
                                current_key = Some(value.clone());
                                count = 1;
                            }
                            Ordering::Less => {
                                return Err(ZiporaError::invalid_data("Input sequences not properly sorted"));
                            }
                        }
                    }
                }
                self.stats.elements_examined += 1;
            }
        }

        // Check the last key
        if let Some(key) = current_key {
            if count == num_ways {
                result.push(key);
            }
        }

        Ok(result)
    }

    /// Compute union of multiple sorted sequences
    ///
    /// Returns all unique elements from input sequences in sorted order.
    pub fn union<T, I>(&mut self, iterators: Vec<I>) -> Result<Vec<T>>
    where
        T: Ord + Clone + std::fmt::Debug,
        I: Iterator<Item = T> + 'static,
    {
        if iterators.is_empty() {
            return Ok(Vec::new());
        }

        let start = std::time::Instant::now();
        self.stats.ways_processed = iterators.len();

        let mut tree = EnhancedLoserTree::new(crate::algorithms::LoserTreeConfig::default());
        
        for iterator in iterators {
            tree.add_way(iterator)?;
        }
        
        tree.initialize()?;

        let mut result = Vec::new();
        let mut last_value: Option<T> = None;

        while !tree.is_empty() {
            if let Some(value) = tree.pop()? {
                // Only add if different from last value (deduplication)
                let should_add = match &last_value {
                    None => true,
                    Some(last) => value.cmp(last) != Ordering::Equal,
                };

                if should_add {
                    result.push(value.clone());
                    last_value = Some(value);
                }

                self.stats.elements_examined += 1;
            }
        }

        // Use nanosecond precision and convert to microseconds, ensuring minimum of 1
        // This handles sub-microsecond operations that would otherwise report as 0
        let elapsed_ns = start.elapsed().as_nanos() as u64;
        self.stats.processing_time_us = std::cmp::max(1, (elapsed_ns + 500) / 1000);
        self.stats.output_elements = result.len();

        Ok(result)
    }

    /// Count frequency of elements across multiple sorted sequences
    pub fn count_frequencies<T, I>(&mut self, iterators: Vec<I>) -> Result<HashMap<T, usize>>
    where
        T: Ord + Clone + std::hash::Hash + std::fmt::Debug,
        I: Iterator<Item = T> + 'static,
    {
        let start = std::time::Instant::now();
        self.stats.ways_processed = iterators.len();

        let mut tree = EnhancedLoserTree::new(crate::algorithms::LoserTreeConfig::default());
        
        for iterator in iterators {
            tree.add_way(iterator)?;
        }
        
        tree.initialize()?;

        let mut frequencies = HashMap::new();

        while !tree.is_empty() {
            if let Some(value) = tree.pop()? {
                *frequencies.entry(value).or_insert(0) += 1;
                self.stats.elements_examined += 1;
            }
        }

        // Use nanosecond precision and convert to microseconds, ensuring minimum of 1
        // This handles sub-microsecond operations that would otherwise report as 0
        let elapsed_ns = start.elapsed().as_nanos() as u64;
        self.stats.processing_time_us = std::cmp::max(1, (elapsed_ns + 500) / 1000);
        self.stats.output_elements = frequencies.len();

        Ok(frequencies)
    }

    /// Filter elements using a custom predicate during merge
    pub fn filter_merge<T, I, P>(&mut self, iterators: Vec<I>, predicate: P) -> Result<Vec<T>>
    where
        T: Ord + Clone + std::fmt::Debug,
        I: Iterator<Item = T> + 'static,
        P: Fn(&T) -> bool,
    {
        let start = std::time::Instant::now();
        self.stats.ways_processed = iterators.len();

        let mut tree = EnhancedLoserTree::new(crate::algorithms::LoserTreeConfig::default());
        
        for iterator in iterators {
            tree.add_way(iterator)?;
        }
        
        tree.initialize()?;

        let mut result = Vec::new();

        while !tree.is_empty() {
            if let Some(value) = tree.pop()? {
                if predicate(&value) {
                    result.push(value);
                }
                self.stats.elements_examined += 1;
            }
        }

        // Use nanosecond precision and convert to microseconds, ensuring minimum of 1
        // This handles sub-microsecond operations that would otherwise report as 0
        let elapsed_ns = start.elapsed().as_nanos() as u64;
        self.stats.processing_time_us = std::cmp::max(1, (elapsed_ns + 500) / 1000);
        self.stats.output_elements = result.len();

        Ok(result)
    }

    /// Get performance statistics from the last operation
    pub fn stats(&self) -> &SetOperationStats {
        &self.stats
    }

    /// Reset statistics
    pub fn reset_stats(&mut self) {
        self.stats = SetOperationStats {
            ways_processed: 0,
            elements_examined: 0,
            output_elements: 0,
            used_bit_mask: false,
            processing_time_us: 0,
        };
    }
}

impl Default for SetOperations {
    fn default() -> Self {
        Self::new()
    }
}

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

    #[test]
    fn test_intersection_bit_mask() {
        let mut ops = SetOperations::new();
        
        let sequences = vec![
            vec![1, 3, 5, 7, 9].into_iter(),
            vec![1, 2, 3, 8, 9].into_iter(),
            vec![1, 3, 4, 6, 9].into_iter(),
        ];

        let result = ops.intersection(sequences).unwrap();
        assert_eq!(result, vec![1, 3, 9]);
        
        let stats = ops.stats();
        assert_eq!(stats.ways_processed, 3);
        assert!(stats.used_bit_mask);
    }

    #[test]
    fn test_intersection_empty() {
        let mut ops = SetOperations::new();
        
        let sequences = vec![
            vec![1, 3, 5].into_iter(),
            vec![2, 4, 6].into_iter(),
        ];

        let result = ops.intersection(sequences).unwrap();
        assert!(result.is_empty());
    }

    #[test]
    fn test_union_operation() {
        let mut ops = SetOperations::new();
        
        let sequences = vec![
            vec![1, 3, 5].into_iter(),
            vec![2, 4, 6].into_iter(),
            vec![1, 2, 7].into_iter(),
        ];

        let result = ops.union(sequences).unwrap();
        assert_eq!(result, vec![1, 2, 3, 4, 5, 6, 7]);
    }

    #[test]
    fn test_frequency_counting() {
        let mut ops = SetOperations::new();
        
        let sequences = vec![
            vec![1, 2, 3].into_iter(),
            vec![1, 2, 4].into_iter(),
            vec![1, 3, 5].into_iter(),
        ];

        let frequencies = ops.count_frequencies(sequences).unwrap();
        
        assert_eq!(frequencies[&1], 3);
        assert_eq!(frequencies[&2], 2);
        assert_eq!(frequencies[&3], 2);
        assert_eq!(frequencies[&4], 1);
        assert_eq!(frequencies[&5], 1);
    }

    #[test]
    fn test_filter_merge() {
        let mut ops = SetOperations::new();
        
        let sequences = vec![
            vec![1, 2, 3, 4, 5].into_iter(),
            vec![2, 4, 6, 8].into_iter(),
        ];

        // Filter only even numbers
        let result = ops.filter_merge(sequences, |x| x % 2 == 0).unwrap();
        assert_eq!(result, vec![2, 2, 4, 4, 6, 8]);
    }

    #[test]
    fn test_large_intersection_general_algorithm() {
        let mut config = SetOperationsConfig::default();
        config.bit_mask_threshold = 2; // Force general algorithm
        
        let mut ops = SetOperations::with_config(config);
        
        let sequences = vec![
            vec![1, 3, 5, 7, 9].into_iter(),
            vec![1, 2, 3, 8, 9].into_iter(),
            vec![1, 3, 4, 6, 9].into_iter(),
        ];

        let result = ops.intersection(sequences).unwrap();
        assert_eq!(result, vec![1, 3, 9]);
        
        let stats = ops.stats();
        assert!(!stats.used_bit_mask);
    }

    #[test]
    fn test_stats_reset() {
        let mut ops = SetOperations::new();
        
        let sequences = vec![
            vec![1, 2, 3].into_iter(),
            vec![1, 2, 4].into_iter(),
        ];

        ops.intersection(sequences).unwrap();
        assert!(ops.stats().elements_examined > 0);
        
        ops.reset_stats();
        assert_eq!(ops.stats().elements_examined, 0);
    }

    #[test]
    fn test_performance_stats() {
        let mut ops = SetOperations::new();
        
        let sequences = vec![
            vec![1, 2, 3, 4, 5].into_iter(),
            vec![1, 3, 5, 7, 9].into_iter(),
        ];

        let result = ops.intersection(sequences).unwrap();
        
        let stats = ops.stats();
        assert!(stats.processing_time_us > 0);
        assert_eq!(stats.output_elements, result.len());
        assert_eq!(stats.ways_processed, 2);
    }
}