blob: 428e35528b6066482b489d6789939ee381482ac5 [file] [log] [blame]
tanjent@gmail.comf67ce942011-03-14 09:11:18 +00001#include "Bitvec.h"
2#include <vector>
3#include <assert.h>
4
5// handle xnor
6
7typedef std::vector<uint32_t> slice;
8typedef std::vector<slice> slice_vec;
9
10int countbits ( slice & v )
11{
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000012 int c = 0;
tanjent@gmail.comf67ce942011-03-14 09:11:18 +000013
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000014 for(size_t i = 0; i < v.size(); i++)
15 {
16 int d = countbits(v[i]);
tanjent@gmail.comf67ce942011-03-14 09:11:18 +000017
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000018 c += d;
19 }
tanjent@gmail.comf67ce942011-03-14 09:11:18 +000020
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000021 return c;
tanjent@gmail.comf67ce942011-03-14 09:11:18 +000022}
23
24int countxor ( slice & a, slice & b )
25{
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000026 assert(a.size() == b.size());
tanjent@gmail.comf67ce942011-03-14 09:11:18 +000027
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000028 int c = 0;
tanjent@gmail.comf67ce942011-03-14 09:11:18 +000029
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000030 for(size_t i = 0; i < a.size(); i++)
31 {
32 int d = countbits(a[i] ^ b[i]);
tanjent@gmail.comf67ce942011-03-14 09:11:18 +000033
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000034 c += d;
35 }
tanjent@gmail.comf67ce942011-03-14 09:11:18 +000036
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000037 return c;
tanjent@gmail.comf67ce942011-03-14 09:11:18 +000038}
39
40void xoreq ( slice & a, slice & b )
41{
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000042 assert(a.size() == b.size());
tanjent@gmail.comf67ce942011-03-14 09:11:18 +000043
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000044 for(size_t i = 0; i < a.size(); i++)
45 {
46 a[i] ^= b[i];
47 }
tanjent@gmail.comf67ce942011-03-14 09:11:18 +000048}
49
50//-----------------------------------------------------------------------------
51// Bitslice a hash set
52
53template< typename hashtype >
54void Bitslice ( std::vector<hashtype> & hashes, slice_vec & slices )
55{
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000056 const int hashbytes = sizeof(hashtype);
57 const int hashbits = hashbytes * 8;
58 const int slicelen = ((int)hashes.size() + 31) / 32;
tanjent@gmail.comf67ce942011-03-14 09:11:18 +000059
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000060 slices.clear();
61 slices.resize(hashbits);
tanjent@gmail.comf67ce942011-03-14 09:11:18 +000062
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000063 for(int i = 0; i < (int)slices.size(); i++)
64 {
65 slices[i].resize(slicelen,0);
66 }
tanjent@gmail.comf67ce942011-03-14 09:11:18 +000067
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000068 for(int j = 0; j < hashbits; j++)
69 {
70 void * sliceblob = &(slices[j][0]);
tanjent@gmail.comf67ce942011-03-14 09:11:18 +000071
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000072 for(int i = 0; i < (int)hashes.size(); i++)
73 {
74 int b = getbit(hashes[i],j);
tanjent@gmail.comf67ce942011-03-14 09:11:18 +000075
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000076 setbit(sliceblob,slicelen*4,i,b);
77 }
78 }
tanjent@gmail.comf67ce942011-03-14 09:11:18 +000079}
80
81void FactorSlices ( slice_vec & slices )
82{
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000083 std::vector<int> counts(slices.size(),0);
tanjent@gmail.comf67ce942011-03-14 09:11:18 +000084
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000085 for(size_t i = 0; i < slices.size(); i++)
86 {
87 counts[i] = countbits(slices[i]);
88 }
tanjent@gmail.comf67ce942011-03-14 09:11:18 +000089
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000090 bool changed = true;
tanjent@gmail.comf67ce942011-03-14 09:11:18 +000091
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000092 while(changed)
93 {
94 int bestA = -1;
95 int bestB = -1;
tanjent@gmail.comf67ce942011-03-14 09:11:18 +000096
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000097 for(int j = 0; j < (int)slices.size()-1; j++)
98 {
99 for(int i = j+1; i < (int)slices.size(); i++)
100 {
101 int d = countxor(slices[i],slices[j]);
tanjent@gmail.comf67ce942011-03-14 09:11:18 +0000102
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000103 if((d < counts[i]) && (d < counts[j]))
104 {
105 if(counts[i] < counts[j])
106 {
107 bestA = j;
108 bestB = i;
109 }
110 }
111 else if(d < counts[i])
112 {
113 //bestA =
114 }
115 }
116 }
117 }
tanjent@gmail.comf67ce942011-03-14 09:11:18 +0000118}
119
120
121void foo ( void )
122{
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000123 slice a;
124 slice_vec b;
tanjent@gmail.comf67ce942011-03-14 09:11:18 +0000125
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000126 Bitslice(a,b);
tanjent@gmail.comf67ce942011-03-14 09:11:18 +0000127}