Data Structures
Hash Lookup
A tiny table can scan slots to find the value stored for a key.
Hash Lookup
hash_lookup.c
#include <stdio.h>
int main(void) {
int target = ;
int keys[4] = {3, 7, 11, 0};
int values[4] = {30, 70, 110, 0};
int found = -1;
for (int i = 0; i < 4; i++) {
if (keys[i] == target) {
found = values[i];
}
}
printf("found=%d\n", found);
return 0;
}
#include <stdio.h>
int main(void) {
int target = ;
int keys[4] = {3, 7, 11, 0};
int values[4] = {30, 70, 110, 0};
int found = -1;
for (int i = 0; i < 4; i++) {
if (keys[i] == target) {
found = values[i];
}
}
printf("found=%d\n", found);
return 0;
}
#include <stdio.h>
int main(void) {
int target = ;
int keys[4] = {3, 7, 11, 0};
int values[4] = {30, 70, 110, 0};
int found = -1;
for (int i = 0; i < 4; i++) {
if (keys[i] == target) {
found = values[i];
}
}
printf("found=%d\n", found);
return 0;
}
key slot
Each occupied slot stores a key and a matching value.
miss value
The lookup keeps a fallback value when no slot contains the target key.