2023-01-06 03:39:19 +08:00
|
|
|
/**
|
2022-12-14 17:18:32 +08:00
|
|
|
* File: array_hash_map.cpp
|
|
|
|
* Created Time: 2022-12-14
|
|
|
|
* Author: msk397 (machangxinq@gmail.com)
|
|
|
|
*/
|
|
|
|
|
2023-04-24 04:11:18 +08:00
|
|
|
#include "../utils/common.hpp"
|
2022-12-14 17:18:32 +08:00
|
|
|
|
2023-06-14 02:01:06 +08:00
|
|
|
/* 键值对 */
|
|
|
|
struct Pair {
|
2023-04-14 03:44:02 +08:00
|
|
|
public:
|
2022-12-14 17:18:32 +08:00
|
|
|
int key;
|
|
|
|
string val;
|
2023-06-14 02:01:06 +08:00
|
|
|
Pair(int key, string val) {
|
2022-12-14 17:18:32 +08:00
|
|
|
this->key = key;
|
|
|
|
this->val = val;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* 基于数组简易实现的哈希表 */
|
|
|
|
class ArrayHashMap {
|
2023-04-14 03:44:02 +08:00
|
|
|
private:
|
2023-06-14 02:01:06 +08:00
|
|
|
vector<Pair *> buckets;
|
2023-04-14 03:44:02 +08:00
|
|
|
|
|
|
|
public:
|
2022-12-14 17:18:32 +08:00
|
|
|
ArrayHashMap() {
|
2023-03-15 03:48:31 +08:00
|
|
|
// 初始化数组,包含 100 个桶
|
2023-06-14 02:01:06 +08:00
|
|
|
buckets = vector<Pair *>(100);
|
2022-12-14 17:18:32 +08:00
|
|
|
}
|
|
|
|
|
2023-04-15 23:51:35 +08:00
|
|
|
~ArrayHashMap() {
|
|
|
|
// 释放内存
|
|
|
|
for (const auto &bucket : buckets) {
|
|
|
|
delete bucket;
|
|
|
|
}
|
|
|
|
buckets.clear();
|
|
|
|
}
|
|
|
|
|
2022-12-14 17:18:32 +08:00
|
|
|
/* 哈希函数 */
|
|
|
|
int hashFunc(int key) {
|
|
|
|
int index = key % 100;
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 查询操作 */
|
|
|
|
string get(int key) {
|
|
|
|
int index = hashFunc(key);
|
2023-06-14 02:01:06 +08:00
|
|
|
Pair *pair = buckets[index];
|
2023-02-08 04:17:26 +08:00
|
|
|
if (pair == nullptr)
|
2023-10-04 02:30:31 +08:00
|
|
|
return "";
|
2022-12-15 09:43:20 +08:00
|
|
|
return pair->val;
|
2022-12-14 17:18:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 添加操作 */
|
|
|
|
void put(int key, string val) {
|
2023-06-14 02:01:06 +08:00
|
|
|
Pair *pair = new Pair(key, val);
|
2022-12-14 17:18:32 +08:00
|
|
|
int index = hashFunc(key);
|
2023-03-15 03:11:43 +08:00
|
|
|
buckets[index] = pair;
|
2022-12-14 17:18:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 删除操作 */
|
|
|
|
void remove(int key) {
|
|
|
|
int index = hashFunc(key);
|
2023-04-15 23:51:35 +08:00
|
|
|
// 释放内存并置为 nullptr
|
|
|
|
delete buckets[index];
|
2023-03-15 03:11:43 +08:00
|
|
|
buckets[index] = nullptr;
|
2022-12-14 17:18:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取所有键值对 */
|
2023-06-14 02:01:06 +08:00
|
|
|
vector<Pair *> pairSet() {
|
|
|
|
vector<Pair *> pairSet;
|
|
|
|
for (Pair *pair : buckets) {
|
2022-12-15 09:43:20 +08:00
|
|
|
if (pair != nullptr) {
|
2023-06-14 02:01:06 +08:00
|
|
|
pairSet.push_back(pair);
|
2022-12-14 17:18:32 +08:00
|
|
|
}
|
|
|
|
}
|
2023-06-14 02:01:06 +08:00
|
|
|
return pairSet;
|
2022-12-14 17:18:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取所有键 */
|
|
|
|
vector<int> keySet() {
|
|
|
|
vector<int> keySet;
|
2023-06-14 02:01:06 +08:00
|
|
|
for (Pair *pair : buckets) {
|
2022-12-15 09:43:20 +08:00
|
|
|
if (pair != nullptr) {
|
|
|
|
keySet.push_back(pair->key);
|
2022-12-14 17:18:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return keySet;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取所有值 */
|
|
|
|
vector<string> valueSet() {
|
|
|
|
vector<string> valueSet;
|
2023-06-14 02:01:06 +08:00
|
|
|
for (Pair *pair : buckets) {
|
2023-04-14 03:44:02 +08:00
|
|
|
if (pair != nullptr) {
|
2022-12-15 09:43:20 +08:00
|
|
|
valueSet.push_back(pair->val);
|
|
|
|
}
|
2022-12-14 17:18:32 +08:00
|
|
|
}
|
|
|
|
return valueSet;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 打印哈希表 */
|
|
|
|
void print() {
|
2023-06-14 02:01:06 +08:00
|
|
|
for (Pair *kv : pairSet()) {
|
2022-12-15 09:43:20 +08:00
|
|
|
cout << kv->key << " -> " << kv->val << endl;
|
2022-12-14 17:18:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2022-12-15 23:08:07 +08:00
|
|
|
|
2023-06-26 23:06:15 +08:00
|
|
|
// 测试样例请见 array_hash_map_test.cpp
|