<commit_before>from Edge import Edge
from Vertex import Vertex


class Graph(object):


    def __init__(self, V, E, tau=set()):

        if isinstance(V, Vertex):
            V = set([V])
        elif not isinstance(V, set):
            V = set(V)

        if isinstance(E, Edge):
            E = set([E])
        elif not isinstance(E, set):
            E = set(E)

        if not isinstance(tau, set):
            tau = set(tau)

        self.V = V
        self.E = E

        for e in self.E:
            self.check_edge(e)

        self.domains = dict()
        for v in self.V:
            if v.domain not in self.domains:
                self.domains[v.get_domain()] = set([v])
            else:
                self.domains[v.get_domain()].add(v)

        self.deleted_edges = set()
        self.deleted_vertices = set()

        self.tau = tau if len(tau) > 0 else self.get_neighboring_domains()
        if self.is_neighborhood_corrupted():
            raise ValueError("Not all neighbours exist")


    def check_edge(self, e):
        if not self.V.issuperset(set(e.get_vertices())):
            raise ValueError("All vertices should be in graph's vertices' set")


    def get_domains(self):
        return self.domains.keys()


    def get_domain(self, domain, with_deleted=False):
        if len(self.deleted_vertices) == 0 or with_deleted == True:
            return self.domains[domain]
        return set(v for v in self.domains[domain]
                     if v not in self.deleted_vertices)


    def get_tau(self):
        return self.tau


    def is_neighborhood_corrupted(self):
        return self.get_tau() != self.get_neighboring_domains()


    def get_neighboring_domains(self):
        neighboring_domains = set()
        vertices = self.get_vertices()
        for e in self.get_edges():
            v = e.get_vertices()
            if not set(v).issubset(vertices):
                continue
            neighboring_domains.add((v[0].get_domain(), v[1].get_domain()))
        return neighboring_domains


    def delete_vertex(self, vertex):
        if vertex not in self.V:
            raise ValueError('Edge does not belong to current graph')
        if vertex in self.deleted_vertices:
            return
        self.deleted_vertices.add(vertex)
        for e in vertex.get_outputs().union(vertex.get_inputs()) \
                                     .difference(self.deleted_edges):
            self.delete_edge(e)


    def delete_edge(self, edge):
        if edge not in self.E:
            raise ValueError('Edge does not belong to current graph')
        if edge in self.deleted_edges:
            return
        start, end = edge.get_vertices()
        edge_set = set([edge])
        start_outputs = start.get_outputs().difference(self.deleted_edges)
        self.deleted_edges.add(edge)
        if start not in self.deleted_vertices \
        and edge in start.get_outputs()\
        and start.get_outputs().issubset(self.deleted_edges):
            self.delete_vertex(start)
        if end not in self.deleted_vertices \
        and edge in end.get_inputs()\
        and end.get_inputs().issubset(self.deleted_edges):
            self.deleted_edges.add(edge)
            self.delete_vertex(end)


    def prepare(self, semiring=None):

        for vertex in self.V:
            vertex.clear_inputs()
            vertex.clear_outputs()
            if semiring is not None and vertex.get_value() is not None \
                and not isinstance(vertex.get_value(), semiring):
                vertex.set_value(semiring(vertex.get_value()))

        for edge in self.E:
            vertices = edge.get_vertices()
            vertices[0].add_output(edge)
            vertices[1].add_input(edge)
            if semiring is not None \
                and not isinstance(edge.get_value(), semiring):
                edge.set_value(semiring(edge.get_value()))
<commit_msg>Fixed Graph edge deletion: removed redundant set operations calls<commit_after>from Edge import Edge
from Vertex import Vertex


class Graph(object):


    def __init__(self, V, E, tau=set()):

        if isinstance(V, Vertex):
            V = set([V])
        elif not isinstance(V, set):
            V = set(V)

        if isinstance(E, Edge):
            E = set([E])
        elif not isinstance(E, set):
            E = set(E)

        if not isinstance(tau, set):
            tau = set(tau)

        self.V = V
        self.E = E

        for e in self.E:
            self.check_edge(e)

        self.domains = dict()
        for v in self.V:
            if v.domain not in self.domains:
                self.domains[v.get_domain()] = set([v])
            else:
                self.domains[v.get_domain()].add(v)

        self.deleted_edges = set()
        self.deleted_vertices = set()

        self.tau = tau if len(tau) > 0 else self.get_neighboring_domains()
        if self.is_neighborhood_corrupted():
            raise ValueError("Not all neighbours exist")


    def check_edge(self, e):
        if not self.V.issuperset(set(e.get_vertices())):
            raise ValueError("All vertices should be in graph's vertices' set")


    def get_domains(self):
        return self.domains.keys()


    def get_domain(self, domain, with_deleted=False):
        if len(self.deleted_vertices) == 0 or with_deleted == True:
            return self.domains[domain]
        return set(v for v in self.domains[domain]
                     if v not in self.deleted_vertices)


    def get_tau(self):
        return self.tau


    def is_neighborhood_corrupted(self):
        return self.get_tau() != self.get_neighboring_domains()


    def get_neighboring_domains(self):
        neighboring_domains = set()
        vertices = self.get_vertices()
        for e in self.get_edges():
            v = e.get_vertices()
            if not set(v).issubset(vertices):
                continue
            neighboring_domains.add((v[0].get_domain(), v[1].get_domain()))
        return neighboring_domains


    def delete_vertex(self, vertex):
        if vertex not in self.V:
            raise ValueError('Edge does not belong to current graph')
        if vertex in self.deleted_vertices:
            return
        self.deleted_vertices.add(vertex)
        for e in vertex.get_outputs().union(vertex.get_inputs()) \
                                     .difference(self.deleted_edges):
            self.delete_edge(e)


    def delete_edge(self, edge):
        if edge not in self.E:
            raise ValueError('Edge does not belong to current graph')
        if edge in self.deleted_edges:
            return
        start, end = edge.get_vertices()
        self.deleted_edges.add(edge)
        if start not in self.deleted_vertices \
        and edge in start.get_outputs()\
        and start.get_outputs().issubset(self.deleted_edges):
            self.delete_vertex(start)
        if end not in self.deleted_vertices \
        and edge in end.get_inputs()\
        and end.get_inputs().issubset(self.deleted_edges):
            self.delete_vertex(end)


    def prepare(self, semiring=None):

        for vertex in self.V:
            vertex.clear_inputs()
            vertex.clear_outputs()
            if semiring is not None and vertex.get_value() is not None \
                and not isinstance(vertex.get_value(), semiring):
                vertex.set_value(semiring(vertex.get_value()))

        for edge in self.E:
            vertices = edge.get_vertices()
            vertices[0].add_output(edge)
            vertices[1].add_input(edge)
            if semiring is not None \
                and not isinstance(edge.get_value(), semiring):
                edge.set_value(semiring(edge.get_value()))

{'fraction_non_alphanumeric': 0.09959797691609389, 'fraction_numerical': 0.001556218389313967, 'mean_word_length': 1.9468857470385939, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5287201', 'n_tokens_mistral': 2294, 'n_tokens_neox': 2219, 'n_words': 563}