Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/test-analyze-tag-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php-version: [ '8.0', '8.1', '8.2', '8.3', '8.4', '8.5' ]
php-version: [ '7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3', '8.4', '8.5' ]

steps:
- name: Checkout code
Expand All @@ -26,6 +26,9 @@ jobs:
coverage: pcov
tools: composer:v2

- name: Remove packages from composer.json
run: composer remove php-cs-fixer/shim phpstan/phpstan --dev

- name: Validate composer.json
run: composer validate --strict

Expand Down
6 changes: 6 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@
return (new Config())
->setRiskyAllowed(false)
->setRules([
'@PHP7x0Migration' => true,
'@Symfony' => true,
'@PhpCsFixer' => true,
'nullable_type_declaration_for_default_null_value' => false,
'phpdoc_no_empty_return' => false,
'modifier_keywords' => [
'elements' => ['property', 'method'],
],
])
// 💡 by default, Fixer looks for `*.php` files excluding `./vendor/` - here, you can groom this config
->setFinder(
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ It is **not** appropriate when an exact distinct count is required.

## Requirements

- **PHP 8.0, 8.1, 8.2, 8.3, 8.4 and 8.5**
- **PHP 7.0, 7.1, 7.2, 7.3, 7.4, 8.0, 8.1, 8.2, 8.3, 8.4 and 8.5**
- No external dependencies

> **Note:** The `xxh3` and `xxh128` hash algorithms are available only when supported by your PHP version and build. If
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
}
],
"require": {
"php": "^8.0"
"php": "^8.0|^7.0"
},
"require-dev": {
"php-cs-fixer/shim": "^3.95",
"phpstan/phpstan": "^2.2",
"phpunit/phpunit": "^9.6"
"phpunit/phpunit": "^9.6|^8.5|^6.5"
},
"autoload": {
"psr-4": {
Expand Down Expand Up @@ -50,4 +50,4 @@
"sort-packages": true,
"optimize-autoloader": true
}
}
}
63 changes: 44 additions & 19 deletions src/HyperLogLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,22 @@
class HyperLogLog
{
/** @var int */
private const TWO_POW_32 = 4_294_967_296;
private int $counterBits;
const TWO_POW_32 = 4294967296;

private string $hashAlgorithm;
/** @var int */
private $counterBits;

/** @var string */
private $hashAlgorithm;

private int $m;
/** @var int */
private $m;

/** @var array<int, int> Counters storing maximum rho values */
private array $counters;
private $counters;

/** @var bool Indicates if the selected hash algorithm produces less than 64 bits */
private bool $needsPadding;
private $needsPadding;

/**
* HyperLogLog constructor.
Expand Down Expand Up @@ -95,8 +99,10 @@ public function setCounters(array $counters): self
* and leading zero count, and the register is updated with the maximum observed rho value.
*
* @param string $value element to insert
*
* @return void
*/
public function add(string $value): void
public function add(string $value)
{
$hashString = $this->hash($value, $this->hashAlgorithm);

Expand Down Expand Up @@ -179,7 +185,9 @@ public function merge(self $other): self
}

$this->counters = array_map(
static fn (int $counter, int $otherCounter): int => max($counter, $otherCounter),
static function (int $counter, int $otherCounter): int {
return max($counter, $otherCounter);
},
$this->counters,
$other->counters
);
Expand Down Expand Up @@ -221,17 +229,34 @@ public function alpha(int $m): float
throw new \InvalidArgumentException('Invalid number of counters, $m must be greater than 0');
}

return match ($m) {
2 => 0.46852874309841,
4 => 0.56806457964166,
8 => 0.63557660535301,
16 => 0.673,
32 => 0.697,
64 => 0.709,
128 => 0.71527049326382,
256 => 0.71827259324955,
default => 0.7213 / (1 + 1.079 / $m),
};
switch ($m) {
case 2:
return 0.46852874309841;

case 4:
return 0.56806457964166;

case 8:
return 0.63557660535301;

case 16:
return 0.673;

case 32:
return 0.697;

case 64:
return 0.709;

case 128:
return 0.71527049326382;

case 256:
return 0.71827259324955;

default:
return 0.7213 / (1 + 1.079 / $m);
}
}

/**
Expand Down
Loading