X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=lib%2Fgraph.njae%2Fvertex.rb;h=6e46dcb40029aef8ceca16ad5c5ea2fc5a789c46;hb=95b8572d01c0b9c3aa8fc5a1e85a045c33d4ecf1;hp=bf14dddac07ce079f13a5d4de2409d8292d449c0;hpb=c34def40508c16cfc815fa02c8743d071491af7b;p=graph.njae.git diff --git a/lib/graph.njae/vertex.rb b/lib/graph.njae/vertex.rb index bf14ddd..6e46dcb 100644 --- a/lib/graph.njae/vertex.rb +++ b/lib/graph.njae/vertex.rb @@ -6,19 +6,19 @@ module GraphNjae # A vertex in a graph. The edge can have arbitrary attributes,treated as # method names. class Vertex < OpenStruct - def initialize - super + def initialize(values = {}) + super(values) self.edges = [] self end # Connect this vertex to another, creating an Edge to do so, and returning # the Edge - def connect(other) - e = Edge.new + def connect(other, edge_attributes = {}) + e = Edge.new edge_attributes e << self << other - self.edges << e - other.edges << e unless self === other + # self.edges << e + # other.edges << e unless self === other e end @@ -31,10 +31,16 @@ module GraphNjae # Return the set of neighbouring vertices 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 + #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# + self.edges.map {|e| e.vertices.take_while {|v| v != self} + + e.vertices.drop_while {|v| v != self}[1..-1]}.flatten + end + + def to_s + '' end end