bloomcraft 0.1.1

Production-grade Bloom filter library for Rust with concurrent variants and optimal performance
Documentation
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
//! Concurrent partitioned Bloom filter using `AtomicU64` for lock-free operations.
//!
//! # When to Use
//!
//! | Workload | Recommendation |
//! |---|---|
//! | Read-heavy (≥90% queries) | Excellent — reads scale with thread count |
//! | Write-heavy or balanced | Prefer [`ShardedBloomFilter`](crate::sync::ShardedBloomFilter) |
//! | Build-once, query-many | Good — single-threaded build, concurrent queries |
//!
//! # Key Properties
//!
//! - Every insert touches all `k` partitions (one per hash function).
//! - Write throughput is bounded by cache-line contention on these `k` partitions
//!   and does **not** scale with thread count.
//! - Read throughput scales near-linearly (`AtomicU64::load` is read-shared).
//! - All operations use `Ordering::Relaxed` — correct for Bloom filter semantics
//!   where false positives are acceptable and bit-sets are idempotent.
//!
//! # References
//!
//! - Putze, F., Sanders, P., & Singler, J. (2009). "Cache-, Hash- and
//!   Space-Efficient Bloom Filters". *J. Experimental Algorithmics*, 14, 4.
//! - Lemire, D. (2019). "Fast Random Integer Generation in an Interval".
//!   *ACM TOMS*, 45(3).

use crate::core::filter::{BloomFilter, ConcurrentBloomFilter};
use crate::core::params::{optimal_bit_count, optimal_hash_count, validate_params};
use crate::error::{BloomCraftError, Result};
use crate::hash::{BloomHasher, StdHasher};
use std::alloc::{alloc, dealloc, handle_alloc_error, Layout};
use std::hash::Hash;
use std::marker::PhantomData;
use std::ptr::NonNull;
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};

/// Cache line size for modern x86-64 processors (bytes).
const DEFAULT_CACHE_LINE_SIZE: usize = 64;

/// Concurrent partitioned Bloom filter with lock-free insert and query operations.
///
/// The bit array is split into `k` cache-aligned partitions of `AtomicU64`, one per
/// hash function. Each insert sets one bit in every partition; each query checks one
/// bit per partition and returns `false` on the first miss.
///
/// # Performance
///
/// | Metric | Characteristic |
/// |---|---|
/// | Read scaling | Near-linear with thread count (read-shared `AtomicU64`) |
/// | Write scaling | **Does not scale** (bounded by `fetch_or` contention) |
/// | Single-thread insert | Depends on `k` and item size |
/// | Partition count | Higher `k` = lower FPR, proportionally slower inserts |
///
/// # Caveats
///
/// - Write throughput is bounded by `fetch_or` contention on `k` shared cache lines.
///   2 threads can be *slower* than 1 due to cache-line ping-pong.
/// - Alignment (64B vs 4096B) has negligible effect — the bottleneck is not false sharing.
/// - Over-filling beyond design capacity degrades query speed and FPR exponentially.
/// - For write-heavy workloads, prefer [`ShardedBloomFilter`](crate::sync::ShardedBloomFilter).
///
/// # Memory Ordering
///
/// All operations use `Ordering::Relaxed`. This is correct because Bloom filter bits
/// are idempotent (false positives are acceptable, false negatives are impossible),
/// and no inter-thread causality is required.
///
/// # Examples
///
/// ```
/// # #[cfg(feature = "concurrent")]
/// # {
/// use bloomcraft::filters::AtomicPartitionedBloomFilter;
/// use bloomcraft::core::ConcurrentBloomFilter;
/// use std::sync::Arc;
/// use std::thread;
///
/// let filter = Arc::new(
///     AtomicPartitionedBloomFilter::<String>::new(1_000_000, 0.01).unwrap()
/// );
///
/// let handles: Vec<_> = (0..8).map(|tid| {
///     let f = Arc::clone(&filter);
///     thread::spawn(move || {
///         for i in 0..10_000 {
///             f.insert_concurrent(&format!("item_{}_{}", tid, i));
///         }
///     })
/// }).collect();
///
/// for handle in handles {
///     handle.join().unwrap();
/// }
/// # }
/// ```
#[derive(Debug)]
pub struct AtomicPartitionedBloomFilter<T, H = StdHasher>
where
    H: BloomHasher + Clone + Default,
{
    /// Base pointer to cache-aligned allocation of `AtomicU64`.
    data: NonNull<AtomicU64>,
    /// Number of partitions (equals k, number of hash functions).
    k: usize,
    /// Size of each partition in bits.
    partition_size: usize,
    /// Stride between partitions in AtomicU64 words (includes padding).
    partition_stride: usize,
    /// Cache alignment in bytes.
    alignment: usize,
    /// Total allocated size in bytes.
    allocated_bytes: usize,
    /// Hash function instance.
    hasher: H,
    /// Expected number of items.
    expected_items: usize,
    /// Target false positive rate.
    target_fpr: f64,
    /// Actual number of items inserted (atomic).
    item_count: AtomicUsize,
    /// Phantom data for type parameter T.
    _phantom: PhantomData<T>,
}

impl<T, H> AtomicPartitionedBloomFilter<T, H>
where
    T: Hash,
    H: BloomHasher + Clone + Default,
{
    /// Create a new atomic partitioned Bloom filter.
    ///
    /// # Arguments
    ///
    /// * `expected_items` - Expected number of items (n > 0)
    /// * `fpr` - Target false positive rate (0 < fpr < 1)
    ///
    /// # Returns
    ///
    /// * `Ok(AtomicPartitionedBloomFilter)` - Lock-free concurrent filter
    /// * `Err(BloomCraftError)` - If parameters invalid or allocation fails
    ///
    /// # Examples
    ///
    /// ```
    /// # #[cfg(feature = "concurrent")]
    /// # {
    /// use bloomcraft::filters::AtomicPartitionedBloomFilter;
    ///
    /// let filter = AtomicPartitionedBloomFilter::<u64>::new(100_000, 0.01).unwrap();
    /// # Ok::<(), bloomcraft::BloomCraftError>(())
    /// # ;
    /// # }
    /// ```
    pub fn new(expected_items: usize, fpr: f64) -> Result<Self> {
        Self::with_hasher_and_alignment(expected_items, fpr, H::default(), DEFAULT_CACHE_LINE_SIZE)
    }

    /// Create with custom hasher and alignment.
    pub fn with_hasher_and_alignment(
        expected_items: usize,
        fpr: f64,
        hasher: H,
        alignment: usize,
    ) -> Result<Self> {
        // Validate inputs
        if expected_items == 0 {
            return Err(BloomCraftError::invalid_item_count(expected_items));
        }
        if fpr <= 0.0 || fpr >= 1.0 {
            return Err(BloomCraftError::fp_rate_out_of_bounds(fpr));
        }
        if !alignment.is_power_of_two() {
            return Err(BloomCraftError::invalid_parameters(format!(
                "Alignment {} must be power of 2",
                alignment
            )));
        }

        // Calculate optimal parameters
        let m = optimal_bit_count(expected_items, fpr)?;
        let k = optimal_hash_count(m, expected_items)?;
        validate_params(m, expected_items, k)?;

        // Calculate partition size
        let base_partition_size = m.div_ceil(k);
        let alignment_bits = alignment * 8;
        let partition_size = base_partition_size.div_ceil(alignment_bits) * alignment_bits;

        // Calculate stride
        let partition_bytes = partition_size.div_ceil(8);
        let partition_stride_bytes = partition_bytes.div_ceil(alignment) * alignment;
        let partition_stride = partition_stride_bytes / 8; // In u64 words

        // Allocate single flat buffer for AtomicU64
        let total_bytes = partition_stride_bytes * k;

        if total_bytes == 0 || total_bytes > isize::MAX as usize {
            return Err(BloomCraftError::invalid_parameters(format!(
                "Invalid allocation size: {}",
                total_bytes
            )));
        }

        let layout = Layout::from_size_align(total_bytes, alignment)
            .map_err(|e| BloomCraftError::invalid_parameters(format!("Invalid layout: {}", e)))?;

        // SAFETY: Layout is valid, size > 0, alignment is power of 2
        let ptr = unsafe { alloc(layout) };
        if ptr.is_null() {
            handle_alloc_error(layout);
        }

        // Zero memory
        unsafe {
            std::ptr::write_bytes(ptr, 0, total_bytes);
        }

        // Cast to AtomicU64 pointer
        let data = NonNull::new(ptr as *mut AtomicU64).expect("Allocation returned null");

        Ok(Self {
            data,
            k,
            partition_size,
            partition_stride,
            alignment,
            allocated_bytes: total_bytes,
            hasher,
            expected_items,
            target_fpr: fpr,
            item_count: AtomicUsize::new(0),
            _phantom: PhantomData,
        })
    }

    /// Get pointer to partition i's start.
    #[inline]
    fn partition_ptr(&self, partition_idx: usize) -> *const AtomicU64 {
        debug_assert!(partition_idx < self.k);
        unsafe {
            self.data
                .as_ptr()
                .add(partition_idx * self.partition_stride)
        }
    }

    /// Get bit at index within partition (atomic load).
    #[inline]
    unsafe fn get_bit_atomic(&self, partition_idx: usize, bit_idx: usize) -> bool {
        debug_assert!(bit_idx < self.partition_size);
        let ptr = self.partition_ptr(partition_idx);
        let word_idx = bit_idx / 64;
        let bit_offset = bit_idx % 64;
        let word = (*ptr.add(word_idx)).load(Ordering::Relaxed);
        (word & (1u64 << bit_offset)) != 0
    }

    /// Set bit at index within partition (atomic fetch_or).
    #[inline]
    unsafe fn set_bit_atomic(&self, partition_idx: usize, bit_idx: usize) {
        debug_assert!(bit_idx < self.partition_size);
        let ptr = self.partition_ptr(partition_idx);
        let word_idx = bit_idx / 64;
        let bit_offset = bit_idx % 64;
        let mask = 1u64 << bit_offset;

        // SAFETY: Atomic fetch_or is lock-free and wait-free
        // Ordering::Relaxed is sufficient because:
        // - Bit-set operations are idempotent
        // - No inter-thread causality required
        (*ptr.add(word_idx)).fetch_or(mask, Ordering::Relaxed);
    }

    /// Unbiased hash to range using Lemire's method.
    #[inline]
    fn hash_to_range(hash: u64, range: usize) -> usize {
        ((hash as u128 * range as u128) >> 64) as usize
    }

    /// Hash item using BloomHasher trait's canonical bridge.
    #[inline]
    fn hash_item(&self, item: &T) -> (u64, u64) {
        self.hasher.hash_item(item)
    }

    /// Get partition count.
    #[inline]
    pub const fn partition_count(&self) -> usize {
        self.k
    }

    /// Get partition size in bits.
    #[inline]
    pub const fn partition_size(&self) -> usize {
        self.partition_size
    }

    /// Get alignment in bytes.
    #[inline]
    pub const fn alignment(&self) -> usize {
        self.alignment
    }

    /// Get target FPR.
    #[inline]
    pub const fn target_fpr(&self) -> f64 {
        self.target_fpr
    }

    /// Get expected items.
    #[inline]
    pub const fn expected_items(&self) -> usize {
        self.expected_items
    }

    /// Get atomic item count.
    #[inline]
    pub fn item_count(&self) -> usize {
        self.item_count.load(Ordering::Relaxed)
    }

    /// Calculate filter saturation (0.0 to 1.0).
    pub fn saturation(&self) -> f64 {
        let mut total_set = 0;
        for partition_idx in 0..self.k {
            let ptr = self.partition_ptr(partition_idx);
            let words = self.partition_size.div_ceil(64);
            for word_idx in 0..words {
                unsafe {
                    let word = (*ptr.add(word_idx)).load(Ordering::Relaxed);
                    total_set += word.count_ones() as usize;
                }
            }
        }
        total_set as f64 / (self.k * self.partition_size) as f64
    }

    /// Estimate actual FPR based on saturation.
    pub fn estimated_fpr(&self) -> f64 {
        let n = self.item_count() as f64;
        if n == 0.0 {
            return 0.0;
        }
        let fill_rate = 1.0 - (-n / self.partition_size as f64).exp();
        fill_rate.powi(self.k as i32)
    }
}

// Implement BloomFilter trait (for non-concurrent operations)
impl<T, H> BloomFilter<T> for AtomicPartitionedBloomFilter<T, H>
where
    T: Hash + Send + Sync,
    H: BloomHasher + Clone + Default,
{
    fn insert(&mut self, item: &T) {
        // Delegate to concurrent version (we have &mut self, so exclusive access)
        self.insert_concurrent(item);
    }

    fn contains(&self, item: &T) -> bool {
        let (h1, h2) = self.hash_item(item);
        for i in 0..self.k {
            let hash = h1.wrapping_add((i as u64).wrapping_mul(h2));
            let bit_idx = Self::hash_to_range(hash, self.partition_size);
            if !unsafe { self.get_bit_atomic(i, bit_idx) } {
                return false;
            }
        }
        true
    }

    fn clear(&mut self) {
        // Zero all memory (exclusive access required)
        unsafe {
            std::ptr::write_bytes(self.data.as_ptr() as *mut u8, 0, self.allocated_bytes);
        }
        self.item_count.store(0, Ordering::Relaxed);
    }

    fn is_empty(&self) -> bool {
        self.item_count() == 0
    }

    fn len(&self) -> usize {
        self.item_count()
    }

    fn false_positive_rate(&self) -> f64 {
        self.estimated_fpr()
    }

    fn expected_items(&self) -> usize {
        self.expected_items
    }

    fn bit_count(&self) -> usize {
        self.k * self.partition_size
    }

    fn hash_count(&self) -> usize {
        self.k
    }

    fn estimate_count(&self) -> usize {
        if self.saturation() < 0.01 {
            return self.item_count();
        }

        let mut total_set = 0;
        for partition_idx in 0..self.k {
            let ptr = self.partition_ptr(partition_idx);
            let words = self.partition_size.div_ceil(64);
            for word_idx in 0..words {
                let word = unsafe { (*ptr.add(word_idx)).load(Ordering::Relaxed) };
                total_set += word.count_ones() as usize;
            }
        }

        let x = total_set as f64;
        let m = (self.k * self.partition_size) as f64;
        let k = self.k as f64;
        let estimated = -(m / k) * (1.0 - x / m).ln();
        estimated.max(0.0) as usize
    }

    // In impl<T, H> BloomFilter<T> for AtomicPartitionedBloomFilter<T, H>
    fn count_set_bits(&self) -> usize {
        let mut total = 0usize;
        for partition_idx in 0..self.k {
            let ptr = self.partition_ptr(partition_idx);
            let words = self.partition_size.div_ceil(64);
            for word_idx in 0..words {
                // SAFETY: same invariants as partition_ptr documentation.
                // Relaxed ordering is sufficient — bit counts are advisory,
                // not used for synchronization decisions.
                total +=
                    unsafe { (*ptr.add(word_idx)).load(Ordering::Relaxed) }.count_ones() as usize;
            }
        }
        total
    }
}

// Implement ConcurrentBloomFilter trait (lock-free operations)
impl<T, H> ConcurrentBloomFilter<T> for AtomicPartitionedBloomFilter<T, H>
where
    T: Hash + Send + Sync,
    H: BloomHasher + Clone + Default + Send + Sync,
{
    fn insert_concurrent(&self, item: &T) {
        let (h1, h2) = self.hash_item(item);
        for i in 0..self.k {
            let hash = h1.wrapping_add((i as u64).wrapping_mul(h2));
            let bit_idx = Self::hash_to_range(hash, self.partition_size);
            unsafe {
                self.set_bit_atomic(i, bit_idx);
            }
        }
        self.item_count.fetch_add(1, Ordering::Relaxed);
    }

    // In impl<T, H> ConcurrentBloomFilter<T> for AtomicPartitionedBloomFilter<T, H>
    fn contains_concurrent(&self, item: &T) -> bool {
        // contains() is already lock-free (atomic loads with Relaxed ordering)
        // and safe for concurrent invocation. Delegation is semantically correct.
        self.contains(item)
    }
}

// Drop implementation
impl<T, H> Drop for AtomicPartitionedBloomFilter<T, H>
where
    H: BloomHasher + Clone + Default,
{
    fn drop(&mut self) {
        unsafe {
            let layout = Layout::from_size_align(self.allocated_bytes, self.alignment)
                .expect("Drop: Layout must match allocation");
            dealloc(self.data.as_ptr() as *mut u8, layout);
        }
    }
}

// Clone implementation
impl<T, H> Clone for AtomicPartitionedBloomFilter<T, H>
where
    T: Hash,
    H: BloomHasher + Clone + Default,
{
    fn clone(&self) -> Self {
        let layout = Layout::from_size_align(self.allocated_bytes, self.alignment)
            .expect("Clone: Layout must be valid");
        let ptr = unsafe { alloc(layout) };
        if ptr.is_null() {
            handle_alloc_error(layout);
        }
        unsafe {
            std::ptr::copy_nonoverlapping(
                self.data.as_ptr() as *const u8,
                ptr,
                self.allocated_bytes,
            );
        }
        let data = NonNull::new(ptr as *mut AtomicU64).expect("Allocation returned null");

        Self {
            data,
            k: self.k,
            partition_size: self.partition_size,
            partition_stride: self.partition_stride,
            alignment: self.alignment,
            allocated_bytes: self.allocated_bytes,
            hasher: self.hasher.clone(),
            expected_items: self.expected_items,
            target_fpr: self.target_fpr,
            item_count: AtomicUsize::new(self.item_count()),
            _phantom: PhantomData,
        }
    }
}

// Thread safety markers
unsafe impl<T, H> Send for AtomicPartitionedBloomFilter<T, H>
where
    T: Send,
    H: BloomHasher + Clone + Default + Send,
{
}

unsafe impl<T, H> Sync for AtomicPartitionedBloomFilter<T, H>
where
    T: Sync,
    H: BloomHasher + Clone + Default + Sync,
{
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Arc;
    use std::thread;

    #[test]
    fn test_basic_concurrent_insert() {
        let filter = AtomicPartitionedBloomFilter::<String>::new(1000, 0.01).unwrap();

        filter.insert_concurrent(&"hello".to_string());
        filter.insert_concurrent(&"world".to_string());

        assert!(filter.contains(&"hello".to_string()));
        assert!(filter.contains(&"world".to_string()));
        assert!(!filter.contains(&"goodbye".to_string()));
    }

    #[test]
    fn test_concurrent_inserts() {
        let filter = Arc::new(AtomicPartitionedBloomFilter::<u64>::new(10_000, 0.01).unwrap());

        let handles: Vec<_> = (0..8)
            .map(|tid| {
                let f = Arc::clone(&filter);
                thread::spawn(move || {
                    for i in 0..1000 {
                        f.insert_concurrent(&(tid * 1000 + i));
                    }
                })
            })
            .collect();

        for handle in handles {
            handle.join().unwrap();
        }

        for tid in 0..8 {
            for i in 0..1000 {
                assert!(filter.contains(&(tid * 1000 + i)));
            }
        }

        assert_eq!(filter.item_count(), 8000);
    }

    #[test]
    fn test_no_false_negatives_concurrent() {
        let filter = Arc::new(AtomicPartitionedBloomFilter::<u64>::new(5000, 0.01).unwrap());

        let items: Vec<u64> = (0..5000).collect();

        let handles: Vec<_> = items
            .chunks(1000)
            .map(|chunk| {
                let f = Arc::clone(&filter);
                let chunk = chunk.to_vec();
                thread::spawn(move || {
                    for &item in &chunk {
                        f.insert_concurrent(&item);
                    }
                })
            })
            .collect();

        for handle in handles {
            handle.join().unwrap();
        }

        for &item in &items {
            assert!(filter.contains(&item), "False negative for {}", item);
        }
    }

    #[test]
    fn test_send_sync() {
        fn assert_send<T: Send>() {}
        fn assert_sync<T: Sync>() {}

        assert_send::<AtomicPartitionedBloomFilter<u64>>();
        assert_sync::<AtomicPartitionedBloomFilter<u64>>();
    }

    #[test]
    fn test_cache_alignment() {
        let filter = AtomicPartitionedBloomFilter::<u64>::new(10_000, 0.01).unwrap();
        let alignment = filter.alignment();
        assert!(alignment.is_power_of_two());
        let base_ptr = filter.data.as_ptr() as usize;
        assert_eq!(
            base_ptr % alignment,
            0,
            "Base pointer not {}-byte aligned",
            alignment
        );
        for i in 0..filter.partition_count() {
            let ptr = filter.partition_ptr(i) as usize;
            assert_eq!(
                ptr % alignment,
                0,
                "Partition {} not {}-byte aligned",
                i,
                alignment
            );
        }
    }

    #[test]
    fn test_parameter_validation() {
        assert!(AtomicPartitionedBloomFilter::<u64>::new(0, 0.01).is_err());
        assert!(AtomicPartitionedBloomFilter::<u64>::new(100, 0.0).is_err());
        assert!(AtomicPartitionedBloomFilter::<u64>::new(100, 1.0).is_err());
        assert!(AtomicPartitionedBloomFilter::<u64>::new(100, -0.1).is_err());
        assert!(AtomicPartitionedBloomFilter::<u64>::new(100, 1.5).is_err());

        let result = AtomicPartitionedBloomFilter::<u64>::with_hasher_and_alignment(
            100,
            0.01,
            StdHasher::new(),
            3, // not power of 2
        );
        assert!(result.is_err());
    }

    #[test]
    fn test_saturation_and_fpr() {
        let filter = AtomicPartitionedBloomFilter::<u64>::new(10_000, 0.01).unwrap();
        for i in 0..10_000 {
            filter.insert_concurrent(&i);
        }
        let sat = filter.saturation();
        assert!(
            sat > 0.2 && sat < 0.8,
            "Saturation {} out of expected range [0.2, 0.8]",
            sat
        );
        let estimated = filter.estimated_fpr();
        let ratio = estimated / 0.01;
        assert!(
            ratio < 5.0,
            "Estimated FPR {:.4} is too far from target 0.01 (ratio {:.2})",
            estimated,
            ratio
        );
    }

    #[test]
    fn test_clear_exclusive() {
        let mut filter = AtomicPartitionedBloomFilter::<u64>::new(1000, 0.01).unwrap();
        filter.insert_concurrent(&42);
        filter.insert_concurrent(&43);
        assert!(!filter.is_empty());

        filter.clear();
        assert!(filter.is_empty());
        assert_eq!(filter.len(), 0);
        assert!(!filter.contains(&42));
        assert!(!filter.contains(&43));
    }

    #[test]
    fn test_drop_safety() {
        {
            let filter = AtomicPartitionedBloomFilter::<u64>::new(10_000, 0.01).unwrap();
            for i in 0..1000 {
                filter.insert_concurrent(&i);
            }
        }
    }

    #[test]
    fn test_clone_independence() {
        let filter = AtomicPartitionedBloomFilter::<u64>::new(1000, 0.01).unwrap();
        for i in 0..50 {
            filter.insert_concurrent(&i);
        }
        let cloned = filter.clone();
        for i in 0..50 {
            assert!(cloned.contains(&i), "Clone missing item {}", i);
        }
        filter.insert_concurrent(&999);
        assert!(filter.contains(&999));
        assert!(!cloned.contains(&999));
        cloned.insert_concurrent(&888);
        assert!(cloned.contains(&888));
        assert!(!filter.contains(&888));
    }

    #[test]
    fn test_clone_many_items() {
        let filter = AtomicPartitionedBloomFilter::<u64>::new(10_000, 0.01).unwrap();
        for i in 0..5000 {
            filter.insert_concurrent(&i);
        }
        let cloned = filter.clone();
        let mut false_negatives = 0;
        for i in 0..5000 {
            if !cloned.contains(&i) {
                false_negatives += 1;
            }
        }
        assert_eq!(false_negatives, 0, "Clone has false negatives");
    }

    #[test]
    fn test_item_count_atomic() {
        let filter = Arc::new(AtomicPartitionedBloomFilter::<u64>::new(10_000, 0.01).unwrap());
        let thread_count = 8;
        let items_per_thread = 1000;

        let handles: Vec<_> = (0..thread_count)
            .map(|tid| {
                let f = Arc::clone(&filter);
                thread::spawn(move || {
                    for i in 0..items_per_thread {
                        f.insert_concurrent(&(tid * items_per_thread + i));
                    }
                })
            })
            .collect();

        for handle in handles {
            handle.join().unwrap();
        }

        let total = thread_count * items_per_thread;
        assert_eq!(filter.item_count(), total as usize);
    }

    #[test]
    fn test_bloom_filter_trait_insert() {
        let mut filter = AtomicPartitionedBloomFilter::<u64>::new(1000, 0.01).unwrap();
        filter.insert(&42);
        assert!(filter.contains(&42));
        assert!(!filter.contains(&99));
    }

    #[test]
    fn test_bloom_filter_trait_methods() {
        let mut filter = AtomicPartitionedBloomFilter::<u64>::new(1000, 0.01).unwrap();
        assert!(filter.is_empty());
        assert_eq!(filter.len(), 0);
        assert_eq!(filter.expected_items(), 1000);
        assert!(filter.hash_count() > 0);
        assert!(filter.bit_count() > 0);
        filter.insert(&42);
        assert!(!filter.is_empty());
        assert_eq!(filter.len(), 1);
        let fpr = filter.false_positive_rate();
        assert!(fpr >= 0.0);
    }

    #[test]
    fn test_estimate_count() {
        let filter = AtomicPartitionedBloomFilter::<u64>::new(10_000, 0.01).unwrap();
        for i in 0..1000 {
            filter.insert_concurrent(&i);
        }
        let estimated = filter.estimate_count();
        let error = (estimated as i64 - 1000).abs() as f64 / 1000.0;
        assert!(
            error < 0.3,
            "Estimation error {:.1}% exceeds 30%",
            error * 100.0
        );
    }

    #[test]
    fn test_count_set_bits() {
        let filter = AtomicPartitionedBloomFilter::<u64>::new(1000, 0.01).unwrap();
        assert_eq!(filter.count_set_bits(), 0);
        filter.insert_concurrent(&42);
        assert!(filter.count_set_bits() > 0);
        assert!(filter.count_set_bits() <= filter.bit_count());
    }

    #[test]
    fn test_zero_item_count_after_clear() {
        let mut filter = AtomicPartitionedBloomFilter::<u64>::new(1000, 0.01).unwrap();
        filter.insert_concurrent(&1);
        filter.insert_concurrent(&2);
        filter.clear();
        assert_eq!(filter.item_count(), 0);
    }

    #[test]
    fn test_partition_count() {
        let filter = AtomicPartitionedBloomFilter::<u64>::new(100, 0.01).unwrap();
        assert_eq!(filter.partition_count(), filter.hash_count());
    }

    #[test]
    fn test_default_hasher_is_std_hasher() {
        let filter: AtomicPartitionedBloomFilter<u64> =
            AtomicPartitionedBloomFilter::new(100, 0.01).unwrap();
        filter.insert_concurrent(&42);
        assert!(filter.contains(&42));
    }

    #[test]
    fn test_multiple_drops() {
        let filters: Vec<_> = (0..10)
            .map(|_| AtomicPartitionedBloomFilter::<u64>::new(1000, 0.01).unwrap())
            .collect();
        drop(filters);
    }
}