Overview

Namespaces

  • PhpCommon
    • Comparison
      • Equivalence
      • Hasher

Interfaces

  • Comparable
  • Comparator
  • Equatable
  • Equivalence
  • Hashable
  • Hasher

Exceptions

  • IncomparableException
  • UnexpectedTypeException
  • Overview
  • Namespace
  • Class

Interface Hasher

A strategy for hashing values and comparing them for equivalence.

The PhpCommon\Comparison\Hasher::hash() method, introduced by this interface, is intended to provide a means for performing fast inequivalence checks and efficient insertion and lookup in hash-based data structures. This method is always consistent with PhpCommon\Comparison\Equivalence::equivalent(), which means that for any references $x and $y, if equivalent($x, $y), then hash($x) === hash($y). However, if equivalence($x, $y) evaluates to false, hash($x) === hash($y) may still be true. Hence why the hash() method is suitable for inequivalence checks, but not equivalence checks.

In accordance with PhpCommon\Comparison\Equivalence, a PhpCommon\Comparison\Hasher can be either generic or type-specific. For that reason, caution should be exercised to ensure the values passed to the methods PhpCommon\Comparison\Equivalence::equivalent() and PhpCommon\Comparison\Hasher::hash() match the type of values supported by the implementing class. Otherwise, an exception may be thrown.

PhpCommon\Comparison\Hasher implements PhpCommon\Comparison\Equivalence

Direct known implementers

PhpCommon\Comparison\Hasher\DateTimeHasher, PhpCommon\Comparison\Hasher\GenericHasher

Indirect known implementers

PhpCommon\Comparison\Hasher\IdentityHasher, PhpCommon\Comparison\Hasher\ValueHasher
Namespace: PhpCommon\Comparison
Author: Marcos Passos marcos@marcospassos.com
Located at Hasher.php
Methods summary
public integer
# hash( mixed $value )

Returns a hash code for the given value.

Returns a hash code for the given value.

This method has the following properties:

  • It is consistent: for any $x, multiple invocations of hash($x) consistently return the same value provided $x remains unchanged according to the definition of the equivalence. The hash need not remain consistent from one execution of an application to another execution of the same application.
  • It is distributable across equivalence: for any $x and $y, if equivalent($x, $y), then hash($x) === hash($y). It is not necessary that the hash be distributable across inequivalence. If equivalence($x, $y) is false, hash($x) === hash($y) may still be true.

Parameters

$value
The value to hash.

Returns

integer
The hash code for the given value.

Throws

PhpCommon\Comparison\UnexpectedTypeException

If the type of the specified value does not match the expected type.


InvalidArgumentException

If some property of the specified value prevents it from being hashed.

See

Equivalence Equivalence relations
Equivalence::equivalent() The equivalent() method
Methods inherited from PhpCommon\Comparison\Equivalence
equivalent()
Methods inherited from PhpCommon\Comparison\Equatable
equals()
PHPCommon Comparison API API documentation generated by ApiGen