Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
reuse queue
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Kliem committed Dec 28, 2020
1 parent eaf982b commit 4fd001e
Showing 1 changed file with 8 additions and 17 deletions.
25 changes: 8 additions & 17 deletions src/sage/graphs/base/c_graph.pyx
Expand Up @@ -46,7 +46,7 @@ method :meth:`realloc <sage.graphs.base.c_graph.CGraph.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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 = <int*>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]
Expand All @@ -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):
Expand Down Expand Up @@ -4849,26 +4842,24 @@ 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:
w_int = cg.next_out_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_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:
Expand Down

0 comments on commit 4fd001e

Please sign in to comment.