This commit is contained in:
krahets
2024-04-09 20:43:40 +08:00
parent d8caf02e9e
commit a6adc8e20a
48 changed files with 1599 additions and 571 deletions

View File

@ -1114,17 +1114,14 @@ comments: true
// 遍历桶,若遇到指定 key ,则更新对应 val 并返回
for pair in bucket {
if pair.key == key {
pair.val = val.clone();
pair.val = val;
return;
}
}
let bucket = &mut self.buckets[index];
// 若无该 key ,则将键值对添加至尾部
let pair = Pair {
key,
val: val.clone(),
};
let pair = Pair { key, val };
bucket.push(pair);
self.size += 1;
}
@ -1328,7 +1325,7 @@ comments: true
capacity = 4
loadThres = 2.0 / 3.0
extendRatio = 2
buckets = ArrayList(capacity)
buckets = mutableListOf()
for (i in 0..<capacity) {
buckets.add(mutableListOf())
}
@ -2960,16 +2957,21 @@ comments: true
```kotlin title="hash_map_open_addressing.kt"
/* 开放寻址哈希表 */
class HashMapOpenAddressing {
private var size: Int = 0 // 键值对数量
private var capacity = 4 // 哈希表容量
private val loadThres: Double = 2.0 / 3.0 // 触发扩容的负载因子阈值
private val extendRatio = 2 // 扩容倍数
private var buckets: Array<Pair?> // 桶数组
private val TOMBSTONE = Pair(-1, "-1") // 删除标记
private var size: Int // 键值对数量
private var capacity: Int // 哈希表容量
private val loadThres: Double // 触发扩容的负载因子阈值
private val extendRatio: Int // 扩容倍数
private var buckets: Array<Pair?> // 桶数组
private val TOMBSTONE: Pair // 删除标记
/* 构造方法 */
init {
size = 0
capacity = 4
loadThres = 2.0 / 3.0
extendRatio = 2
buckets = arrayOfNulls(capacity)
TOMBSTONE = Pair(-1, "-1")
}
/* 哈希函数 */

View File

@ -1589,15 +1589,9 @@ index = hash(key) % capacity
/* 基于数组实现的哈希表 */
class ArrayHashMap {
// 初始化数组,包含 100 个桶
private val buckets = arrayOfNulls<Pair>(100)
init {
// 初始化数组,包含 100 个桶
for (i in 0..<100) {
buckets[i] = null
}
}
/* 哈希函数 */
fun hashFunc(key: Int): Int {
val index = key % 100
@ -1627,25 +1621,27 @@ index = hash(key) % capacity
/* 获取所有键值对 */
fun pairSet(): MutableList<Pair> {
val pairSet = ArrayList<Pair>()
val pairSet = mutableListOf<Pair>()
for (pair in buckets) {
if (pair != null) pairSet.add(pair)
if (pair != null)
pairSet.add(pair)
}
return pairSet
}
/* 获取所有键 */
fun keySet(): MutableList<Int> {
val keySet = ArrayList<Int>()
val keySet = mutableListOf<Int>()
for (pair in buckets) {
if (pair != null) keySet.add(pair.key)
if (pair != null)
keySet.add(pair.key)
}
return keySet
}
/* 获取所有值 */
fun valueSet(): MutableList<String> {
val valueSet = ArrayList<String>()
val valueSet = mutableListOf<String>()
for (pair in buckets) {
pair?.let { valueSet.add(it.value) }
}
@ -1657,22 +1653,16 @@ index = hash(key) % capacity
for (kv in pairSet()) {
val key = kv.key
val value = kv.value
println("${key}->${value}")
println("${key} -> ${value}")
}
}
}
/* 基于数组实现的哈希表 */
class ArrayHashMap {
// 初始化数组,包含 100 个桶
private val buckets = arrayOfNulls<Pair>(100)
init {
// 初始化数组,包含 100 个桶
for (i in 0..<100) {
buckets[i] = null
}
}
/* 哈希函数 */
fun hashFunc(key: Int): Int {
val index = key % 100
@ -1702,25 +1692,27 @@ index = hash(key) % capacity
/* 获取所有键值对 */
fun pairSet(): MutableList<Pair> {
val pairSet = ArrayList<Pair>()
val pairSet = mutableListOf<Pair>()
for (pair in buckets) {
if (pair != null) pairSet.add(pair)
if (pair != null)
pairSet.add(pair)
}
return pairSet
}
/* 获取所有键 */
fun keySet(): MutableList<Int> {
val keySet = ArrayList<Int>()
val keySet = mutableListOf<Int>()
for (pair in buckets) {
if (pair != null) keySet.add(pair.key)
if (pair != null)
keySet.add(pair.key)
}
return keySet
}
/* 获取所有值 */
fun valueSet(): MutableList<String> {
val valueSet = ArrayList<String>()
val valueSet = mutableListOf<String>()
for (pair in buckets) {
pair?.let { valueSet.add(it.value) }
}
@ -1732,7 +1724,7 @@ index = hash(key) % capacity
for (kv in pairSet()) {
val key = kv.key
val value = kv.value
println("${key}->${value}")
println("${key} -> ${value}")
}
}
}