X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=lib%2Fgraph%2Fvertex.rb;fp=lib%2Fgraph%2Fvertex.rb;h=0000000000000000000000000000000000000000;hb=0908114d61c0effab6a568dd47053244df92cde8;hp=cb315e783facc188541a6386a9574d01d2754ebc;hpb=7bd4317633966d7fcddff3b05767fcbf79a56ebf;p=graph.njae.git diff --git a/lib/graph/vertex.rb b/lib/graph/vertex.rb deleted file mode 100644 index cb315e7..0000000 --- a/lib/graph/vertex.rb +++ /dev/null @@ -1,41 +0,0 @@ -require 'ostruct' - -# A simple graph library - -module Graph - # A vertex in a graph. The edge can have arbitrary attributes,treated as - # method names. - class Vertex < OpenStruct - def initialize - super - 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 - e << self << other - self.edges << e - other.edges << e unless self === other - e - end - - # Connect this vertex to another, creating an Edge to do so, and returning - # this Vertex - 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} - other_vertices = vertices.select {|v| v != self} - (vertices_to_me[1..-1] || []) + other_vertices - end - - end -end