mirror of
https://github.com/krahets/hello-algo.git
synced 2026-02-03 19:03:42 +08:00
* Review the EN heading format. * Fix pythontutor headings. * Fix pythontutor headings. * bug fixes * Fix headings in **/summary.md * Revisit the CN-to-EN translation for Python code using Claude-4.5 * Revisit the CN-to-EN translation for Java code using Claude-4.5 * Revisit the CN-to-EN translation for Cpp code using Claude-4.5. * Fix the dictionary. * Fix cpp code translation for the multipart strings. * Translate Go code to English. * Update workflows to test EN code. * Add EN translation for C. * Add EN translation for CSharp. * Add EN translation for Swift. * Trigger the CI check. * Revert. * Update en/hash_map.md * Add the EN version of Dart code. * Add the EN version of Kotlin code. * Add missing code files. * Add the EN version of JavaScript code. * Add the EN version of TypeScript code. * Fix the workflows. * Add the EN version of Ruby code. * Add the EN version of Rust code. * Update the CI check for the English version code. * Update Python CI check. * Fix cmakelists for en/C code. * Fix Ruby comments
111 lines
2.3 KiB
C++
111 lines
2.3 KiB
C++
/**
|
|
* File: array_hash_map.cpp
|
|
* Created Time: 2022-12-14
|
|
* Author: msk397 (machangxinq@gmail.com)
|
|
*/
|
|
|
|
#include "../utils/common.hpp"
|
|
|
|
/* Key-value pair */
|
|
struct Pair {
|
|
public:
|
|
int key;
|
|
string val;
|
|
Pair(int key, string val) {
|
|
this->key = key;
|
|
this->val = val;
|
|
}
|
|
};
|
|
|
|
/* Hash table based on array implementation */
|
|
class ArrayHashMap {
|
|
private:
|
|
vector<Pair *> buckets;
|
|
|
|
public:
|
|
ArrayHashMap() {
|
|
// Initialize array with 100 buckets
|
|
buckets = vector<Pair *>(100);
|
|
}
|
|
|
|
~ArrayHashMap() {
|
|
// Free memory
|
|
for (const auto &bucket : buckets) {
|
|
delete bucket;
|
|
}
|
|
buckets.clear();
|
|
}
|
|
|
|
/* Hash function */
|
|
int hashFunc(int key) {
|
|
int index = key % 100;
|
|
return index;
|
|
}
|
|
|
|
/* Query operation */
|
|
string get(int key) {
|
|
int index = hashFunc(key);
|
|
Pair *pair = buckets[index];
|
|
if (pair == nullptr)
|
|
return "";
|
|
return pair->val;
|
|
}
|
|
|
|
/* Add operation */
|
|
void put(int key, string val) {
|
|
Pair *pair = new Pair(key, val);
|
|
int index = hashFunc(key);
|
|
buckets[index] = pair;
|
|
}
|
|
|
|
/* Remove operation */
|
|
void remove(int key) {
|
|
int index = hashFunc(key);
|
|
// Free memory and set to nullptr
|
|
delete buckets[index];
|
|
buckets[index] = nullptr;
|
|
}
|
|
|
|
/* Get all key-value pairs */
|
|
vector<Pair *> pairSet() {
|
|
vector<Pair *> pairSet;
|
|
for (Pair *pair : buckets) {
|
|
if (pair != nullptr) {
|
|
pairSet.push_back(pair);
|
|
}
|
|
}
|
|
return pairSet;
|
|
}
|
|
|
|
/* Get all keys */
|
|
vector<int> keySet() {
|
|
vector<int> keySet;
|
|
for (Pair *pair : buckets) {
|
|
if (pair != nullptr) {
|
|
keySet.push_back(pair->key);
|
|
}
|
|
}
|
|
return keySet;
|
|
}
|
|
|
|
/* Get all values */
|
|
vector<string> valueSet() {
|
|
vector<string> valueSet;
|
|
for (Pair *pair : buckets) {
|
|
if (pair != nullptr) {
|
|
valueSet.push_back(pair->val);
|
|
}
|
|
}
|
|
return valueSet;
|
|
}
|
|
|
|
/* Print hash table */
|
|
void print() {
|
|
for (Pair *kv : pairSet()) {
|
|
cout << kv->key << " -> " << kv->val << endl;
|
|
}
|
|
}
|
|
};
|
|
|
|
// See array_hash_map_test.cpp for test cases
|