Key를 받아서 이를 해쉬 함수를 적용함.
코틀린 문서에는 HashMap의 put, get연산에 hash함수를 적용하는데, Key값의 hashCode를 가지고
새로운 해쉬 값을 만듦. -> 충돌 방지인듯.
해쉬 함수의 결과 hashCode(해쉬 값)을 가지고 저장될 위치를 결정함.
https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&fname=https://blog.kakaocdn.net/dn/bTF67c/btqL7xx3OGw/DM8KEKU5x7dx6Nks4JR7K1/img.png
internal fun addKey(key: K): Int {
checkIsMutable()
retry@ while (true) {
var hash = hash(key)
// put is allowed to grow maxProbeDistance with some limits (resize hash on reaching limits)
val tentativeMaxProbeDistance = (maxProbeDistance * 2).coerceAtMost(hashSize / 2)
var probeDistance = 0
while (true) {
val index = hashArray[hash]
if (index <= 0) { // claim or reuse hash slot
if (length >= capacity) {
ensureExtraCapacity(1)
continue@retry
}
val putIndex = length++
keysArray[putIndex] = key
presenceArray[putIndex] = hash
hashArray[hash] = putIndex + 1
_size++
if (probeDistance > maxProbeDistance) maxProbeDistance = probeDistance
return putIndex
}
if (keysArray[index - 1] == key) {
return -index
}
if (++probeDistance > tentativeMaxProbeDistance) {
rehash(hashSize * 2) // cannot find room even with extra "tentativeMaxProbeDistance" -- grow hash
continue@retry
}
if (hash-- == 0) hash = hashSize - 1
}
}
}
index <= 0
), 새로운 키를 추가하고, 해당 인덱스에 대한 정보도 업데이트 함.