-
Notifications
You must be signed in to change notification settings - Fork 12
/
Stats.h
193 lines (148 loc) · 5.08 KB
/
Stats.h
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#pragma once
#include "Types.h"
#include "HashFunc.h"
#include "SimpleStats.h"
#include "TAP.h"
//-----------------------------------------------------------------------------
// Sort the hash list, count the total number of collisions and return
// the first N collisions for further processing
//
// Used by TestHashList which is widely used elsewhere.
template< typename hashtype >
int FindCollisions ( std::vector<hashtype> & hashes,
HashSet<hashtype> & collisions,
int maxCollisions )
{
int collcount = 0;
std::sort(hashes.begin(),hashes.end());
for(size_t i = 1; i < hashes.size(); i++)
{
if(hashes[i] == hashes[i-1])
{
collcount++;
if((int)collisions.size() < maxCollisions)
{
collisions.insert(hashes[i]);
}
}
}
return collcount;
}
//----------------------------------------------------------------------------
// Measure the distribution "score" for each possible N-bit span up to 20 bits
//
// Used by TestHashList, which is widely used elsewhere.
template< typename hashtype >
bool TestDistribution ( std::vector<hashtype> & hashes, double confidence, bool drawDiagram, const char *name )
{
//printf("# Testing distribution - ");
//drawDiagram = true;
if(drawDiagram) printf("\n"); // nl ok
const int hashbits = sizeof(hashtype) * 8;
int maxwidth = 20;
// We need at least 5 keys per bin to reliably test distribution biases
// down to 1%, so don't bother to test sparser distributions than that
while(double(hashes.size()) / double(1 << maxwidth) < 5.0)
{
maxwidth--;
}
std::vector<int> bins;
bins.resize(1 << maxwidth);
double worst = 0;
int worstStart = -1;
int worstWidth = -1;
int scores = 0;
for(int start = 0; start < hashbits; start++)
{
int width = maxwidth;
int bincount = (1 << width);
memset(&bins[0],0,sizeof(int)*bincount);
for(size_t j = 0; j < hashes.size(); j++)
{
hashtype & hash = hashes[j];
uint32_t index = window(&hash, sizeof(hash), start, width);
bins[index]++;
}
// Test the distribution, then fold the bins in half,
// repeat until we're down to 256 bins
if(drawDiagram) printf("[");
while(bincount >= 256)
{
double n = calcScore(&bins[0],bincount,(int)hashes.size(), confidence);
scores++;
if(drawDiagram) plot(n);
if(n > worst)
{
worst = n;
worstStart = start;
worstWidth = width;
}
width--;
bincount /= 2;
if(width < 8) break;
for(int i = 0; i < bincount; i++)
{
bins[i] += bins[i+bincount];
}
}
if(drawDiagram) printf("]\n");
}
bool result = !worst;
if (worst)
printf("# worst bias is the %3d-bit window at bit %3d - score %5.3f (from %d scores)\n",
worstWidth, worstStart, worst, scores);
return okf(result, "Distribution Bias Check for %s", name);
}
//----------------------------------------------------------------------------
template < typename hashtype >
bool TestHashList ( std::vector<hashtype> & hashes, std::vector<hashtype> & collisions, double confidence , bool drawDiagram, const char *name )
{
bool result = true;
size_t count = hashes.size();
double expected = (double(count) * double(count-1)) / pow(2.0,double(sizeof(hashtype) * 8 + 1));
printf("# Testing collisions - Expected %8.2f, ",expected);
HashSet<hashtype> my_collisions;
double collcount = FindCollisions(hashes,my_collisions,1000);
if( sizeof(hashtype) == sizeof(uint32_t) )
{
// 2x expected collisions = fail
// #TODO - collision failure cutoff needs to be expressed as a standard deviation instead
// of a scale factor, otherwise we fail erroneously if there are a small expected number
// of collisions
if(double(collcount) / double(expected) > 2.0 &&
fabs(double(collcount) - double(expected)) > 1)
{
result = false;
}
}
else
if(collcount > 0) {
// For all hashes larger than 32 bits, _any_ collisions are a failure.
// not sure this is right really. but for now we will live with it
result = false;
}
if (collcount / expected < 1000) {
printf("actual %8.0f (%5.2fx) - %s\n",collcount, collcount / expected,
result ? "passed" : "failed");
} else if (collcount == count - 1) {
printf("actual %8.0f - ALL keys were in collision - %s\n",collcount,
result ? "passed" : "failed" );
} else {
printf("actual %8.0f - Excessive keys in collision - %s\n",collcount,
result ? "passed" : "failed");
}
okf(result,"Collision Rate for %s",name);
//----------
if(confidence)
result &= TestDistribution<hashtype>(hashes,confidence,drawDiagram, name);
return result;
}
//----------
template < typename hashtype >
bool TestHashList ( std::vector<hashtype> & hashes, bool /*testColl*/, double confidence, bool drawDiagram, const char *name )
{
std::vector<hashtype> collisions;
return TestHashList<hashtype>(hashes,collisions,confidence,drawDiagram,name);
}
//-----------------------------------------------------------------------------
/* vim: set sts=2 sw=2 et: */