{"id":228,"date":"2025-08-03T19:52:29","date_gmt":"2025-08-03T11:52:29","guid":{"rendered":"http:\/\/www.tgwttt.xyz\/?p=228"},"modified":"2025-11-24T16:39:35","modified_gmt":"2025-11-24T08:39:35","slug":"%ef%bc%88%e5%93%88%e5%b8%8c%e8%a1%a8%e6%89%a9%e5%b1%95%ef%bc%89%e4%bd%8d%e5%9b%be-%e5%b8%83%e9%9a%86%e8%bf%87%e6%bb%a4%e5%99%a8","status":"publish","type":"post","link":"https:\/\/www.tgwttt.xyz\/?p=228","title":{"rendered":"\uff08\u54c8\u5e0c\u8868\u6269\u5c55\uff09\u4f4d\u56fe \u5e03\u9686\u8fc7\u6ee4\u5668"},"content":{"rendered":"\n<p>\u4ed3\u5e93\u5730\u5740 <a href=\"https:\/\/gitee.com\/tgwTTT\/data-structure\">https:\/\/gitee.com\/tgwTTT\/data-structure<\/a><\/p>\n\n\n\n<p>\u5728\u8ba1\u7b97\u673a\u79d1\u5b66\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u4e00\u4e2a\u95ee\u9898\uff1a\u5982\u4f55\u5224\u65ad\u4e00\u4e2a\u5143\u7d20\u662f\u5426\u5b58\u5728\u4e8e\u4e00\u4e2a\u96c6\u5408\u4e2d\uff1f<\/p>\n\n\n\n<p>\u5f53\u6570\u636e\u91cf\u8f83\u5c0f\u65f6\uff0c\u4f7f\u7528\u54c8\u5e0c\u8868\u6216\u5e73\u8861\u6811\u5c31\u80fd\u5f88\u597d\u89e3\u51b3\u3002\u4f46\u5f53\u6570\u636e\u91cf\u8fbe\u5230\u6570\u5341\u4ebf\u7ea7\u522b\u65f6\uff0c\u4f20\u7edf\u7684\u6570\u636e\u7ed3\u6784\u4f1a\u9047\u5230\u5185\u5b58\u74f6\u9888\u3002\u8fd9\u65f6\uff0c\u5e03\u9686\u8fc7\u6ee4\u5668\uff08Bloom Filter\uff09\u4ee5\u5176\u72ec\u7279\u7684\u6982\u7387\u578b\u6570\u636e\u7ed3\u6784\uff0c\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u4e00\u79cd\u4f18\u96c5\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n\n\n\n<p>\u5728\u7406\u89e3\u5e03\u9686\u8fc7\u6ee4\u5668\u4e4b\u524d\uff0c\u6211\u4eec\u5148\u770b\u5b83\u7684\u524d\u8eab\u2014\u2014\u4f4d\u56fe\uff08Bitmap\uff09\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u9996\u5148\u5148\u770b\u4e00\u4e2a\u9898\u76ee\uff1a<\/p>\n\n\n\n<p>\u7ed940\u4ebf\u4e2a\u4e0d\u91cd\u590d\u7684\u2f46\u7b26\u53f7\u6574\u6570\uff0c\u6ca1\u6392\u8fc7\u5e8f\u3002\u7ed9\u2f00\u4e2a\u2f46\u7b26\u53f7\u6574\u6570\uff0c\u5982\u4f55\u5feb\u901f\u5224\u65ad\u2f00\u4e2a\u6570\u662f\u5426\u5728\u8fd940\u4ebf\u4e2a\u6570\u4e2d\u3002<\/p>\n\n\n\n<p>\u89e3\u9898\u601d\u8def1\uff1a\u66b4\u2f12\u904d\u5386\uff0c\u65f6\u95f4\u590d\u6742\u5ea6O(N)\uff0c\u592a\u6162 <\/p>\n\n\n\n<p>\u89e3\u9898\u601d\u8def2\uff1a\u6392\u5e8f+\u2f06\u5206\u67e5\u627e\u3002\u65f6\u95f4\u590d\u6742\u5ea6\u6d88\u8017 O(N*logN) + O(logN) <\/p>\n\n\n\n<p>\u6df1\u2f0a\u5206\u6790\uff1a\u89e3\u9898\u601d\u8def2\u662f\u5426\u53ef\u2f8f\uff0c\u6211\u4eec\u5148\u7b97\u7b9740\u4ebf\u4e2a\u6570\u636e\u2f24\u6982\u9700\u8981\u591a\u5c11\u5185\u5b58\u3002 1G = 1024MB = 1024*1024KB = 1024*1024*1024Byte \u7ea6\u7b49\u4e8e10\u4ebf\u591aByte \u90a3\u4e4840\u4ebf\u4e2a\u6574\u6570\u7ea6\u7b49\u4e8e16G\uff0c\u8bf4\u660e40\u4ebf\u4e2a\u6570\u662f\u2f46\u6cd5\u76f4\u63a5\u653e\u5230\u5185\u5b58\u4e2d\u7684\uff0c\u53ea\u80fd\u653e\u5230\u786c\u76d8\u2f42\u4ef6\u4e2d\u3002\u2f7d\u2f06\u5206\u67e5\u627e\u53ea\u80fd\u5bf9\u5185\u5b58\u6570\u7ec4\u4e2d\u7684\u6709\u5e8f\u6570\u636e\u8fdb\u2f8f\u67e5\u627e\u3002<\/p>\n\n\n\n<p>\u89e3\u9898\u601d\u8def3\uff1a\u6570\u636e\u662f\u5426\u5728\u7ed9\u5b9a\u7684\u6574\u5f62\u6570\u636e\u4e2d\uff0c\u7ed3\u679c\u662f\u5728\u6216\u8005\u4e0d\u5728\uff0c\u521a\u597d\u662f\u4e24\u79cd\u72b6\u6001\uff0c\u90a3\u4e48\u53ef\u4ee5\u4f7f\u2f64\u2f00\u4e2a\u2f06\u8fdb\u5236\u2f50\u7279\u4f4d\u6765\u4ee3\u8868\u6570\u636e\u662f\u5426\u5b58\u5728\u7684\u4fe1\u606f\uff0c\u5982\u679c\u2f06\u8fdb\u5236\u2f50\u7279\u4f4d\u4e3a1\uff0c\u4ee3\u8868\u5b58\u5728\uff0c\u4e3a0\u4ee3\u8868\u4e0d\u5b58\u5728\u3002\u90a3\u4e48\u6211\u4eec\u8bbe\u8ba1\u2f00\u4e2a\u2f64\u4f4d\u8868\u2f70\u6570\u636e\u662f\u5426\u5b58\u5728\u7684\u6570\u636e\u7ed3\u6784\uff0c\u8fd9\u4e2a\u6570\u636e\u7ed3\u6784\u5c31\u53eb\u4f4d\u56fe\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f4d\u56fe\u7684\u6838\u5fc3\u601d\u60f3<\/h3>\n\n\n\n<p>\u4f4d\u56fe\u7528\u4e00\u4e2a\u6bd4\u7279\u4f4d\u6765\u8868\u793a\u4e00\u4e2a\u5143\u7d20\u662f\u5426\u5b58\u5728\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>     \u7d22\u5f15: 0 1 2 3 4 5 6 7\n     \u4f4d\u56fe: 0 0 1 0 1 0 0 1<\/code><\/pre>\n\n\n\n<p>\u4f4d\u56fe[2]=1 \u2192 \u8868\u793a\u6570\u5b572\u5b58\u5728\u4e8e\u96c6\u5408\u4e2d<\/p>\n\n\n\n<p>\u4f4d\u56fe[7]=1 \u2192 \u8868\u793a\u6570\u5b577\u5b58\u5728\u4e8e\u96c6\u5408\u4e2d<\/p>\n\n\n\n<p>\u5176\u4ed6\u4f4d\u4e3a0 \u2192 \u8868\u793a\u5bf9\u5e94\u6570\u5b57\u4e0d\u5b58\u5728<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f4d\u56fe\u7684\u5c40\u9650\u6027<\/h4>\n\n\n\n<p>\u4f4d\u56fe\u53ea\u80fd\u5904\u7406\u6574\u6570\uff0c\u4e14\u5f53\u6570\u503c\u8303\u56f4\u5f88\u5927\u65f6\uff08\u598264\u4f4d\u6574\u6570\uff09\uff0c\u4f4d\u56fe\u4f1a\u53d8\u5f97\u6781\u5176\u7a00\u758f\uff1a<\/p>\n\n\n\n<p>\u8981\u8868\u793a\u96c6\u5408{1, 1000000}\uff0c\u9700\u89811,000,001\u4f4d\uff0c\u4f46\u53ea\u4f7f\u7528\u4e862\u4f4d<\/p>\n\n\n\n<p>\u5185\u5b58\u6d6a\u8d39\u4e25\u91cd\uff1a1MB\u53ea\u80fd\u8868\u793a800\u4e07\u4e2a\u6574\u6570\u72b6\u6001<\/p>\n\n\n\n<p>\u4e0b\u9762\u662f\u4f4d\u56fe\u4ee3\u7801\u5c55\u793a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#pragma once\n#include<iostream>\n#include<vector>\n#include<string>\nusing namespace std;\nnamespace bit {\n\ttemplate<size_t N>\n\tclass bitmap {\n\tpublic:\n\t\tbitmap() {\n\t\t\tvt.resize(N \/ 32 + 1);\n\t\t}\n\t\tvoid set(int key) {\n\t\t\tint i = key \/ 32;\n\t\t\tint j = key % 32;\n\t\t\tvt[i] |= (1 << j);\n\t\t}\n\t\tvoid reset(int key) {\n\t\t\tint i = key \/ 32;\n\t\t\tint j = key % 32;\n\t\t\tvt[i] &#038;= ~(1 << j);\n\t\t}\n\t\tbool test(int key) {\n\t\t\tint i = key \/ 32;\n\t\t\tint j = key % 32;\n\t\t\treturn vt[i] &#038; (1 << j);\n\t\t}\n\tprivate:\n\t\tvector<int>vt;\n\t};\n}\nvoid testbitmap() {\n\tint num = 1e9;\n\tbit::bitmap<0xffffffff>mp;\n\tfor (int i = 0; i < num; i++) {\n\t\tmp.set(rand() + i);\n\t}\n\tmp.reset(1002);\n\tif (mp.test(1002)) {\n\t\tcout << \"1002\u5728\u6570\u7ec4\u4e2d\" << endl;\n\t}\n\telse {\n\t\tcout << \"\u4e0d\u5728\" << endl;\n\t}\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u5e03\u9686\u8fc7\u6ee4\u5668\uff1a\u4f4d\u56fe\u7684\u8fdb\u5316<\/h3>\n\n\n\n<p>\u5e03\u9686\u8fc7\u6ee4\u5668\u901a\u8fc7\u591a\u4e2a\u54c8\u5e0c\u51fd\u6570\u548c\u4f4d\u6570\u7ec4\u7684\u7ec4\u5408\uff0c\u89e3\u51b3\u4e86\u4f4d\u56fe\u7684\u6269\u5c55\u6027\u95ee\u9898\u3002<\/p>\n\n\n\n<ul start=\"1\" class=\"wp-block-list\">\n<li>\u521d\u59cb\u5316\uff1a\u521b\u5efa\u4e00\u4e2am\u4f4d\u7684\u4f4d\u6570\u7ec4\uff0c\u6240\u6709\u4f4d\u521d\u59cb\u4e3a0<\/li>\n\n\n\n<li>\u6dfb\u52a0\u5143\u7d20\uff1a\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528k\u4e2a\u4e0d\u540c\u7684\u54c8\u5e0c\u51fd\u6570\u8ba1\u7b97\u5143\u7d20\u7684k\u4e2a\u54c8\u5e0c\u503c<\/li>\n\n\n\n<li>\u5c06\u6bcf\u4e2a\u54c8\u5e0c\u503c\u5bf9\u5e94\u7684\u4f4d\u8bbe\u4e3a1<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>\u67e5\u8be2\u5143\u7d20\uff1a\n<ul class=\"wp-block-list\">\n<li>\u8ba1\u7b97\u5143\u7d20\u7684k\u4e2a\u54c8\u5e0c\u503c<\/li>\n\n\n\n<li>\u68c0\u67e5\u5bf9\u5e94\u4f4d\u662f\u5426\u5168\u4e3a1\uff1a<\/li>\n\n\n\n<li>\u5982\u679c\u6709\u4efb\u610f\u4e00\u4f4d\u4e3a0 \u2192 \u5143\u7d20\u4e00\u5b9a\u4e0d\u5b58\u5728<\/li>\n\n\n\n<li>\u5982\u679c\u5168\u4e3a1 \u2192 \u5143\u7d20\u53ef\u80fd\u5b58\u5728\uff08\u53ef\u80fd\u6709\u8bef\u5224\uff09<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u5b9e\u9645\u5e94\u7528\u573a\u666f\uff1a<\/h4>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li>\u6570\u636e\u5e93\u7cfb\u7edf\uff1a\u5982Google Bigtable\u3001HBase\u7528\u5e03\u9686\u8fc7\u6ee4\u5668\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u78c1\u76d8\u67e5\u627e<\/li>\n\n\n\n<li>\u7f51\u7edc\u722c\u866b\uff1a\u68c0\u6d4bURL\u662f\u5426\u5df2\u722c\u53d6<\/li>\n\n\n\n<li>\u5783\u573e\u90ae\u4ef6\u8fc7\u6ee4\uff1a\u68c0\u67e5\u53d1\u4ef6\u4eba\u662f\u5426\u5728\u9ed1\u540d\u5355<\/li>\n\n\n\n<li>\u533a\u5757\u94fe\uff1a\u6bd4\u7279\u5e01\u7528\u5e03\u9686\u8fc7\u6ee4\u5668\u52a0\u901f\u94b1\u5305\u540c\u6b65<\/li>\n\n\n\n<li>CDN\u7f13\u5b58\uff1a\u5224\u65ad\u8d44\u6e90\u662f\u5426\u5df2\u5728\u8fb9\u7f18\u8282\u70b9\u7f13\u5b58<\/li>\n<\/ol>\n\n\n\n<p>\u4e0b\u9762\u662f\u5e03\u9686\u8fc7\u6ee4\u5668\u4ee3\u7801\u5c55\u793a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#pragma once\n#include\"Hash.h\"\n#include<functional>\nstruct HashFuncBKDR\n{\n\t\/\/ @detail \u672c \u7b97\u6cd5\u7531\u4e8e\u5728Brian Kernighan\u4e0eDennis Ritchie\u7684\u300aThe CProgramming Language\u300b\n\t\/\/ \u4e00\u4e66\u88ab\u5c55\u793a\u800c\u5f97 \u540d\uff0c\u662f\u4e00\u79cd\u7b80\u5355\u5feb\u6377\u7684hash\u7b97\u6cd5\uff0c\u4e5f\u662fJava\u76ee\u524d\u91c7\u7528\u7684\u5b57\u7b26\u4e32\u7684Hash\u7b97\u6cd5\u7d2f\u4e58\u56e0\u5b50\u4e3a31\u3002\n\tsize_t operator()(const std::string& s)\n\t{\n\t\tsize_t hash = 0;\n\t\tfor (auto ch : s)\n\t\t{\n\t\t\thash *= 31;\n\t\t\thash += ch;\n\t\t}\n\t\treturn hash;\n\t}\n};\nstruct HashFuncAP\n{\n\t\/\/ \u7531Arash Partow\u53d1\u660e\u7684\u4e00\u79cdhash\u7b97\u6cd5\u3002  \n\tsize_t operator()(const std::string& s)\n\t{\n\t\tsize_t hash = 0;\n\t\tfor (size_t i = 0; i < s.size(); i++)\n\t\t{\n\t\t\tif ((i &#038; 1) == 0) \/\/ \u5076\u6570\u4f4d\u5b57\u7b26\n\t\t\t{\n\t\t\t\thash ^= ((hash << 7) ^ (s[i]) ^ (hash >> 3));\n\t\t\t}\n\t\t\telse              \/\/ \u5947\u6570\u4f4d\u5b57\u7b26\n\t\t\t{\n\t\t\t\thash ^= (~((hash << 11) ^ (s[i]) ^ (hash >> 5)));\n\t\t\t}\n\t\t}\n\n\t\treturn hash;\n\t}\n};\nstruct HashFuncDJB\n{\n\t\/\/ \u7531Daniel J. Bernstein\u6559\u6388\u53d1\u660e\u7684\u4e00\u79cdhash\u7b97\u6cd5\u3002 \n\tsize_t operator()(const std::string& s)\n\t{\n\t\tsize_t hash = 5381;\n\t\tfor (auto ch : s)\n\t\t{\n\t\t\thash = hash * 33 ^ ch;\n\t\t}\n\n\t\treturn hash;\n\t}\n};\n\ntemplate<size_t N,size_t X=3, class K=string,class hash1= HashFuncBKDR\n\t,class hash2= HashFuncAP,class hash3= HashFuncDJB>\nclass BloomFilter {\npublic:\n\tvoid set(K key) {\n\t\tsize_t num1 = hash1()(key) % M;\n\t\tsize_t num2 = hash2()(key) % M;\n\t\tsize_t num3 = hash3()(key) % M;\n\t\tmp.set(num1);\n\t\tmp.set(num2);\n\t\tmp.set(num3);\n\t}\n\tbool Test(K key) {\n\t\tsize_t num1 = hash1()(key) % M;\n\t\tif (!mp.test(num1))return false;\n\t\tsize_t num2 = hash2()(key) % M;\n\t\tif (!mp.test(num2))return false;\n\t\tsize_t num3 = hash3()(key) % M;\n\t\tif (!mp.test(num3))return false;\n\t\treturn true;\n\t}\n\tdouble getFalseProbability()\n\t{\n\t\tdouble p = pow((1.0 - pow(2.71, -3.0 \/ X)), 3.0);\n\n\t\treturn p;\n\t}\nprivate:\n\tstatic const size_t M = N * X;\n\tbit::bitmap<M>mp;\n};\nvoid TestBloomFilter1()\n{\n\tBloomFilter<10> bf;\n\tbf.set(\"\u732a\u516b\u6212\");\n\tbf.set(\"\u5b59\u609f\u7a7a\");\n\tbf.set(\"\u5510\u50e7\");\n\n\tcout << bf.Test(\"\u732a\u516b\u6212\") << endl;\n\tcout << bf.Test(\"\u5b59\u609f\u7a7a\") << endl;\n\tcout << bf.Test(\"\u5510\u50e7\") << endl;\n\tcout << bf.Test(\"\u6c99\u50e7\") << endl;\n\tcout << bf.Test(\"\u732a\u516b\u62121\") << endl;\n\tcout << bf.Test(\"\u732a\u6212\u516b\") << endl;\n}\n<\/code><\/pre>\n\n\n\n<p>\u4eca\u5929\u7684\u66f4\u65b0\u5c31\u5230\u8fd9\u91cc\uff0c\u5982\u6709\u9519\u8bef\u6b22\u996e\u8bc4\u8bba\u533a\u6307\u51fa\uff01\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ed3\u5e93\u5730\u5740 https:\/\/gitee.com\/tgwTTT\/data-structure \u5728\u8ba1\u7b97\u673a\u79d1\u5b66\u4e2d\uff0c\u6211\u4eec&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"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":[4],"tags":[],"class_list":["post-228","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/228","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=228"}],"version-history":[{"count":4,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/228\/revisions"}],"predecessor-version":[{"id":858,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/228\/revisions\/858"}],"wp:attachment":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}