-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_bitset.cpp
138 lines (106 loc) · 4.14 KB
/
test_bitset.cpp
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
128
129
130
131
132
133
134
135
136
137
138
/*
* Copyright (C) 2009-2012 Simon A. Berger
*
* This file is part of papara.
*
* papara is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* papara is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with papara. If not, see <http://www.gnu.org/licenses/>.
*/
#include <boost/dynamic_bitset.hpp>
#include <vector>
//#include <tr1/unordered_set>
#include <boost/tr1/unordered_set.hpp>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <ivymike/time.h>
template<typename Block>
class bitset_hash_iterator : public std::iterator<std::output_iterator_tag,void,void,void,void> {
Block &hash;
size_t i;
public:
bitset_hash_iterator ( Block &out_hash ) : hash(out_hash), i(1) { hash = 1234; }
inline bitset_hash_iterator<Block>& operator= (const Block &v ) {
hash ^= v * i++;
return *this;
}
inline bitset_hash_iterator<Block>& operator* ()
{ return *this; }
inline bitset_hash_iterator<Block>& operator++ ()
{ return *this; }
inline bitset_hash_iterator<Block>& operator++ (int)
{ return *this; }
};
class bitset_hash {
public:
inline size_t operator()( const boost::dynamic_bitset<> &bs ) const {
// on 64bit windows 'block_type' appears to be 32bit vs. 64bit size_t.
// I guess the >= in the assert should work, even though the hashing
// might be suboptimal if only half of the 64bit value is used. don't
// know what to do. The type chaos (aka. LLP64) on windows sucks.
BOOST_STATIC_ASSERT( sizeof( size_t ) >= sizeof( boost::dynamic_bitset<>::block_type ) );
boost::dynamic_bitset<>::block_type hash = 0;
to_block_range( bs, bitset_hash_iterator<boost::dynamic_bitset<>::block_type>(hash));
return size_t(hash);
}
};
int main() {
std::vector<boost::dynamic_bitset<> > temp;
std::vector<boost::dynamic_bitset<> > temp_search;
for( int i = 0; i < 1024; i++ ) {
temp.push_back(boost::dynamic_bitset<>());
for( int j = 0; j < 1024; j++ ) {
int b = rand() >= RAND_MAX / 2;
temp.back().push_back(b);
}
}
temp_search = temp;
std::random_shuffle( temp_search.begin(), temp_search.end());
for( int i = 0; i < 1024; i++ ) {
temp_search.push_back(boost::dynamic_bitset<>());
for( int j = 0; j < 1024; j++ ) {
int b = rand() >= RAND_MAX / 2;
temp_search.back().push_back(b);
}
}
// temp_search = temp;
size_t found_vec = 0;
size_t found_set = 0;
ivy_mike::perf_timer pt;
std::vector<boost::dynamic_bitset<> > svec = temp;
std::sort(svec.begin(), svec.end());
pt.add_int();
for( int i = 0; i < 1000; ++i ) {
for( std::vector< boost::dynamic_bitset< long unsigned int > >::iterator it = temp_search.begin(); it != temp_search.end(); ++it ) {
bool found = std::binary_search( svec.begin(), svec.end(), *it );
if( found ) {
found_vec++;
}
}
}
pt.add_int();
std::tr1::unordered_set<boost::dynamic_bitset<>, bitset_hash> sset( temp.begin(), temp.end() );
pt.add_int();
for( int i = 0; i < 1000; ++i ) {
for( std::vector< boost::dynamic_bitset< long unsigned int > >::iterator it = temp_search.begin(); it != temp_search.end(); ++it ) {
bool found = sset.find(*it) != sset.end();
if( found ) {
found_set++;
}
}
}
pt.add_int();
pt.print();
std::cout << svec.size() << " " << sset.size() << "\n";
std::cout << found_vec << " " << found_set << "\n";
}