filter.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #include<iostream>
  2. #include<cmath>
  3. #include<cstdlib>
  4. #include<cstring>
  5. #include<exception>
  6. #include"../basic/nanxing_operator_check.h"
  7. namespace nanxing_extend
  8. {
  9. //这是个意外,本来是想工厂类和不同的过滤器划分到不同文件的,但是由于贯彻head_only,还没想出来怎么组织文件,只能丢在一起了
  10. //同时也是一个控制类,即这个类本身带有控制功能,只允许一次产生持有唯一的filter
  11. //如果不对产生的类进行管控,最后的结果很可能是完全无法处理,因为C++中内存分配方式过于多样,malloc,new,new[],数组。。。。乱七八糟,最后类内生成的空间完全无法析构,因为根本不知道类内怎么实现的
  12. //当然也可以通过返回智能指针的方式进行管控,但是没有选择那么做的原因在于过滤器本身不会大量存在
  13. //一个程序中最多一到两个过滤器
  14. //但是但是相信我不会有God class
  15. template<typename K>
  16. class FilterPolicy
  17. {
  18. static_assert(NANXING_BASIC_OPERATOR_(K,compare),"the type of key cannot compare");
  19. private:
  20. FilterPolicy& operator=(FilterPolicy const&)=delete;
  21. FilterPolicy(FilterPolicy const&)=delete;
  22. FilterPolicy* producter=nullptr; //用于管控生成的类
  23. public:
  24. //限制当主动生成一个工厂类后,不管怎么赋值,最后只有一个工厂
  25. //move-only
  26. FilterPolicy(){};
  27. FilterPolicy(FilterPolicy&&){}
  28. virtual void policy_print(){producter->policy_print();} //输出对应的计算公式
  29. virtual void parameter_print(){producter->parameter_print();} //输出过滤器的参数
  30. virtual void insert(K key)noexcept{};
  31. //两个工厂函数用于生成不同的过滤器
  32. FilterPolicy* creat_Bloomfilter(int N,int P);
  33. FilterPolicy* creat_Cuckoofilter();
  34. virtual void init_filter(){producter->init_filter();} //初始化过滤器
  35. virtual ~FilterPolicy(){};
  36. };
  37. //bloomfilter
  38. template<typename K>
  39. class bloomfilter:public FilterPolicy<K>
  40. {
  41. static_assert(NANXING_BASIC_OPERATOR_(K,compare),"the type of key cannot compare");
  42. private:
  43. //这里使用线性混合同余来构造哈希函数,因为线性混合同余的随机性和运算效率的平衡比较好
  44. constexpr static const int hash_prime_number1[10]={52223,52237,79939,19937,8243,24133,12647,13147,62459,94547}; //给出10个大素数作为哈希函数的素数种子
  45. constexpr static const int hash_prime_number2[10]={94399,94421,94427,94433,94439,94441,94447,94463,94477,94483}; //哈希函数的置偏值
  46. struct param{
  47. float P; //概率参数
  48. int k=0; //哈希函数个数
  49. int n; //n数据长度
  50. int m=0; //bloomfilter长度
  51. param(float _p,int _n):P(_p),n(_n){}
  52. };
  53. private:
  54. char* bitarray; //bloomfilter的核心数据结构,一个比特数组
  55. param parameter;
  56. protected:
  57. bloomfilter()=delete;
  58. bloomfilter(bloomfilter&)=delete;
  59. bloomfilter& operator=(bloomfilter)=delete; //不允许复制构造
  60. void caculater()noexcept //计算bloomfilter的参数
  61. {
  62. this->parameter.m=static_cast<int>(-(parameter.n*std::log(parameter.P)/0.4804530139));
  63. this->parameter.k=static_cast<int>(0.6931471806*(parameter.m/parameter.n));
  64. }
  65. int hash_function(int i,K data) //i代表第i个哈希函数,返回在bloomfilter中的位置
  66. {
  67. return ((static_cast<int>(data))*(this->hash_prime_number1[i])+(this->hash_prime_number2[i]))%(this->parameter.m);
  68. }
  69. public:
  70. bloomfilter(float _p,int _n):bitarray(nullptr),parameter(_p,_n){}
  71. void init_filter()override{
  72. LOOP:
  73. caculater();
  74. LOOP1:
  75. try{
  76. bitarray=new char[static_cast<int>(parameter.m/8)+1]; //构建数组
  77. }
  78. catch(std::bad_alloc)
  79. {
  80. char tmp_input;
  81. std::cerr<<"May not have enough memory to use"<<std::endl;
  82. std::cerr<<"if you want to try again,please input r"<<std::endl;
  83. std::cerr<<"if you want to exit please input e"<<std::endl;
  84. std::cerr<<"if you want to use less bloomfilter with higher error rates,please input p"<<std::endl;
  85. std::cin>>tmp_input;
  86. switch(tmp_input)
  87. {
  88. case 'r':
  89. goto LOOP1;
  90. break;
  91. case 'e':
  92. std::terminate();
  93. break;
  94. case 'p':
  95. std::cerr<<"please input the new P by float"<<std::endl;
  96. std::cin>>this->parameter.P;
  97. goto LOOP;
  98. break;
  99. }
  100. }
  101. std::memset(bitarray,0,sizeof(char)*static_cast<int>(parameter.m/8)+1); //初始化
  102. }
  103. void policy_print()override //打印出使用的公式
  104. {
  105. std::cout<<"m=-((nlnP)/((ln2)^2))"<<"//m为bloomfilter的长度"<<std::endl;
  106. std::cout<<"k=ln2*(m/n)"<<"//k为所需的哈希函数的个数"<<std::endl;
  107. }
  108. void insert(K key)noexcept override
  109. {
  110. if(this->parameter.k==0)
  111. {
  112. std::cerr<<"the filter never init,and the filter is useless."<<std::endl;
  113. return;
  114. }
  115. for(int i=0;i<this->parameter.k;i++)
  116. {
  117. int tmp=this->hash_function(i,key);
  118. int filter_u=static_cast<int>(tmp/8);
  119. int move=tmp%8;
  120. this->bitarray[filter_u]||('1'<<(7-move));
  121. }
  122. }
  123. virtual ~bloomfilter()
  124. {
  125. if(bitarray!=nullptr)
  126. {
  127. delete[] bitarray;
  128. }
  129. }
  130. };
  131. //布谷鸟过滤器
  132. template<typename K>
  133. class Cuckoofilter:public FilterPolicy<K>
  134. {
  135. static_assert(NANXING_BASIC_OPERATOR_(K,compare),"the type of key cannot compare");
  136. };
  137. template<typename K>
  138. FilterPolicy<K>* FilterPolicy<K>::creat_Bloomfilter(int N,int P)
  139. {
  140. if(producter!=nullptr)
  141. {
  142. delete producter;
  143. }
  144. producter=new bloomfilter<K>(N,P);
  145. return producter;
  146. }
  147. template<typename K>
  148. FilterPolicy<K>* FilterPolicy<K>::creat_Cuckoofilter()
  149. {
  150. if(producter!=nullptr)
  151. {
  152. delete producter;
  153. }
  154. producter=new Cuckoofilter<K>;
  155. return producter;
  156. }
  157. }