{"id":157,"date":"2025-07-10T18:50:02","date_gmt":"2025-07-10T10:50:02","guid":{"rendered":"http:\/\/www.tgwttt.xyz\/?p=157"},"modified":"2025-11-24T16:41:40","modified_gmt":"2025-11-24T08:41:40","slug":"%e5%93%88%e5%b8%8c%e6%a1%b6%e7%9a%84%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/www.tgwttt.xyz\/?p=157","title":{"rendered":"\u54c8\u5e0c\u6876\u7684\u5b9e\u73b0"},"content":{"rendered":"\n<p>\u5df2\u4e0b\u4ee3\u7801\u5df2\u5728gitee\u4e0a\u5f00\u6e90\u5382\u5e93\u5730\u5740<a href=\"https:\/\/gitee.com\/tgwTTT\/c-lreant\/blob\/master\/Hashbucket\/hashbucket.h\">https:\/\/gitee.com\/tgwTTT\/c-lreant\/blob\/master\/Hashbucket\/hashbucket.h<\/a><\/p>\n\n\n\n<p>\u672c\u6587\u662f\u57fa\u4e8e\u4e0a\u4e00\u7bc7\u6587\u7ae0\u54c8\u5e0c\u8868<a href=\"http:\/\/www.tgwttt.xyz\/?p=150\">http:\/\/www.tgwttt.xyz\/?p=150<\/a>\u7684\u57fa\u7840\u4e0a\u6539\u52a8<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"631\" height=\"215\" src=\"http:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/07\/image-29.png\" alt=\"\" class=\"wp-image-160\" srcset=\"https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/07\/image-29.png 631w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/07\/image-29-300x102.png 300w\" sizes=\"auto, (max-width: 631px) 100vw, 631px\" \/><\/figure>\n\n\n\n<p>\u54c8\u5e0c\u6876\u662f\u54c8\u5e0c\u8868\u89e3\u51b3hash\u51b2\u7a81\u7684\u4e00\u79cd\u65b9\u6cd5\uff0c\u4ed6\u662f\u5229\u7528\u94fe\u8868\u53bb\u5b58\u50a8\u6570\u636e\uff0c\u5f53\u9047\u5230hash\u51b2\u7a81\u65f6\u5c31\u9700\u8981\u5f80\u4e0b\u67e5\u8be2\uff0c\u4ee5\u89e3\u51b3hash\u51b2\u7a81\u3002\u4e0b\u9762\u662f\u5b9e\u73b0<\/p>\n\n\n\n<p>\u9996\u5148\u5c31\u662f\u5b9a\u4e49\u8282\u70b9\uff0c\u4e0e\u666e\u901ahash\u8868\u4e0d\u540c\u662f\u5728\u6570\u636e\u8282\u70b9\u7684\u5b9a\u4e49\u3002\u53e6\u5916\uff0c\u6211\u4eec\u5f15\u5165c++\u91cc\u9762unordered_map\u5e95\u5c42\u7684\u8d28\u7d20\u6570\u8868\u6bcf\u5f53\u6269\u5bb9\u65f6\u5c31\u6362\u8d28\u6570\u8868\u4e2d\u7684\u6570<\/p>\n\n\n\n<p>\u8d28\u6570\u8868\u7684\u5b9e\u73b0\uff1a                                                                                                                                                           inline unsigned long __stl_next_prime(unsigned long n)<br>{<br>\/\/ Note: assumes long is at least 32 bits.<br>static const int __stl_num_primes = 28;<br>static const unsigned long __stl_prime_list[__stl_num_primes] = {<br>53, 97, 193, 389, 769,<br>1543, 3079, 6151, 12289, 24593,<br>49157, 98317, 196613, 393241, 786433,<br>1572869, 3145739, 6291469, 12582917, 25165843,<br>50331653, 100663319, 201326611, 402653189, 805306457,<br>1610612741, 3221225473, 4294967291<br>};<br>const unsigned long* first = __stl_prime_list;<br>const unsigned long* last = __stl_prime_list + __stl_num_primes;<br>const unsigned long* pos = lower_bound(first, last, n);<br>return pos == last ? *(last &#8211; 1) : *pos;<br>}<\/p>\n\n\n\n<p>\u4e0b\u9762\u662f\u8282\u70b9\u5b9a\u4e49<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>template<class K,class V>\nstruct HashNode {\n\tpair<K, V>_kv;\n\tHashNode<K, V>* _next;\n\tHashNode(const pair<K,V>&kv)\n\t\t:_kv(kv)\n\t\t,_next(nullptr)\n\t{ }\n};\n\/\/\u8282\u70b9\u5b9a\u4e49\u4e0e\u524d\u9762hash\u8868\u4e00\u81f4\uff0c\u4f46\u662f\u591a\u4e86\u4e00\u4e2a\u8282\u70b9\u6307\u9488\uff0c\u6307\u5411\u4e0b\u4e00\u4e2a\u8282\u70b9\n<\/code><\/pre>\n\n\n\n<p>\u5176\u4e2d\u6700\u6838\u5fc3\u548c\u51fd\u6570\u5c31\u662f\u63d2\u5165\u51fd\u6570Insert:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>bool Insert(const pair<K, V>& kv) {\n\tif (Find(kv.first))return false;\n\t\/\/\u8d1f\u8f7d\u56e0\u5b50==1\u65f6\u6269\u5bb9\n\tif (_n == _tables.size()) {\n\t\t\/\/Hash<K, V> newht;\n\t\tvector<Node*>newTable(__stl_next_prime(_tables.size() + 1));\n\t\tfor (size_t i = 0; i < _tables.size(); i++) {\n\t\t\tNode* cur = _tables[i];\n\t\t\twhile (cur) {\n\t\t\t\t\/\/\u5934\u63d2\u5230\u65b0\u8868\n\t\t\t\tNode* next = cur->_next;\n\t\t\t\tsize_t hashi = cur->_kv.first % newTable.size();\n\t\t\t\tcur->_next = newTable[hashi];\n\t\t\t\tnewTable[hashi] = cur;\n\t\t\t\tcur = next;\n\t\t\t}\n\t\t\t_tables[i] = nullptr;\n\t\t}\n\t\t_tables.swap(newTable);\n\t}\n\tsize_t hashi = kv.first % _tables.size();\n\t\/\/\u5934\u63d2\n\tNode* newnode = new Node(kv);\n\tnewnode->_next = _tables[hashi];\n\t_tables[hashi] = newnode;\n\t++_n;\n\treturn true;\n};\n\u5728\u63d2\u5165\u65f6\u5fc5\u987b\u5148\u68c0\u67e5\u51fd\u6570\u7684\u8d1f\u8f7d\u56e0\u5b50\u5f53\u8d1f\u8f7d\u56e0\u5b50\u7b49\u4e8e1\u65f6\u5c31\u5fc5\u987b\u8981\u6269\u5bb9\u4e86\uff0c\u6269\u5bb9\u6570\u91cf\u4e3a\u4e0a\u9762\u8d28\u6570\u8868\u4e2d\u7684\u4e0b\u4e00\u4e2a\u6570\u636e\uff0c\u4f46\u662f\u5f53\u6269\u5bb9\u65f6\u5c31\u4e0d\u548c\u54c8\u5e0c\u8868\u4e00\u6837\u62f7\u8d1d\u8fc7\u53bb\uff0c\u518d\u7528swap\u4ea4\u6362\uff0c\u8fd9\u91cc\u53ef\u4ee5\u76f4\u63a5\u5c06\u65e7\u8868\u7684\u6570\u636e\u63d2\u5165\u65b0\u8868\u3002\nFind\u51fd\u6570\u5c31\u4e0d\u8d58\u8ff0\u4e86\uff0c\u770b\u4ee3\u7801\u5f88\u5bb9\u6613\u660e\u767d\nNode* Find(const K& key) {\n\tsize_t hashi = key % _tables.size();\n\tNode* cur = _tables[hashi];\n\twhile (cur) {\n\t\tif (cur->_kv.first == key) {\n\t\t\treturn cur;\n\t\t}\n\t\tcur = cur->_next;\n\t}\n\treturn nullptr;\n}\n\u5220\u9664\u51fd\u6570\u5fc5\u987b\u5206\u4e24\u79cd\u60c5\u51b5\uff0c\u5220\u9664\u7684\u8282\u70b9\u662fvector\u4e2d\u7684\u8282\u70b9\uff0c\u6216\u662f\u4e2d\u95f4\u8282\u70b9\uff0c\u5982\u679c\u662fvector\u91cc\u9762\u7684\u8282\u70b9\u5c31\u5fc5\u987b\u5148\u5c06cur\u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\u653e\u5165\u91cc\u9762\u3002\nbool Erase(const K& key) {\n\tsize_t hashi = key % _tables.size();\n\tNode* prev = nullptr;\n\tNode* cur = _tables[hashi];\n\tif (cur) {\n\t\tif (cur->_kv.first == key) {\n\t\t\t\/\/\u5220\u9664\n\t\t\tif (prev == nullptr) {\/\/\u5220\u9664\u7684\u662f\u5934\u7ed3\u70b9\n\t\t\t\t_tables[hashi] = cur->_next;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tprev->_next = cur->_next;\n\t\t\t}\n\t\t\tdelete cur;\n\t\t\treturn true;\n\t\t}\n\t\telse {\n\t\t\tprev = cur;\n\t\t\tcur = cur->_next;\n\t\t}\n\t}\n\treturn false;\n}\n\u5b8c\u6574\u4ee3\u7801\u5728gitee\u91cc\u9762\u81ea\u53d6\n\u4eca\u5929\u7684\u66f4\u65b0\u5c31\u5230\u8fd9\u91cc\u4e86\uff0c\u5982\u6709\u9519\u8bef\u6b22\u8fce\u6307\u51fa<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5df2\u4e0b\u4ee3\u7801\u5df2\u5728gitee\u4e0a\u5f00\u6e90\u5382\u5e93\u5730\u5740https:\/\/gitee.com\/tgwTTT\/c-lreant\/blo&#8230;<\/p>\n","protected":false},"author":1,"featured_media":159,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-157","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/157","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=157"}],"version-history":[{"count":6,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/157\/revisions"}],"predecessor-version":[{"id":876,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/157\/revisions\/876"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/media\/159"}],"wp:attachment":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=157"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=157"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=157"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}