Skip to content

Instantly share code, notes, and snippets.

@joonas-yoon
Last active February 21, 2020 11:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joonas-yoon/df146af030aefdea1646c24df818b025 to your computer and use it in GitHub Desktop.
Save joonas-yoon/df146af030aefdea1646c24df818b025 to your computer and use it in GitHub Desktop.
hashing string to unique integer + padding (it can be compare)
#define MAX_LENGTH 13
unsigned long long MAX_LENGTH_VALUE; // 꽤 큰 값은 리터럴 상수 선언이 안되는 것 같다.
unsigned long long str2llu(const char s[MAX_LENGTH + 1]) {
unsigned long long r = 0, x = MAX_LENGTH_VALUE;
for (int i = 0; s[i]; ++i) {
r += x * (s[i] - 'a' + 1LLU);
x /= 27;
}
return r;
}
int main(){
// 27 ^ (MAX_LENGTH - 1) 으로 만든다.
// 참고로 27^12 = 150,094,635,296,999,121 이다.
MAX_LENGTH_VALUE = 1;
for (int i = 0; i < MAX_LENGTH - 1; ++i) MAX_LENGTH_VALUE *= 27LLU;
assert(str2llu("aaa") < str2llu("ab")); // it should be true
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment