大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE稳定放心使用
谷歌变种murmurHash的编写与测试
#include<iostream>
#include<stdint.h>
#include<cstring>
#include<time.h>
#include<iterator>
#include<vector>
#include<algorithm>
using namespace std;
bool CheckLittleEndin();
uint32_t DecodeFixed32(const char* data);
uint32_t Hash(const char* data,int size,int seed);
int main(){
const char* data="assdascxasasdz" ;
vector<uint32_t> data_;
vector<uint32_t>::iterator iter;
vector<uint32_t>::iterator temp;
int size = strlen(data);
clock_t start = clock();
for(int i = 1 ;i<1000000; i++){
data_.push_back(Hash(data,size,i));
}
sort(data_.begin(),data_.end());
for(iter = data_.begin();iter!=data_.end();iter++){
cout<<*iter<<endl;
}
temp = unique(data_.begin(),data_.end());
cout<<"the last is"<<*(--temp);
clock_t end = clock();
cout<<endl;
cout<<double(end-start)<<"ms";
return 0;
}
bool CheckLittleEndin(){
int test = 1;
int temp = (unsigned char) test;
if(test) return true;
else return false;
}
uint32_t DecodeFixed32(const char* data){
int result;
if(CheckLittleEndin()){
memcpy(&result,data,sizeof(result));
}
else{
return ((static_cast<uint32_t>(static_cast<unsigned char>(data[0])))
| (static_cast<uint32_t>(static_cast<unsigned char>(data[1])) << 8)
| (static_cast<uint32_t>(static_cast<unsigned char>(data[2])) << 16)
| (static_cast<uint32_t>(static_cast<unsigned char>(data[3])) << 24));
}
}
//32bit
uint32_t Hash(const char* data, int size ,int seed){
const uint32_t r = 24;
const int m = 0xc6a4a793;
const char* limit = data + size;
uint32_t h = seed^(size*m);
while(data+4<limit){
uint32_t w = DecodeFixed32(data);
data = data+4;
h += w; //循环操作;
h *= m;
h ^= (h >> 16);
}
switch (limit - data) {
case 3:
h += static_cast<unsigned char>(data[2]) << 16;
break;
case 2:
h += static_cast<unsigned char>(data[1]) << 8;
break;
case 1:
h += static_cast<unsigned char>(data[0]);
h *= m;
h ^= (h >> r); //仅剩余一字节;
break;
}
return h;
}
//你可以修改参数自己测试下,散列性与且计算很高效!
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/180437.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...