// // Created by E2hang on 2025/8/5. // #ifndef HASHTABLE_H #define HASHTABLE_H #include #include template class hashTable { private: std::hash hashFunc; std::vector>> table; int dsize; int division; public: hashTable(int d = 1009); ~hashTable() = default; //hashTable(const hashTable&); int searchj(const K &key); int searchi(const K &key) {return hashFunc(key) % division;}; void insert(const K& key, const E& e); void erase(const K& key); std::pair* find(const K& key); }; template hashTable::hashTable(int d) : dsize(0), division(d) { table.resize(d); } template int hashTable::searchj(const K &key) { int i = static_cast(hashFunc(key) % division); int j = 0; for (std::pair x : table[i]) { if (x.first == key ) return j; j++; } return -1; } template void hashTable::insert(const K &key, const E &e) { int i = static_cast(hashFunc(key) % division); for (auto &x : table[i]) { if (x.first == key) { x.second = e; return; } } table[i].push_back(std::make_pair(key, e)); } template void hashTable::erase(const K &key) { int i = static_cast(hashFunc(key) % division); auto &bucket = table[i]; for (auto it = bucket.begin(); it != bucket.end(); ++it) { if (it->first == key) { bucket.erase(it); return; // 删除后退出 } } } template std::pair* hashTable::find(const K &key) { int i = static_cast(hashFunc(key) % division); int j = searchj(key); if (j == -1) { return nullptr; // 找不到返回 nullptr } return &table[i][j]; } #endif //HASHTABLE_H