Added hash parameter to graph initialization
[graph.njae.git] / lib / graph.njae / graph.rb
1 require 'ostruct'
2
3 # A simple graph library
4
5 module GraphNjae
6
7 # A container for all the parts of a graph. The graph can have arbitrary attributes,
8 # treated as method names.
9 class Graph < OpenStruct
10 def initialize(values = {})
11 super(values)
12 self.edges = Array.new
13 self.vertices = Array.new
14 self
15 end
16
17 # Add a Vertex or Edge to the graph.
18 def <<(other)
19 if other.class.ancestors.include? Vertex
20 self.vertices << other
21 elsif other.class.ancestors.include? Edge
22 self.edges << other
23 end
24 self
25 end
26
27 # Form a product graph of this graph and the other.
28 # Return the new graph.
29 def product(other)
30 product_graph = Graph.new
31 self.vertices.each do |v1|
32 other.vertices.each do |v2|
33 product_vertex = Vertex.new
34 product_vertex.left_node = v1
35 product_vertex.right_node = v2
36 product_graph << product_vertex
37 end
38 end
39 end
40
41 end # class
42 end