< 返回版块

t924417424 发表于 2021-05-21 19:39

Tags:rust,bloom,filter

Rust语言bloom_filter包(布隆过滤器)

在https://crates.io中查看

测试用例

use bloom_filter_plus::*;

fn main() {
    // test 1
    let mut filter = BloomFilter::new();
    filter.insert("key");
    assert_eq!(true, filter.contains("key"));
    assert_eq!(false, filter.contains("key1"));

    // test2
    let mut filter2 = BloomFilter::new().set_size(10).set_hash_loop(20);
    filter2.insert("key");
    filter2.debug();
    assert_eq!(true, filter2.contains("key"));
    assert_eq!(false, filter2.contains("key1"));
}

Go版本基准测试

查看Go版本实现

Insert:

goos: darwin
goarch: amd64
pkg: github.com/t924417424/BloomFilter
cpu: Intel(R) Core(TM) i3-8100B CPU @ 3.60GHz
Benchmark_Insert-4   	  976336	      1075 ns/op	     160 B/op	       1 allocs/op
PASS
ok  	github.com/t924417424/BloomFilter	1.505s

Contains:

goos: darwin
goarch: amd64
pkg: github.com/t924417424/BloomFilter
cpu: Intel(R) Core(TM) i3-8100B CPU @ 3.60GHz
Benchmark_Contains-4   	 1000000	      1054 ns/op	     160 B/op	       1 allocs/op
PASS
ok  	github.com/t924417424/BloomFilter	1.169s

Ext Link: https://crates.io/crates/bloom_filter_plus

评论区

写评论
作者 t924417424 2021-05-22 09:33

我也改一下

--
👇
hscspring: 很喜欢这个API:

BloomFilter::new()
    .size(DEFAULT_SIZE)
    .hash_loop(20);

--
👇
Neutron3529: ``` BloomFilter::new(BloomConfig { size: Some(DEFAULT_SIZE), hash_loop: Some(20), });

这个语法略丑

据传,一个优雅的写法是:

BloomFilter::new() .size(DEFAULT_SIZE) .hash_loop(20);

或者,一个更好实现的版本:

BloomFilter::Config() .size(DEFAULT_SIZE) .hash_loop(20) .new();

直接上`Some()`有点丑

作者 t924417424 2021-05-22 09:32

受教了

--
👇
Neutron3529: ``` BloomFilter::new(BloomConfig { size: Some(DEFAULT_SIZE), hash_loop: Some(20), });

这个语法略丑

据传,一个优雅的写法是:

BloomFilter::new() .size(DEFAULT_SIZE) .hash_loop(20);

或者,一个更好实现的版本:

BloomFilter::Config() .size(DEFAULT_SIZE) .hash_loop(20) .new();

直接上`Some()`有点丑
Neutron3529 2021-05-22 01:20

这个招数是我从这里学的,学艺不精,并不会制作这样的API

我只会

let bloom_filter=-BloomFilter::new_neg()
    .size(DEFAULT_SIZE)
    .hash_loop(20);

demo如下:

#[derive(Clone,Copy,Debug)]
struct Cfg{
    a:i32,
    b:i32
}
#[derive(Debug)]
struct Target{
    config:Cfg,
    result:i32
}
use std::ops::Neg;//如果不喜欢Neg,可以换成Deref(有误用风险但解释性强),也可以使用Not,假装这是一个marco
impl Neg for Cfg{//这是核心,因为大概无论如何我们都不会对Config做Neg操作,所以impl Neg for Cfg很安全
    type Output=Target;//这里其实是Builder
    fn neg(self)->Self::Output{
        Self::Output{
            config:self,
            result:self.a+self.b
        }
    }
}
impl Cfg{
    fn new()->Self{Self{a:0,b:0}}
    fn a(mut self,a:i32)->Self{self.a=a;self}
    fn b(mut self,b:i32)->Self{self.b=b;self}
}
fn main(){
    let build=-Cfg::new().a(5).b(3);
    println!("{:?}",build);
}

输出:

rustc test.rs -C opt-level=3 -C target-cpu=native -C codegen-units=1 -C lto -o test  && ./test
Target { config: Cfg { a: 5, b: 3 }, result: 8 }

👇
hscspring: 很喜欢这个API:

BloomFilter::new()
    .size(DEFAULT_SIZE)
    .hash_loop(20);

--
👇
Neutron3529: ``` BloomFilter::new(BloomConfig { size: Some(DEFAULT_SIZE), hash_loop: Some(20), });

这个语法略丑

据传,一个优雅的写法是:

BloomFilter::new() .size(DEFAULT_SIZE) .hash_loop(20);

或者,一个更好实现的版本:

BloomFilter::Config() .size(DEFAULT_SIZE) .hash_loop(20) .new();

直接上`Some()`有点丑

长琴 2021-05-21 22:47

很喜欢这个API:

BloomFilter::new()
    .size(DEFAULT_SIZE)
    .hash_loop(20);

--
👇
Neutron3529: ``` BloomFilter::new(BloomConfig { size: Some(DEFAULT_SIZE), hash_loop: Some(20), });

这个语法略丑

据传,一个优雅的写法是:

BloomFilter::new() .size(DEFAULT_SIZE) .hash_loop(20);

或者,一个更好实现的版本:

BloomFilter::Config() .size(DEFAULT_SIZE) .hash_loop(20) .new();

直接上`Some()`有点丑
Neutron3529 2021-05-21 21:15
 BloomFilter::new(BloomConfig {
        size: Some(DEFAULT_SIZE),
        hash_loop: Some(20),
    });

这个语法略丑

据传,一个优雅的写法是:

BloomFilter::new()
    .size(DEFAULT_SIZE)
    .hash_loop(20);

或者,一个更好实现的版本:

BloomFilter::Config()
    .size(DEFAULT_SIZE)
    .hash_loop(20)
    .new();

直接上Some()有点丑

1 共 5 条评论, 1 页