Update array_hash_map

This commit is contained in:
krahets
2023-03-15 03:11:43 +08:00
parent 669388128f
commit d5afd93571
10 changed files with 98 additions and 98 deletions

View File

@ -20,11 +20,11 @@ public:
/* 基于数组简易实现的哈希表 */
class ArrayHashMap {
private:
vector<Entry*> bucket;
vector<Entry*> buckets;
public:
ArrayHashMap() {
// 初始化一个长度为 100 的桶(数组)
bucket= vector<Entry*>(100);
buckets= vector<Entry*>(100);
}
/* 哈希函数 */
@ -36,7 +36,7 @@ public:
/* 查询操作 */
string get(int key) {
int index = hashFunc(key);
Entry* pair = bucket[index];
Entry* pair = buckets[index];
if (pair == nullptr)
return nullptr;
return pair->val;
@ -46,20 +46,20 @@ public:
void put(int key, string val) {
Entry* pair = new Entry(key, val);
int index = hashFunc(key);
bucket[index] = pair;
buckets[index] = pair;
}
/* 删除操作 */
void remove(int key) {
int index = hashFunc(key);
// 置为 nullptr ,代表删除
bucket[index] = nullptr;
buckets[index] = nullptr;
}
/* 获取所有键值对 */
vector<Entry*> entrySet() {
vector<Entry*> entrySet;
for (Entry* pair: bucket) {
for (Entry* pair: buckets) {
if (pair != nullptr) {
entrySet.push_back(pair);
}
@ -70,7 +70,7 @@ public:
/* 获取所有键 */
vector<int> keySet() {
vector<int> keySet;
for (Entry* pair: bucket) {
for (Entry* pair: buckets) {
if (pair != nullptr) {
keySet.push_back(pair->key);
}
@ -81,7 +81,7 @@ public:
/* 获取所有值 */
vector<string> valueSet() {
vector<string> valueSet;
for (Entry* pair: bucket) {
for (Entry* pair: buckets) {
if (pair != nullptr){
valueSet.push_back(pair->val);
}