2024-06-25 17:45:10 -05:00
|
|
|
from random import randint
|
|
|
|
|
|
2024-07-09 19:11:29 -05:00
|
|
|
from constants import *
|
|
|
|
|
|
|
|
|
|
# COL_SIZE = 10
|
2024-06-25 17:45:10 -05:00
|
|
|
REPLICATION_FACTOR = 4
|
|
|
|
|
|
2024-07-09 19:11:29 -05:00
|
|
|
|
2024-07-10 18:41:50 -05:00
|
|
|
def calculate_subnets(node_list, num_subnets):
|
2024-07-09 19:11:29 -05:00
|
|
|
subnets = {}
|
|
|
|
|
for i, n in enumerate(node_list):
|
2024-07-10 18:41:50 -05:00
|
|
|
idx = i % num_subnets
|
2024-06-25 17:45:10 -05:00
|
|
|
|
|
|
|
|
if idx not in subnets:
|
|
|
|
|
subnets[idx] = []
|
|
|
|
|
subnets[idx].append(n)
|
|
|
|
|
|
|
|
|
|
listlen = len(node_list)
|
|
|
|
|
i = listlen
|
2024-07-10 18:41:50 -05:00
|
|
|
while i < num_subnets:
|
2024-06-25 17:45:10 -05:00
|
|
|
subnets[i] = []
|
2024-07-09 19:11:29 -05:00
|
|
|
subnets[i].append(node_list[i % listlen])
|
2024-06-25 17:45:10 -05:00
|
|
|
i += 1
|
|
|
|
|
|
2024-07-10 18:41:50 -05:00
|
|
|
if listlen < REPLICATION_FACTOR * num_subnets:
|
2024-06-25 17:45:10 -05:00
|
|
|
for subnet in subnets:
|
2024-07-09 19:11:29 -05:00
|
|
|
last = subnets[subnet][len(subnets[subnet]) - 1].get_id()
|
2024-06-25 17:45:10 -05:00
|
|
|
idx = -1
|
2024-07-09 19:11:29 -05:00
|
|
|
for j, n in enumerate(node_list):
|
2024-06-25 17:45:10 -05:00
|
|
|
if n.get_id() == last:
|
2024-07-09 19:11:29 -05:00
|
|
|
idx = j + 1
|
2024-06-25 17:45:10 -05:00
|
|
|
while len(subnets[subnet]) < REPLICATION_FACTOR:
|
2024-07-09 19:11:29 -05:00
|
|
|
if idx > len(node_list) - 1:
|
2024-06-25 17:45:10 -05:00
|
|
|
idx = 0
|
|
|
|
|
if node_list[idx] in subnets[subnet]:
|
|
|
|
|
idx += 1
|
|
|
|
|
continue
|
|
|
|
|
subnets[subnet].append(node_list[idx])
|
|
|
|
|
idx += 1
|
|
|
|
|
|
|
|
|
|
return subnets
|