-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashHist.cpp
More file actions
104 lines (92 loc) · 1.82 KB
/
HashHist.cpp
File metadata and controls
104 lines (92 loc) · 1.82 KB
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
// $Id: HashHist.cpp,v 1.1 2008/08/11 20:45:55 samn Exp $
#include "stdafx.h"
#include "HashHist.h"
#include "Hist.h"
HashHist::HashHist()
:m_iSize(0)
{}
HashHist::~HashHist()
{}
void HashHist::Clear()
{
m_iSize = 0;
m_oHash.clear();
}
int HashHist::Count()
{
return m_iSize;
}
float HashHist::Entropy()
{
HIT IT;
float dEntrop = 0;
double sz = m_iSize;
for(IT=m_oHash.begin();IT!=m_oHash.end();IT++)
{
if(IT->second<=0) continue;
double dv = IT->second / sz;
dEntrop += dv * log2(dv);
}
return -dEntrop;
}
int HashHist::ForceCount()
{
HIT IT;
int cnt = 0;
for(IT=m_oHash.begin();IT!=m_oHash.end();IT++)
cnt += IT->second;
return cnt;
}
float HashHist::Prob(const char* str)
{
HIT IT = m_oHash.find(str);
if(IT==m_oHash.end())
return 0.0f;
return IT->second / static_cast<float>(m_iSize);
}
float HashHist::KLDiv(HashHist& Q)
{
HIT IT;
float dKLD = 0.0f;
float sz = m_iSize;
for(IT=m_oHash.begin();IT!=m_oHash.end();IT++)
{
if(IT->second<=0) continue;
float pp = IT->second / sz;
float qp = Q.Prob(IT->first);
if(pp <= 0.0f || qp <= 0.0f)
continue;
dKLD += pp * log2(pp/qp);
}
return dKLD;
}
bool HashHist::RemoveVertex(const char* str)
{
HIT IT = m_oHash.find(str);
if(IT==m_oHash.end())
return false;
if((*IT).second > 1)
(*IT).second -= 1;
else
m_oHash.erase(str);
m_iSize -= 1;
return true;
}
void HashHist::AddVertex(const char* str)
{
HIT IT = m_oHash.find(str);
if(IT==m_oHash.end())
m_oHash.insert(pair<const char*,int>(str,1));
//H[str]=1;
else
(*IT).second += 1;
m_iSize += 1;
}
void HashHist::Print2Log()
{
Write2LogPlain("Hash:");
HIT IT;
for(IT=m_oHash.begin();IT!=m_oHash.end();IT++)
Write2LogPlain("%s : %d",IT->first,IT->second);
Write2LogPlain("\n");
}