forked from danielwood95/heavyHitters
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspaceSavingProb.py
More file actions
62 lines (48 loc) · 1019 Bytes
/
spaceSavingProb.py
File metadata and controls
62 lines (48 loc) · 1019 Bytes
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
import sys
import random
import math
SRC = './srcCaida500000.txt'
K = 900
def getProbMinKey(table):
minKey = ''
minVal = sys.maxint
for i in range(0, 10):
r = random.choice(table.keys())
v = table[r]
if v < minVal:
minKey = r
minVal = v
return minKey
def getMinKey(table):
minKey = ''
minVal = sys.maxint
for k in table:
v = table[k]
if v < minVal:
minKey = k
minVal = v
return minKey
table = {}
with open(SRC) as f:
ip = f.readline().strip()
while ip:
if ip in table:
table[ip] = table[ip] + 1
else:
if len(table) < K:
table[ip] = 1
else:
# probability of eviction
r = random.uniform(0,1)
minKey = getProbMinKey(table)
minVal = table[minKey]
if r < (1/(5*math.log(minVal+1))):
#if r < 1/(minVal + 1):
#if r < 0.02:
table.pop(minKey)
table[ip] = minVal + 1
ip = f.readline().strip()
sorted_table = sorted(table, key=table.__getitem__, reverse=True)
for k in sorted_table:
v = table[k]
print k + '\t\t\t' + str(v)