PHP code example of pouya1364 / probabilistic-php

1. Go to this page and download the library: Download pouya1364/probabilistic-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

pouya1364 / probabilistic-php example snippets


use Probabilistic\BloomFilter;
use Probabilistic\CountingBloomFilter;
use Probabilistic\CuckooFilter;
use Probabilistic\CountMinSketch;
use Probabilistic\HyperLogLog;

$bloom = BloomFilter::create(expectedItems: 10_000, falsePositiveRate: 0.01);
$bloom->add('[email protected]');

$bloom->mightContain('[email protected]'); // true
$bloom->mightContain('[email protected]'); // false (probably)

$counting = CountingBloomFilter::create(expectedItems: 10_000, falsePositiveRate: 0.01);
$counting->add('session-abc');
$counting->remove('session-abc');

$counting->mightContain('session-abc'); // false

$cuckoo = CuckooFilter::create(expectedItems: 10_000);
$cuckoo->add('192.168.1.1');

$cuckoo->contains('192.168.1.1'); // true
$cuckoo->remove('192.168.1.1'); // true

$cms = CountMinSketch::create(width: 2_000, depth: 5);
$cms->increment('page:/home');
$cms->increment('page:/home');

$cms->estimate('page:/home'); // 2 (or slightly higher, never lower)

$hll = new HyperLogLog(precision: 14); // ~16 KB of memory

foreach ($visitorIds as $id) {
    $hll->add($id);
}

$hll->estimate(); // approximately the number of distinct visitor IDs

use Probabilistic\Exception\ExceptionInterface;

try {
    $bloom = BloomFilter::create(expectedItems: 0, falsePositiveRate: 0.01);
} catch (ExceptionInterface $e) {
    // any error originating from this package
}
bash
composer