// 校验登录 boolverify(const string& username, const string& password)const{ const UserEntry* e = find_user(username); if (!e) returnfalse; string h = SimplePasswordHasher::hash_password(e->salt, password); return h == e->pw_hash_hex; }
// 删除用户 boolerase(const string& username){ size_t idx = index_hash(username); auto& lst = buckets[idx]; for (auto it = lst.begin(); it != lst.end(); ++it) { if (it->username == username) { lst.erase(it); returntrue; } } returnfalse; }
// 调试/查看:把每个桶里的人数统计一下 vector<size_t> bucket_sizes_sample(size_t first_k = 20)const{ first_k = min(first_k, M); vector<size_t> a(first_k); for (size_t i = 0; i < first_k; ++i) a[i] = buckets[i].size(); return a; }
private: size_t M; vector<list<UserEntry>> buckets;
// ====== 哈希表索引的哈希公式(重点:含“取模”)====== // 经典的多项式滚动:h = (h * B + byte) % M // 这里 M 是桶数(素数),B 选个小质数即可,比如 131 size_tindex_hash(const string& key)const{ constuint64_t B = 131; // 小质数 uint64_t h = 0; for (unsignedchar c : key) { h = (h * B + c) % M; // —— 关键的“取模 M” —— 把哈希值映射到 [0, M) } return (size_t)h; }
const UserEntry* find_user(const string& username)const{ size_t idx = index_hash(username); constauto& lst = buckets[idx]; for (constauto& e : lst) { if (e.username == username) return &e; } returnnullptr; } };