Skip to content
This repository has been archived by the owner on Oct 21, 2021. It is now read-only.

Commit

Permalink
added @mlubin's suggestions for indexing to preserve KeyIndex and oth…
Browse files Browse the repository at this point in the history
…ers O(1)
  • Loading branch information
sbromberger committed Dec 21, 2014
1 parent fe2b228 commit ceb7b4c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
2 changes: 0 additions & 2 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
11 changes: 10 additions & 1 deletion src/graph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,15 @@ num_edges(g::GenericGraph) = length(g.edges)
edges(g::GenericGraph) = g.edges

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

function vertex_index{V<:ProvidedVertexType}(v::V, g::GenericGraph{V})
if applicable(vertex_index, v) # use O(1) if possible
return vertex_index(v)
else # use O(n)
return vertex_index(v,vertices(g))
end
end

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 @@ -71,6 +79,7 @@ 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[])
Expand Down

0 comments on commit ceb7b4c

Please sign in to comment.