#include "1MemBF.h"


OMABF::OMABF()
{
}
OMABF::~OMABF()
{
	delete[] bf_base;
}

OMABF::OMABF(uint bf_m, uint bf_k) {
	this->bf_m = bf_m;
	this->bf_k = bf_k;
	this->bf_n = 0;

	this->bf_base = new uint64[bf_m/64+1];
	memset(bf_base, 0, sizeof(uint64) * (bf_m/64+1)); //sizeof(uint64) = 8

	querymemAccNum = 0;

	wordSelectHash.initialize(8); //prime[8] = 23~~~

	int cnt = 0;
	for (uint i = 0; i < bf_k; i++)
		bf_hfp[i].initialize(cnt++);

}

uint OMABF::insert(const unsigned char * str, uint len) {

	uint wp = wordSelectHash.run(str, len) % (bf_m/64+1);

	uint64 tmp = bf_base[wp]; //Read memory

	for (uint i = 0; i < bf_k; i++) {
		uint64 value = bf_hfp[i].run(str, len) % WORD_LEN;
		tmp |= (9223372036854775808 >> value);
	}
	bf_base[wp] = tmp; //write memory

	bf_n++;
	return 1;
}

uint OMABF::query(const unsigned char * str, uint len) {

	uint wp = wordSelectHash.run(str, len) % (bf_m/64+1);
	uint64 tmp = bf_base[wp]; //Read memory

	querymemAccNum++;

	for (uint i = 0; i < bf_k; i++){
		uint64 value = bf_hfp[i].run(str, len) % WORD_LEN;
		if (0 == (tmp & (9223372036854775808 >> value)))return 0;
	}
	return 1;
}



