Sha256: 27813334c89a8df5590b7b5281751c4fe2d126263e9744e5b49f1eb90ed2a650
Contents?: true
Size: 978 Bytes
Versions: 3
Compression:
Stored size: 978 Bytes
Contents
require 'ostruct' # A simple graph library module GraphNjae # 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
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
graph.njae-0.2.2 | lib/graph.njae/vertex.rb |
graph.njae-0.2.1 | lib/graph.njae/vertex.rb |
graph.njae-0.2.0 | lib/graph.njae/vertex.rb |