Tidied up, added comments, created documentation.
[graph.njae.git] / lib / graph / vertex.rb~
1 require 'ostruct'
2
3 module Graph
4 class Vertex < OpenStruct
5 def initialize
6 super
7 self.edges = []
8 self
9 end
10
11 def connect(other)
12 e = Edge.new
13 e << self << other
14 self.edges << e
15 other.edges << e unless self === other
16 e
17 end
18
19 def <<(other)
20 connect(other)
21 self
22 end
23
24 def neighbours
25 vertices = self.edges.map {|e| e.vertices}.flatten
26 vertices_to_me = vertices.select {|v| v == self}
27 other_vertices = vertices.select {|v| v != self}
28 (vertices_to_me[1..-1] || []) + other_vertices
29 end
30
31 end
32 end