diff --git a/codes/dart/chapter_hashing/built_in_hash.dart b/codes/dart/chapter_hashing/built_in_hash.dart new file mode 100644 index 00000000..70fba328 --- /dev/null +++ b/codes/dart/chapter_hashing/built_in_hash.dart @@ -0,0 +1,34 @@ +/** + * File: built_in_hash.dart + * Created Time: 2023-06-25 + * Author: liuyuxin (gvenusleo@gmail.com) + */ + +import '../chapter_stack_and_queue/linkedlist_deque.dart'; + +/* Driver Code */ +void main() { + int num = 3; + int hashNum = num.hashCode; + print("整数 $num 的哈希值为 $hashNum"); + + bool bol = true; + int hashBol = bol.hashCode; + print("布尔值 $bol 的哈希值为 $hashBol"); + + double dec = 3.14159; + int hashDec = dec.hashCode; + print("小数 $dec 的哈希值为 $hashDec"); + + String str = "Hello 算法"; + int hashStr = str.hashCode; + print("字符串 $str 的哈希值为 $hashStr"); + + List arr = [12836, "小哈"]; + int hashArr = arr.hashCode; + print("数组 $arr 的哈希值为 $hashArr"); + + ListNode obj = new ListNode(0); + int hashObj = obj.hashCode; + print("节点对象 $obj 的哈希值为 $hashObj"); +} diff --git a/codes/dart/chapter_hashing/hash_map_chaining.dart b/codes/dart/chapter_hashing/hash_map_chaining.dart new file mode 100644 index 00000000..91649d5d --- /dev/null +++ b/codes/dart/chapter_hashing/hash_map_chaining.dart @@ -0,0 +1,138 @@ +/** + * File: hash_map_chaining.dart + * Created Time: 2023-06-24 + * Author: liuyuxin (gvenusleo@gmail.com) + */ + +/* 键值对 */ +class Pair { + int key; + String val; + Pair(this.key, this.val); +} + +/* 链式地址哈希表 */ +class HashMapChaining { + late int size; // 键值对数量 + late int capacity; // 哈希表容量 + late double loadThres; // 触发扩容的负载因子阈值 + late int extendRatio; // 扩容倍数 + late List> buckets; // 桶数组 + + /* 构造方法 */ + HashMapChaining() { + size = 0; + capacity = 4; + loadThres = 2 / 3.0; + extendRatio = 2; + buckets = List.generate(capacity, (_) => []); + } + + /* 哈希函数 */ + int hashFunc(int key) { + return key % capacity; + } + + /* 负载因子 */ + double loadFactor() { + return size / capacity; + } + + /* 查询操作 */ + String? get(int key) { + int index = hashFunc(key); + List bucket = buckets[index]; + // 遍历桶,若找到 key 则返回对应 val + for (Pair pair in bucket) { + if (pair.key == key) { + return pair.val; + } + } + // 若未找到 key 则返回 null + return null; + } + + /* 添加操作 */ + void put(int key, String val) { + // 当负载因子超过阈值时,执行扩容 + if (loadFactor() > loadThres) { + extend(); + } + int index = hashFunc(key); + List bucket = buckets[index]; + // 遍历桶,若遇到指定 key ,则更新对应 val 并返回 + for (Pair pair in bucket) { + if (pair.key == key) { + pair.val = val; + return; + } + } + // 若无该 key ,则将键值对添加至尾部 + Pair pair = Pair(key, val); + bucket.add(pair); + size++; + } + + /* 删除操作 */ + void remove(int key) { + int index = hashFunc(key); + List bucket = buckets[index]; + // 遍历桶,从中删除键值对 + bucket.removeWhere((Pair pair) => pair.key == key); + size--; + } + + /* 扩容哈希表 */ + void extend() { + // 暂存原哈希表 + List> bucketsTmp = buckets; + // 初始化扩容后的新哈希表 + capacity *= extendRatio; + buckets = List.generate(capacity, (_) => []); + size = 0; + // 将键值对从原哈希表搬运至新哈希表 + for (List bucket in bucketsTmp) { + for (Pair pair in bucket) { + put(pair.key, pair.val); + } + } + } + + /* 打印哈希表 */ + void printHashMap() { + for (List bucket in buckets) { + List res = []; + for (Pair pair in bucket) { + res.add("${pair.key} -> ${pair.val}"); + } + print(res); + } + } +} + +/* Driver Code */ +void main() { + /* 初始化哈希表 */ + HashMapChaining map = HashMapChaining(); + + /* 添加操作 */ + // 在哈希表中添加键值对 (key, value) + map.put(12836, "小哈"); + map.put(15937, "小啰"); + map.put(16750, "小算"); + map.put(13276, "小法"); + map.put(10583, "小鸭"); + print("\n添加完成后,哈希表为\nKey -> Value"); + map.printHashMap(); + + /* 查询操作 */ + // 向哈希表输入键 key ,得到值 value + String? name = map.get(13276); + print("\n输入学号 13276 ,查询到姓名 ${name}"); + + /* 删除操作 */ + // 在哈希表中删除键值对 (key, value) + map.remove(12836); + print("\n删除 12836 后,哈希表为\nKey -> Value"); + map.printHashMap(); +} diff --git a/codes/dart/chapter_hashing/hash_map_open_addressing.dart b/codes/dart/chapter_hashing/hash_map_open_addressing.dart new file mode 100644 index 00000000..eb3cbbb1 --- /dev/null +++ b/codes/dart/chapter_hashing/hash_map_open_addressing.dart @@ -0,0 +1,157 @@ +/** + * File: hash_map_open_addressing.dart + * Created Time: 2023-06-25 + * Author: liuyuxin (gvenusleo@gmail.com) + */ + +/* 键值对 */ +class Pair { + int key; + String val; + Pair(this.key, this.val); +} + +/* 开放寻址哈希表 */ +class HashMapOpenAddressing { + late int _size; // 键值对数量 + late int _capacity; // 哈希表容量 + late double _loadThres; // 触发扩容的负载因子阈值 + late int _extendRatio; // 扩容倍数 + late List _buckets; // 桶数组 + late Pair _removed; // 删除标记 + + /* 构造方法 */ + HashMapOpenAddressing() { + _size = 0; + _capacity = 4; + _loadThres = 2.0 / 3.0; + _extendRatio = 2; + _buckets = List.generate(_capacity, (index) => null); + _removed = Pair(-1, "-1"); + } + + /* 哈希函数 */ + int hashFunc(int key) { + return key % _capacity; + } + + /* 负载因子 */ + double loadFactor() { + return _size / _capacity; + } + + /* 查询操作 */ + String? get(int key) { + int index = hashFunc(key); + // 线性探测,从 index 开始向后遍历 + for (int i = 0; i < _capacity; i++) { + // 计算桶索引,越过尾部返回头部 + int j = (index + i) % _capacity; + // 若遇到空桶,说明无此 key ,则返回 null + if (_buckets[j] == null) return null; + // 若遇到指定 key ,则返回对应 val + if (_buckets[j]!.key == key && _buckets[j] != _removed) + return _buckets[j]!.val; + } + return null; + } + + /* 添加操作 */ + void put(int key, String val) { + // 当负载因子超过阈值时,执行扩容 + if (loadFactor() > _loadThres) { + extend(); + } + int index = hashFunc(key); + // 线性探测,从 index 开始向后遍历 + for (int i = 0; i < _capacity; i++) { + // 计算桶索引,越过尾部返回头部 + int j = (index + i) % _capacity; + // 若遇到空桶、或带有删除标记的桶,则将键值对放入该桶 + if (_buckets[j] == null || _buckets[j] == _removed) { + _buckets[j] = new Pair(key, val); + _size += 1; + return; + } + // 若遇到指定 key ,则更新对应 val + if (_buckets[j]!.key == key) { + _buckets[j]!.val = val; + return; + } + } + } + + /* 删除操作 */ + void remove(int key) { + int index = hashFunc(key); + // 线性探测,从 index 开始向后遍历 + for (int i = 0; i < _capacity; i++) { + // 计算桶索引,越过尾部返回头部 + int j = (index + i) % _capacity; + // 若遇到空桶,说明无此 key ,则直接返回 + if (_buckets[j] == null) { + return; + } + // 若遇到指定 key ,则标记删除并返回 + if (_buckets[j]!.key == key) { + _buckets[j] = _removed; + _size -= 1; + return; + } + } + } + + /* 扩容哈希表 */ + void extend() { + // 暂存原哈希表 + List bucketsTmp = _buckets; + // 初始化扩容后的新哈希表 + _capacity *= _extendRatio; + _buckets = List.generate(_capacity, (index) => null); + _size = 0; + // 将键值对从原哈希表搬运至新哈希表 + for (Pair? pair in bucketsTmp) { + if (pair != null && pair != _removed) { + put(pair.key, pair.val); + } + } + } + + /* 打印哈希表 */ + void printHashMap() { + for (Pair? pair in _buckets) { + if (pair != null) { + print("${pair.key} -> ${pair.val}"); + } else { + print(null); + } + } + } +} + +/* Driver Code */ +void main() { + /* 初始化哈希表 */ + HashMapOpenAddressing map = HashMapOpenAddressing(); + + /* 添加操作 */ + // 在哈希表中添加键值对 (key, value) + map.put(12836, "小哈"); + map.put(15937, "小啰"); + map.put(16750, "小算"); + map.put(13276, "小法"); + map.put(10583, "小鸭"); + print("\n添加完成后,哈希表为\nKey -> Value"); + map.printHashMap(); + + /* 查询操作 */ + // 向哈希表输入键 key ,得到值 value + String? name = map.get(13276); + print("\n输入学号 13276 ,查询到姓名 $name"); + + /* 删除操作 */ + // 在哈希表中删除键值对 (key, value) + map.remove(16750); + print("\n删除 16750 后,哈希表为\nKey -> Value"); + map.printHashMap(); +} diff --git a/codes/dart/chapter_hashing/simple_hash.dart b/codes/dart/chapter_hashing/simple_hash.dart new file mode 100644 index 00000000..3c928417 --- /dev/null +++ b/codes/dart/chapter_hashing/simple_hash.dart @@ -0,0 +1,62 @@ +/** + * File: simple_hash.dart + * Created Time: 2023-06-25 + * Author: liuyuxin (gvenusleo@gmail.com) + */ + +/* 加法哈希 */ +int addHash(String key) { + int hash = 0; + final int MODULUS = 1000000007; + for (int i = 0; i < key.length; i++) { + hash = (hash + key.codeUnitAt(i)) % MODULUS; + } + return hash; +} + +/* 乘法哈希 */ +int mulHash(String key) { + int hash = 0; + final int MODULUS = 1000000007; + for (int i = 0; i < key.length; i++) { + hash = (31 * hash + key.codeUnitAt(i)) % MODULUS; + } + return hash; +} + +/* 异或哈希 */ +int xorHash(String key) { + int hash = 0; + final int MODULUS = 1000000007; + for (int i = 0; i < key.length; i++) { + hash ^= key.codeUnitAt(i); + } + return hash & MODULUS; +} + +/* 旋转哈希 */ +int rotHash(String key) { + int hash = 0; + final int MODULUS = 1000000007; + for (int i = 0; i < key.length; i++) { + hash = ((hash << 4) ^ (hash >> 28) ^ key.codeUnitAt(i)) % MODULUS; + } + return hash; +} + +/* Dirver Code */ +void main() { + String key = "Hello 算法"; + + int hash = addHash(key); + print("加法哈希值为 $hash"); + + hash = mulHash(key); + print("乘法哈希值为 $hash"); + + hash = xorHash(key); + print("异或哈希值为 $hash"); + + hash = rotHash(key); + print("旋转哈希值为 $hash"); +} diff --git a/codes/java/chapter_hashing/hash_map_open_addressing.java b/codes/java/chapter_hashing/hash_map_open_addressing.java index 2116bfb9..d63eff6d 100644 --- a/codes/java/chapter_hashing/hash_map_open_addressing.java +++ b/codes/java/chapter_hashing/hash_map_open_addressing.java @@ -1,5 +1,5 @@ /** - * File: hash_map_chaining.java + * File: hash_map_open_addressing.java * Created Time: 2023-06-13 * Author: Krahets (krahets@163.com) */ diff --git a/codes/java/chapter_hashing/simple_hash.java b/codes/java/chapter_hashing/simple_hash.java index c2f39708..ccd77cd2 100644 --- a/codes/java/chapter_hashing/simple_hash.java +++ b/codes/java/chapter_hashing/simple_hash.java @@ -32,7 +32,6 @@ public class simple_hash { int hash = 0; final int MODULUS = 1000000007; for (char c : key.toCharArray()) { - System.out.println((int)c); hash ^= (int) c; } return hash & MODULUS; diff --git a/docs/chapter_hashing/hash_algorithm.md b/docs/chapter_hashing/hash_algorithm.md index 295d4425..074023ea 100644 --- a/docs/chapter_hashing/hash_algorithm.md +++ b/docs/chapter_hashing/hash_algorithm.md @@ -356,7 +356,29 @@ $$ === "Dart" ```dart title="built_in_hash.dart" - + int num = 3; + int hashNum = num.hashCode; + // 整数 3 的哈希值为 34803 + + bool bol = true; + int hashBol = bol.hashCode; + // 布尔值 true 的哈希值为 1231 + + double dec = 3.14159; + int hashDec = dec.hashCode; + // 小数 3.14159 的哈希值为 2570631074981783 + + String str = "Hello 算法"; + int hashStr = str.hashCode; + // 字符串 Hello 算法 的哈希值为 468167534 + + List arr = [12836, "小哈"]; + int hashArr = arr.hashCode; + // 数组 [12836, 小哈] 的哈希值为 976512528 + + ListNode obj = new ListNode(0); + int hashObj = obj.hashCode; + // 节点对象 Instance of 'ListNode' 的哈希值为 1033450432 ``` 在大多数编程语言中,**只有不可变对象才可作为哈希表的 `key`** 。假如我们将列表(动态数组)作为 `key` ,当列表的内容发生变化时,它的哈希值也随之改变,我们就无法在哈希表中查询到原先的 `value` 了。