hash 함수

최대 1 분 소요

해시 함수란?

해시 함수(hash function)는 임의의 길이의 데이터를 고정된 길이의 데이터로 매핑하는 함수이다.

SW expert academy의 reference code의 해시 함수 일부

unsigned long hash(const char *str)
{
    unsigned long hash = 5381;
    int c;
 
    while (c = *str++)
    {
        hash = (((hash << 5) + hash) + c) % MAX_TABLE;
    }
 
    return hash % MAX_TABLE;
}

djb2 알고리즘이라고한다. 5381의 매직넘버가 쓰이는 이유는 다음과 같다.

5381 is just a number that, in testing, resulted in fewer collisions and better avalanching. You’ll find “magic constants” in just about every hash algo.

참조

https://swexpertacademy.com/main/code/referenceCode/referenceCodeDetail.do?referenceId=HASH&category=undefined http://www.cse.yorku.ca/~oz/hash.html https://stackoverrun.com/ko/q/2833167