Skip to content

Commit f06ec33

Browse files
committed
Add defaulted constructors to GeoDiffCount for convenience
Provide new impls for GeoDiffCount with C: Default, allowing construction from bytes, ones, or pseudorandom data without explicitly passing config. Update tests to use these new constructors.
1 parent 279cf3c commit f06ec33

2 files changed

Lines changed: 35 additions & 14 deletions

File tree

crates/geo_filters/evaluation/performance.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use criterion::{black_box, criterion_group, criterion_main, Criterion};
1+
use std::hint::black_box;
2+
3+
use criterion::{criterion_group, criterion_main, Criterion};
24
use geo_filters::build_hasher::UnstableDefaultBuildHasher;
35
use geo_filters::config::VariableConfig;
46
use geo_filters::diff_count::{GeoDiffCount, GeoDiffCount13};

crates/geo_filters/src/diff_count.rs

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ impl<'a, C: GeoConfig<Diff>> GeoDiffCount<'a, C> {
303303

304304
/// Create a new [`GeoDiffCount`] from a slice of bytes
305305
#[cfg(target_endian = "little")]
306-
pub fn from_bytes(c: C, buf: &'a [u8]) -> Self {
306+
pub fn from_bytes_with_config(c: C, buf: &'a [u8]) -> Self {
307307
if buf.is_empty() {
308308
return Self::new(c);
309309
}
@@ -341,7 +341,7 @@ impl<'a, C: GeoConfig<Diff>> GeoDiffCount<'a, C> {
341341
}
342342

343343
#[cfg(any(test, feature = "test-support"))]
344-
pub fn from_ones(config: C, ones: impl IntoIterator<Item = C::BucketType>) -> Self {
344+
pub fn from_ones_with_config(config: C, ones: impl IntoIterator<Item = C::BucketType>) -> Self {
345345
let mut result = Self::new(config);
346346
for one in ones {
347347
result.xor_bit(one);
@@ -358,7 +358,7 @@ impl<'a, C: GeoConfig<Diff>> GeoDiffCount<'a, C> {
358358
/// is seeded using the number of items so for a given number of items
359359
/// the resulting geofilter should always be the same.
360360
#[cfg(any(test, feature = "test-support"))]
361-
pub fn pseudorandom_filter(config: C, items: usize) -> Self {
361+
pub fn pseudorandom_filter_with_config(config: C, items: usize) -> Self {
362362
use rand::RngCore;
363363
use rand_chacha::rand_core::SeedableRng;
364364

@@ -371,6 +371,23 @@ impl<'a, C: GeoConfig<Diff>> GeoDiffCount<'a, C> {
371371
}
372372
}
373373

374+
impl<'a, C: GeoConfig<Diff> + Default> GeoDiffCount<'a, C> {
375+
#[cfg(target_endian = "little")]
376+
pub fn from_bytes(buf: &'a [u8]) -> Self {
377+
Self::from_bytes_with_config(C::default(), buf)
378+
}
379+
380+
#[cfg(any(test, feature = "test-support"))]
381+
pub fn from_ones(ones: impl IntoIterator<Item = C::BucketType>) -> Self {
382+
Self::from_ones_with_config(C::default(), ones)
383+
}
384+
385+
#[cfg(any(test, feature = "test-support"))]
386+
pub fn pseudorandom_filter(items: usize) -> Self {
387+
Self::pseudorandom_filter_with_config(C::default(), items)
388+
}
389+
}
390+
374391
/// Applies a repeated bit mask to the underlying filter.
375392
/// E.g. given the bit mask `0b110100` with modulus 6, we filter the bitset of the geometric filter as follows:
376393
/// bitset of the geometric filter: 011010 101101 001010
@@ -490,8 +507,8 @@ mod tests {
490507

491508
#[test]
492509
fn test_xor() {
493-
let a = GeoDiffCount7::from_ones(Default::default(), 0..1000);
494-
let b = GeoDiffCount7::from_ones(Default::default(), 10..1010);
510+
let a = GeoDiffCount7::from_ones(0..1000);
511+
let b = GeoDiffCount7::from_ones(10..1010);
495512
let c = xor(&a, &b);
496513
let d = xor(&a, &b);
497514
assert_eq!(a.iter_ones().count(), 1000);
@@ -511,7 +528,7 @@ mod tests {
511528
m.xor_bit(10);
512529
assert!(m.iter_ones().collect_vec().is_empty());
513530

514-
let mut m = GeoDiffCount7::from_ones(Default::default(), 0..100);
531+
let mut m = GeoDiffCount7::from_ones(0..100);
515532
assert_eq!(m.iter_ones().count(), 100);
516533
m.xor_bit(10);
517534
assert_eq!(m.iter_ones().count(), 99);
@@ -593,20 +610,19 @@ mod tests {
593610
// masked bitset : 010000 100100 000000
594611
// after compression : 01 0 10 1 00 0
595612
// bitset of the returned filter : 010 101000
596-
let m = GeoDiffCount7::from_ones(Default::default(), [16, 15, 13, 11, 9, 8, 6, 3, 1]);
613+
let m = GeoDiffCount7::from_ones([16, 15, 13, 11, 9, 8, 6, 3, 1]);
597614
let n = masked(&m, 0b110100, 6);
598615
assert_eq!(n.iter_ones().collect_vec(), vec![16, 11, 8]);
599616

600617
for i in 0..100 {
601-
let m = GeoDiffCount7::from_ones(Default::default(), (0..i).collect_vec());
618+
let m = GeoDiffCount7::from_ones((0..i).collect_vec());
602619
let n = masked(&m, 0b111, 3);
603620
assert_eq!(m, n);
604621
}
605622

606623
for i in 0..300 {
607-
let m = GeoDiffCount7::from_ones(Default::default(), (0..i).collect_vec());
608-
let slow =
609-
GeoDiffCount::from_ones(Default::default(), masked(&m, 0b110, 3).iter_ones());
624+
let m = GeoDiffCount7::from_ones((0..i).collect_vec());
625+
let slow = GeoDiffCount::from_ones(masked(&m, 0b110, 3).iter_ones());
610626
let n = masked(&m, 0b110, 3);
611627
assert_eq!(slow, n, "in iteration: {i}");
612628
}
@@ -667,7 +683,7 @@ mod tests {
667683

668684
assert_eq!(writer.len(), 0);
669685

670-
let after = GeoDiffCount7::from_bytes(before.config.clone(), &writer);
686+
let after = GeoDiffCount7::from_bytes_with_config(before.config.clone(), &writer);
671687

672688
assert_eq!(before, after);
673689
}
@@ -694,7 +710,10 @@ mod tests {
694710
let pad_amount = (0..8).choose(rnd).unwrap();
695711
writer.write_all(&padding[..pad_amount]).unwrap();
696712
before.write(&mut writer).unwrap();
697-
let after = GeoDiffCount::<'_, C>::from_bytes(before.config.clone(), &writer[pad_amount..]);
713+
let after = GeoDiffCount::<'_, C>::from_bytes_with_config(
714+
before.config.clone(),
715+
&writer[pad_amount..],
716+
);
698717
assert_eq!(before, after);
699718
}
700719

0 commit comments

Comments
 (0)