Hash Tables Slides
Hash Tables Slides
Robert Horvick
SOFTWARE ENGINEER
@bubbafat www.roberthorvick.com
Hash Table Overview
- Associative Array
Hashing Algorithms
- Stable
- Uniform Distribution
- Secure
headers["content-length"] = "8056";
headers["content-type"] = "image/png";
HTTP Headers
The header name and value become the key and value in the associative array
Environment Variables
Environment env;
env[”SSH_TTY"] = ”/dev/pts/0";
env[”PATH"] = ”/usr/local/jdk/bin/...";
Environment Variables
The header name and value become the key and value in the associative array
Hash Table
An associative array container that provides O(1) insert,
delete and search operations.
HashTable<String, String> headers;
headers["content-length"] = "8056";
headers["content-type"] = "image/png";
Hash Table
The hash table stores HTTP header data where the key and value are both
strings
HashTable<String, HttpHeaderValue> headers;
headers["content-length"] = IntHeader(8056)
headers["content-type"] = StringHeader("image/png");
Hash Table
Hash table key and value types do not have to be the same
Hash Function
A function that maps data of arbitrary size to data of a
fixed size.
Hash Function Examples
Stable Unstable
public int StableHash(string input) public int UnstableHash(string input)
{ {
int result = 0; int result = DateTime.Now.Second;
Stable Unstable
StableHash("foo"); UnstableHash("foo");
StableHash("foo"); UnstableHash("foo");
StableHash("foo"); UnstableHash("foo");
324 1449447443
324 1449447444
324 1449447445
Uniformity
A hash algorithm should distribute its resulting hash
value uniformly throughout the output space.
Uniform Distribution
Uniform Distribution
Uniform Distribution
Uniform Distribution
Uniform Distribution
Uniform Distribution
Uniform Distribution
Uniform Distribution
Uniform Distribution
Uniform Distribution
Uniform Distribution
SDBMHash(”oof"); StableHash(”oof");
SDBMHash(”ofo"); StableHash(”ofo");
849955110 324
924308646 324
923718264 324
Security
A secure hashing algorithm cannot be inverted (the
input derived from the output hash).
Username Password Hash (sdbm)
evelyn 3471203675
brian 969889485
will 1978836480
Password Hash
aaaaaaaa 3834880256
aaaaaaab 3834880257
aaaaaaac 3834880258
aaaaaaad 3834880259
… …
zzzzzzzz 528284160
Username Password Hash (sdbm)
evelyn 3471203675
brian 969889485
will 1978836480
Pros f o o
Stable
Fast
Cons
Poor uniformity 102 111 111
Poor security
Pros l o r em i p s um d o l o r
Stable
Fast
Better Uniformity
1701998444 1885937773 544044403 1869377380 114
Cons
Poor Security 1701998444 -707031079 -162986676 1706390704 1706390818
44
Dbj2 Hash
public TValue this[TKey key] Functions to support indexed get and set
operations using the key type
{
get => table[Index(key)]; Retrieve the value with that key
set => table[Index(key)] = value; Set the value with that key
}
private uint Index(TKey key)
{
return Hash(key) % table.Length;
}
}
HashTable<string, string> table = new HashTable<string, string>();
table["content-length"] = "8056";
table["content-type"] = "image/png";
Adding Values
HashTable<string, string> table = new HashTable<string, string>();
table["content-length"] = "8056";
table["content-type"] = "image/png";
Adding Values
HashTable<string, string> table = new HashTable<string, string>();
table["content-length"] = "8056";
table["content-type"] = "image/png";
Adding Values
8056
HashTable<string, string> table = new HashTable<string, string>();
table["content-length"] = "8056";
table["content-type"] = "image/png";
Adding Values
HashTable<string, string> table = new HashTable<string, string>();
table["content-length"] = "8056";
table["content-type"] = "image/png";
Adding Values
8056
Hash Collision
When multiple distinct keys would be inserted at the
same hash table index.
Separate Chaining
Collisions in a hash table are chained together into a
linked list whose root node is the hash table array entry.
internal class HashTableEntry<TKey, TValue>
Separate Chaining
The hash table handles collisions by linking all the values with the same table
index into a linked list of entries.
table["content-length"] = "8056";
table["content-type"] = "image/png";
table["content-length"] = "8056"; Key: “content-length”
Value: “8056”
Next: null
table["content-type"] = "image/png";
table["content-length"] = "8056"; Key: “content-length”
Value: “8056”
Next: null
table["content-type"] = "image/png";
Key: “content-type”
Value: “image/png”
Next: null
… …
… … … …
Fill Factor
The percentage of capacity representing the maximum
number of entries before the table will grow. E.g., 0.80
Growth Factor
The multiple to increase the capacity of the hash table
when the fill factor has been exceeded. E.g., 1.50
Hash Table Growth
Determine the new index for the existing items in the hash table
… …
Hash Table Growth
… …
Hash Table Growth
… …
Hash Table Growth
… …
Hash Table Growth
… …
Hash Table Growth
… …
Hash Table Growth
… … …
Hash Table Growth
… … … …
Hash Table Growth
… … … …
Hash Table Growth
… … … …
Iteration
…
… … … …
…
Determine the index Search the entry list Remove the entry
Dog