Skip to content

Commit

Permalink
Remove vertex_index(::Integer), make special case for SimpleGraph
Browse files Browse the repository at this point in the history
  • Loading branch information
mlubin authored and ggggggggg committed Mar 17, 2015
1 parent 5e38d35 commit b731e8c
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 17 deletions.
11 changes: 2 additions & 9 deletions doc/source/vertex_edge.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Vertices and Edges
Vertex Types
-------------

A vertex can be of any Julia type. For example, it can be an integer, a character, or a string. In a simplistic setting where there is no additional information associated with a vertex, it is recommended to use ``Int`` as the vertex type, which would lead to the best performance.
A vertex can be of any Julia type. For example, it can be an integer, a character, or a string.

This package provides two specific vertex types: ``KeyVertex`` and ``ExVertex``. The definition of ``KeyVertex`` is:

Expand Down Expand Up @@ -40,14 +40,7 @@ The ``ExVertex`` type implements a ``vertex_index`` function, as
returns the index of the vertex ``v``.

In addition, for integers, we have

.. code-block:: python
vertex_index(v::Integer) = v
This makes it convenient to use integers as vertices in graphs.

``SimpleGraph`` is a special case where the vertices are of type ``Int`` and store both their index and identity. In all other graphs, ``Int`` vertices are unordered indices.

Edge Types
-----------
Expand Down
1 change: 0 additions & 1 deletion src/adjacency_list.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ is_directed(g::GenericAdjacencyList) = g.is_directed

num_vertices(g::GenericAdjacencyList) = length(g.vertices)
vertices(g::GenericAdjacencyList) = g.vertices
vertex_index{V<:ProvidedVertexType}(v::V, g::GenericAdjacencyList{V}) = vertex_index(v)

num_edges(g::GenericAdjacencyList) = g.nedges

Expand Down
7 changes: 4 additions & 3 deletions src/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ typealias AttributeDict Dict{UTF8String, Any}
#
################################################

vertex_index(v::Integer) = v

immutable KeyVertex{K}
index::Int
key::K
Expand Down Expand Up @@ -37,7 +35,10 @@ typealias ProvidedVertexType Union(Integer, KeyVertex, ExVertex)

function vertex_index{V}(v::V, g::AbstractGraph{V})
@graph_requires g vertex_list
return vertex_index(v, vertices(g))
if applicable(vertex_index, v)
return vertex_index(v)
end
return vertex_index(v, vertices(g)) # slow linear search
end

vertex_index(v, vs::AbstractArray) = findfirst(vs, v)
Expand Down
1 change: 0 additions & 1 deletion src/edge_list.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ is_directed(g::GenericEdgeList) = g.is_directed

num_vertices(g::GenericEdgeList) = length(g.vertices)
vertices(g::GenericEdgeList) = g.vertices
vertex_index{V<:ProvidedVertexType}(v::V, g::GenericEdgeList{V}) = vertex_index(v)

num_edges(g::GenericEdgeList) = length(g.edges)
edges(g::GenericEdgeList) = g.edges
Expand Down
14 changes: 13 additions & 1 deletion src/graph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ vertices(g::GenericGraph) = g.vertices
num_edges(g::GenericGraph) = length(g.edges)
edges(g::GenericGraph) = g.edges

vertex_index{V<:ProvidedVertexType}(v::V, g::GenericGraph{V}) = vertex_index(v)
vertex_index(v::Integer, g::SimpleGraph) = (v <= g.vertices[end]? v: 0)

edge_index{V,E}(e::E, g::GenericGraph{V,E}) = edge_index(e)

out_edges{V}(v::V, g::GenericGraph{V}) = g.finclist[vertex_index(v, g)]
Expand All @@ -69,6 +70,17 @@ in_neighbors{V}(v::V, g::GenericGraph{V}) = SourceIterator(g, in_edges(v, g))

# mutation

function add_vertex!(g::SimpleGraph)
# ensure SimpleGraph indices are consecutive, allowing O(1) indexing
v = g.vertices[end] + 1
g.vertices = 1:v
push!(g.finclist, Int[])
push!(g.binclist, Int[])
v
end

@deprecate add_vertex!(g::SimpleGraph,v) add_vertex!(g::SimpleGraph)

function add_vertex!{V}(g::GenericGraph{V}, v::V)
push!(g.vertices, v)
push!(g.finclist, Int[])
Expand Down
1 change: 0 additions & 1 deletion src/incidence_list.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ vertices(g::GenericIncidenceList) = g.vertices

num_edges(g::GenericIncidenceList) = g.nedges

vertex_index{V<:ProvidedVertexType}(v::V, g::GenericIncidenceList{V}) = vertex_index(v)
edge_index{V,E}(e::E, g::GenericIncidenceList{V,E}) = edge_index(e)

out_edges{V}(v::V, g::GenericIncidenceList{V}) = g.inclist[vertex_index(v, g)]
Expand Down
3 changes: 2 additions & 1 deletion test/graph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ using Base.Test
#
#################################################

sgd = simple_graph(4)
sgd = simple_graph(3)
add_vertex!(sgd)

# concept test

Expand Down

0 comments on commit b731e8c

Please sign in to comment.