From 4fd001e8bf5ec293913531ba56a891181edf8f45 Mon Sep 17 00:00:00 2001 From: Jonathan Kliem Date: Mon, 28 Dec 2020 19:38:45 +0100 Subject: [PATCH] reuse queue --- src/sage/graphs/base/c_graph.pyx | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/src/sage/graphs/base/c_graph.pyx b/src/sage/graphs/base/c_graph.pyx index 10c6dd8b113..72284b1d35f 100644 --- a/src/sage/graphs/base/c_graph.pyx +++ b/src/sage/graphs/base/c_graph.pyx @@ -46,7 +46,7 @@ method :meth:`realloc `. from sage.data_structures.bitset_base cimport * from sage.rings.integer cimport Integer from sage.arith.long cimport pyobject_to_long -from libcpp.queue cimport priority_queue +from libcpp.queue cimport priority_queue, queue from libcpp.pair cimport pair from sage.rings.integer_ring import ZZ from cysignals.memory cimport check_allocarray, sig_free @@ -4716,9 +4716,7 @@ cdef class Search_iterator: cdef CGraphBackend graph cdef int direction cdef list stack - cdef int* queue - cdef int queue_begin - cdef int queue_end + cdef queue[int] fifo cdef int n cdef bitset_t seen cdef bint test_out @@ -4795,10 +4793,7 @@ cdef class Search_iterator: raise LookupError("vertex ({0}) is not a vertex of the graph".format(repr(v))) if direction == 0: - self.queue = sig_malloc(self.n * sizeof(int)) - self.queue_begin = 0 - self.queue_end = 0 - self.queue[0] = v_id + self.fifo.push(v_id) bitset_add(self.seen, v_id) else: self.stack = [v_id] @@ -4816,8 +4811,6 @@ cdef class Search_iterator: r""" Freeing the memory """ - if self.direction == 0: - sig_free(self.queue) bitset_free(self.seen) def __iter__(self): @@ -4849,9 +4842,9 @@ cdef class Search_iterator: cdef int l cdef CGraph cg = self.graph.cg() - if self.queue_begin <= self.queue_end: - v_int = self.queue[self.queue_begin] - self.queue_begin += 1 + while not self.fifo.empty(): + v_int = self.fifo.front() + self.fifo.pop() value = self.graph.vertex_label(v_int) if self.test_out: @@ -4859,16 +4852,14 @@ cdef class Search_iterator: while w_int != -1: if bitset_not_in(self.seen, w_int): bitset_add(self.seen, w_int) - self.queue_end += 1 - self.queue[self.queue_end] = w_int + self.fifo.push(w_int) w_int = cg.next_out_neighbor_unsafe(v_int, w_int, &l) if self.test_in: w_int = cg.next_in_neighbor_unsafe(v_int, -1, &l) while w_int != -1: if bitset_not_in(self.seen, w_int): bitset_add(self.seen, w_int) - self.queue_end += 1 - self.queue[self.queue_end] = w_int + self.fifo.push(w_int) w_int = cg.next_in_neighbor_unsafe(v_int, w_int, &l) else: