Ticket #13730: trac_13730-vd.patch

File trac_13730-vd.patch, 3.4 KB (added by vdelecroix, 6 years ago)
  • sage/graphs/base/dense_graph.pxd

    # HG changeset patch
    # User Vincent Delecroix <20100.delecroix at gmail.com>
    # Date 1370418142 -7200
    # Node ID 3c565797013f9a7439e53306d77cc39aa4e58e53
    # Parent  16768eaa774a10ee080f972ed1a80ab32baeaad9
    trac 13730: faster iterator for neighbors for graphs
    
    diff --git a/sage/graphs/base/dense_graph.pxd b/sage/graphs/base/dense_graph.pxd
    a b  
    1010include '../../ext/stdsage.pxi'
    1111include '../../misc/bitset_pxd.pxi'
    1212
     13
    1314cdef class DenseGraph(CGraph):
    1415    # Values inherited from CGraph:
    1516    # cdef int num_verts
     
    2930    # cdef int out_neighbors_unsafe(self, int, int *, int)
    3031    # cdef int in_neighbors_unsafe(self, int, int *, int)
    3132
     33cdef class DenseGraphOutNeighborIterator:
     34    cdef DenseGraph G
     35    cdef unsigned long word, data
     36    cdef int place, i, v
    3237
    3338
    3439
    35 
    36 
  • sage/graphs/base/dense_graph.pyx

    diff --git a/sage/graphs/base/dense_graph.pyx b/sage/graphs/base/dense_graph.pyx
    a b  
    122122
    123123include '../../misc/bitset.pxi'
    124124
     125cdef class DenseGraphOutNeighborIterator:
     126    """
     127    Iterator over all ``v`` such that ``(u, v)`` is an arc of a DenseGraph.
     128
     129    Note: This class duplicates the code in out_neighbors_unsafe in order to
     130    have a fast iterator available at Python level.
     131
     132    EXAMPLES::
     133
     134        sage: from sage.graphs.base.dense_graph import DenseGraph
     135        sage: from sage.graphs.base.dense_graph import DenseGraphOutNeighborIterator
     136
     137        sage: G = DenseGraph(nverts=10, extra_vertices=10)
     138        sage: G.add_vertex(0)
     139        0
     140        sage: it = DenseGraphOutNeighborIterator(G, 0)
     141        sage: it.next()
     142        Traceback (most recent call last):
     143        ...
     144        StopIteration
     145
     146        sage: G.add_arc(5,8)
     147        sage: G.add_arc(5,2)
     148        sage: it = DenseGraphOutNeighborIterator(G, 5)
     149        sage: list(it)
     150        [2, 8]
     151
     152        sage: it = DenseGraphOutNeighborIterator(G, 12)
     153        Traceback (most recent call last):
     154        ...
     155        LookupError: Vertex (12) is not a vertex of the graph.
     156    """
     157    def __init__(self, G, u):
     158        r"""
     159        INPUT:
     160
     161        - G -- a DenseGraph
     162
     163        - u -- a non-negative integer
     164        """
     165        cdef int uu = <int> u
     166
     167        self.G = G
     168        self.G.check_vertex(uu)
     169
     170        self.place = uu * self.G.num_longs
     171        self.i = -1
     172        self.v = 0
     173        self.word = 0
     174
     175    def __iter__(self):
     176        return self
     177
     178    def __next__(self):
     179        r"""
     180        Return the next item or raise StopIteration
     181        """
     182        if self.i == self.G.num_longs:
     183            raise StopIteration
     184
     185        if not self.word:
     186            self.i += 1
     187            if self.i == self.G.num_longs:
     188                raise StopIteration
     189            self.data = self.G.edges[self.place + self.i]
     190            self.word = 1
     191
     192        else:
     193            self.word = self.word << 1
     194            self.v += 1
     195
     196        while not (self.word & self.data):
     197            if not self.word:
     198                self.i += 1
     199                if self.i == self.G.num_longs:
     200                    raise StopIteration
     201                self.data = self.G.edges[self.place + self.i]
     202                self.word = 1
     203
     204            else:
     205                self.word = self.word << 1
     206                self.v += 1
     207
     208        return self.v
     209
    125210cdef class DenseGraph(CGraph):
    126211    """
    127212    Compiled dense graphs.