Bitslice.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include "Bitvec.h"
  2. #include <vector>
  3. #include <assert.h>
  4. // handle xnor
  5. typedef std::vector<uint32_t> slice;
  6. typedef std::vector<slice> slice_vec;
  7. int countbits ( slice & v )
  8. {
  9. int c = 0;
  10. for(size_t i = 0; i < v.size(); i++)
  11. {
  12. int d = countbits(v[i]);
  13. c += d;
  14. }
  15. return c;
  16. }
  17. int countxor ( slice & a, slice & b )
  18. {
  19. assert(a.size() == b.size());
  20. int c = 0;
  21. for(size_t i = 0; i < a.size(); i++)
  22. {
  23. int d = countbits(a[i] ^ b[i]);
  24. c += d;
  25. }
  26. return c;
  27. }
  28. void xoreq ( slice & a, slice & b )
  29. {
  30. assert(a.size() == b.size());
  31. for(size_t i = 0; i < a.size(); i++)
  32. {
  33. a[i] ^= b[i];
  34. }
  35. }
  36. //-----------------------------------------------------------------------------
  37. // Bitslice a hash set
  38. template< typename hashtype >
  39. void Bitslice ( std::vector<hashtype> & hashes, slice_vec & slices )
  40. {
  41. const int hashbytes = sizeof(hashtype);
  42. const int hashbits = hashbytes * 8;
  43. const int slicelen = ((int)hashes.size() + 31) / 32;
  44. slices.clear();
  45. slices.resize(hashbits);
  46. for(int i = 0; i < (int)slices.size(); i++)
  47. {
  48. slices[i].resize(slicelen,0);
  49. }
  50. for(int j = 0; j < hashbits; j++)
  51. {
  52. void * sliceblob = &(slices[j][0]);
  53. for(int i = 0; i < (int)hashes.size(); i++)
  54. {
  55. int b = getbit(hashes[i],j);
  56. setbit(sliceblob,slicelen*4,i,b);
  57. }
  58. }
  59. }
  60. void FactorSlices ( slice_vec & slices )
  61. {
  62. std::vector<int> counts(slices.size(),0);
  63. for(size_t i = 0; i < slices.size(); i++)
  64. {
  65. counts[i] = countbits(slices[i]);
  66. }
  67. bool changed = true;
  68. while(changed)
  69. {
  70. int bestA = -1;
  71. int bestB = -1;
  72. for(int j = 0; j < (int)slices.size()-1; j++)
  73. {
  74. for(int i = j+1; i < (int)slices.size(); i++)
  75. {
  76. int d = countxor(slices[i],slices[j]);
  77. if((d < counts[i]) && (d < counts[j]))
  78. {
  79. if(counts[i] < counts[j])
  80. {
  81. bestA = j;
  82. bestB = i;
  83. }
  84. }
  85. else if(d < counts[i])
  86. {
  87. //bestA =
  88. }
  89. }
  90. }
  91. }
  92. }
  93. void foo ( void )
  94. {
  95. slice a;
  96. slice_vec b;
  97. Bitslice(a,b);
  98. }