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

Commit

Permalink
trac #18346: Turn the backends into cdef classes
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanncohen committed May 1, 2015
1 parent 5b765e1 commit 2704dda
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 31 deletions.
16 changes: 13 additions & 3 deletions src/sage/graphs/base/c_graph.pxd
Expand Up @@ -6,6 +6,7 @@
#**************************************************************************

from sage.data_structures.bitset cimport bitset_t
from graph_backends cimport GenericGraphBackend

cdef class CGraph:
cdef int num_verts
Expand Down Expand Up @@ -39,9 +40,18 @@ cdef class CGraph:
cpdef realloc(self, int)
cdef int add_vertex_unsafe(self, int) except -1

cdef int get_vertex(object u, dict vertex_ints, dict vertex_labels, CGraph G) except ? -2
cdef object vertex_label(int u_int, dict vertex_ints, dict vertex_labels, CGraph G)
cdef int check_vertex(object u, dict vertex_ints, dict vertex_labels, CGraph G, CGraph G_revx, bint reverse) except ? -1
cdef class CGraphBackend(GenericGraphBackend):
cdef int get_vertex(self, object u) except ? -2
cdef object vertex_label(self, int u_int)
cdef int check_labelled_vertex(self, object u, bint reverse) except ? -1
cdef CGraph _cg
cdef CGraph _cg_rev
cdef bint _directed
cdef dict vertex_labels
cdef dict vertex_ints
cdef dict edge_labels
cdef bint _loops
cdef bint _multiple_edges
# TODO: edge functions!


21 changes: 8 additions & 13 deletions src/sage/graphs/base/c_graph.pyx
Expand Up @@ -39,7 +39,6 @@ method :meth:`realloc <sage.graphs.base.c_graph.CGraph.realloc>`.

include "sage/data_structures/bitset.pxi"

from graph_backends import GenericGraphBackend
from sage.rings.integer cimport Integer

cdef class CGraph:
Expand Down Expand Up @@ -1115,7 +1114,7 @@ cdef class CGraph:
"""
return self.num_arcs

class CGraphBackend(GenericGraphBackend):
cdef class CGraphBackend(GenericGraphBackend):
"""
Base class for sparse and dense graph backends.
Expand All @@ -1136,7 +1135,7 @@ class CGraphBackend(GenericGraphBackend):
sage: CGB.degree(0, True)
Traceback (most recent call last):
...
AttributeError: 'CGraphBackend' object has no attribute 'vertex_ints'
TypeError: 'NoneType' object is not iterable
The appropriate way to use these backends is via Sage graphs::
Expand All @@ -1154,10 +1153,6 @@ class CGraphBackend(GenericGraphBackend):
-- backend for dense graphs.
"""

_cg = None
_cg_rev = None
_directed = None

cdef int get_vertex(self, object u) except ? -2:
"""
Returns an int representing the arbitrary hashable vertex u (whether or not
Expand Down Expand Up @@ -1941,23 +1936,23 @@ class CGraphBackend(GenericGraphBackend):
2
sage: from sage.graphs.base.sparse_graph import SparseGraphBackend
sage: S = SparseGraphBackend(7)
sage: S.num_edges(directed=False)
sage: S.num_edges(False)
0
sage: S.loops(True)
sage: S.add_edge(1, 1, None, directed=False)
sage: S.num_edges(directed=False)
sage: S.num_edges(False)
1
sage: S.multiple_edges(True)
sage: S.add_edge(1, 1, None, directed=False)
sage: S.num_edges(directed=False)
sage: S.num_edges(False)
2
sage: from sage.graphs.base.dense_graph import DenseGraphBackend
sage: D = DenseGraphBackend(7)
sage: D.num_edges(directed=False)
sage: D.num_edges(False)
0
sage: D.loops(True)
sage: D.add_edge(1, 1, None, directed=False)
sage: D.num_edges(directed=False)
sage: D.num_edges(False)
1
"""
if directed:
Expand Down Expand Up @@ -2974,7 +2969,7 @@ cdef class Search_iterator:
self.graph = graph
self.direction = direction

bitset_init(self.seen, (<CGraph>self.graph._cg).active_vertices.size)
bitset_init(self.seen, self.graph._cg.active_vertices.size)
bitset_set_first_n(self.seen, 0)

cdef int v_id = self.graph.get_vertex(v)
Expand Down
11 changes: 6 additions & 5 deletions src/sage/graphs/base/dense_graph.pyx
Expand Up @@ -234,13 +234,14 @@ cdef class DenseGraph(CGraph):
"""
from sage.graphs.all import DiGraph
D = DiGraph(implementation='c_graph', sparse=False)
D._backend._cg = self
cdef CGraphBackend cgb = <CGraphBackend> D._backend
cgb._cg = self
cdef int i
D._backend.vertex_labels = {}
cgb.vertex_labels = {}
for i from 0 <= i < self.active_vertices.size:
if bitset_in(self.active_vertices, i):
D._backend.vertex_labels[i] = i
D._backend.vertex_ints = D._backend.vertex_labels
cgb.vertex_labels[i] = i
cgb.vertex_ints = cgb.vertex_labels
return (DenseGraph, (0, self.active_vertices.size, self.verts(), D.edges(labels=False)))

cpdef realloc(self, int total_verts):
Expand Down Expand Up @@ -694,7 +695,7 @@ def _test_adjacency_sequence_out():

from c_graph cimport CGraphBackend

class DenseGraphBackend(CGraphBackend):
cdef class DenseGraphBackend(CGraphBackend):
"""
Backend for Sage graphs using DenseGraphs.
Expand Down
3 changes: 3 additions & 0 deletions src/sage/graphs/base/sparse_graph.pxd
Expand Up @@ -39,4 +39,7 @@ cdef class SparseGraph(CGraph):
cdef int out_neighbors_BTNode_unsafe(self, int u, SparseGraphBTNode *** p_pointers)
cdef list out_arcs_unsafe(self, int u, bint labels)

cdef class SparseGraphBackend(CGraphBackend):
pass

cdef int new_edge_label(object l, dict edge_labels)
13 changes: 7 additions & 6 deletions src/sage/graphs/base/sparse_graph.pyx
Expand Up @@ -415,14 +415,15 @@ cdef class SparseGraph(CGraph):
"""
from sage.graphs.all import DiGraph
D = DiGraph(implementation='c_graph', sparse=True, multiedges=True, loops=True)
D._backend._cg = self
cdef CGraphBackend cgb = <CGraphBackend?> D._backend
cgb._cg = self
cdef int i
D._backend.vertex_labels = {}
cgb.vertex_labels = {}
for i from 0 <= i < self.active_vertices.size:
if bitset_in(self.active_vertices, i):
D._backend.vertex_labels[i] = i
D._backend.vertex_ints = D._backend.vertex_labels
D._backend.edge_labels = id_dict()
cgb.vertex_labels[i] = i
cgb.vertex_ints = cgb.vertex_labels
cgb.edge_labels = id_dict()
arcs = [(u,v,l) if l is not None else (u,v,0) for u,v,l in D.edges(labels=True)]
return (SparseGraph, (0, self.hash_length, self.active_vertices.size, self.verts(), arcs))

Expand Down Expand Up @@ -1605,7 +1606,7 @@ cdef int new_edge_label(object l, dict edge_labels):
edge_labels[max+1] = l
return max+1

class SparseGraphBackend(CGraphBackend):
cdef class SparseGraphBackend(CGraphBackend):
"""
Backend for Sage graphs using SparseGraphs.
Expand Down
6 changes: 6 additions & 0 deletions src/sage/graphs/base/static_sparse_backend.pxd
Expand Up @@ -10,3 +10,9 @@ cdef class StaticSparseCGraph(CGraph):

cpdef int out_degree(self, int u) except -1
cpdef int in_degree(self, int u) except -1

cdef class StaticSparseBackend(CGraphBackend):
cdef int _order
cdef bint _multiedges
cdef list _vertex_to_labels
cdef dict _vertex_to_int
6 changes: 3 additions & 3 deletions src/sage/graphs/base/static_sparse_backend.pyx
Expand Up @@ -323,7 +323,7 @@ cdef class StaticSparseCGraph(CGraph):
else:
return self.g_rev.neighbors[u+1] - self.g_rev.neighbors[u]

class StaticSparseBackend(CGraphBackend):
cdef class StaticSparseBackend(CGraphBackend):

def __init__(self, G, loops = False, multiedges=False):
"""
Expand Down Expand Up @@ -1136,9 +1136,9 @@ def _run_it_on_static_instead(f):
sage: Graph.new_graph_method = new_graph_method
sage: g = Graph(5)
sage: print "My backend is of type", g._backend
My backend is of type <class 'sage.graphs.base.sparse_graph.SparseGraphBackend'>
My backend is of type <type 'sage.graphs.base.sparse_graph.SparseGraphBackend'>
sage: g.new_graph_method()
My backend is of type <class 'sage.graphs.base.static_sparse_backend.StaticSparseBackend'>
My backend is of type <type 'sage.graphs.base.static_sparse_backend.StaticSparseBackend'>
"""
def same_function_on_static_version(*kwd,**kwds):
if not isinstance(kwd[0]._backend,StaticSparseBackend):
Expand Down
2 changes: 1 addition & 1 deletion src/sage/graphs/base/static_sparse_graph.pyx
Expand Up @@ -517,7 +517,7 @@ def triangles_count(G):
# g is a copy of G. If G is internally a static sparse graph, we use it.
cdef short_digraph g
G = G.copy(immutable=True)
g[0] = (<StaticSparseCGraph?> (G._backend._cg)).g[0]
g[0] = (<StaticSparseCGraph> (<StaticSparseBackend> G._backend)._cg).g[0]

cdef uint64_t count = 0
cdef uint32_t u,v,i
Expand Down

0 comments on commit 2704dda

Please sign in to comment.