def __str__(self): if self.prefix is not None: straa = pc.int_ip_to_str(self.prefix[0]) + "\\" + str(self.prefix[1]) else: straa = "" straa += " depth: " + str(self.depth) return straa
def build_avl_tree(table, step=False): root = None entries = [] for now_entry in table: entries.append(now_entry) entries = sorted(entries, key=pc.prefix_key) for x in entries: print(pc.prefix_key(x), pc.int_ip_to_str(x[0]), format(x[0], "032b")) counter = 1 for now_entry in entries: # print(now_entry) root = add_node(now_entry, table[now_entry], root) if step: nodes = [] root.generate_image(nodes, -1) DotExporter(nodes[0]).to_picture("outs/out_" + str(counter) + ".png") counter += 1 nodes = [] root.generate_image(nodes, -1) print(len(nodes)) DotExporter(nodes[0]).to_picture("out.png") while True: str_ip = input().strip() if str_ip == "end": break now_prefix = (pc.str_ip_to_int(str_ip), 32) node = find_prefix(root, now_prefix) print_prefix([node.prefix, now_prefix]) print(format(pc.prefix_key(node.prefix), "032b"), node.prefix, format(pc.prefix_key(now_prefix), "032b"), now_prefix[1]) print_prefix([now_prefix, node.prefix], mod=lambda x: format(x, "032b")) print_prefix([node.prefix, now_prefix], mod=int)
#! /usr/bin/python3 """convert table module""" import prefix_compare as pc if __name__ == "__main__": while True: TMP = input().split() print(pc.int_ip_to_str(int(TMP[0])), int(TMP[1]))
def main(mod): """main function for test""" if mod == 1: while True: str_ip_1, mask_1 = input( "first prefix (ip mask): ").strip().split() str_ip_2, mask_2 = input( "secon prefix (ip mask): ").strip().split() prefix_1 = (pc.str_ip_to_int(str_ip_1), int(mask_1)) prefix_2 = (pc.str_ip_to_int(str_ip_2), int(mask_2)) print(pc.prefix_compare(prefix_1, prefix_2)) if mod == 2: file_name = input("please enter table_name: ").strip() table_file = open(file_name, "r") table = [] for line in table_file: prefix = line.split() if not int(prefix[1]): continue prefix = (int(prefix[0]), int(prefix[1])) table.append(prefix) for now in table: tmp = pc.prefix_key(now) print(pc.int_ip_to_str(now[0]), now[1], tmp) print() # print_table(table) table_1 = sorted(table, key=pc.prefix_key) # print_table(table_1) shuffle(table) table_2 = sorted(table, key=pc.prefix_key) for now in enumerate(table_2): if now[1] != table_1[now[0]]: print_prefix((now[1], table_1[now[0]]), end=" === ") print(pc.prefix_key(now[1]), " === ", pc.prefix_key(table_1[now[0]])) run = [1] """ for i in tqdm(range(10000)): shuffle(table) table_2 = sorted(table, key=pc.prefix_key) counter = 0 for now in enumerate(table_2): if now[1] != table_1[now[0]]: counter += 1 run.append(counter) """ print(*[(x, it) for it, x in enumerate(run) if x != 0]) print("Average miss: ", sum(run) / len(run)) print("total miss: ", sum(run))
def print_table(table): print(*[(pc.int_ip_to_str(x), i) for x, i in table])