php实现布隆过滤器

<?php
class Bloom {

    // 哈希函数的数量
    protected $hashNum = 3;

    // 位数组的大小
    protected $bitArrayCount = 1024*10;

    // 位数组
    protected $bitArray = [];

    public function __construct()
    {
        // 构建默认的位数组,全部置为 false
        $this->bitArray = array_pad([], $this->bitArrayCount, false);
    }

    /**
     * 获取 hash 函数;也就是在位数组中,需要改为 true 的索引
     * @param string $key 元素
     * @return array
     */
    protected function getIndexes($key)
    {
        $indexes = [];
        for ($i = 0; $i < $this->hashNum; $i ++) {
            $index = sprintf('%u', crc32($key . $i));  // 使用 crc32 散列
            $index = $index % $this->bitArrayCount;	   // 获取 在位数组中的 位置
            $indexes[] = $index;
        }

        return $indexes;
    }

    /**
     * 向过滤器中添加元素
     * @param string $key 要添加的元素
     */
    public function addItem($key)
    {
        $indexes = $this->getIndexes($key);

        // 将 hash 结果对应的位修改为 true
        foreach ($indexes as $index) {
            $this->bitArray[$index] = true;
        }
    }

    /**
     * 过滤器中是否存在这个元素; true 表示很可能存在,false 表示一定不存在
     * @param string $key 元素
     * @return array
     */
    public function mightExist($key)
    {
        $indexes = $this->getIndexes($key);

        foreach ($indexes as $index) {
            if (! $this->bitArray[$index]) {
                return false;
            }
        }
        return true;
    }
}

class Test
{
    public function run()
    {
        $bloom = new Bloom();

        // 向过滤器中添加 1000 个元素
        for ($i = 0; $i < 100; $i ++) {
            $bloom->addItem($i);
        }

        // 测试 过滤器判断结果
        for ($i = 90; $i < 110; $i ++) {
            
            $mightExist = $bloom->mightExist($i);

            if ($mightExist) {
                echo "might exist ", $i, PHP_EOL;
            } else {
                echo "not exist ", $i, PHP_EOL;
            }
        }
    }
}

(new Test())->run();

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值