Ejemplo n.º 1
0
def process_tree(row, main_file, err_del_nodes_old, del_nodes_old, is_holl):

    # находим ноды для нового дерева
    row_float = float(row['x1'])
    all_x2 = [float(row['x2']), ]

    print 'STAAAAAAAAAAAAAAAAAAAAAAAAAAAAAART'
    print 'is_holl', is_holl

    try:
        next_line = main_file.next()
        next_row = get_row_dict(next_line)
        print 'next_row', next_row
        next_float = float(next_row['x1'])
    except StopIteration:
        return None, 1, 1, 1

    add_nodes = [row, ]

    while next_float == row_float:
        add_nodes.append(next_row)

        all_x2.append(float(next_row['x2']))

        try:
            next_line = main_file.next()
            next_row = get_row_dict(next_line)
            next_float = float(next_row['x1'])
        except StopIteration:
            next_float = 181
            break

    min_x2_float = min(all_x2)
    max_x2_float = max(all_x2)

    x_middle = (min_x2_float + row_float) / 2
    # print 'all_x2', all_x2
    # print 'x_middle', x_middle, 'prev_row', row_float, 'next_row', next_float
    prev_tree = ALL_XS[-1][1]

    next_tree = AVLTree()
    # l()
    # print 'prev_tree'
    # prev_tree.show()
    # l()
    print 'err_del_nodes_old', err_del_nodes_old
    l()
    print 'prev_tree'
    prev_tree.show()
    # l()
    print 'next_tree'
    next_tree.show()

    # актуализируем все значения новых нодов для добавления
    update_dict_vals(add_nodes, x_middle)

    # если предыдущее дерево пусто или между полигонами дыра
    if is_holl or prev_tree.root.val is None:
        for a in add_nodes:
            # print 'add', float(a['val'])
            next_tree.add(next_tree.root, float(a['val']), a['a'], a['b'], a['pol_id'], a['x2'], a['y2'])

    else:
        # удаляем ноды битые, у которых х2 меньше, чем следующий х1
        for err_d in err_del_nodes_old:
            print 'delete err_del_node', float(err_d['val'])
            next_tree.delete_versionly(prev_tree, float(err_d['val'])) # 45.7427962225

        print 'del_nodes_old', del_nodes_old

        # обработка нодов на замену/добавление/удаление
        to_replace, proc_del, proc_add = treatment_add_del(del_nodes_old, add_nodes)

        print 'proc_del', proc_del

        for d in proc_del:
            next_tree.delete_versionly(prev_tree, float(d['val']))

        print 'to_replace', to_replace

        for (d, a) in to_replace:
            next_tree.replace_versionly(prev_tree, float(d['val']), a)
        # актуализируем все значения нодов в дереве
        next_tree.update_vals(x_middle)
        # актуализируем все значения новых нодов для добавления
        # update_dict_vals(proc_add, x_middle)

        print 'proc_add', proc_add

        # предыдущее дерево непусто, но новое дерево пусто,
        # и если мы удаляли уже, то без версионности
        if next_tree.root.val is None and (del_nodes_old or err_del_nodes_old):
            l()
            print 'next_tree.root.val is None and (del_nodes_old or err_del_nodes_old)!!!'
            for a in proc_add:
                # print 'add', a['val']
                next_tree.add(next_tree.root, float(a['val']), a['a'], a['b'], a['pol_id'], a['x2'], a['y2'])
        # новое просто пусто(т.к. ничего не удадяли) или непусто, то версионность
        else:
            l()
            print 'next_tree.root.val is None or is not empty!!!'
            for a in proc_add:
                # print 'add_versionly', a['val']
                next_tree.add_versionly(prev_tree, a)

    # обнуляем версионность дерева next_tree
    next_tree.remove_update_flags(next_tree.root)

    ALL_XS.append([float(row_float), next_tree])

    err_del_nodes = [node for node in add_nodes if float(node['x2']) < next_float]
    print 'err_del_nodes', err_del_nodes
    # if err_del_nodes:
    #     print 'Err_del_nodes exists', row_float, err_del_nodes
    del_nodes = [node for node in add_nodes if float(node['x2']) == next_float]

    print 'next_tree'
    next_tree.show()

    print 'EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEND'
    print 'max_x2_float', max_x2_float, 'next_float', next_float
    l()

    return next_row, err_del_nodes, del_nodes, max_x2_float < next_float
Ejemplo n.º 2
0
    def process_tree(prev_info, build_first):

        for_tree_build = list()
        for_tree_build.append(get_dict(prev_info))

        prev_x1 = float(prev_info[x1i])
        ALL_XS.append([prev_x1, None])

        next_info = f.readline().split()

        if next_info:
            next_x1 = float(next_info[x1i])

            while prev_x1 == next_x1:
                for_tree_build.append(get_dict(next_info))

                next_info = f.readline().split()
                next_x1 = float(next_info[x1i])

            # середина между x(i) и x(i+1) для нахождений val-ов
            x_middle = (next_x1+prev_x1)/2

            prev_to_delete = to_delete

            # обрабатываем ноды будущего дерева
            to_add = process_tree_nodes(for_tree_build, x_middle, next_info[x1i])

            print '-'*80
            print to_add
            print prev_to_delete

            if build_first:
                tree = AVLTree()
                for n in to_add:
                    tree.add(tree.root, n['val'], n['a'], n['b'], n['pol_id'])
                tree.show()
                ref_to_tree = tree
            else:
                # если кривые координаты, идут вразброс с дырами,
                # то строим новое дерево
                #  __           __
                # /  \ пустота /  \
                # \__/         \__/

                if not prev_to_delete:
                    print 'Hole'
                    tree = AVLTree()
                    for n in to_add:
                        tree.add(tree.root, n['val'], n['a'], n['b'], n['pol_id'])

                    tree.show()
                    ref_to_tree = tree
                else:
                    prev_tree = ALL_XS[-2][1]
                    next_tree = deepcopy(prev_tree)

                    # пока грубое добавление/удаление
                    for n in to_add:
                        next_tree.add(next_tree.root, n['val'], n['a'], n['b'], n['pol_id'])
                    for n in prev_to_delete:
                        next_tree.delete(n['val'])

                    ref_to_tree = next_tree
                    next_tree.show()

            ALL_XS[-1][1] = ref_to_tree

            return next_info

        # TODO обработать последнюю строку(последний x),
        # TODO а то теряется последнее дерево

        return None