liveBook · Manning

Hash value

As explained in Fabulous Adventures in Data Structures and Algorithms

A hash code is an integer value computed from data to support its placement and lookup in a hash-based data structure.

A hash value is an integer computed from data so a hash set or dictionary can place that data into a bucket and find it efficiently. For a string, hashing processes its characters; lookup then depends on the hashing work rather than the number of stored items. This helps Contains, but not StartsWith, because hashing deliberately distributes dissimilar words instead of preserving useful prefixes. section 5.1.1

Hashing becomes part of equality design when an object is used in a hash set or as a dictionary key. Equivalent objects must have equal hash values, although equal hash values do not prove equivalence. Any mutation that changes a stored object's hash value can make that object unfindable. section 5.4.2

When combining several fields into a hash value:

  • Include the data used to decide equivalence. section 5.4.2
  • Preserve the order in which fields are added; changing that order changes the result. section 5.4.2
  • Do not assume every equivalence input is necessary: Count may participate in equivalence without appearing in the hash, provided equivalent nodes still hash equally. section 5.4.2
  • Use C#'s HashCode helper when combining multiple pieces of data is error-prone. section 5.4.2

Hashing equivalent graph nodes

A node comparer adds the node's end marker, then each edge label and the node reached by that edge, ordering edge labels first. The resulting hash supports equivalence checks for mutable directed acyclic word-graph nodes. section 5.4.2

Listing 5.7 Finishing the Node equality comparer
        public int GetHashCode(Node n)
        {
            var h = new HashCode();
            h.Add(n.IsEnd);
            foreach(var e in n.EdgeLabels.OrderBy(c=>c))
            {
                h.Add(e);
                h.Add(n.FollowEdge(e)); #1
            }
            return h.ToHashCode();
        }
    }

Inspect the C# GetHashCode implementation that combines node properties and ordered edges. section 5.4.2

How Fabulous Adventures in Data Structures and Algorithms covers it

How deep it goes
Explains bucket lookup, equality contracts, mutation hazards, and multi-field construction, but stops short of hashing-function internals.
Examples
Uses string buckets, StartsWith limitations, and a directed acyclic word-graph node comparer with ordered edge hashing.
What you can do after
Apply it via C#'s HashCode class when implementing GetHashCode for objects compared by hash-based collections.
How it connects
Connects hash values to hash functions, hash sets, dictionaries, equality comparers, object mutability, and graph nodes.

Also explained in

Compare how 7 books explain hash value →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Fabulous Adventures in Data Structures and Algorithms. The section links point to the parts of the book it draws on.