Sha256: 26d21e9d2bc33d3d04bb70b9a74feb7f7d39b8035e7ed82d87ee092fdd823be6

Contents?: true

Size: 1.06 KB

Versions: 2

Compression:

Stored size: 1.06 KB

Contents

require 'ostruct'

# A simple graph library

module GraphNjae
  
  # An edge (or multiedge) in a graph. The edge can have arbitrary attributes,
  # treated as method names.
  #
  # Each connection is handled by a Graph::Connection object, so that each end
  # of the Edge can have it's own attributes.
  class Edge < OpenStruct
    def initialize
      super
      self.connections = []
      self
    end
    
    # Connect this edge to a vertex
    def <<(other)
      c = Connection.new
      c.end = other
      self.connections << c
      self
    end
    
    # Return the set of vertices this edge connects.
    def vertices
      self.connections.map {|c| c.end}
    end
    
    # Return the connection object that joins this Edge to the specified Vertex
    def connection_at(vertex)
      self.connections.select {|c| c.end.equal?  vertex}.first
    end
  end
  
  # A connection between an Edge and a Vertex.The connection can have arbitrary attributes,
  # treated as method names.
  class Connection < OpenStruct
    def initialize
      super
      self
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
graph.njae-0.2.1 lib/graph.njae/edge.rb
graph.njae-0.2.0 lib/graph.njae/edge.rb