diff --git a/src/sage/graphs/base/static_sparse_backend.pyx b/src/sage/graphs/base/static_sparse_backend.pyx index 3f6840ac447..74b215dd03e 100644 --- a/src/sage/graphs/base/static_sparse_backend.pyx +++ b/src/sage/graphs/base/static_sparse_backend.pyx @@ -252,6 +252,54 @@ cdef class StaticSparseCGraph(CGraph): """ return self.has_arc_unsafe(u, v) + cdef inline int next_out_neighbor_unsafe(self, int u, int v, int* l) except -2: + """ + Return the next out-neighbor of ``u`` after ``v``. + + If ``v`` is ``-1`` return the first neighbor of ``u``. + + Return ``-1`` in case there does not exist such an out-neighbor. + + .. NOTE:: + + A caller may not alter ``l``. + It is used to keep track of the current position. + """ + cdef int degree = self.g.neighbors[u+1] - self.g.neighbors[u] + if v == -1: + l[0] = -1 + for i in range(l[0] + 1, degree): + if self.g.neighbors[u][i] != v: + l[0] = i + return self.g.neighbors[u][i] + else: + return -1 + + cdef inline int next_in_neighbor_unsafe(self, int u, int v, int* l) except -2: + """ + Return the next in-neighbor of ``u`` after ``v``. + + If ``v`` is ``-1`` return the first neighbor of ``u``. + + Return ``-1`` in case there does not exist such an in-neighbor. + + .. NOTE:: + + A caller may not alter ``l``. + It is used to keep track of the current position. + """ + if not self._directed: + return self.next_out_neighbor_unsafe(u, v, l) + cdef int degree = self.g_rev.neighbors[u+1] - self.g.neighbors[u] + if v == -1: + l[0] = -1 + for i in range(l[0] + 1, degree): + if self.g_rev.neighbors[u][i] != v: + l[0] = i + return self.g_rev.neighbors[u][i] + else: + return -1 + cdef int out_neighbors_unsafe(self, int u, int *neighbors, int size) except -2: cdef int degree = self.g.neighbors[u+1] - self.g.neighbors[u] cdef int i