forked from danielwood95/heavyHitters
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashPipe.py
More file actions
56 lines (45 loc) · 1.1 KB
/
hashPipe.py
File metadata and controls
56 lines (45 loc) · 1.1 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
SRC = './sourceCaida.txt'
MEM_SIZE = 400
D = 6
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
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)
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])