This repository has been archived by the owner on Nov 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Hash functions #10
Open
babakbehzad
wants to merge
9
commits into
master
Choose a base branch
from
hash_functions
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Hash functions #10
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
92645ac
add hash-functions feature to support different hash functions
babakbehzad 3c281ff
add hash-functions feature to support different hash functions
babakbehzad f509d7d
update test_hash_map to work for several hash_maps
babakbehzad 39f09ae
add size function to hash_map, fixed Pearson hash function
babakbehzad 41a22c0
fix two warnings regarding hash function signature
babakbehzad 51f5b08
Merge branch 'master' into hash_functions
babakbehzad c1f5a53
fix a bug thrown by gcc
babakbehzad 1af4325
update doxygen for the new stuff
babakbehzad e0bde7a
address Seans comments
babakbehzad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
#include <stdlib.h> | ||
#include <stdint.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include <stdbool.h> | ||
|
||
#include "hash_functions.h" | ||
#include "memory.h" | ||
|
||
size_t hash_map_default_hash_func(const void *key, size_t capacity, int len) { | ||
return *((size_t *) key) % capacity; | ||
} | ||
|
||
size_t additive(const void *key, size_t capacity, int len) { | ||
const char *key2 = (const char *) key; | ||
int hash = len; | ||
|
||
for (int i = 0; i < len; ++i) { | ||
hash += key2[i]; | ||
} | ||
|
||
|
||
return (hash % capacity); | ||
} | ||
|
||
size_t pearson_hash(const void *x_copy, size_t capacity, int len) { | ||
int h, i, j; | ||
unsigned char ch; | ||
size_t hex; | ||
|
||
// to store h values | ||
struct { | ||
int a; | ||
} hh[8]; | ||
|
||
// 256 values 0-255 in any (random) order suffices | ||
struct DAT { | ||
int a; | ||
} | ||
|
||
T[256] = { | ||
98, 6, 85, 150, 36, 23, 112, 164, 135, 207, 169, 5, 26, 64, 165, 219, // 1 | ||
61, 20, 68, 89, 130, 63, 52, 102, 24, 229, 132, 245, 80, 216, 195, 115, // 2 | ||
90, 168, 156, 203, 177, 120, 2, 190, 188, 7, 100, 185, 174, 243, 162, 10, // 3 | ||
237, 18, 253, 225, 8, 208, 172, 244, 255, 126, 101, 79, 145, 235, 228, 121, // 4 | ||
123, 251, 67, 250, 161, 0, 107, 97, 241, 111, 181, 82, 249, 33, 69, 55, // 5 | ||
59, 153, 29, 9, 213, 167, 84, 93, 30, 46, 94, 75, 151, 114, 73, 222, // 6 | ||
197, 96, 210, 45, 16, 227, 248, 202, 51, 152, 252, 125, 81, 206, 215, 186, // 7 | ||
39, 158, 178, 187, 131, 136, 1, 49, 50, 17, 141, 91, 47, 129, 60, 99, // 8 | ||
154, 35, 86, 171, 105, 34, 38, 200, 147, 58, 77, 118, 173, 246, 76, 254, // 9 | ||
133, 232, 196, 144, 198, 124, 53, 4, 108, 74, 223, 234, 134, 230, 157, 139, // 10 | ||
189, 205, 199, 128, 176, 19, 211, 236, 127, 192, 231, 70, 233, 88, 146, 44, // 11 | ||
183, 201, 22, 83, 13, 214, 116, 109, 159, 32, 95, 226, 140, 220, 57, 12, // 12 | ||
221, 31, 209, 182, 143, 92, 149, 184, 148, 62, 113, 65, 37, 27, 106, 166, // 13 | ||
3, 14, 204, 72, 21, 41, 56, 66, 28, 193, 40, 217, 25, 54, 179, 117, // 14 | ||
238, 87, 240, 155, 180, 170, 242, 212, 191, 163, 78, 218, 137, 194, 175, 110, // 15 | ||
43, 119, 224, 71, 122, 142, 42, 160, 104, 48, 247, 103, 15, 11, 138, 239 // 16 | ||
}; | ||
|
||
char *x = safe_malloc(len); | ||
memcpy(x, x_copy, len); | ||
|
||
int r; | ||
ch = x[0]; // save first byte | ||
for (j = 0 ; j < 8; j++) { | ||
// standard Pearson hash (output is h) | ||
h = 0; | ||
|
||
for (i = 0; i < len; i++) { | ||
r = abs(h ^ x[i]); | ||
h = T[r].a; | ||
} | ||
|
||
hh[j].a = h; // store result | ||
x[0] = x[0] + 1; // increment first data byte by 1 | ||
} | ||
|
||
x[0] = ch; // restore first byte | ||
|
||
safe_free(x); | ||
|
||
// concatenate the 8 stored values of h | ||
/*sprintf(hex,"%02X%02X%02X%02X%02X%02X%02X%02X", | ||
hh[0].a, hh[1].a, | ||
hh[2].a, hh[3].a, | ||
hh[4].a, hh[5].a, | ||
hh[6].a, hh[7].a);*/ | ||
|
||
// For now let me do it this way, but Liu doesn't think this is a good thing | ||
for (int i = 0; i < 8; i++) { | ||
hex = hex << 8; | ||
hex = hex & hh[i].a; | ||
} | ||
|
||
return hex; // output 64-bit 16 hex bytes hash | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* @file | ||
* Implementation of different hash functions. | ||
*/ | ||
|
||
#ifndef HASH_FUNCTIONS_H | ||
#define HASH_FUNCTIONS_H | ||
|
||
/** | ||
* The default hash function. This is the function to be used if the user does not explicitly provide any hash function. It is a basic `mod` operator. | ||
* @param key key to hash | ||
* @param capacity maximum size of the map | ||
* @param len the size of the key passed as the first argument | ||
* @return an offset within the range `[0, capacity)` | ||
*/ | ||
size_t hash_map_default_hash_func(const void *key, size_t capacity, int len); | ||
|
||
/** | ||
* The additive hash function. Both the explanation and code are based on [Chasan Chouse](http://www.chasanc.com/old/hashing_func.htm) | ||
* @param key key to hash | ||
* @param capacity maximum size of the map | ||
* @param len the size of the key passed as the first argument | ||
* @return an offset within the range `[0, capacity)` | ||
*/ | ||
size_t additive(const void *x, size_t capacity, int len); | ||
|
||
/** | ||
* The Pearson hash function. Both the explanation and code are based on [Wikipedia](http://en.wikipedia.org/wiki/Pearson_hashing) | ||
* @param key key to hash | ||
* @param capacity maximum size of the map | ||
* @param len the size of the key passed as the first argument | ||
* @return an offset within the range `[0, capacity)` | ||
*/ | ||
size_t pearson_hash(const void *x, size_t capacity, int len); | ||
|
||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
#include "memory.h" | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Try not to include changes like this. I usually look at the diff before committing to make sure I don't have stuff like this.