summaryrefslogtreecommitdiff
path: root/Bitslice.cpp
blob: 45a2249f283be702ba5ce032d8db89d2701a863c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "Bitvec.h"
#include <vector>
#include <assert.h>

// handle xnor

typedef std::vector<uint32_t> slice;
typedef std::vector<slice> slice_vec;

int countbits ( slice & v )
{
  int c = 0;

  for(size_t i = 0; i < v.size(); i++)
  {
    int d = countbits(v[i]);

    c += d;
  }

  return c;
}

int countxor ( slice & a, slice & b )
{
  assert(a.size() == b.size());

  int c = 0;

  for(size_t i = 0; i < a.size(); i++)
  {
    int d = countbits(a[i] ^ b[i]);

    c += d;
  }

  return c;
}

void xoreq ( slice & a, slice & b )
{
  assert(a.size() == b.size());

  for(size_t i = 0; i < a.size(); i++)
  {
    a[i] ^= b[i];
  }
}

//-----------------------------------------------------------------------------
// Bitslice a hash set

template< typename hashtype >
void Bitslice ( std::vector<hashtype> & hashes, slice_vec & slices )
{
  const int hashbytes = sizeof(hashtype);
  const int hashbits = hashbytes * 8;
  const int slicelen = ((int)hashes.size() + 31) / 32;

  slices.clear();
  slices.resize(hashbits);

  for(int i = 0; i < (int)slices.size(); i++)
  {
    slices[i].resize(slicelen,0);
  }

  for(int j = 0; j < hashbits; j++)
  {
    void * sliceblob = &(slices[j][0]);

    for(int i = 0; i < (int)hashes.size(); i++)
    {
      int b = getbit(hashes[i],j);

      setbit(sliceblob,slicelen*4,i,b);
    }
  }
}

void FactorSlices ( slice_vec & slices )
{
  std::vector<int> counts(slices.size(),0);

  for(size_t i = 0; i < slices.size(); i++)
  {
    counts[i] = countbits(slices[i]);
  }

  bool changed = true;

  while(changed)
  {
    int bestA = -1;
    int bestB = -1;

    for(int j = 0; j < (int)slices.size()-1; j++)
    {
      for(int i = j+1; i < (int)slices.size(); i++)
      {
        int d = countxor(slices[i],slices[j]);

        if((d < counts[i]) && (d < counts[j]))
        {
          if(counts[i] < counts[j])
          {
            bestA = j;
            bestB = i;
          }
        }
        else if(d < counts[i])
        {
          //bestA = 
        }
      }
    }
  }
}


void foo ( void )
{
  slice a;
  slice_vec b;

  Bitslice(a,b);
}