Initial commit
[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 end
9
10 def connect(other)
11 e = Edge.new
12 e << self << other
13 self.edges << e
14 other.edges << e unless self === other
15 e
16 end
17
18 def <<(other)
19 connect(other)
20 self
21 end
22
23 def neighbours
24 vertices = self.edges.map {|e| e.vertices}.flatten
25 vertices_to_me = vertices.select {|v| v == self}
26 other_vertices = vertices.select {|v| v != self}
27 (vertices_to_me[1..-1] || []) + other_vertices
28 end
29
30 end
31 end