Edit

Share via


ReadOnlySpanExtensions.GetDjb2HashCode<T>(ReadOnlySpan<T>) Method

Definition

Gets a content hash from the input ReadOnlySpan<T> instance using the Djb2 algorithm. It was designed by Daniel J. Bernstein and is a non-cryptographic has function. The main advantages of this algorithm are a good distribution of the resulting hash codes, which results in a relatively low number of collisions, while at the same time being particularly fast to process, making it suitable for quickly hashing even long sequences of values. For the reference implementation, see: http://www.cse.yorku.ca/~oz/hash.html. For details on the used constants, see the details provided in this StackOverflow answer (as well as the accepted one): https://stackoverflow.com/questions/10696223/reason-for-5381-number-in-djb-hash-function/13809282#13809282. Additionally, a comparison between some common hashing algorithms can be found in the reply to this StackExchange question: https://softwareengineering.stackexchange.com/questions/49550/which-hashing-algorithm-is-best-for-uniqueness-and-speed. Note that the exact implementation is slightly different in this method when it is not called on a sequence of Byte values: in this case the GetHashCode() method will be invoked for each T value in the provided ReadOnlySpan<T> instance, and then those values will be combined using the Djb2 algorithm.

public static int GetDjb2HashCode<T> (this ReadOnlySpan<T> span);
static member GetDjb2HashCode : ReadOnlySpan<'T> -> int
<Extension()>
Public Function GetDjb2HashCode(Of T) (span As ReadOnlySpan(Of T)) As Integer

Type Parameters

T

The type of items in the input ReadOnlySpan<T> instance.

Parameters

span
ReadOnlySpan<T>

The input ReadOnlySpan<T> instance.

Returns

The Djb2 value for the input ReadOnlySpan<T> instance.

Remarks

The Djb2 hash is fully deterministic and with no random components.

Applies to