X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=lib%2Fgraph%2Fvertex.rb~;fp=lib%2Fgraph%2Fvertex.rb~;h=6a85373fc76f03a656d6aa6878de66b19c63f718;hb=624e339a169bd96eb01da7288a8904e0d1830e42;hp=0000000000000000000000000000000000000000;hpb=7f38e9910061e8a71db6dac57327ca1f5d3c27cd;p=graph.njae.git diff --git a/lib/graph/vertex.rb~ b/lib/graph/vertex.rb~ new file mode 100644 index 0000000..6a85373 --- /dev/null +++ b/lib/graph/vertex.rb~ @@ -0,0 +1,31 @@ +require 'ostruct' + +module Graph + class Vertex < OpenStruct + def initialize + super + self.edges = [] + end + + def connect(other) + e = Edge.new + e << self << other + self.edges << e + other.edges << e unless self === other + e + end + + def <<(other) + connect(other) + self + end + + def neighbours + vertices = self.edges.map {|e| e.vertices}.flatten + vertices_to_me = vertices.select {|v| v == self} + other_vertices = vertices.select {|v| v != self} + (vertices_to_me[1..-1] || []) + other_vertices + end + + end +end