Skip to content

Instantly share code, notes, and snippets.

@matsumotory
Created March 30, 2016 12:00
Show Gist options
  • Select an option

  • Save matsumotory/ec4867e0aa60dec98853e3f0a2eab802 to your computer and use it in GitHub Desktop.

Select an option

Save matsumotory/ec4867e0aa60dec98853e3f0a2eab802 to your computer and use it in GitHub Desktop.
//
// $ ./a.out 728_time_time_time
// key offset: 5819
// $ ./a.out 414_time_time_time
// key offset: 5819
//
#include <stdio.h>
#include <string.h>
#define BUCKETS 20731
unsigned long hash(const char *s, size_t l)
{
unsigned long v;
size_t i;
for (v = 0, i = 0; i++ < l; s++) { v = *s + 31 * v; }
return v;
}
int main(int argc, char **argv)
{
if (argc != 2)
return 1;
printf("key offset: %lu\n", hash(argv[1], strlen(argv[1])) % BUCKETS);
return 0;
}
@matsumotory
Copy link
Author

https://twitter.com/kazuho/status/715280296489275392
Kazuho Oku @kazuho
31=英語のアルファベットの種類より大きく、最小のコスト(ビットシフトと加算1回)で乗算できる素数 RT @matsumotory: 緩募:このキーオフセットの決め方の意味 / “create_key.c · GitHub” http://htn.to/aS71tSSnXJu
Today at 05:51

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment