ary*_*axt 6 hashtable hashcode swift
我正在尝试在Swift中实现HashTable.根据我的理解,哈希值被用作数组中使用的索引.问题是哈希值是非常大的数字,例如.
"1" => 4,799,450,059,485,597,623
"2" => 4,799,450,059,485,597,624
"3" => 4,799,450,059,485,597,629
Run Code Online (Sandbox Code Playgroud)
使用这些哈希值生成数组索引的正确方法是什么?
class HashTable <K: Hashable, V> {
private var values : [V?]
init(size: Int) {
values = [V?](count: size, repeatedValue: nil)
}
func push(key: K, value: V?) {
values[key.hashValue] = value
}
subscript (key: K) -> V? {
get {
return values[key.hashValue]
}
set {
push(key, value: newValue)
}
}
}
Run Code Online (Sandbox Code Playgroud)
我最终将 LinkedNodes 存储在数组中而不是值中。
hashIndex = hashValue % values.count
Run Code Online (Sandbox Code Playgroud)
当搜索或删除LinkedList中有多个节点时,我直接比较hashValues而不是hashIndex。(处理碰撞)
想知道是否有更好的解决方案