Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
Donate
Please sign in before you donate.
Scan WeChat QR to Pay
Cancel
Complete
Prompt
Switch to Alipay.
OK
Cancel
1 Star 0 Fork 265

yt/python-learn

forked from mktime/python-learn
Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
文件
master
Branches (1)
master
master
Branches (1)
master
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
master
Branches (1)
master
python-learn
/
bloom_1.cpp
python-learn
/
bloom_1.cpp
bloom_1.cpp 6.10 KB
Copy Edit Raw Blame History
mr.github authored 2015年05月21日 13:41 +08:00 . bloom filter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
//关于布隆过滤器在URL去重中的应用
//问题背景
/**
假设你想从网上(新浪新闻)去下载一批网页,做信息检索(搜索引擎)的第一步.
你已经从网上下载下来了一批网页,并且有这批网页的URL,不过你还有一批需要下载的网页的URL,
问题是这样的,如果有些URL已经被下载过了,你就不必要再次下载了,现在让你快速的识别
出哪些URL上的是还没被下载的,可以有一定的误差,但是不能超过1%。.
**/
//本例中URL初始数量为20万条 ,如果有其它规模的数据,可以将具体参数进行相应更改
//测试URL数量为186083条
//请使用标准C++进行编译
//更多hash函数请登录 泪下的天空
//原代码高亮显示
#include <string>
#include <iostream>
#include <assert.h>
#include <fstream>
#include <time.h>
using namespace std;
#define FUNC_NUM 8
#define BIT_MAX 3999949 //这是一个素数,why?
const int HASH_SIZE = BIT_MAX / 8 + 1;
char hash[HASH_SIZE];
int strInt[FUNC_NUM];
//以下标<<[1-8]>>数字的是字符串散列函数,本程序中我使用了8个散列函数
//<<1>>
unsigned int RSHash(const std::string& str)
{
unsigned int b = 378551;
unsigned int a = 63689;
unsigned int hash = 0;
for(std::size_t i = 0; i < str.length(); i++)
{
hash = hash * a + str[i];
a = a * b;
}
return hash;
}
//<<2>>
unsigned int JSHash(const std::string& str)
{
unsigned int hash = 1315423911;
for(std::size_t i = 0; i < str.length(); i++)
{
hash ^= ((hash << 5) + str[i] + (hash >> 2));
}
return hash;
}
//<<3>>
unsigned int PJWHash(const std::string& str)
{
unsigned int BitsInUnsignedInt = (unsigned int)(sizeof(unsigned int) * 8);
unsigned int ThreeQuarters = (unsigned int)((BitsInUnsignedInt * 3) / 4);
unsigned int OneEighth = (unsigned int)(BitsInUnsignedInt / 8);
unsigned int HighBits = (unsigned int)(0xFFFFFFFF) << (BitsInUnsignedInt - OneEighth);
unsigned int hash = 0;
unsigned int test = 0;
for(std::size_t i = 0; i < str.length(); i++)
{
hash = (hash << OneEighth) + str[i];
if((test = hash & HighBits) != 0)
{
hash = (( hash ^ (test >> ThreeQuarters)) & (~HighBits));
}
}
return hash;
}
//<<4>>
unsigned int APHash(const std::string& str)
{
unsigned int hash = 0xAAAAAAAA;
for(std::size_t i = 0; i < str.length(); i++)
{
hash ^= ((i & 1) == 0) ? ( (hash << 7) ^ str[i] * (hash >> 3)) :
(~((hash << 11) + (str[i] ^ (hash >> 5))));
}
return hash;
}
//<<5>>
unsigned int BKDRHash(const std::string& str)
{
unsigned int seed = 131; // 31 131 1313 13131 131313 etc..
unsigned int hash = 0;
for(std::size_t i = 0; i < str.length(); i++)
{
hash = (hash * seed) + str[i];
}
return hash;
}
//<<6>>
unsigned int SDBMHash(const std::string& str)
{
unsigned int hash = 0;
for(std::size_t i = 0; i < str.length(); i++)
{
hash = str[i] + (hash << 6) + (hash << 16) - hash;
}
return hash;
}
//<<7>>
unsigned int FNVHash(const std::string& str)
{
const unsigned int fnv_prime = 0x811C9DC5;
unsigned int hash = 0;
for(std::size_t i = 0; i < str.length(); i++)
{
hash *= fnv_prime;
hash ^= str[i];
}
return hash;
}
//<<8>>
unsigned int Hflp(string str){
unsigned int len = str.length();
unsigned int sum = 0;
for(std::size_t i=0;i<len;i++){
sum ^= str[i] << (8*(i%4));
}
return sum & 0x7FFFFFFF;
}
//更多hash函数请登录 http://jinyun2012.blog.sohu.com
//将一个具体的url散列成一组整数
void getIntSet(string url , int * set)
{
set[0] = RSHash(url) % BIT_MAX;
set[1] = JSHash(url) % BIT_MAX;
set[2] = PJWHash(url) % BIT_MAX;
set[3] = APHash(url) % BIT_MAX;
set[4] = BKDRHash(url) % BIT_MAX;
set[5] = SDBMHash(url) % BIT_MAX;
set[6] = FNVHash(url) % BIT_MAX;
set[7] = Hflp(url) % BIT_MAX;
}
//将每个url映射到hash数组中
void shadeHash(string url){
getIntSet(url , strInt);
for(int i=0;i<FUNC_NUM;i++){
int pos = (strInt[i] >> 3);
int mod = strInt[i] & 7;
int val = 1 << (7 - mod);
hash[pos] |= val;
}
}
//查找url是否存在于url.dat文件中
bool find(string url)
{
getIntSet(url , strInt);
bool res = true;
for(int i=0;i<FUNC_NUM && res == true; i++){
int pos = (strInt[i] >> 3);
int mod = strInt[i] & 7;
int val = 1 << (7 - mod);
res &= (bool)(hash[pos] & val);
}
return res;
}
int main(int argc, char* argv[])
{
ifstream url_in("url.dat");
assert(url_in != NULL);
string url;
int len(0);
time_t con_start = time(NULL);
while(getline(url_in , url)){
len += url.length();
shadeHash(url);
}
time_t con_end = time(NULL);
url_in.close();
//读取文件中测试数据
ifstream test_in("test_url.dat");
assert(test_in);
int count(0) , size(0);
time_t test_start = time(NULL);
while(getline(test_in , url)){
size++;
if(find(url))count++;
}
time_t test_end = time(NULL);
cout<<"测试URL数量:"<<size<<endl;
cout<<"错配URL数量:"<<count<<endl;
cout<<"错配概率 : "<<count * 1.0 / size<<endl << endl;
cout<<"关于优势--->"<<endl;
cout<<"原URL所占存储空间: "<<len <<" byte"<<endl;
cout<<"程序需要存储空间 : "<< HASH_SIZE<<" byte"<<endl;
cout<<"空间节约 : "<< 1.0 - HASH_SIZE * 1.0 / len <<endl;
cout<<"构造hash表所用时间 "<<(con_end - con_start)<<"s"<<endl;
cout<<"测试所用时间 "<<(test_end - test_start)<<"s"<<endl;
return 0;
}
Loading...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

About

deepseek 多轮对话,人脸检测及特征提取,个人AI助理,音频实时监听,opencv获取摄像头视频截图;Python基础编程示例:Excel读写追加处理,XML解析、JSON解析、FLV与MP4转换,PyQT界面应用程序开发示例等,https证书到期检测,糗百爬虫,pdf和图片互相转换,socket使用,百度OCR调用例子,IP及端口快速扫描。
Cancel

Releases

No release

Contributors

All

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/yt__num/python-learn.git
git@gitee.com:yt__num/python-learn.git
yt__num
python-learn
python-learn
master
Going to Help Center

Search

Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /