Skip to content

Instantly share code, notes, and snippets.

@daniello
Created June 2, 2009 10:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daniello/122172 to your computer and use it in GitHub Desktop.
Save daniello/122172 to your computer and use it in GitHub Desktop.
class BasicGraph
include_package 'clojure.lang'
attr_reader :nodes, :edges
def self.empty
BasicGraph.new(PersistentHashMap::EMPTY, PersistentHashMap::EMPTY)
end
def add_node(node, id)
BasicGraph.new(@nodes.assoc(id, node), @edges)
end
def add_edge(source_id, target_id, edge)
if (@nodes.containsKey(source_id) && @nodes.containsKey(target_id))
BasicGraph.new(@nodes, @edges.assoc([source_id, target_id], edge))
else
self
end
end
private
attr_accessor :nodes, :edges
def initialize(nodes, edges)
@nodes = nodes
@edges = edges
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment