Last active
July 9, 2022 13:49
-
-
Save audinue/a6926ea432d7ff5f43e5df7969af2aef to your computer and use it in GitHub Desktop.
This file contains hidden or 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
int hashc(char* p, int sz) | |
{ | |
if (!sz) | |
return 0; | |
sz /= sizeof(char); | |
int h = 1; | |
for (int i = 0; i < sz; ++i) | |
h = 31 * h + *(p + i); | |
return h; | |
} | |
int hashs(short* p, int sz) | |
{ | |
if (!sz) | |
return 0; | |
sz /= sizeof(short); | |
int h = 1; | |
for (int i = 0; i < sz; ++i) | |
h = 31 * h + *(p + i); | |
return h; | |
} | |
int hashi(int* p, int sz) | |
{ | |
if (!sz) | |
return 0; | |
sz /= sizeof(int); | |
int h = 1; | |
for (int i = 0; i < sz; ++i) | |
h = 31 * h + *(p + i); | |
return h; | |
} | |
int hashl(long long* p, int sz) | |
{ | |
if (!sz) | |
return 0; | |
sz /= sizeof(long long); | |
int h = 1; | |
for (int i = 0; i < sz; ++i) | |
h = 31 * h + *(p + i); | |
return h; | |
} |
This file contains hidden or 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
#include <stdio.h> | |
void main() | |
{ | |
char a[] = { 65, 66, 67, 68 }; | |
short b[] = { 65, 66, 67, 68 }; | |
int c[] = { 65, 66, 67, 68 }; | |
long long d[] = { 65, 66, 67, 68 }; | |
char e[] = "ABCD"; | |
wchar_t f[] = L"ABCD"; | |
// These yields the same result | |
printf("%d\n", hashc(a, sizeof(a))); | |
printf("%d\n", hashs(b, sizeof(b))); | |
printf("%d\n", hashi(c, sizeof(c))); | |
printf("%d\n", hashl(d, sizeof(d))); | |
printf("%d\n", hashc(e, sizeof(e) - sizeof(char))); | |
printf("%d\n", hashs(f, sizeof(f) - sizeof(short))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment