1<?php
2
3declare(strict_types=1);
4
5namespace Antlr\Antlr4\Runtime\Comparison;
6
7final class Hasher
8{
9    private function __construct()
10    {
11    }
12
13    public static function hash(...$values) : int
14    {
15        return self::hashArray($values);
16    }
17
18    /**
19     * @param array<mixed> $values
20     */
21    private static function hashArray(array $values) : int
22    {
23        $result = 1;
24        foreach ($values as $value) {
25            $elementHash = self::hashValue($value);
26            $result = (31 * $result + $elementHash) & 0xffffffff;
27        }
28
29        return $result;
30    }
31
32    public static function hashValue($value) : int
33    {
34        if ($value === null) {
35            return 0;
36        }
37
38        if (\is_int($value)) {
39            return $value;
40        }
41
42        if ($value instanceof Hashable) {
43            return $value->hashCode();
44        }
45
46        if (\is_object($value)) {
47            return \spl_object_id($value);
48        }
49
50        if (\is_array($value)) {
51            return self::hashArray($value);
52        }
53
54        if (\is_bool($value) || \is_float($value) || \is_resource($value)) {
55            return (int) $value;
56        }
57
58        return \crc32((string) $value);
59    }
60}
61