projects
/
graph.njae.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Regenerate gemspec for version
[graph.njae.git]
/
lib
/
graph
/
vertex.rb
diff --git
a/lib/graph/vertex.rb
b/lib/graph/vertex.rb
index 0bccb57296cde47787b5b6b77f54a34a87f0393b..cb315e783facc188541a6386a9574d01d2754ebc 100644
(file)
--- a/
lib/graph/vertex.rb
+++ b/
lib/graph/vertex.rb
@@
-1,6
+1,10
@@
require 'ostruct'
require 'ostruct'
+# A simple graph library
+
module Graph
module Graph
+ # A vertex in a graph. The edge can have arbitrary attributes,treated as
+ # method names.
class Vertex < OpenStruct
def initialize
super
class Vertex < OpenStruct
def initialize
super
@@
-8,6
+12,8
@@
module Graph
self
end
self
end
+ # Connect this vertex to another, creating an Edge to do so, and returning
+ # the Edge
def connect(other)
e = Edge.new
e << self << other
def connect(other)
e = Edge.new
e << self << other
@@
-16,11
+22,14
@@
module Graph
e
end
e
end
+ # Connect this vertex to another, creating an Edge to do so, and returning
+ # this Vertex
def <<(other)
connect(other)
self
end
def <<(other)
connect(other)
self
end
+ # Return the set of neighbouring vertices
def neighbours
vertices = self.edges.map {|e| e.vertices}.flatten
vertices_to_me = vertices.select {|v| v == self}
def neighbours
vertices = self.edges.map {|e| e.vertices}.flatten
vertices_to_me = vertices.select {|v| v == self}