Пример #1
0
def add_node(entry, port, node):
    if node is None:
        return Node(None, None, entry, port, 1)

    if pc.prefix_key(entry) < pc.prefix_key(node.prefix):
        node.left = add_node(entry, port, node.left)
    else:
        node.right = add_node(entry, port, node.right)

    update_node_depth(node)

    balance = get_node_balance(node)

    # print(balance)
    entry_key = pc.prefix_key(entry)
    left_key = pc.prefix_key(node.left.prefix) if node.left is not None else 0
    right_key = pc.prefix_key(
        node.right.prefix) if node.right is not None else 0

    if balance > 1 and entry_key < left_key:
        return right_rotate(node)
    if balance < -1 and entry_key > right_key:
        return left_rotate(node)
    if balance > 1 and entry_key > left_key:
        node.left = left_rotate(node.left)
        return right_rotate(node)
    if balance < -1 and entry_key < right_key:
        node.right = right_rotate(node.right)
        return left_rotate(node)

    return node
Пример #2
0
def find_prefix(node, prefix):
    tmp = None
    if pc.prefix_key(
        (prefix[0], node.prefix[1])) == pc.prefix_key(node.prefix):
        tmp = node
    if pc.prefix_key((prefix[0], node.prefix[1])) < pc.prefix_key(node.prefix):
        if node.left is not None:
            sec_tmp = find_prefix(node.left, prefix)
            return sec_tmp if sec_tmp is not None else tmp
        else:
            return tmp
    else:
        if node.right is not None:
            sec_tmp = find_prefix(node.right, prefix)
            return sec_tmp if sec_tmp is not None else tmp
        else:
            return tmp
Пример #3
0
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)
Пример #4
0
 def __str__(self):
     # straa = pc.int_ip_to_str(self.prefix[0]) + "\\" + str(self.prefix[1])
     straa = str(pc.prefix_key(self.prefix)) + "\\" + str(self.prefix[1])
     straa += " " + str(self.depth)
     return straa
Пример #5
0
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))