zipora 3.1.7

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
use super::config::RadixSortConfig;
use super::advanced::RadixSortable;
use crate::algorithms::{Algorithm, AlgorithmStats};
use crate::error::{Result, ZiporaError};
use rayon::prelude::*;

// AVX2/BMI2 intrinsics for advanced SIMD acceleration
#[cfg(target_arch = "x86_64")]
use std::arch::x86_64::{
    __m256i, _mm256_and_si256, _mm256_loadu_si256, _mm256_set1_epi32, _mm256_srlv_epi32,
    _mm256_storeu_si256,
};

// AVX-512 intrinsics (avx512 feature)
#[cfg(all(target_arch = "x86_64", feature = "avx512"))]
use std::arch::x86_64::{
    __m512i, _mm512_and_si512, _mm512_loadu_si512, _mm512_set1_epi32, _mm512_srlv_epi32,
    _mm512_storeu_si512,
};

/// High-performance radix sort implementation
pub struct RadixSort {
    config: RadixSortConfig,
    stats: AlgorithmStats,
}

impl RadixSort {
    /// Create a new radix sort instance
    pub fn new() -> Self {
        Self::with_config(RadixSortConfig::default())
    }

    /// Create a radix sort instance with custom configuration
    pub fn with_config(config: RadixSortConfig) -> Self {
        Self {
            config,
            stats: AlgorithmStats {
                items_processed: 0,
                processing_time_us: 0,
                memory_used: 0,
                used_parallel: false,
                used_simd: false,
            },
        }
    }

    /// Sort a slice of unsigned 32-bit integers
    pub fn sort_u32(&mut self, data: &mut [u32]) -> Result<()> {
        let start_time = std::time::Instant::now();

        if data.is_empty() {
            return Ok(());
        }

        let used_parallel =
            data.len() >= self.config.parallel_threshold && self.config.use_parallel;

        if used_parallel {
            self.sort_u32_parallel(data)?;
        } else {
            self.sort_u32_sequential(data)?;
        }

        let elapsed = start_time.elapsed();
        self.stats = AlgorithmStats {
            items_processed: data.len(),
            processing_time_us: elapsed.as_micros() as u64,
            memory_used: self.estimate_memory_u32(data.len()),
            used_parallel,
            used_simd: self.config.use_simd,
        };

        Ok(())
    }

    /// Sort a slice of unsigned 64-bit integers
    pub fn sort_u64(&mut self, data: &mut [u64]) -> Result<()> {
        let start_time = std::time::Instant::now();

        if data.is_empty() {
            return Ok(());
        }

        let used_parallel =
            data.len() >= self.config.parallel_threshold && self.config.use_parallel;

        if used_parallel {
            self.sort_u64_parallel(data)?;
        } else {
            self.sort_u64_sequential(data)?;
        }

        let elapsed = start_time.elapsed();
        self.stats = AlgorithmStats {
            items_processed: data.len(),
            processing_time_us: elapsed.as_micros() as u64,
            memory_used: self.estimate_memory_u64(data.len()),
            used_parallel,
            used_simd: self.config.use_simd,
        };

        Ok(())
    }

    /// Get algorithm statistics
    pub fn stats(&self) -> &AlgorithmStats {
        &self.stats
    }

    /// Sort a slice of byte arrays by their content
    pub fn sort_bytes(&mut self, data: &mut Vec<Vec<u8>>) -> Result<()> {
        let start_time = std::time::Instant::now();

        if data.is_empty() {
            return Ok(());
        }

        self.sort_bytes_msd(data.as_mut_slice(), 0)?;

        let elapsed = start_time.elapsed();
        let total_bytes: usize = data.iter().map(|v| v.len()).sum();

        self.stats = AlgorithmStats {
            items_processed: data.len(),
            processing_time_us: elapsed.as_micros() as u64,
            memory_used: total_bytes + data.len() * std::mem::size_of::<Vec<u8>>(),
            used_parallel: false, // MSD radix sort is inherently sequential for strings
            used_simd: false,
        };

        Ok(())
    }

    fn sort_u32_sequential(&self, data: &mut [u32]) -> Result<()> {
        if data.len() <= self.config.use_counting_sort_threshold {
            self.counting_sort_u32(data);
            return Ok(());
        }

        let radix = 1usize << self.config.radix_bits;
        let mask = (radix - 1) as u32;
        let mut buffer = vec![0u32; data.len()];
        let mut counts = vec![0usize; radix];

        let max_passes = 32_usize.div_ceil(self.config.radix_bits);

        for pass in 0..max_passes {
            let shift = pass * self.config.radix_bits;

            // Count occurrences with AVX-512 acceleration when available
            counts.fill(0);

            #[cfg(all(target_arch = "x86_64", feature = "avx512"))]
            {
                if self.config.use_simd && data.len() >= 16 && shift < 24 {
                    // Use AVX-512 for faster counting when available
                    if std::arch::is_x86_feature_detected!("avx512f")
                        && std::arch::is_x86_feature_detected!("avx512bw")
                    {
                        // SAFETY: avx512f and avx512bw features detected at runtime
                        unsafe {
                            self.count_digits_avx512(data, shift, mask, &mut counts);
                        }
                    } else {
                        // Fallback to sequential counting
                        for &value in data.iter() {
                            let digit = ((value >> shift) & mask) as usize;
                            counts[digit] += 1;
                        }
                    }
                } else {
                    // Sequential counting for small data or high shifts
                    for &value in data.iter() {
                        let digit = ((value >> shift) & mask) as usize;
                        counts[digit] += 1;
                    }
                }
            }

            #[cfg(not(all(target_arch = "x86_64", feature = "avx512")))]
            {
                // Standard sequential counting
                for &value in data.iter() {
                    let digit = ((value >> shift) & mask) as usize;
                    counts[digit] += 1;
                }
            }

            // Convert counts to positions
            let mut pos = 0;
            for count in counts.iter_mut() {
                let old_count = *count;
                *count = pos;
                pos += old_count;
            }

            // Distribute elements
            for &value in data.iter() {
                let digit = ((value >> shift) & mask) as usize;
                buffer[counts[digit]] = value;
                counts[digit] += 1;
            }

            // Copy back
            data.copy_from_slice(&buffer);
        }

        Ok(())
    }

    fn sort_u32_parallel(&self, data: &mut [u32]) -> Result<()> {
        // For very large datasets, use parallel radix sort
        // This is a simplified version - full parallel radix sort is quite complex

        if data.len() < 2 * self.config.parallel_threshold {
            return self.sort_u32_sequential(data);
        }

        // Split into chunks and sort in parallel
        let num_threads = rayon::current_num_threads();
        let chunk_size = data.len().div_ceil(num_threads); // Round up

        data.par_chunks_mut(chunk_size).for_each(|chunk| {
            let temp_sorter = RadixSort::with_config(RadixSortConfig {
                use_parallel: false,
                ..self.config.clone()
            });
            let _ = temp_sorter.sort_u32_sequential(chunk);
        });

        // Use proper multi-way merge instead of sort_unstable()
        self.multiway_merge_u32_chunks(data, chunk_size)?;

        Ok(())
    }

    fn sort_u64_sequential(&self, data: &mut [u64]) -> Result<()> {
        let radix = 1usize << self.config.radix_bits;
        let mask = (radix - 1) as u64;
        let mut buffer = vec![0u64; data.len()];
        let mut counts = vec![0usize; radix];

        let max_passes = 64_usize.div_ceil(self.config.radix_bits);

        for pass in 0..max_passes {
            let shift = pass * self.config.radix_bits;

            // Count occurrences
            counts.fill(0);
            for &value in data.iter() {
                let digit = ((value >> shift) & mask) as usize;
                counts[digit] += 1;
            }

            // Convert counts to positions
            let mut pos = 0;
            for count in counts.iter_mut() {
                let old_count = *count;
                *count = pos;
                pos += old_count;
            }

            // Distribute elements
            for &value in data.iter() {
                let digit = ((value >> shift) & mask) as usize;
                buffer[counts[digit]] = value;
                counts[digit] += 1;
            }

            // Copy back
            data.copy_from_slice(&buffer);
        }

        Ok(())
    }

    fn sort_u64_parallel(&self, data: &mut [u64]) -> Result<()> {
        // Similar to u32 parallel sort
        if data.len() < 2 * self.config.parallel_threshold {
            return self.sort_u64_sequential(data);
        }

        let num_threads = rayon::current_num_threads();
        let chunk_size = data.len().div_ceil(num_threads); // Round up

        data.par_chunks_mut(chunk_size).for_each(|chunk| {
            let temp_sorter = RadixSort::with_config(RadixSortConfig {
                use_parallel: false,
                ..self.config.clone()
            });
            let _ = temp_sorter.sort_u64_sequential(chunk);
        });

        // Use proper multi-way merge instead of sort_unstable()
        self.multiway_merge_u64_chunks(data, chunk_size)?;

        Ok(())
    }

    fn counting_sort_u32(&self, data: &mut [u32]) {
        if data.is_empty() {
            return;
        }

        // SAFETY: is_empty() check above guarantees iterator has at least one element
        let max_val = *data.iter().max().expect("non-empty input") as usize;
        let mut counts = vec![0usize; max_val + 1];

        // Count occurrences
        for &value in data.iter() {
            counts[value as usize] += 1;
        }

        // Reconstruct sorted array
        let mut index = 0;
        for (value, &count) in counts.iter().enumerate() {
            for _ in 0..count {
                data[index] = value as u32;
                index += 1;
            }
        }
    }

    fn sort_bytes_msd(&self, data: &mut [Vec<u8>], depth: usize) -> Result<()> {
        if data.len() <= 1 {
            return Ok(());
        }

        let mut counts = [0usize; 257];

        for item in data.iter() {
            let b = if depth < item.len() {
                item[depth] as usize + 1
            } else {
                0
            };
            counts[b] += 1;
        }

        let mut offsets = [0usize; 257];
        let mut current_pos = 0;
        for i in 0..257 {
            offsets[i] = current_pos;
            current_pos += counts[i];
        }

        let mut next_free = offsets;

        for b in 0..257 {
            let end = if b == 256 { data.len() } else { offsets[b + 1] };
            while next_free[b] < end {
                let pos = next_free[b];
                let item_b = if depth < data[pos].len() {
                    data[pos][depth] as usize + 1
                } else {
                    0
                };

                if item_b == b {
                    next_free[b] += 1;
                } else {
                    data.swap(pos, next_free[item_b]);
                    next_free[item_b] += 1;
                }
            }
        }

        // Recursively sort each bucket (skip bucket 0 as it contains strings that have ended)
        for b in 1..257 {
            let start = offsets[b];
            let end = if b == 256 { data.len() } else { offsets[b + 1] };
            if end - start > 1 {
                self.sort_bytes_msd(&mut data[start..end], depth + 1)?;
            }
        }

        Ok(())
    }

    fn estimate_memory_u32(&self, len: usize) -> usize {
        let radix = 1usize << self.config.radix_bits;
        len * std::mem::size_of::<u32>() + // buffer
        radix * std::mem::size_of::<usize>() // counts
    }

    fn estimate_memory_u64(&self, len: usize) -> usize {
        let radix = 1usize << self.config.radix_bits;
        len * std::mem::size_of::<u64>() + // buffer
        radix * std::mem::size_of::<usize>() // counts
    }

    /// Multi-way merge for u32 chunks
    fn multiway_merge_u32_chunks(&self, data: &mut [u32], chunk_size: usize) -> Result<()> {
        use crate::algorithms::multiway_merge::{MultiWayMerge, SliceSource};

        // If no merging needed
        if data.is_empty() || chunk_size >= data.len() {
            return Ok(());
        }

        let mut sources = Vec::new();
        for chunk in data.chunks(chunk_size) {
            sources.push(SliceSource::new(chunk));
        }

        let mut merger = MultiWayMerge::new();
        let merged = merger.merge(sources)?;

        data.copy_from_slice(&merged);

        Ok(())
    }

    /// Multi-way merge for u64 chunks
    fn multiway_merge_u64_chunks(&self, data: &mut [u64], chunk_size: usize) -> Result<()> {
        use crate::algorithms::multiway_merge::{MultiWayMerge, SliceSource};

        if data.is_empty() || chunk_size >= data.len() {
            return Ok(());
        }

        let mut sources = Vec::new();
        for chunk in data.chunks(chunk_size) {
            sources.push(SliceSource::new(chunk));
        }

        let mut merger = MultiWayMerge::new();
        let merged = merger.merge(sources)?;

        data.copy_from_slice(&merged);

        Ok(())
    }

    /// AVX-512 accelerated digit counting for radix sort
    ///
    /// Processes multiple integers simultaneously to count digit occurrences,
    /// providing significant speedup for the counting phase of radix sort.
    #[cfg(all(target_arch = "x86_64", feature = "avx512"))]
    #[target_feature(enable = "avx512f")]
    unsafe fn count_digits_avx512(
        &self,
        data: &[u32],
        shift: usize,
        mask: u32,
        counts: &mut [usize],
    ) {
        let mut i = 0;
        let shift_vec = _mm512_set1_epi32(shift as i32);

        // Process 16 u32 values at a time using AVX-512
        while i + 16 <= data.len() {
            // Load 16 x u32 values (512 bits)
            // SAFETY: #[target_feature] ensures avx512f, i+16 <= data.len() ensures 64 bytes available
            let values = unsafe { _mm512_loadu_si512(data[i..].as_ptr() as *const __m512i) };

            // Shift all values to extract the desired digit
            let shifted = if shift > 0 {
                _mm512_srlv_epi32(values, shift_vec) // Variable shift per element
            } else {
                values
            };

            // Apply mask to get only the desired bits
            let mask_vec = _mm512_set1_epi32(mask as i32);
            let digits = _mm512_and_si512(shifted, mask_vec);

            // Extract digits and count them
            // Note: This could be further optimized with gather operations
            // For now, extract to array and count sequentially
            let mut digit_array = [0u32; 16];
            // SAFETY: digit_array is 16 u32s = 64 bytes, matches __m512i size
            unsafe { _mm512_storeu_si512(digit_array.as_mut_ptr() as *mut __m512i, digits) };

            for digit in digit_array.iter() {
                counts[*digit as usize] += 1;
            }

            i += 16;
        }

        // Handle remaining elements sequentially
        for &value in &data[i..] {
            let digit = ((value >> shift) & mask) as usize;
            counts[digit] += 1;
        }
    }
}

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

impl Algorithm for RadixSort {
    type Config = RadixSortConfig;
    type Input = Vec<u32>;
    type Output = Vec<u32>;

    fn execute(&self, config: &Self::Config, mut input: Self::Input) -> Result<Self::Output> {
        let mut sorter = Self::with_config(config.clone());
        sorter.sort_u32(&mut input)?;
        Ok(input)
    }

    fn stats(&self) -> AlgorithmStats {
        self.stats.clone()
    }

    fn estimate_memory(&self, input_size: usize) -> usize {
        self.estimate_memory_u32(input_size)
    }

    fn supports_parallel(&self) -> bool {
        true
    }

    fn supports_simd(&self) -> bool {
        cfg!(feature = "simd")
    }
}

/// Specialized radix sort for key-value pairs
pub struct KeyValueRadixSort<K, V> {
    config: RadixSortConfig,
    _phantom: std::marker::PhantomData<(K, V)>,
}

#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub(super) struct KVPair {
    key: u64,
    index: usize,
}

impl RadixSortable for KVPair {
    fn extract_key(&self) -> u64 {
        self.key
    }

    fn get_byte(&self, position: usize) -> Option<u8> {
        if position < 8 {
            Some((self.key >> ((7 - position) * 8)) as u8)
        } else {
            None
        }
    }

    fn max_bytes(&self) -> usize {
        8
    }
}

impl<K, V> KeyValueRadixSort<K, V>
where
    K: Copy + Into<u64>,
    V: Clone,
{
    /// Create a new key-value radix sort
    pub fn new() -> Self {
        Self {
            config: RadixSortConfig::default(),
            _phantom: std::marker::PhantomData,
        }
    }

    /// Sort key-value pairs by key
    pub fn sort_by_key(&self, data: &mut [(K, V)]) -> Result<()> {
        if data.is_empty() {
            return Ok(());
        }

        let mut indices: Vec<KVPair> = data
            .iter()
            .enumerate()
            .map(|(i, (k, _))| KVPair {
                key: (*k).into(),
                index: i,
            })
            .collect();

        let mut config = super::config::AdvancedRadixSortConfig::default();
        config.use_parallel = self.config.use_parallel;
        config.parallel_threshold = self.config.parallel_threshold;
        config.radix_bits = self.config.radix_bits;

        let mut sorter = super::advanced::AdvancedRadixSort::<KVPair>::with_config(config)
            .unwrap_or_else(|_| super::advanced::AdvancedRadixSort::new().unwrap());

        sorter.sort(&mut indices)?;

        let mut targets = vec![0; data.len()];
        for (i, ki) in indices.iter().enumerate() {
            targets[ki.index] = i;
        }

        for i in 0..data.len() {
            while targets[i] != i {
                let alt = targets[i];
                data.swap(i, alt);
                targets.swap(i, alt);
            }
        }

        Ok(())
    }
}

impl<K, V> Default for KeyValueRadixSort<K, V>
where
    K: Copy + Into<u64>,
    V: Clone,
{
    fn default() -> Self {
        Self::new()
    }
}