-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
51 lines (40 loc) · 1.5 KB
/
main.py
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
def hash_4bit(key):
hash_value = hash(key)
hash_4bit = hash_value % (2**4)
return hash_4bit
class node:
def __init__(self, id):
self.hash = hash_4bit(id)
self.pred = None
self.succ = None
self.data = []
class chord_dht:
def __init__(self,ids, data):
self.nodes = [node(id) for id in ids]
self.data = data
for i, nodei in enumerate(self.nodes):
if(i < len(self.nodes) - 1):
print(i)
nodei.succ = self.nodes[i + 1]
if(i == 0):
nodei.pred = self.nodes[-1]
else:
nodei.pred = self.nodes[i - 1]
if(i == len(self.nodes) - 1):
nodei.succ = self.nodes[0]
nodei.pred = self.nodes[i - 1]
for i, nodei in enumerate(self.nodes):
for j, dataj in enumerate(self.data):
if nodei.pred.hash < nodei.hash:
if (dataj <= nodei.hash and dataj > nodei.pred.hash):
nodei.data.append(dataj)
else:
if (dataj <= nodei.hash or dataj > nodei.pred.hash):
nodei.data.append(dataj)
data = [0, 2, 3, 5, 6, 8, 9, 10, 11, 13, 15]
hashed_data = [hash_4bit(data) for _, data in enumerate(data)]
chord = chord_dht([1, 4, 7, 12, 14], hashed_data)
print([node.hash for node in chord.nodes])
print([node.data for node in chord.nodes])
print(hashed_data)
print(1 + ((2*3) % (2 ** 4)))