forked from danielwood95/heavyHitters
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashPipeDaniel.py
More file actions
63 lines (52 loc) · 1.29 KB
/
hashPipeDaniel.py
File metadata and controls
63 lines (52 loc) · 1.29 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
import math
import random
SRC = './srcCaida500000.txt'
MEM_SIZE = 300
D = 16
STAGE_SIZE = int(MEM_SIZE / D)
table = [[(0,0) for i in range(STAGE_SIZE)] for j in range(D)]
def getHash(s, stage):
return abs(hash(s + str(stage))) % STAGE_SIZE
#return random.randint(0,STAGE_SIZE-1)
with open(SRC) as f:
ip = f.readline().strip()
while ip:
index = getHash(ip, 0)
key = table[0][index][0]
val = table[0][index][1]
if key == ip:
table[0][index] = (key, val + 1)
elif val == 0:
table[0][index] = (ip, 1)
else:
cKey = ip
cVal = val
for i in range(1,D):
index = getHash(ip, i)
key = table[i][index][0]
val = table[i][index][1]
if key == cKey:
table[i][index] = (cKey, val + cVal)
break
elif val == 0:
table[i][index] = (cKey, cVal)
break
elif val < cVal:
tempKey = cKey
tempVal = cVal
cKey = key
cVal = val
table[i][index] = (tempKey, tempVal)
r = random.uniform(0,1)
if r > (1/(5*math.log(cVal+1))):
index = getHash(ip,0)
table[0][index] = (cKey,cVal)
ip = f.readline().strip()
allTuples = []
for stg in table:
for x in stg:
if x[0] != 0:
allTuples.append(x)
sortedTuples = sorted(allTuples, key=lambda tup: tup[1], reverse=True)
for t in sortedTuples:
print str(t[0]) + '\t\t\t' + str(t[1])