From aeb4e6077d30adec964f1e4cd3871f966b9332fd Mon Sep 17 00:00:00 2001 From: machangxin Date: Wed, 14 Dec 2022 17:18:32 +0800 Subject: [PATCH 1/5] Add C++, Python, Go code for chapter_hashing --- codes/cpp/chapter_hashing/array_hash_map.cpp | 139 +++++++++++ codes/cpp/chapter_hashing/hash_map.cpp | 52 ++++ codes/cpp/include/PrintUtil.hpp | 17 +- codes/go/chapter_hashing/array_hash_map.go | 96 ++++++++ .../go/chapter_hashing/array_hash_map_test.go | 52 ++++ codes/go/chapter_hashing/hash_map_test.go | 57 +++++ .../java/chapter_hashing/array_hash_map.java | 2 +- .../python/chapter_hashing/array_hash_map.py | 112 +++++++++ codes/python/chapter_hashing/hash_map.py | 46 ++++ docs/chapter_hashing/hash_map.md | 227 +++++++++++++++++- 10 files changed, 792 insertions(+), 8 deletions(-) create mode 100644 codes/cpp/chapter_hashing/array_hash_map.cpp create mode 100644 codes/cpp/chapter_hashing/hash_map.cpp create mode 100644 codes/go/chapter_hashing/array_hash_map.go create mode 100644 codes/go/chapter_hashing/array_hash_map_test.go create mode 100644 codes/go/chapter_hashing/hash_map_test.go create mode 100644 codes/python/chapter_hashing/array_hash_map.py create mode 100644 codes/python/chapter_hashing/hash_map.py diff --git a/codes/cpp/chapter_hashing/array_hash_map.cpp b/codes/cpp/chapter_hashing/array_hash_map.cpp new file mode 100644 index 00000000..bca27ca7 --- /dev/null +++ b/codes/cpp/chapter_hashing/array_hash_map.cpp @@ -0,0 +1,139 @@ +/* + * File: array_hash_map.cpp + * Created Time: 2022-12-14 + * Author: msk397 (machangxinq@gmail.com) + */ + +#include "../include/include.hpp" + +/* 键值对 int->String */ +struct Entry { +public: + int key; + string val; + Entry(int key, string val) { + this->key = key; + this->val = val; + } + Entry()=default; +}; + +/* 基于数组简易实现的哈希表 */ +class ArrayHashMap { +private: + vector bucket; +public: + ArrayHashMap() { + // 初始化一个长度为 100 的桶(数组) + bucket= vector(100); + } + + /* 哈希函数 */ + int hashFunc(int key) { + int index = key % 100; + return index; + } + + /* 查询操作 */ + string get(int key) { + int index = hashFunc(key); + Entry pair = bucket[index]; + return pair.val; + } + + /* 添加操作 */ + void put(int key, string val) { + Entry pair = Entry(key, val); + int index = hashFunc(key); + bucket[index] = pair; + } + + /* 删除操作 */ + void remove(int key) { + int index = hashFunc(key); + // 置为空字符,代表删除 + bucket[index] = *new Entry(); + } + + /* 获取所有键值对 */ + vector entrySet() { + vector entrySet; + for (Entry pair: bucket) { + if (pair.key != 0) { + entrySet.push_back(pair); + } + } + return entrySet; + } + + /* 获取所有键 */ + vector keySet() { + vector keySet; + for (Entry pair: bucket) { + if (pair.key != 0) { + keySet.push_back(pair.key); + } + } + return keySet; + } + + /* 获取所有值 */ + vector valueSet() { + vector valueSet; + for (Entry pair: bucket) { + if (pair.key != 0) + valueSet.push_back(pair.val); + } + return valueSet; + } + + /* 打印哈希表 */ + void print() { + for (Entry kv: entrySet()) { + cout << kv.key << " -> " << kv.val << endl; + } + } +}; +int main() { + /* 初始化哈希表 */ + ArrayHashMap map = ArrayHashMap(); + + /* 添加操作 */ + // 在哈希表中添加键值对 (key, value) + map.put(12836, "小哈"); + map.put(15937, "小啰"); + map.put(16750, "小算"); + map.put(13276, "小法"); + map.put(10583, "小鸭"); + cout << "\n添加完成后,哈希表为\nKey -> Value" << endl; + map.print(); + + /* 查询操作 */ + // 向哈希表输入键 key ,得到值 value + string name = map.get(15937); + cout << "\n输入学号 15937 ,查询到姓名 " << name << endl; + + /* 删除操作 */ + // 在哈希表中删除键值对 (key, value) + map.remove(10583); + cout << "\n删除 10583 后,哈希表为\nKey -> Value" << endl; + map.print(); + + /* 遍历哈希表 */ + cout << "\n遍历键值对 Key->Value" << endl; + for (auto kv: map.entrySet()) { + cout << kv.key << " -> " << kv.val << endl; + } + + cout << "\n单独遍历键 Key" << endl; + for (auto key: map.keySet()) { + cout << key << endl; + } + + cout << "\n单独遍历值 Value" << endl; + for (auto val: map.valueSet()) { + cout << val << endl; + } + + return 0; +} \ No newline at end of file diff --git a/codes/cpp/chapter_hashing/hash_map.cpp b/codes/cpp/chapter_hashing/hash_map.cpp new file mode 100644 index 00000000..6c8df4b8 --- /dev/null +++ b/codes/cpp/chapter_hashing/hash_map.cpp @@ -0,0 +1,52 @@ +/* + * File: hash_map.cpp + * Created Time: 2022-12-14 + * Author: msk397 (machangxinq@gmail.com) + */ + +#include "../include/include.hpp" + + +int main() { + /* 初始化哈希表 */ + unordered_map map; + + /* 添加操作 */ + // 在哈希表中添加键值对 (key, value) + map[12836] = "小哈"; + map[15937] = "小啰"; + map[16750] = "小算"; + map[13276] = "小法"; + map[10583] = "小鸭"; + cout << "\n添加完成后,哈希表为\nKey -> Value" << endl; + PrintUtil::printHashMap(map); + + /* 查询操作 */ + // 向哈希表输入键 key ,得到值 value + string name = map[15937]; + cout << "\n输入学号 15937 ,查询到姓名 " << name << endl; + + /* 删除操作 */ + // 在哈希表中删除键值对 (key, value) + map.erase(10583); + cout << "\n删除 10583 后,哈希表为\nKey -> Value" << endl; + PrintUtil::printHashMap(map); + + /* 遍历哈希表 */ + cout << "\n遍历键值对 Key->Value" << endl; + for (auto kv: map) { + cout << kv.first << " -> " << kv.second << endl; + } + + cout << "\n单独遍历键 Key" << endl; + for (auto key: map) { + cout << key.first << endl; + } + + cout << "\n单独遍历值 Value" << endl; + for (auto val: map) { + cout << val.second << endl; + } + + return 0; +} \ No newline at end of file diff --git a/codes/cpp/include/PrintUtil.hpp b/codes/cpp/include/PrintUtil.hpp index 772eb9b2..544ee85d 100644 --- a/codes/cpp/include/PrintUtil.hpp +++ b/codes/cpp/include/PrintUtil.hpp @@ -1,7 +1,7 @@ /* * File: PrintUtil.hpp * Created Time: 2021-12-19 - * Author: Krahets (krahets@163.com) + * Author: Krahets (krahets@163.com), msk397 (machangxinq@gmail.com) */ #pragma once @@ -277,4 +277,19 @@ class PrintUtil { } cout << "[" + s.str() + "]" << '\n'; } + + /** + * @brief Print a HashMap + * + * @tparam TKey + * @tparam TValue + * @param map + */ + // 定义模板参数 TKey 和 TValue,用于指定键值对的类型 + template + static void printHashMap(unordered_map map) { + for (auto kv : map) { + cout << kv.first << " -> " << kv.second << '\n'; + } + } }; diff --git a/codes/go/chapter_hashing/array_hash_map.go b/codes/go/chapter_hashing/array_hash_map.go new file mode 100644 index 00000000..c895ef84 --- /dev/null +++ b/codes/go/chapter_hashing/array_hash_map.go @@ -0,0 +1,96 @@ +// File: array_hash_map.go +// Created Time: 2022-12-14 +// Author: msk397 (machangxinq@gmail.com) + +package chapter_hashing + +import "fmt" + +// 键值对 int->String +type Entry struct { + key int + val string +} + +// 基于数组简易实现的哈希表 +type ArrayHashMap struct { + bucket []Entry +} + +func newArrayHashMap() *ArrayHashMap { + // 初始化一个长度为 100 的桶(数组) + bucket := make([]Entry, 100) + return &ArrayHashMap{bucket: bucket} +} + +// 哈希函数 +func (a *ArrayHashMap) hashFunc(key int) int { + index := key % 100 + return index +} + +// 查询操作 +func (a *ArrayHashMap) get(key int) string { + index := a.hashFunc(key) + pair := a.bucket[index] + if pair.key == 0 { + return "" + } + return pair.val +} + +// 添加操作 +func (a *ArrayHashMap) put(key int, val string) { + pair := Entry{key: key, val: val} + index := a.hashFunc(key) + a.bucket[index] = pair +} + +// 删除操作 +func (a *ArrayHashMap) remove(key int) { + index := a.hashFunc(key) + // 置为空字符,代表删除 + a.bucket[index] = Entry{} +} + +// 获取所有键对 +func (a *ArrayHashMap) entrySet() []Entry { + var pairs []Entry + for _, pair := range a.bucket { + if pair.key != 0 { + pairs = append(pairs, pair) + } + } + return pairs +} + +// 获取所有键 +func (a *ArrayHashMap) keySet() []int { + var keys []int + for _, pair := range a.bucket { + if pair.key != 0 { + keys = append(keys, pair.key) + } + } + return keys +} + +// 获取所有值 +func (a *ArrayHashMap) valueSet() []string { + var values []string + for _, pair := range a.bucket { + if pair.key != 0 { + values = append(values, pair.val) + } + } + return values +} + +// 打印哈希表 +func (a *ArrayHashMap) print() { + for _, pair := range a.bucket { + if pair.key != 0 { + fmt.Println(pair.key, "->", pair.val) + } + } +} diff --git a/codes/go/chapter_hashing/array_hash_map_test.go b/codes/go/chapter_hashing/array_hash_map_test.go new file mode 100644 index 00000000..218fd623 --- /dev/null +++ b/codes/go/chapter_hashing/array_hash_map_test.go @@ -0,0 +1,52 @@ +// File: array_hash_map_test.go +// Created Time: 2022-12-14 +// Author: msk397 (machangxinq@gmail.com) + +package chapter_hashing + +import ( + "fmt" + "testing" +) + +func TestArrayHashMap(t *testing.T) { + /* 初始化哈希表 */ + map1 := newArrayHashMap() + + /* 添加操作 */ + // 在哈希表中添加键值对 (key, value) + map1.put(12836, "小哈") + map1.put(15937, "小啰") + map1.put(16750, "小算") + map1.put(13276, "小法") + map1.put(10583, "小鸭") + fmt.Println("\n添加完成后,哈希表为\nKey -> Value") + map1.print() + + /* 查询操作 */ + // 向哈希表输入键 key ,得到值 value + name := map1.get(15937) + fmt.Println("\n输入学号 15937 ,查询到姓名 " + name) + + /* 删除操作 */ + // 在哈希表中删除键值对 (key, value) + map1.remove(10583) + fmt.Println("\n删除 10583 后,哈希表为\nKey -> Value") + map1.print() + + /* 遍历哈希表 */ + fmt.Println("\n遍历键值对 Key->Value") + for _, kv := range map1.entrySet() { + fmt.Println(kv.key, " -> ", kv.val) + } + + fmt.Println("\n单独遍历键 Key") + for _, key := range map1.keySet() { + fmt.Println(key) + } + + fmt.Println("\n单独遍历值 Value") + for _, val := range map1.valueSet() { + fmt.Println(val) + } +} diff --git a/codes/go/chapter_hashing/hash_map_test.go b/codes/go/chapter_hashing/hash_map_test.go new file mode 100644 index 00000000..ee768336 --- /dev/null +++ b/codes/go/chapter_hashing/hash_map_test.go @@ -0,0 +1,57 @@ +// File: hash_map_test.go +// Created Time: 2022-12-14 +// Author: msk397 (machangxinq@gmail.com) + +package chapter_hashing + +import ( + "fmt" + "testing" +) + +func TestHashmap(t *testing.T) { + // 初始化哈希表 + Map := make(map[int]string) + + // 添加操作 + // 在哈希表中添加键值对 (key, value) + Map[12836] = "小哈" + Map[15937] = "小啰" + Map[16750] = "小算" + Map[13276] = "小法" + Map[10583] = "小鸭" + fmt.Println("\n添加完成后,哈希表为\nKey -> Value") + for key, value := range Map { + fmt.Printf("%d -> %s\n", key, value) + } + + // 查询操作 + // 向哈希表输入键 key ,得到值 value + name := Map[15937] + fmt.Println("\n输入学号 15937 ,查询到姓名 ", name) + + // 删除操作 + // 在哈希表中删除键值对 (key, value) + delete(Map, 10583) + fmt.Println("\n删除 10583 后,哈希表为\nKey -> Value") + for key, value := range Map { + fmt.Printf("%d -> %s\n", key, value) + } + + /* 遍历哈希表 */ + // 遍历键值对 key->value + fmt.Println("\n遍历键值对 Key->Value") + for key, value := range Map { + fmt.Println(key, "->", value) + } + // 单独遍历键 key + fmt.Println("\n单独遍历键 Key") + for key := range Map { + fmt.Println(key) + } + // 单独遍历值 value + fmt.Println("\n单独遍历值 Value") + for _, value := range Map { + fmt.Println(value) + } +} diff --git a/codes/java/chapter_hashing/array_hash_map.java b/codes/java/chapter_hashing/array_hash_map.java index 89082c2f..12f23d5b 100644 --- a/codes/java/chapter_hashing/array_hash_map.java +++ b/codes/java/chapter_hashing/array_hash_map.java @@ -21,7 +21,7 @@ class Entry { class ArrayHashMap { private List bucket; public ArrayHashMap() { - // 初始化一个长度为 10 的桶(数组) + // 初始化一个长度为 100 的桶(数组) bucket = new ArrayList<>(); for (int i = 0; i < 100; i++) { bucket.add(null); diff --git a/codes/python/chapter_hashing/array_hash_map.py b/codes/python/chapter_hashing/array_hash_map.py new file mode 100644 index 00000000..a9fbf58a --- /dev/null +++ b/codes/python/chapter_hashing/array_hash_map.py @@ -0,0 +1,112 @@ +""" +File: array_hash_map.py +Created Time: 2022-12-14 +Author: msk397 (machangxinq@gmail.com) +""" + +# 键值对 int->String +class Entry: + def __init__(self, key, val): + self.key = key + self.val = val + + +# 基于数组简易实现的哈希表 +class ArrayHashMap: + def __init__(self): + # 初始化一个长度为 100 的桶(数组) + self.bucket = [None] * 100 + + # 哈希函数 + def hashFunc(self, key): + index = key % 100 + return index + + # 查询操作 + def get(self, key): + index = self.hashFunc(key) + pair = self.bucket[index] + if pair is None: + return None + return pair.val + + # 添加操作 + def put(self, key, val): + pair = Entry(key, val) + index = self.hashFunc(key) + self.bucket[index] = pair + + # 删除操作 + def remove(self, key): + index = self.hashFunc(key) + # 置为空字符,代表删除 + self.bucket[index] = None + + # 获取所有键值对 + def entrySet(self): + result = [] + for pair in self.bucket: + if pair is not None: + result.append(pair) + return result + + # 获取所有键 + def keySet(self): + result = [] + for pair in self.bucket: + if pair is not None: + result.append(pair.key) + return result + + # 获取所有值 + def valueSet(self): + result = [] + for pair in self.bucket: + if pair is not None: + result.append(pair.val) + return result + + # 打印哈希表 + def print(self): + for pair in self.bucket: + if pair is not None: + print(pair.key, "->", pair.val) + + +if __name__ == "__main__": + """ 初始化哈希表 """ + Map = ArrayHashMap() + + """ 添加操作 """ + # 在哈希表中添加键值对 (key, value) + Map.put(12836, "小哈") + Map.put(15937, "小啰") + Map.put(16750, "小算") + Map.put(13276, "小法") + Map.put(10583, "小鸭") + print("\n添加完成后,哈希表为\nKey -> Value") + Map.print() + + """ 查询操作 """ + # 向哈希表输入键 key ,得到值 value + name = Map.get(15937) + print("\n输入学号 15937 ,查询到姓名 " + name) + + """ 删除操作 """ + # 在哈希表中删除键值对 (key, value) + Map.remove(10583) + print("\n删除 10583 后,哈希表为\nKey -> Value") + Map.print() + + """ 遍历哈希表 """ + print("\n遍历键值对 Key->Value") + for pair in Map.entrySet(): + print(pair.key, "->", pair.val) + + print("\n单独遍历键 Key") + for key in Map.keySet(): + print(key) + + print("\n单独遍历值 Value") + for val in Map.valueSet(): + print(val) diff --git a/codes/python/chapter_hashing/hash_map.py b/codes/python/chapter_hashing/hash_map.py new file mode 100644 index 00000000..98b7dedc --- /dev/null +++ b/codes/python/chapter_hashing/hash_map.py @@ -0,0 +1,46 @@ +""" +File: hash_map.py +Created Time: 2022-12-14 +Author: msk397 (machangxinq@gmail.com) +""" + +if __name__ == "__main__": + """ 初始化哈希表 """ + Map = {} + + """ 添加操作 """ + # 在哈希表中添加键值对 (key, value) + Map[12836] = "小哈" + Map[15937] = "小啰" + Map[16750] = "小算" + Map[13276] = "小法" + Map[10583] = "小鸭" + print("\n添加完成后,哈希表为\nKey -> Value") + for key, value in Map.items(): + print(key, "->", value) + + """ 查询操作 """ + # 向哈希表输入键 key ,得到值 value + name = Map[15937] + print("\n输入学号 15937 ,查询到姓名 " + name) + + """ 删除操作 """ + # 在哈希表中删除键值对 (key, value) + Map.pop(10583) + print("\n删除 10583 后,哈希表为\nKey -> Value") + for key, value in Map.items(): + print(key, "->", value) + + """ 遍历哈希表 """ + print("\n遍历键值对 Key->Value") + for key, value in Map.items(): + print(key, "->", value) + + print("\n单独遍历键 Key") + for key in Map.keys(): + print(key) + + print("\n单独遍历值 Value") + for val in Map.values(): + print(val) + diff --git a/docs/chapter_hashing/hash_map.md b/docs/chapter_hashing/hash_map.md index 793d9032..3ce9c08b 100644 --- a/docs/chapter_hashing/hash_map.md +++ b/docs/chapter_hashing/hash_map.md @@ -63,19 +63,70 @@ comments: true === "C++" ```cpp title="hash_map.cpp" + /* 初始化哈希表 */ + unordered_map map; + /* 添加操作 */ + // 在哈希表中添加键值对 (key, value) + map[12836] = "小哈"; + map[15937] = "小啰"; + map[16750] = "小算"; + map[13276] = "小法"; + map[10583] = "小鸭"; + + /* 查询操作 */ + // 向哈希表输入键 key ,得到值 value + string name = map[15937]; + + /* 删除操作 */ + // 在哈希表中删除键值对 (key, value) + map.erase(10583); ``` === "Python" ```python title="hash_map.py" + # 初始化哈希表 + Map = {} + # 添加操作 + # 在哈希表中添加键值对 (key, value) + Map[12836] = "小哈" + Map[15937] = "小啰" + Map[16750] = "小算" + Map[13276] = "小法" + Map[10583] = "小鸭" + + # 查询操作 + # 向哈希表输入键 key ,得到值 value + name = Map[15937] + + # 删除操作 + # 在哈希表中删除键值对 (key, value) + Map.pop(10583) ``` === "Go" - ```go title="hash_map.go" + ```go title="hash_map_test.go" + // 初始化哈希表 + Map := make(map[int]string) + // 添加操作 + // 在哈希表中添加键值对 (key, value) + Map[12836] = "小哈" + Map[15937] = "小啰" + Map[16750] = "小算" + Map[13276] = "小法" + Map[10583] = "小鸭" + + // 查询操作 + // 向哈希表输入键 key ,得到值 value + name := Map[15937] + + // 删除操作 + // 在哈希表中删除键值对 (key, value) + delete(Map, 10583) ``` === "JavaScript" @@ -125,19 +176,52 @@ comments: true === "C++" ```cpp title="hash_map.cpp" - + /* 遍历哈希表 */ + // 遍历键值对 key->value + for (auto kv: map) { + cout << kv.first << " -> " << kv.second << endl; + } + // 单独遍历键 key + for (auto key: map) { + cout << key.first << endl; + } + // 单独遍历值 value + for (auto val: map) { + cout << val.second << endl; + } ``` === "Python" ```python title="hash_map.py" - + """ 遍历哈希表 """ + # 遍历键值对 key->value + for key, value in Map.items(): + print(key, "->", value) + # 单独遍历键 key + for key in Map.keys(): + print(key) + # 单独遍历值 value + for value in Map.values(): + print(value) ``` === "Go" - ```go title="hash_map.go" - + ```go title="hash_map_test.go" + /* 遍历哈希表 */ + // 遍历键值对 key->value + for key, value := range Map { + fmt.Println(key, "->", value) + } + // 单独遍历键 key + for key := range Map { + fmt.Println(key) + } + // 单独遍历值 value + for _, value := range Map { + fmt.Println(value) + } ``` === "JavaScript" @@ -242,19 +326,150 @@ $$ === "C++" ```cpp title="array_hash_map.cpp" + /* 键值对 int->String */ + struct Entry { + public: + int key; + string val; + Entry(int key, string val) { + this->key = key; + this->val = val; + } + Entry(){ + this->key = -1; + this->val = ""; + } + }; + /* 基于数组简易实现的哈希表 */ + class ArrayHashMap { + private: + vector bucket; + public: + ArrayHashMap() { + // 初始化一个长度为 100 的桶(数组) + bucket= vector(100); + } + + /* 哈希函数 */ + int hashFunc(int key) { + int index = key % 100; + return index; + } + + /* 查询操作 */ + string get(int key) { + int index = hashFunc(key); + Entry pair = bucket[index]; + return pair.val; + } + + /* 添加操作 */ + void put(int key, string val) { + Entry pair = Entry(key, val); + int index = hashFunc(key); + bucket[index] = pair; + } + + /* 删除操作 */ + void remove(int key) { + int index = hashFunc(key); + // 置为空,代表删除 + bucket[index] = *new Entry(); + } + }; ``` === "Python" ```python title="array_hash_map.py" - + # 键值对 int->String + class Entry: + def __init__(self, key, val): + self.key = key + self.val = val + + # 基于数组简易实现的哈希表 + class ArrayHashMap: + def __init__(self): + # 初始化一个长度为 100 的桶(数组) + self.bucket = [None] * 100 + + # 哈希函数 + def hashFunc(self, key): + index = key % 100 + return index + + # 查询操作 + def get(self, key): + index = self.hashFunc(key) + pair = self.bucket[index] + if pair is None: + return None + return pair.val + + # 添加操作 + def put(self, key, val): + pair = Entry(key, val) + index = self.hashFunc(key) + self.bucket[index] = pair + + # 删除操作 + def remove(self, key): + index = self.hashFunc(key) + # 置为空字符,代表删除 + self.bucket[index] = None ``` === "Go" ```go title="array_hash_map.go" + // 键值对 int->String + type Entry struct { + key int + val string + } + // 基于数组简易实现的哈希表 + type ArrayHashMap struct { + bucket []Entry + } + + func newArrayHashMap() *ArrayHashMap { + // 初始化一个长度为 100 的桶(数组) + bucket := make([]Entry, 100) + return &ArrayHashMap{bucket: bucket} + } + + // 哈希函数 + func (a *ArrayHashMap) hashFunc(key int) int { + index := key % 100 + return index + } + + // 查询操作 + func (a *ArrayHashMap) get(key int) string { + index := a.hashFunc(key) + pair := a.bucket[index] + if pair.key == 0 { + return "" + } + return pair.val + } + + // 添加操作 + func (a *ArrayHashMap) put(key int, val string) { + pair := Entry{key: key, val: val} + index := a.hashFunc(key) + a.bucket[index] = pair + } + + // 删除操作 + func (a *ArrayHashMap) remove(key int) { + index := a.hashFunc(key) + // 置为空字符,代表删除 + a.bucket[index] = Entry{} + } ``` === "JavaScript" From 1229f98e9221139844d36baf83800f2fad402e10 Mon Sep 17 00:00:00 2001 From: machangxin Date: Thu, 15 Dec 2022 09:43:20 +0800 Subject: [PATCH 2/5] Fixed some problems, indentation and Pointer --- codes/cpp/chapter_hashing/array_hash_map.cpp | 43 ++++--- codes/go/chapter_hashing/array_hash_map.go | 46 +++---- .../go/chapter_hashing/array_hash_map_test.go | 27 +++-- codes/go/chapter_hashing/hash_map_test.go | 34 +++--- .../python/chapter_hashing/array_hash_map.py | 48 ++++---- codes/python/chapter_hashing/hash_map.py | 35 +++--- codes/python/include/__init__.py | 2 +- codes/python/include/print_util.py | 11 +- docs/chapter_hashing/hash_map.md | 114 +++++++++--------- 9 files changed, 185 insertions(+), 175 deletions(-) diff --git a/codes/cpp/chapter_hashing/array_hash_map.cpp b/codes/cpp/chapter_hashing/array_hash_map.cpp index bca27ca7..2e0f2662 100644 --- a/codes/cpp/chapter_hashing/array_hash_map.cpp +++ b/codes/cpp/chapter_hashing/array_hash_map.cpp @@ -15,17 +15,16 @@ public: this->key = key; this->val = val; } - Entry()=default; }; /* 基于数组简易实现的哈希表 */ class ArrayHashMap { private: - vector bucket; + vector bucket; public: ArrayHashMap() { // 初始化一个长度为 100 的桶(数组) - bucket= vector(100); + bucket= vector(100); } /* 哈希函数 */ @@ -37,13 +36,16 @@ public: /* 查询操作 */ string get(int key) { int index = hashFunc(key); - Entry pair = bucket[index]; - return pair.val; + Entry* pair = bucket[index]; + if (pair == nullptr) { + return "Not Found"; + } + return pair->val; } /* 添加操作 */ void put(int key, string val) { - Entry pair = Entry(key, val); + Entry* pair = new Entry(key, val); int index = hashFunc(key); bucket[index] = pair; } @@ -52,14 +54,14 @@ public: void remove(int key) { int index = hashFunc(key); // 置为空字符,代表删除 - bucket[index] = *new Entry(); + bucket[index] = nullptr; } /* 获取所有键值对 */ - vector entrySet() { - vector entrySet; - for (Entry pair: bucket) { - if (pair.key != 0) { + vector entrySet() { + vector entrySet; + for (Entry* pair: bucket) { + if (pair != nullptr) { entrySet.push_back(pair); } } @@ -69,9 +71,9 @@ public: /* 获取所有键 */ vector keySet() { vector keySet; - for (Entry pair: bucket) { - if (pair.key != 0) { - keySet.push_back(pair.key); + for (Entry* pair: bucket) { + if (pair != nullptr) { + keySet.push_back(pair->key); } } return keySet; @@ -80,17 +82,18 @@ public: /* 获取所有值 */ vector valueSet() { vector valueSet; - for (Entry pair: bucket) { - if (pair.key != 0) - valueSet.push_back(pair.val); + for (Entry* pair: bucket) { + if (pair != nullptr){ + valueSet.push_back(pair->val); + } } return valueSet; } /* 打印哈希表 */ void print() { - for (Entry kv: entrySet()) { - cout << kv.key << " -> " << kv.val << endl; + for (Entry* kv: entrySet()) { + cout << kv->key << " -> " << kv->val << endl; } } }; @@ -122,7 +125,7 @@ int main() { /* 遍历哈希表 */ cout << "\n遍历键值对 Key->Value" << endl; for (auto kv: map.entrySet()) { - cout << kv.key << " -> " << kv.val << endl; + cout << kv->key << " -> " << kv->val << endl; } cout << "\n单独遍历键 Key" << endl; diff --git a/codes/go/chapter_hashing/array_hash_map.go b/codes/go/chapter_hashing/array_hash_map.go index c895ef84..7a55055d 100644 --- a/codes/go/chapter_hashing/array_hash_map.go +++ b/codes/go/chapter_hashing/array_hash_map.go @@ -6,91 +6,91 @@ package chapter_hashing import "fmt" -// 键值对 int->String +/* 键值对 int->String */ type Entry struct { key int val string } -// 基于数组简易实现的哈希表 +/* 基于数组简易实现的哈希表 */ type ArrayHashMap struct { - bucket []Entry + bucket []*Entry } func newArrayHashMap() *ArrayHashMap { // 初始化一个长度为 100 的桶(数组) - bucket := make([]Entry, 100) + bucket := make([]*Entry, 100) return &ArrayHashMap{bucket: bucket} } -// 哈希函数 +/* 哈希函数 */ func (a *ArrayHashMap) hashFunc(key int) int { index := key % 100 return index } -// 查询操作 +/* 查询操作 */ func (a *ArrayHashMap) get(key int) string { index := a.hashFunc(key) pair := a.bucket[index] - if pair.key == 0 { - return "" + if pair == nil { + return "Not Found" } return pair.val } -// 添加操作 +/* 添加操作 */ func (a *ArrayHashMap) put(key int, val string) { - pair := Entry{key: key, val: val} + pair := &Entry{key: key, val: val} index := a.hashFunc(key) a.bucket[index] = pair } -// 删除操作 +/* 删除操作 */ func (a *ArrayHashMap) remove(key int) { index := a.hashFunc(key) // 置为空字符,代表删除 - a.bucket[index] = Entry{} + a.bucket[index] = nil } -// 获取所有键对 -func (a *ArrayHashMap) entrySet() []Entry { - var pairs []Entry +/* 获取所有键对 */ +func (a *ArrayHashMap) entrySet() []*Entry { + var pairs []*Entry for _, pair := range a.bucket { - if pair.key != 0 { + if pair != nil { pairs = append(pairs, pair) } } return pairs } -// 获取所有键 +/* 获取所有键 */ func (a *ArrayHashMap) keySet() []int { var keys []int for _, pair := range a.bucket { - if pair.key != 0 { + if pair != nil { keys = append(keys, pair.key) } } return keys } -// 获取所有值 +/* 获取所有值 */ func (a *ArrayHashMap) valueSet() []string { var values []string for _, pair := range a.bucket { - if pair.key != 0 { + if pair != nil { values = append(values, pair.val) } } return values } -// 打印哈希表 +/* 打印哈希表 */ func (a *ArrayHashMap) print() { for _, pair := range a.bucket { - if pair.key != 0 { + if pair != nil { fmt.Println(pair.key, "->", pair.val) } } -} +} \ No newline at end of file diff --git a/codes/go/chapter_hashing/array_hash_map_test.go b/codes/go/chapter_hashing/array_hash_map_test.go index 218fd623..9b0954c4 100644 --- a/codes/go/chapter_hashing/array_hash_map_test.go +++ b/codes/go/chapter_hashing/array_hash_map_test.go @@ -11,42 +11,43 @@ import ( func TestArrayHashMap(t *testing.T) { /* 初始化哈希表 */ - map1 := newArrayHashMap() + mapp := newArrayHashMap() /* 添加操作 */ // 在哈希表中添加键值对 (key, value) - map1.put(12836, "小哈") - map1.put(15937, "小啰") - map1.put(16750, "小算") - map1.put(13276, "小法") - map1.put(10583, "小鸭") + mapp.put(12836, "小哈") + mapp.put(15937, "小啰") + mapp.put(16750, "小算") + mapp.put(13276, "小法") + mapp.put(10583, "小鸭") fmt.Println("\n添加完成后,哈希表为\nKey -> Value") - map1.print() + mapp.print() /* 查询操作 */ // 向哈希表输入键 key ,得到值 value - name := map1.get(15937) + name := mapp.get(15937) fmt.Println("\n输入学号 15937 ,查询到姓名 " + name) /* 删除操作 */ // 在哈希表中删除键值对 (key, value) - map1.remove(10583) + mapp.remove(10583) fmt.Println("\n删除 10583 后,哈希表为\nKey -> Value") - map1.print() + mapp.print() /* 遍历哈希表 */ fmt.Println("\n遍历键值对 Key->Value") - for _, kv := range map1.entrySet() { + for _, kv := range mapp.entrySet() { fmt.Println(kv.key, " -> ", kv.val) } fmt.Println("\n单独遍历键 Key") - for _, key := range map1.keySet() { + for _, key := range mapp.keySet() { fmt.Println(key) } fmt.Println("\n单独遍历值 Value") - for _, val := range map1.valueSet() { + for _, val := range mapp.valueSet() { fmt.Println(val) } } + diff --git a/codes/go/chapter_hashing/hash_map_test.go b/codes/go/chapter_hashing/hash_map_test.go index ee768336..10b2c967 100644 --- a/codes/go/chapter_hashing/hash_map_test.go +++ b/codes/go/chapter_hashing/hash_map_test.go @@ -10,48 +10,48 @@ import ( ) func TestHashmap(t *testing.T) { - // 初始化哈希表 - Map := make(map[int]string) + /* 初始化哈希表 */ + mapp := make(map[int]string) - // 添加操作 + /* 添加操作 */ // 在哈希表中添加键值对 (key, value) - Map[12836] = "小哈" - Map[15937] = "小啰" - Map[16750] = "小算" - Map[13276] = "小法" - Map[10583] = "小鸭" + mapp[12836] = "小哈" + mapp[15937] = "小啰" + mapp[16750] = "小算" + mapp[13276] = "小法" + mapp[10583] = "小鸭" fmt.Println("\n添加完成后,哈希表为\nKey -> Value") - for key, value := range Map { + for key, value := range mapp { fmt.Printf("%d -> %s\n", key, value) } - // 查询操作 + /* 查询操作 */ // 向哈希表输入键 key ,得到值 value - name := Map[15937] + name := mapp[15937] fmt.Println("\n输入学号 15937 ,查询到姓名 ", name) - // 删除操作 + /* 删除操作 */ // 在哈希表中删除键值对 (key, value) - delete(Map, 10583) + delete(mapp, 10583) fmt.Println("\n删除 10583 后,哈希表为\nKey -> Value") - for key, value := range Map { + for key, value := range mapp { fmt.Printf("%d -> %s\n", key, value) } /* 遍历哈希表 */ // 遍历键值对 key->value fmt.Println("\n遍历键值对 Key->Value") - for key, value := range Map { + for key, value := range mapp { fmt.Println(key, "->", value) } // 单独遍历键 key fmt.Println("\n单独遍历键 Key") - for key := range Map { + for key := range mapp { fmt.Println(key) } // 单独遍历值 value fmt.Println("\n单独遍历值 Value") - for _, value := range Map { + for _, value := range mapp { fmt.Println(value) } } diff --git a/codes/python/chapter_hashing/array_hash_map.py b/codes/python/chapter_hashing/array_hash_map.py index a9fbf58a..fdb57e4e 100644 --- a/codes/python/chapter_hashing/array_hash_map.py +++ b/codes/python/chapter_hashing/array_hash_map.py @@ -4,25 +4,25 @@ Created Time: 2022-12-14 Author: msk397 (machangxinq@gmail.com) """ -# 键值对 int->String +""" 键值对 int->String """ class Entry: def __init__(self, key, val): self.key = key self.val = val -# 基于数组简易实现的哈希表 +""" 基于数组简易实现的哈希表 """ class ArrayHashMap: def __init__(self): # 初始化一个长度为 100 的桶(数组) self.bucket = [None] * 100 - # 哈希函数 + """ 哈希函数 """ def hashFunc(self, key): index = key % 100 return index - # 查询操作 + """ 查询操作 """ def get(self, key): index = self.hashFunc(key) pair = self.bucket[index] @@ -30,19 +30,19 @@ class ArrayHashMap: return None return pair.val - # 添加操作 + """ 添加操作 """ def put(self, key, val): pair = Entry(key, val) index = self.hashFunc(key) self.bucket[index] = pair - # 删除操作 + """ 删除操作 """ def remove(self, key): index = self.hashFunc(key) # 置为空字符,代表删除 self.bucket[index] = None - # 获取所有键值对 + """ 获取所有键值对 """ def entrySet(self): result = [] for pair in self.bucket: @@ -50,7 +50,7 @@ class ArrayHashMap: result.append(pair) return result - # 获取所有键 + """ 获取所有键 """ def keySet(self): result = [] for pair in self.bucket: @@ -58,7 +58,7 @@ class ArrayHashMap: result.append(pair.key) return result - # 获取所有值 + """ 获取所有值 """ def valueSet(self): result = [] for pair in self.bucket: @@ -66,7 +66,7 @@ class ArrayHashMap: result.append(pair.val) return result - # 打印哈希表 + """ 打印哈希表 """ def print(self): for pair in self.bucket: if pair is not None: @@ -75,38 +75,38 @@ class ArrayHashMap: if __name__ == "__main__": """ 初始化哈希表 """ - Map = ArrayHashMap() + mapp = ArrayHashMap() """ 添加操作 """ # 在哈希表中添加键值对 (key, value) - Map.put(12836, "小哈") - Map.put(15937, "小啰") - Map.put(16750, "小算") - Map.put(13276, "小法") - Map.put(10583, "小鸭") + mapp.put(12836, "小哈") + mapp.put(15937, "小啰") + mapp.put(16750, "小算") + mapp.put(13276, "小法") + mapp.put(10583, "小鸭") print("\n添加完成后,哈希表为\nKey -> Value") - Map.print() + mapp.print() """ 查询操作 """ # 向哈希表输入键 key ,得到值 value - name = Map.get(15937) + name = mapp.get(15937) print("\n输入学号 15937 ,查询到姓名 " + name) """ 删除操作 """ # 在哈希表中删除键值对 (key, value) - Map.remove(10583) + mapp.remove(10583) print("\n删除 10583 后,哈希表为\nKey -> Value") - Map.print() + mapp.print() """ 遍历哈希表 """ print("\n遍历键值对 Key->Value") - for pair in Map.entrySet(): + for pair in mapp.entrySet(): print(pair.key, "->", pair.val) print("\n单独遍历键 Key") - for key in Map.keySet(): + for key in mapp.keySet(): print(key) print("\n单独遍历值 Value") - for val in Map.valueSet(): - print(val) + for val in mapp.valueSet(): + print(val) \ No newline at end of file diff --git a/codes/python/chapter_hashing/hash_map.py b/codes/python/chapter_hashing/hash_map.py index 98b7dedc..1afa9feb 100644 --- a/codes/python/chapter_hashing/hash_map.py +++ b/codes/python/chapter_hashing/hash_map.py @@ -4,43 +4,44 @@ Created Time: 2022-12-14 Author: msk397 (machangxinq@gmail.com) """ +import sys, os.path as osp +sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) +from include import * + if __name__ == "__main__": """ 初始化哈希表 """ - Map = {} + mapp = {} """ 添加操作 """ # 在哈希表中添加键值对 (key, value) - Map[12836] = "小哈" - Map[15937] = "小啰" - Map[16750] = "小算" - Map[13276] = "小法" - Map[10583] = "小鸭" + mapp[12836] = "小哈" + mapp[15937] = "小啰" + mapp[16750] = "小算" + mapp[13276] = "小法" + mapp[10583] = "小鸭" print("\n添加完成后,哈希表为\nKey -> Value") - for key, value in Map.items(): - print(key, "->", value) + print_dict(mapp) """ 查询操作 """ # 向哈希表输入键 key ,得到值 value - name = Map[15937] + name = mapp[15937] print("\n输入学号 15937 ,查询到姓名 " + name) """ 删除操作 """ # 在哈希表中删除键值对 (key, value) - Map.pop(10583) + mapp.pop(10583) print("\n删除 10583 后,哈希表为\nKey -> Value") - for key, value in Map.items(): - print(key, "->", value) + print_dict(mapp) """ 遍历哈希表 """ print("\n遍历键值对 Key->Value") - for key, value in Map.items(): + for key, value in mapp.items(): print(key, "->", value) print("\n单独遍历键 Key") - for key in Map.keys(): + for key in mapp.keys(): print(key) print("\n单独遍历值 Value") - for val in Map.values(): - print(val) - + for val in mapp.values(): + print(val) \ No newline at end of file diff --git a/codes/python/include/__init__.py b/codes/python/include/__init__.py index 77cf855a..31ed69ac 100644 --- a/codes/python/include/__init__.py +++ b/codes/python/include/__init__.py @@ -7,4 +7,4 @@ import collections from typing import List from .linked_list import ListNode, list_to_linked_list, linked_list_to_list, get_list_node from .binary_tree import TreeNode, list_to_tree, tree_to_list, get_tree_node -from .print_util import print_matrix, print_linked_list, print_tree \ No newline at end of file +from .print_util import print_matrix, print_linked_list, print_tree, print_dict \ No newline at end of file diff --git a/codes/python/include/print_util.py b/codes/python/include/print_util.py index f315add8..f84d548b 100644 --- a/codes/python/include/print_util.py +++ b/codes/python/include/print_util.py @@ -1,7 +1,7 @@ ''' File: print_util.py Created Time: 2021-12-11 -Author: Krahets (krahets@163.com) +Author: Krahets (krahets@163.com), msk397 (machangxinq@gmail.com) ''' import copy @@ -72,3 +72,12 @@ def print_tree(root, prev=None, isLeft=False): prev.str = prev_str trunk.str = ' |' print_tree(root.left, trunk, False) + +def print_dict(d): + """Print a dict + + Args: + d ([type]): [description] + """ + for key, value in d.items(): + print(key, '->', value) \ No newline at end of file diff --git a/docs/chapter_hashing/hash_map.md b/docs/chapter_hashing/hash_map.md index 3ce9c08b..69dfdc96 100644 --- a/docs/chapter_hashing/hash_map.md +++ b/docs/chapter_hashing/hash_map.md @@ -86,47 +86,47 @@ comments: true === "Python" ```python title="hash_map.py" - # 初始化哈希表 - Map = {} + """ 初始化哈希表 """ + mapp = {} - # 添加操作 + """ 添加操作 """ # 在哈希表中添加键值对 (key, value) - Map[12836] = "小哈" - Map[15937] = "小啰" - Map[16750] = "小算" - Map[13276] = "小法" - Map[10583] = "小鸭" + mapp[12836] = "小哈" + mapp[15937] = "小啰" + mapp[16750] = "小算" + mapp[13276] = "小法" + mapp[10583] = "小鸭" - # 查询操作 + """ 查询操作 """ # 向哈希表输入键 key ,得到值 value - name = Map[15937] + name = mapp[15937] - # 删除操作 + """ 删除操作 """ # 在哈希表中删除键值对 (key, value) - Map.pop(10583) + mapp.pop(10583) ``` === "Go" ```go title="hash_map_test.go" - // 初始化哈希表 - Map := make(map[int]string) + /* 初始化哈希表 */ + mapp := make(map[int]string) - // 添加操作 + /* 添加操作 */ // 在哈希表中添加键值对 (key, value) - Map[12836] = "小哈" - Map[15937] = "小啰" - Map[16750] = "小算" - Map[13276] = "小法" - Map[10583] = "小鸭" + mapp[12836] = "小哈" + mapp[15937] = "小啰" + mapp[16750] = "小算" + mapp[13276] = "小法" + mapp[10583] = "小鸭" - // 查询操作 + /* 查询操作 */ // 向哈希表输入键 key ,得到值 value - name := Map[15937] + name := mapp[15937] - // 删除操作 + /* 删除操作 */ // 在哈希表中删除键值对 (key, value) - delete(Map, 10583) + delete(mapp, 10583) ``` === "JavaScript" @@ -196,13 +196,13 @@ comments: true ```python title="hash_map.py" """ 遍历哈希表 """ # 遍历键值对 key->value - for key, value in Map.items(): + for key, value in mapp.items(): print(key, "->", value) # 单独遍历键 key - for key in Map.keys(): + for key in mapp.keys(): print(key) # 单独遍历值 value - for value in Map.values(): + for value in mapp.values(): print(value) ``` @@ -211,15 +211,15 @@ comments: true ```go title="hash_map_test.go" /* 遍历哈希表 */ // 遍历键值对 key->value - for key, value := range Map { + for key, value := range mapp { fmt.Println(key, "->", value) } // 单独遍历键 key - for key := range Map { + for key := range mapp { fmt.Println(key) } // 单独遍历值 value - for _, value := range Map { + for _, value := range mapp { fmt.Println(value) } ``` @@ -335,20 +335,16 @@ $$ this->key = key; this->val = val; } - Entry(){ - this->key = -1; - this->val = ""; - } }; /* 基于数组简易实现的哈希表 */ class ArrayHashMap { private: - vector bucket; + vector bucket; public: ArrayHashMap() { // 初始化一个长度为 100 的桶(数组) - bucket= vector(100); + bucket= vector(100); } /* 哈希函数 */ @@ -360,13 +356,13 @@ $$ /* 查询操作 */ string get(int key) { int index = hashFunc(key); - Entry pair = bucket[index]; - return pair.val; + Entry* pair = bucket[index]; + return pair->val; } /* 添加操作 */ void put(int key, string val) { - Entry pair = Entry(key, val); + Entry* pair = new Entry(key, val); int index = hashFunc(key); bucket[index] = pair; } @@ -374,8 +370,8 @@ $$ /* 删除操作 */ void remove(int key) { int index = hashFunc(key); - // 置为空,代表删除 - bucket[index] = *new Entry(); + // 置为空字符,代表删除 + bucket[index] = nullptr; } }; ``` @@ -383,24 +379,24 @@ $$ === "Python" ```python title="array_hash_map.py" - # 键值对 int->String + """ 键值对 int->String """ class Entry: def __init__(self, key, val): self.key = key self.val = val - # 基于数组简易实现的哈希表 + """ 基于数组简易实现的哈希表 """ class ArrayHashMap: def __init__(self): # 初始化一个长度为 100 的桶(数组) self.bucket = [None] * 100 - # 哈希函数 + """ 哈希函数 """ def hashFunc(self, key): index = key % 100 return index - # 查询操作 + """ 查询操作 """ def get(self, key): index = self.hashFunc(key) pair = self.bucket[index] @@ -408,13 +404,13 @@ $$ return None return pair.val - # 添加操作 + """ 添加操作 """ def put(self, key, val): pair = Entry(key, val) index = self.hashFunc(key) self.bucket[index] = pair - # 删除操作 + """ 删除操作 """ def remove(self, key): index = self.hashFunc(key) # 置为空字符,代表删除 @@ -424,51 +420,51 @@ $$ === "Go" ```go title="array_hash_map.go" - // 键值对 int->String + /* 键值对 int->String */ type Entry struct { key int val string } - // 基于数组简易实现的哈希表 + /* 基于数组简易实现的哈希表 */ type ArrayHashMap struct { - bucket []Entry + bucket []*Entry } func newArrayHashMap() *ArrayHashMap { // 初始化一个长度为 100 的桶(数组) - bucket := make([]Entry, 100) + bucket := make([]*Entry, 100) return &ArrayHashMap{bucket: bucket} } - // 哈希函数 + /* 哈希函数 */ func (a *ArrayHashMap) hashFunc(key int) int { index := key % 100 return index } - // 查询操作 + /* 查询操作 */ func (a *ArrayHashMap) get(key int) string { index := a.hashFunc(key) pair := a.bucket[index] - if pair.key == 0 { - return "" + if pair == nil { + return "Not Found" } return pair.val } - // 添加操作 + /* 添加操作 */ func (a *ArrayHashMap) put(key int, val string) { - pair := Entry{key: key, val: val} + pair := &Entry{key: key, val: val} index := a.hashFunc(key) a.bucket[index] = pair } - // 删除操作 + /* 删除操作 */ func (a *ArrayHashMap) remove(key int) { index := a.hashFunc(key) // 置为空字符,代表删除 - a.bucket[index] = Entry{} + a.bucket[index] = nil } ``` From c071ab88d27774859d4b7ffb075577343c1e26e4 Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Thu, 15 Dec 2022 23:08:07 +0800 Subject: [PATCH 3/5] Update hash map --- codes/cpp/chapter_hashing/array_hash_map.cpp | 6 ++++-- codes/cpp/chapter_hashing/hash_map.cpp | 3 ++- codes/go/chapter_hashing/array_hash_map.go | 4 ++-- codes/go/chapter_hashing/array_hash_map_test.go | 1 - codes/python/chapter_hashing/array_hash_map.py | 3 ++- codes/python/chapter_hashing/hash_map.py | 4 +++- 6 files changed, 13 insertions(+), 8 deletions(-) diff --git a/codes/cpp/chapter_hashing/array_hash_map.cpp b/codes/cpp/chapter_hashing/array_hash_map.cpp index 2e0f2662..394e3681 100644 --- a/codes/cpp/chapter_hashing/array_hash_map.cpp +++ b/codes/cpp/chapter_hashing/array_hash_map.cpp @@ -53,7 +53,7 @@ public: /* 删除操作 */ void remove(int key) { int index = hashFunc(key); - // 置为空字符,代表删除 + // 置为 nullptr ,代表删除 bucket[index] = nullptr; } @@ -97,6 +97,8 @@ public: } } }; + +/* Driver Code */ int main() { /* 初始化哈希表 */ ArrayHashMap map = ArrayHashMap(); @@ -139,4 +141,4 @@ int main() { } return 0; -} \ No newline at end of file +} diff --git a/codes/cpp/chapter_hashing/hash_map.cpp b/codes/cpp/chapter_hashing/hash_map.cpp index 6c8df4b8..898f679a 100644 --- a/codes/cpp/chapter_hashing/hash_map.cpp +++ b/codes/cpp/chapter_hashing/hash_map.cpp @@ -7,6 +7,7 @@ #include "../include/include.hpp" +/* Driver Code */ int main() { /* 初始化哈希表 */ unordered_map map; @@ -49,4 +50,4 @@ int main() { } return 0; -} \ No newline at end of file +} diff --git a/codes/go/chapter_hashing/array_hash_map.go b/codes/go/chapter_hashing/array_hash_map.go index 7a55055d..a962b6a4 100644 --- a/codes/go/chapter_hashing/array_hash_map.go +++ b/codes/go/chapter_hashing/array_hash_map.go @@ -49,7 +49,7 @@ func (a *ArrayHashMap) put(key int, val string) { /* 删除操作 */ func (a *ArrayHashMap) remove(key int) { index := a.hashFunc(key) - // 置为空字符,代表删除 + // 置为 nil ,代表删除 a.bucket[index] = nil } @@ -93,4 +93,4 @@ func (a *ArrayHashMap) print() { fmt.Println(pair.key, "->", pair.val) } } -} \ No newline at end of file +} diff --git a/codes/go/chapter_hashing/array_hash_map_test.go b/codes/go/chapter_hashing/array_hash_map_test.go index 9b0954c4..6925f0d5 100644 --- a/codes/go/chapter_hashing/array_hash_map_test.go +++ b/codes/go/chapter_hashing/array_hash_map_test.go @@ -50,4 +50,3 @@ func TestArrayHashMap(t *testing.T) { fmt.Println(val) } } - diff --git a/codes/python/chapter_hashing/array_hash_map.py b/codes/python/chapter_hashing/array_hash_map.py index fdb57e4e..046e5c61 100644 --- a/codes/python/chapter_hashing/array_hash_map.py +++ b/codes/python/chapter_hashing/array_hash_map.py @@ -73,6 +73,7 @@ class ArrayHashMap: print(pair.key, "->", pair.val) +""" Driver Code """ if __name__ == "__main__": """ 初始化哈希表 """ mapp = ArrayHashMap() @@ -109,4 +110,4 @@ if __name__ == "__main__": print("\n单独遍历值 Value") for val in mapp.valueSet(): - print(val) \ No newline at end of file + print(val) diff --git a/codes/python/chapter_hashing/hash_map.py b/codes/python/chapter_hashing/hash_map.py index 1afa9feb..f5db7863 100644 --- a/codes/python/chapter_hashing/hash_map.py +++ b/codes/python/chapter_hashing/hash_map.py @@ -8,6 +8,8 @@ import sys, os.path as osp sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) from include import * + +""" Driver Code """ if __name__ == "__main__": """ 初始化哈希表 """ mapp = {} @@ -44,4 +46,4 @@ if __name__ == "__main__": print("\n单独遍历值 Value") for val in mapp.values(): - print(val) \ No newline at end of file + print(val) From 26ad485dd271a6520bd94ff41623202c8b34eac0 Mon Sep 17 00:00:00 2001 From: machangxin Date: Fri, 16 Dec 2022 10:55:28 +0800 Subject: [PATCH 4/5] Add function PrintMap() in Go --- codes/go/chapter_hashing/hash_map_test.go | 10 ++++------ codes/go/pkg/print_utils.go | 9 ++++++++- codes/python/chapter_hashing/array_hash_map.py | 2 +- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/codes/go/chapter_hashing/hash_map_test.go b/codes/go/chapter_hashing/hash_map_test.go index 10b2c967..c53d3991 100644 --- a/codes/go/chapter_hashing/hash_map_test.go +++ b/codes/go/chapter_hashing/hash_map_test.go @@ -7,6 +7,8 @@ package chapter_hashing import ( "fmt" "testing" + + . "github.com/krahets/hello-algo/pkg" ) func TestHashmap(t *testing.T) { @@ -21,9 +23,7 @@ func TestHashmap(t *testing.T) { mapp[13276] = "小法" mapp[10583] = "小鸭" fmt.Println("\n添加完成后,哈希表为\nKey -> Value") - for key, value := range mapp { - fmt.Printf("%d -> %s\n", key, value) - } + PrintMap(mapp) /* 查询操作 */ // 向哈希表输入键 key ,得到值 value @@ -34,9 +34,7 @@ func TestHashmap(t *testing.T) { // 在哈希表中删除键值对 (key, value) delete(mapp, 10583) fmt.Println("\n删除 10583 后,哈希表为\nKey -> Value") - for key, value := range mapp { - fmt.Printf("%d -> %s\n", key, value) - } + PrintMap(mapp) /* 遍历哈希表 */ // 遍历键值对 key->value diff --git a/codes/go/pkg/print_utils.go b/codes/go/pkg/print_utils.go index e42d4068..41a8ed26 100644 --- a/codes/go/pkg/print_utils.go +++ b/codes/go/pkg/print_utils.go @@ -1,6 +1,6 @@ // File: print_utils.go // Created Time: 2022-12-03 -// Author: Reanon (793584285@qq.com), Krahets (krahets@163.com) +// Author: Reanon (793584285@qq.com), Krahets (krahets@163.com), msk397 (machangxinq@gmail.com) package pkg @@ -96,3 +96,10 @@ func showTrunk(t *trunk) { showTrunk(t.prev) fmt.Print(t.str) } + +// PrintHashMap Print a hash map +func PrintMap(m map[int]string) { + for key, value := range m { + fmt.Printf("%d -> %s\n", key, value) + } +} \ No newline at end of file diff --git a/codes/python/chapter_hashing/array_hash_map.py b/codes/python/chapter_hashing/array_hash_map.py index 046e5c61..a65f1544 100644 --- a/codes/python/chapter_hashing/array_hash_map.py +++ b/codes/python/chapter_hashing/array_hash_map.py @@ -39,7 +39,7 @@ class ArrayHashMap: """ 删除操作 """ def remove(self, key): index = self.hashFunc(key) - # 置为空字符,代表删除 + # 置为None,代表删除 self.bucket[index] = None """ 获取所有键值对 """ From ba5b5a1f09a35ef573b421ed70a60508b9b93e27 Mon Sep 17 00:00:00 2001 From: machangxin Date: Fri, 16 Dec 2022 17:03:41 +0800 Subject: [PATCH 5/5] function PrintMap supports Generics --- codes/go/pkg/print_utils.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/codes/go/pkg/print_utils.go b/codes/go/pkg/print_utils.go index 41a8ed26..3d9b83a2 100644 --- a/codes/go/pkg/print_utils.go +++ b/codes/go/pkg/print_utils.go @@ -97,9 +97,9 @@ func showTrunk(t *trunk) { fmt.Print(t.str) } -// PrintHashMap Print a hash map -func PrintMap(m map[int]string) { +// PrintMap Print a hash map +func PrintMap[K comparable, V any](m map[K]V) { for key, value := range m { - fmt.Printf("%d -> %s\n", key, value) + fmt.Println(key, "->", value) } } \ No newline at end of file