Menu

[6a1b37]: / dcpp / HashBloom.cpp  Maximize Restore History

Download this file

99 lines (82 with data), 2.2 kB

 1 2 3 4 5 6 7 8 91011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
/*
* Copyright (C) 2001-2024 Jacek Sieka, arnetheduck on gmail point com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include"stdinc.h"
#include"HashBloom.h"
#include<cmath>
#include"MerkleTree.h"
namespacedcpp{
size_tHashBloom::get_k(size_tn,size_th){
for(size_tk=TTHValue::BITS/h;k>1;--k){
uint64_tm=get_m(n,k);
if(m>>24==0){
returnk;
}
}
return1;
}
uint64_tHashBloom::get_m(size_tn,size_tk){
uint64_tm=(static_cast<uint64_t>(ceil(static_cast<double>(n)*k/log(2.))));
// 64-bit boundary as per spec
return((m+63ULL)/64ULL)*64ULL;
}
voidHashBloom::add(constTTHValue&tth){
for(size_ti=0;i<k;++i){
bloom[pos(tth,i)]=true;
}
}
boolHashBloom::match(constTTHValue&tth)const{
if(bloom.empty()){
returnfalse;
}
for(size_ti=0;i<k;++i){
if(!bloom[pos(tth,i)]){
returnfalse;
}
}
returntrue;
}
voidHashBloom::push_back(boolv){
bloom.push_back(v);
}
voidHashBloom::reset(size_tk_,size_tm,size_th_){
bloom.resize(m);
k=k_;
h=h_;
}
size_tHashBloom::pos(constTTHValue&tth,size_tn)const{
if((n+1)*h>TTHValue::BITS){
return0;
}
uint64_tx=0;
size_tstart=n*h;
for(size_ti=0;i<h;++i){
size_tbit=start+i;
size_tbyte=bit/8;
size_tpos=bit%8;
if(tth.data[byte]&(1<<pos)){
x|=(1LL<<i);
}
}
returnx%bloom.size();
}
voidHashBloom::copy_to(ByteVector&v)const{
v.resize(bloom.size()/8);
for(size_ti=0;i<bloom.size();++i){
v[i/8]|=bloom[i]<<(i%8);
}
}
}
close