zipora 3.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
//! SIMD-Accelerated Sorted Set Intersection for u32 Posting Lists
//!
//! Three strategies, adaptively selected:
//! - **SIMD 4×4 block comparison** (SSE4.1): 16 comparisons per cycle for balanced lists
//! - **Scalar galloping** (exponential + binary search): O(n log m) for skewed lists
//! - **Adaptive**: picks the best strategy based on |A|/|B| ratio
//!
//! # Examples
//!
//! ```rust
//! use zipora::algorithms::simd_set_intersect::*;
//!
//! let a = vec![1, 3, 5, 7, 9, 11, 13, 15];
//! let b = vec![2, 3, 6, 7, 10, 11, 14, 15];
//!
//! let mut out = Vec::new();
//! sorted_intersect_adaptive(&a, &b, &mut out);
//! assert_eq!(out, vec![3, 7, 11, 15]);
//!
//! assert_eq!(sorted_intersect_count(&a, &b), 4);
//! ```

/// Galloping threshold: when |A|/|B| > this ratio, use galloping instead of merge.
const GALLOP_RATIO: usize = 32;

/// Minimum size to use SIMD (below this, scalar merge is faster due to setup cost).
const SIMD_MIN_SIZE: usize = 8;

// ============================================================================
// Public API
// ============================================================================

/// Intersect two sorted u32 slices. Adaptive: picks SIMD or galloping based on sizes.
///
/// Output is written to `out` (cleared first). Returns the intersection count.
#[inline]
pub fn sorted_intersect_adaptive(a: &[u32], b: &[u32], out: &mut Vec<u32>) -> usize {
    out.clear();
    if a.is_empty() || b.is_empty() { return 0; }

    // Ensure a is the smaller list
    let (small, large) = if a.len() <= b.len() { (a, b) } else { (b, a) };

    if large.len() / small.len().max(1) > GALLOP_RATIO {
        intersect_galloping(small, large, out)
    } else if small.len() >= SIMD_MIN_SIZE {
        #[cfg(target_arch = "x86_64")]
        {
            if std::arch::is_x86_feature_detected!("sse4.1") {
                return unsafe { intersect_simd_sse41(small, large, out) };
            }
        }
        intersect_scalar_merge(small, large, out)
    } else {
        intersect_scalar_merge(small, large, out)
    }
}

/// Count-only intersection (no materialization). Faster when you only need the count.
#[inline]
pub fn sorted_intersect_count(a: &[u32], b: &[u32]) -> usize {
    if a.is_empty() || b.is_empty() { return 0; }

    let (small, large) = if a.len() <= b.len() { (a, b) } else { (b, a) };

    if large.len() / small.len().max(1) > GALLOP_RATIO {
        intersect_galloping_count(small, large)
    } else {
        #[cfg(target_arch = "x86_64")]
        {
            if std::arch::is_x86_feature_detected!("sse4.1") {
                return unsafe { intersect_simd_count_sse41(small, large) };
            }
        }
        intersect_scalar_merge_count(small, large)
    }
}

/// SIMD intersection (always uses SIMD if available, fallback to scalar).
#[inline]
pub fn sorted_intersect_simd(a: &[u32], b: &[u32], out: &mut Vec<u32>) -> usize {
    out.clear();
    if a.is_empty() || b.is_empty() { return 0; }

    let (small, large) = if a.len() <= b.len() { (a, b) } else { (b, a) };

    #[cfg(target_arch = "x86_64")]
    {
        if std::arch::is_x86_feature_detected!("sse4.1") {
            return unsafe { intersect_simd_sse41(small, large, out) };
        }
    }
    intersect_scalar_merge(small, large, out)
}

/// Scalar galloping intersection (best for skewed sizes).
#[inline]
pub fn sorted_intersect_galloping(a: &[u32], b: &[u32], out: &mut Vec<u32>) -> usize {
    out.clear();
    if a.is_empty() || b.is_empty() { return 0; }
    let (small, large) = if a.len() <= b.len() { (a, b) } else { (b, a) };
    intersect_galloping(small, large, out)
}

// ============================================================================
// Scalar implementations
// ============================================================================

/// Linear merge intersection — O(n + m), best for equal-sized lists.
fn intersect_scalar_merge(a: &[u32], b: &[u32], out: &mut Vec<u32>) -> usize {
    let mut i = 0usize;
    let mut j = 0usize;
    let mut count = 0;

    while i < a.len() && j < b.len() {
        let va = unsafe { *a.get_unchecked(i) };
        let vb = unsafe { *b.get_unchecked(j) };

        if va == vb {
            out.push(va);
            count += 1;
            i += 1;
            j += 1;
        } else if va < vb {
            i += 1;
        } else {
            j += 1;
        }
    }

    count
}

/// Scalar merge count-only.
fn intersect_scalar_merge_count(a: &[u32], b: &[u32]) -> usize {
    let mut i = 0usize;
    let mut j = 0usize;
    let mut count = 0;

    while i < a.len() && j < b.len() {
        let va = unsafe { *a.get_unchecked(i) };
        let vb = unsafe { *b.get_unchecked(j) };

        if va == vb {
            count += 1;
            i += 1;
            j += 1;
        } else if va < vb {
            i += 1;
        } else {
            j += 1;
        }
    }

    count
}

/// Galloping intersection — O(n log m), best when |small| << |large|.
fn intersect_galloping(small: &[u32], large: &[u32], out: &mut Vec<u32>) -> usize {
    let mut count = 0;
    let mut lo = 0usize;

    for &val in small {
        // Gallop: exponential search to find upper bound
        let mut step = 1usize;
        let mut hi = lo;

        while hi < large.len() && large[hi] < val {
            lo = hi;
            step <<= 1;
            hi = lo + step;
        }
        hi = hi.min(large.len());

        // Binary search in [lo, hi)
        let pos = large[lo..hi].partition_point(|&x| x < val);
        lo += pos;

        if lo < large.len() && large[lo] == val {
            out.push(val);
            count += 1;
            lo += 1; // Move past the match
        }
    }

    count
}

/// Galloping count-only.
fn intersect_galloping_count(small: &[u32], large: &[u32]) -> usize {
    let mut count = 0;
    let mut lo = 0usize;

    for &val in small {
        let mut step = 1usize;
        let mut hi = lo;

        while hi < large.len() && large[hi] < val {
            lo = hi;
            step <<= 1;
            hi = lo + step;
        }
        hi = hi.min(large.len());

        let pos = large[lo..hi].partition_point(|&x| x < val);
        lo += pos;

        if lo < large.len() && large[lo] == val {
            count += 1;
            lo += 1;
        }
    }

    count
}

// ============================================================================
// SSE4.1 SIMD implementation — 4×4 block comparison (Schlegel et al.)
// ============================================================================

#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "sse4.1")]
unsafe fn intersect_simd_sse41(a: &[u32], b: &[u32], out: &mut Vec<u32>) -> usize {
    use std::arch::x86_64::*;

    let mut count = 0;
    let mut i = 0usize;
    let mut j = 0usize;

    let a_len = a.len();
    let b_len = b.len();

    out.reserve(a_len.min(b_len));

    // SIMD 4×4 block comparison (Schlegel et al.)
    while i + 4 <= a_len && j + 4 <= b_len {
        // SAFETY: i+4 <= a_len and j+4 <= b_len checked above, SSE4.1 guaranteed by target_feature
        unsafe {
            let va = _mm_loadu_si128(a.as_ptr().add(i) as *const __m128i);
            let vb = _mm_loadu_si128(b.as_ptr().add(j) as *const __m128i);

            // 4 rotated comparisons = 16 pairs checked
            let cmp0 = _mm_cmpeq_epi32(va, vb);
            let cmp1 = _mm_cmpeq_epi32(va, _mm_shuffle_epi32(vb, 0x39));
            let cmp2 = _mm_cmpeq_epi32(va, _mm_shuffle_epi32(vb, 0x4E));
            let cmp3 = _mm_cmpeq_epi32(va, _mm_shuffle_epi32(vb, 0x93));

            let or01 = _mm_or_si128(cmp0, cmp1);
            let or23 = _mm_or_si128(cmp2, cmp3);
            let matches = _mm_or_si128(or01, or23);

            let mask = _mm_movemask_ps(_mm_castsi128_ps(matches)) as u32;

            if mask != 0 {
                for k in 0..4u32 {
                    if (mask >> k) & 1 != 0 {
                        out.push(*a.get_unchecked(i + k as usize));
                        count += 1;
                    }
                }
            }

            let a_max = *a.get_unchecked(i + 3);
            let b_max = *b.get_unchecked(j + 3);

            if a_max <= b_max { i += 4; }
            if b_max <= a_max { j += 4; }
        }
    }

    // Scalar tail
    while i < a_len && j < b_len {
        unsafe {
            let va = *a.get_unchecked(i);
            let vb = *b.get_unchecked(j);

            if va == vb { out.push(va); count += 1; i += 1; j += 1; }
            else if va < vb { i += 1; }
            else { j += 1; }
        }
    }

    count
}

/// SIMD count-only intersection (no output materialization).
#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "sse4.1")]
unsafe fn intersect_simd_count_sse41(a: &[u32], b: &[u32]) -> usize {
    use std::arch::x86_64::*;

    let mut count = 0;
    let mut i = 0usize;
    let mut j = 0usize;

    let a_len = a.len();
    let b_len = b.len();

    while i + 4 <= a_len && j + 4 <= b_len {
        unsafe {
            let va = _mm_loadu_si128(a.as_ptr().add(i) as *const __m128i);
            let vb = _mm_loadu_si128(b.as_ptr().add(j) as *const __m128i);

            let cmp0 = _mm_cmpeq_epi32(va, vb);
            let cmp1 = _mm_cmpeq_epi32(va, _mm_shuffle_epi32(vb, 0x39));
            let cmp2 = _mm_cmpeq_epi32(va, _mm_shuffle_epi32(vb, 0x4E));
            let cmp3 = _mm_cmpeq_epi32(va, _mm_shuffle_epi32(vb, 0x93));

            let or01 = _mm_or_si128(cmp0, cmp1);
            let or23 = _mm_or_si128(cmp2, cmp3);
            let matches = _mm_or_si128(or01, or23);

            let mask = _mm_movemask_ps(_mm_castsi128_ps(matches)) as u32;
            count += mask.count_ones() as usize;

            let a_max = *a.get_unchecked(i + 3);
            let b_max = *b.get_unchecked(j + 3);

            if a_max <= b_max { i += 4; }
            if b_max <= a_max { j += 4; }
        }
    }

    while i < a_len && j < b_len {
        unsafe {
            let va = *a.get_unchecked(i);
            let vb = *b.get_unchecked(j);

            if va == vb { count += 1; i += 1; j += 1; }
            else if va < vb { i += 1; }
            else { j += 1; }
        }
    }

    count
}

// ============================================================================
// Tests
// ============================================================================

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

    #[test]
    fn test_empty_inputs() {
        let mut out = Vec::new();
        assert_eq!(sorted_intersect_adaptive(&[], &[1, 2, 3], &mut out), 0);
        assert_eq!(out.len(), 0);
        assert_eq!(sorted_intersect_adaptive(&[1, 2, 3], &[], &mut out), 0);
        assert_eq!(sorted_intersect_adaptive(&[], &[], &mut out), 0);
    }

    #[test]
    fn test_no_overlap() {
        let a = vec![1, 3, 5, 7];
        let b = vec![2, 4, 6, 8];
        let mut out = Vec::new();
        assert_eq!(sorted_intersect_adaptive(&a, &b, &mut out), 0);
        assert_eq!(out.len(), 0);
    }

    #[test]
    fn test_full_overlap() {
        let a = vec![1, 2, 3, 4, 5];
        let b = vec![1, 2, 3, 4, 5];
        let mut out = Vec::new();
        assert_eq!(sorted_intersect_adaptive(&a, &b, &mut out), 5);
        assert_eq!(out, vec![1, 2, 3, 4, 5]);
    }

    #[test]
    fn test_partial_overlap() {
        let a = vec![1, 3, 5, 7, 9, 11, 13, 15];
        let b = vec![2, 3, 6, 7, 10, 11, 14, 15];
        let mut out = Vec::new();
        assert_eq!(sorted_intersect_adaptive(&a, &b, &mut out), 4);
        assert_eq!(out, vec![3, 7, 11, 15]);
    }

    #[test]
    fn test_single_element() {
        let mut out = Vec::new();
        assert_eq!(sorted_intersect_adaptive(&[5], &[5], &mut out), 1);
        assert_eq!(out, vec![5]);

        assert_eq!(sorted_intersect_adaptive(&[5], &[6], &mut out), 0);
        assert_eq!(out.len(), 0);
    }

    #[test]
    fn test_count_only() {
        let a: Vec<u32> = (0..1000).step_by(2).collect();
        let b: Vec<u32> = (0..1000).step_by(3).collect();

        let mut out = Vec::new();
        let materialized = sorted_intersect_adaptive(&a, &b, &mut out);
        let counted = sorted_intersect_count(&a, &b);

        assert_eq!(materialized, counted);
        assert_eq!(counted, out.len());
    }

    #[test]
    fn test_skewed_sizes_galloping() {
        let small = vec![50, 500, 5000, 50000];
        let large: Vec<u32> = (0..100000).collect();

        let mut out = Vec::new();
        let count = sorted_intersect_adaptive(&small, &large, &mut out);
        assert_eq!(count, 4);
        assert_eq!(out, vec![50, 500, 5000, 50000]);
    }

    #[test]
    fn test_simd_vs_scalar_consistency() {
        // Generate two sorted lists with known intersection
        let a: Vec<u32> = (0..500).map(|i| i * 3).collect();
        let b: Vec<u32> = (0..500).map(|i| i * 5).collect();

        let mut simd_out = Vec::new();
        let mut scalar_out = Vec::new();

        let simd_count = sorted_intersect_simd(&a, &b, &mut simd_out);
        let scalar_count = intersect_scalar_merge(&a, &b, &mut scalar_out);

        assert_eq!(simd_count, scalar_count);
        assert_eq!(simd_out, scalar_out);
    }

    #[test]
    fn test_large_balanced() {
        let a: Vec<u32> = (0..10000).step_by(2).collect(); // 5000 evens
        let b: Vec<u32> = (0..10000).step_by(3).collect(); // 3334 multiples of 3

        let mut out = Vec::new();
        let count = sorted_intersect_adaptive(&a, &b, &mut out);

        // Intersection = multiples of 6 in [0, 10000)
        let expected: Vec<u32> = (0..10000).step_by(6).collect();
        assert_eq!(count, expected.len());
        assert_eq!(out, expected);
    }

    #[test]
    fn test_adjacent_no_match() {
        let a = vec![1, 3, 5, 7, 9, 11, 13, 15, 17, 19];
        let b = vec![2, 4, 6, 8, 10, 12, 14, 16, 18, 20];
        let mut out = Vec::new();
        assert_eq!(sorted_intersect_adaptive(&a, &b, &mut out), 0);
    }

    #[test]
    fn test_subset() {
        let a = vec![2, 4, 6, 8, 10];
        let b: Vec<u32> = (1..=20).collect();
        let mut out = Vec::new();
        assert_eq!(sorted_intersect_adaptive(&a, &b, &mut out), 5);
        assert_eq!(out, vec![2, 4, 6, 8, 10]);
    }

    #[test]
    fn test_duplicates_in_input() {
        // Posting lists shouldn't have duplicates, but be robust
        let a = vec![1, 2, 2, 3, 4];
        let b = vec![2, 2, 3, 5];
        let mut out = Vec::new();
        let count = sorted_intersect_adaptive(&a, &b, &mut out);
        // Should match elements present in both (with duplicates preserved)
        assert!(count >= 2); // At least 2 and 3
    }

    #[test]
    fn test_max_u32() {
        let a = vec![0, u32::MAX / 2, u32::MAX];
        let b = vec![0, u32::MAX / 2, u32::MAX];
        let mut out = Vec::new();
        assert_eq!(sorted_intersect_adaptive(&a, &b, &mut out), 3);
    }

    /// Performance test — only meaningful in release mode.
    #[test]
    fn test_performance_10k_balanced() {
        let a: Vec<u32> = (0..10000).step_by(2).collect();
        let b: Vec<u32> = (0..10000).step_by(3).collect();
        let mut out = Vec::new();

        let start = std::time::Instant::now();
        for _ in 0..1000 {
            sorted_intersect_adaptive(&a, &b, &mut out);
        }
        let elapsed = start.elapsed();

        #[cfg(not(debug_assertions))]
        {
            let per_call = elapsed / 1000;
            eprintln!("SIMD intersect 5K×3.3K: {:?}/call, {} matches", per_call, out.len());
            // Should be under 50µs per call in release
            assert!(per_call.as_micros() < 100, "Too slow: {:?}", per_call);
        }
    }

    #[test]
    fn test_performance_skewed() {
        let small: Vec<u32> = (0..100).map(|i| i * 100).collect();
        let large: Vec<u32> = (0..100000).collect();
        let mut out = Vec::new();

        let start = std::time::Instant::now();
        for _ in 0..1000 {
            sorted_intersect_adaptive(&small, &large, &mut out);
        }
        let elapsed = start.elapsed();

        #[cfg(not(debug_assertions))]
        {
            let per_call = elapsed / 1000;
            eprintln!("Galloping intersect 100×100K: {:?}/call, {} matches", per_call, out.len());
            assert!(per_call.as_micros() < 50, "Too slow: {:?}", per_call);
        }
    }
}